Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak retrieve custom attributes to KeycloakPrincipal

In my rest service i can obtain the principal information after authentication using

KeycloakPrincipal kcPrincipal = (KeycloakPrincipal) servletRequest.getUserPrincipal();

statement.

Keycloak principal doesn't contain all the information i need about the authenticated user. Is it possible to customize my own principal type? On the keycloak-server-end I've developed a user federation provider. I saw that UserModel makes possible to add a set of custom attributes to my user.

Is it possible to insert my custom principal in that code?

Is it possible to retrieve this attributes from keycloak principal?

What is the way?

like image 774
Alex Avatar asked Sep 20 '15 11:09

Alex


People also ask

How do I add a custom claim to a Keycloak?

Adding in request a custom header containing our custom claims json - base64 encoded. Implement a protocol mapper, which configuration of the name of the custom header and type (Json) The mapper read the custom header value, decode the value and the json value to the access token.

How do I get JWT token from Keycloak?

GET AccessTokenWe send a POST request to the token endpoint: http://localhost:8090/auth/realms/wstutorial/protocol/openid-connect/token. We use openid-connect protocol which is an authentication layer on top of OAuth 2.0. Within the POST request we send data as name=value pairs separated with &


3 Answers

To add custom attributes you need to do three things:

  1. Add attributes to admin console
  2. Add claim mapping
  3. Access claims

The first one is explained pretty good here: https://www.keycloak.org/docs/latest/server_admin/index.html#user-attributes

Add claim mapping:

  1. Open the admin console of your realm.
  2. Go to Clients and open your client
  3. This only works for Settings > Access Type confidential or public (not bearer-only)
  4. Go to Mappers
  5. Create a mapping from your attribute to json
  6. Check "Add to ID token"

Access claims:

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.

like image 87
lisa p. Avatar answered Oct 17 '22 20:10

lisa p.


  • Select Users > Lookup > click on ID > go to attributes tab > Add attribute > e.g.: phone > Save enter image description here

  • Select Clients > click on Client ID > go to Mappers Tab > create mapper

    enter image description here

    enter image description here

    enter image description here

  • Get custom attributes

    enter image description here

    enter image description here

UPDATE

  • Add 'phone' attribute on Group level, assign user to that group, and you get 'phone' attribute from group level for all users

  • Go back to mapper and update 'phone' with 'Aggregate attribute values = true' and 'Multivalued=true', and you get 'phone' as list with both attributes from group and user level. If you keep 'Aggregate attribute values = false' or 'Multivalued=false', you get just one value, where 'phone' attribute from user will override 'phone' attribute from group (which make sense)

like image 45
Neeraj Benjwal Avatar answered Oct 17 '22 20:10

Neeraj Benjwal


For Keycloak > 18 the configuration of the mappers has moved in the UI:

Inside Clients > Your selected client under the tab Client Scopes, one has to select account-dedicated:

enter image description here

There custom mappers can be added:

enter image description here

like image 3
Stefan Avatar answered Oct 17 '22 21:10

Stefan