Repository navigation
Manage OIDC broker email verification #50699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| import org.keycloak.protocol.oidc.mappers.HardcodedClaim; | ||
| import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper; | ||
| import org.keycloak.provider.ProviderConfigProperty; | ||
| import org.keycloak.representations.IDToken; | ||
| import org.keycloak.representations.idm.ClientRepresentation; | ||
| import org.keycloak.representations.idm.FederatedIdentityRepresentation; | ||
| import org.keycloak.representations.idm.IdentityProviderMapperRepresentation; | ||
|
|
@@ -76,6 +77,7 @@ | |
| import static org.hamcrest.Matchers.not; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
|
||
| /** | ||
| * Final class as it's not intended to be overriden. Feel free to remove "final" if you really know what you are doing. | ||
|
|
@@ -494,6 +496,84 @@ public void testVerifyEmailWhenUpdateProfileAndEmailVerifiedAtIdP() { | |
| assertThat(users.get(0).getEmail(), is("[email protected]")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmailVerifiedFromUserInfo() { | ||
| RealmResource providerRealm = adminClient.realm(bc.providerRealmName()); | ||
| RealmResource consumerRealm = adminClient.realm(bc.consumerRealmName()); | ||
|
|
||
| IdentityProviderRepresentation idpRep = identityProviderResource.toRepresentation(); | ||
| idpRep.setTrustEmail(true); | ||
| idpRep.getConfig().put("disableUserInfo", "false"); | ||
| identityProviderResource.update(idpRep); | ||
|
|
||
| ClientScopeResource emailClientScope = AdminApiUtil.findClientScopeByName(providerRealm, "email"); | ||
| List<ProtocolMapperRepresentation> originalMappers = emailClientScope.getProtocolMappers().getMappers(); | ||
| originalMappers.forEach(protocolMapper -> emailClientScope.getProtocolMappers().delete(protocolMapper.getId())); | ||
|
|
||
| // Save and remove the "email" protocol mapper from the brokerapp client so that the email claim | ||
| // is not included in the ID token, allowing us to test that the email comes from userinfo only | ||
| ClientRepresentation brokerAppClient = providerRealm.clients().findByClientId("brokerapp").get(0); | ||
| ClientResource brokerAppResource = providerRealm.clients().get(brokerAppClient.getId()); | ||
| ProtocolMapperRepresentation brokerAppEmailMapper = brokerAppResource.getProtocolMappers().getMappers().stream() | ||
| .filter(mapper -> "email".equals(mapper.getName())) | ||
| .findFirst() | ||
| .orElse(null); | ||
| brokerAppResource.getProtocolMappers().delete(brokerAppEmailMapper.getId()); | ||
|
|
||
|
graziang marked this conversation as resolved.
Resolved
|
||
| ProtocolMapperRepresentation userinfoEmailVerifiedMapper = new ProtocolMapperRepresentation(); | ||
| userinfoEmailVerifiedMapper.setName("userinfo-email-verified"); | ||
| userinfoEmailVerifiedMapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); | ||
| userinfoEmailVerifiedMapper.setProtocolMapper(HardcodedClaim.PROVIDER_ID); | ||
| userinfoEmailVerifiedMapper.getConfig().put(OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, IDToken.EMAIL_VERIFIED); | ||
| userinfoEmailVerifiedMapper.getConfig().put(OIDCAttributeMapperHelper.JSON_TYPE, "boolean"); | ||
| userinfoEmailVerifiedMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_ACCESS_TOKEN, "false"); | ||
| userinfoEmailVerifiedMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, "false"); | ||
| userinfoEmailVerifiedMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_USERINFO, "true"); | ||
| userinfoEmailVerifiedMapper.getConfig().put(HardcodedClaim.CLAIM_VALUE, "false"); | ||
| emailClientScope.getProtocolMappers().createMapper(userinfoEmailVerifiedMapper).close(); | ||
|
graziang marked this conversation as resolved.
Resolved
|
||
|
|
||
| ProtocolMapperRepresentation userinfoEmailMapper = new ProtocolMapperRepresentation(); | ||
| userinfoEmailMapper.setName("userinfo-email"); | ||
| userinfoEmailMapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); | ||
| userinfoEmailMapper.setProtocolMapper(HardcodedClaim.PROVIDER_ID); | ||
| userinfoEmailMapper.getConfig().put(OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME, IDToken.EMAIL); | ||
| userinfoEmailMapper.getConfig().put(OIDCAttributeMapperHelper.JSON_TYPE, "String"); | ||
| userinfoEmailMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_ACCESS_TOKEN, "false"); | ||
| userinfoEmailMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, "false"); | ||
| userinfoEmailMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_USERINFO, "true"); | ||
| userinfoEmailMapper.getConfig().put(HardcodedClaim.CLAIM_VALUE, "[email protected]"); | ||
| emailClientScope.getProtocolMappers().createMapper(userinfoEmailMapper).close(); | ||
|
|
||
|
|
||
| oauth.client("broker-app"); | ||
| loginPage.open(bc.consumerRealmName()); | ||
| logInWithBroker(bc); | ||
|
|
||
| waitForPage(driver, "update account information", false); | ||
| updateAccountInformationPage.assertCurrent(); | ||
| updateAccountInformationPage.updateAccountInformation("FirstName", "LastName"); | ||
|
|
||
| // Verify user was created with email from ID token, not userinfo | ||
|
graziang marked this conversation as resolved.
Resolved
|
||
| List<UserRepresentation> users = consumerRealm.users().search(bc.getUserLogin()); | ||
| assertEquals(1, users.size()); | ||
| UserRepresentation consumerUser = users.get(0); | ||
|
|
||
| assertEquals(consumerUser.getEmail(), "[email protected]"); | ||
|
graziang marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Email should be not verified because it came from ID token which has email_verified=false | ||
| assertFalse(consumerUser.isEmailVerified()); | ||
|
graziang marked this conversation as resolved.
Resolved
|
||
|
|
||
| emailClientScope.getProtocolMappers().getMappers().forEach(protocolMapper -> emailClientScope.getProtocolMappers().delete(protocolMapper.getId())); | ||
| emailClientScope.getProtocolMappers().createMapper(originalMappers); | ||
|
|
||
| // Restore the "email" protocol mapper on the brokerapp client | ||
| if (brokerAppEmailMapper != null) { | ||
| brokerAppEmailMapper.setId(null); | ||
| brokerAppResource.getProtocolMappers().createMapper(brokerAppEmailMapper).close(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testVerifyEmailWhenUpdateProfileSameEmailAndEmailVerifiedAtIdP() { | ||
| RealmResource providerRealm = adminClient.realm(bc.providerRealmName()); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.