Skip to content

Commit 7db9fd7

Browse files
authored
Merge pull request #4 from douph1/main
Add AuthNote EXISTING_USER_INFO to allow using next authenticator
2 parents 6462fdd + 6f21bf0 commit 7db9fd7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/main/java/foundation/softwaredesign/keycloak/authenticators/CustomAttributeIdpLinkingAuthenticator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.keycloak.authentication.AuthenticationFlowContext;
66
import org.keycloak.authentication.AuthenticationFlowError;
77
import org.keycloak.authentication.authenticators.broker.IdpAutoLinkAuthenticator;
8+
import org.keycloak.authentication.authenticators.broker.util.ExistingUserInfo;
89
import org.keycloak.authentication.authenticators.broker.util.SerializedBrokeredIdentityContext;
910
import org.keycloak.broker.provider.BrokeredIdentityContext;
1011
import org.keycloak.events.Errors;
@@ -36,6 +37,12 @@ protected void authenticateImpl(AuthenticationFlowContext context, SerializedBro
3637
brokerContext.getIdpConfig().getAlias(), brokerContext.getUsername());
3738

3839
context.setUser(existingUser);
40+
41+
var shortAttributName = getShortAttributName(config);
42+
43+
ExistingUserInfo duplication = new ExistingUserInfo(existingUser.getId(), shortAttributName, existingUser.getFirstAttribute(shortAttributName));
44+
context.getAuthenticationSession().setAuthNote(EXISTING_USER_INFO, duplication.serialize());
45+
3946
context.success();
4047
} else {
4148
if (failOnNoMatch.equals("true")) {
@@ -47,6 +54,14 @@ protected void authenticateImpl(AuthenticationFlowContext context, SerializedBro
4754

4855
}
4956

57+
private static String getShortAttributName(AuthenticatorConfigModel config) {
58+
var fullAttributeName = config.getConfig().get(CustomAttributeIdpLinkingAuthenticatorFactory.CONFIG_IDP_ATTRIBUTE);
59+
return Optional.of(fullAttributeName)
60+
.filter(a -> a.contains("."))
61+
.map(a -> a.substring(a.lastIndexOf(".") + 1))
62+
.orElse(fullAttributeName);
63+
}
64+
5065
private AuthenticatorConfigModel validateConfig(AuthenticationFlowContext context) {
5166
if (context.getAuthenticatorConfig() == null) {
5267
log.warn("Config must not be empty.");

0 commit comments

Comments
 (0)