Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak: mapping username on subject claim

Tags:

keycloak

I need to configure Keycloak so that it creates a JWT with claim "sub" populated with the username, instead of the default userId in sub.

It means that instead of this token:

{
    "jti": "b1384883-9b59-4788-b09f-98b40b7e3c3b",
   ...
    "sub": "fbdb4e4a-6e93-4b08-a1e7-0b7bd08520a6",
    "preferred_username": "m123456"
}

I need to receive:

 {
    "jti": "b1384883-9b59-4788-b09f-98b40b7e3c3b",
    ...
    "sub": "m123456",
    "preferred_username": "m123456"
 }

Could you please suggest how to do that?

I tried username mapper, but it adds a second "sub" claim and with the jwt is not valid.

like image 800
Nicola Ben Avatar asked Dec 17 '22 16:12

Nicola Ben


1 Answers

or this way: with User Property Mapper type.

mapping user property to claim

    {
      "id": "5d45fe41-83c6-4457-807b-5240ff7c09b9",
      "name": "UsernameInSubject",
      "protocol": "openid-connect",
      "protocolMapper": "oidc-usermodel-property-mapper",
      "consentRequired": false,
      "config": {
        "userinfo.token.claim": "true",
        "user.attribute": "username",
        "id.token.claim": "true",
        "access.token.claim": "true",
        "claim.name": "sub",
        "jsonType.label": "String"
      }
like image 126
Shuky Avatar answered Feb 12 '23 17:02

Shuky