I would like Keycloak (1.4.0) to include the users' chosen locale to the ID token.
I have e as far as creating a user attribute mapper, which was supposed to map the locale attribute to the token, but it does not work.
Does anybody know how to do this?
Thanks in advance.
Edit: I have learnt what I know abput Keycloak Locales from this class: .jboss/nexus/content/repositories/releases/org.keycloak/keycloak-forms-mon-freemarker/1.2.0.Final/org/keycloak/freemarker/LocaleHelper.java#LocaleHelper.0LOGGER
I would like Keycloak (1.4.0) to include the users' chosen locale to the ID token.
I have e as far as creating a user attribute mapper, which was supposed to map the locale attribute to the token, but it does not work.
Does anybody know how to do this?
Thanks in advance.
Edit: I have learnt what I know abput Keycloak Locales from this class: http://grepcode./file/repository.jboss/nexus/content/repositories/releases/org.keycloak/keycloak-forms-mon-freemarker/1.2.0.Final/org/keycloak/freemarker/LocaleHelper.java#LocaleHelper.0LOGGER
Share Improve this question asked Sep 21, 2015 at 13:24 LukaLuka 4551 gold badge7 silver badges19 bronze badges2 Answers
Reset to default 4I have managed to solve the problem on my own. I ended up using loadUserProfile() function from Keycloak JS adapter. It loads all the user attributes (including locale) into keycloak.profile object, so I didn't have to configure any mappers.
I suppose you already have something like this:
- Open the admin console of your realm.
- Go to Clients and select your client
- This only works for Settings > Access Type confidential or public (not bearer-only)
- Go to Mappers
- Create a mapping from your attribute to json
- Check "Add to ID token"
To access the mapped claim you use something like this:
final Principal userPrincipal = httpRequest.getUserPrincipal();
if (userPrincipal instanceof KeycloakPrincipal) {
KeycloakPrincipal<KeycloakSecurityContext> kp = (KeycloakPrincipal<KeycloakSecurityContext>) userPrincipal;
IDToken token = kp.getKeycloakSecurityContext().getIdToken();
Map<String, Object> otherClaims = token.getOtherClaims();
if (otherClaims.containsKey("YOUR_CLAIM_KEY")) {
yourClaim = String.valueOf(otherClaims.get("YOUR_CLAIM_KEY"));
}
} else {
throw new RuntimeException(...);
}
Hope this helps and fits your use case. I used this for a custom attribute I added with a custom theme.