Skip to content

Latest commit

 

History

History
121 lines (99 loc) · 8.57 KB

File metadata and controls

121 lines (99 loc) · 8.57 KB

Keycloak custom protocol mapper example / customize JWT tokens

Per default Keycloak writes a lot of things into the JWT tokens, e.g. the preferred username. If that is not enough, a lot of additional built in protocol mappers can be added to customize the JWT token created by Keycloak even further. They can be added in the client section via the mappers tab (see the documentation). But sometimes the build in protocol mappers are not enough. If this is the case, an own protocol mapper can be added to Keycloak via an (not yet) official service provider API. This project shows how this can be done.

Entrypoints into this project

  1. data-setup: Project to configure Keycloak via its REST API. Configures a realm so that it uses the example protocol mapper. Contains a main method which can be executed against a running Keycloak instance. Doesn't need to be executed manually because it's executed automatically by the docker-entrypoint.sh during startup.
  2. protocol-mapper: Contains the protocol mapper code. The resulting jar file will be deployed to Keycloak. I tried to explain things needed in comments in the protocol-mapper project
  3. Dockerfile: Adds the jar file containing the protocol mapper, created by the protocol-mapper project, to the keycloak instance.

Try it out

To try it out do the following things:

Konfiguration of keycloak

  1. If you have already started this project and changed something, execute docker-compose down -v so that the volumes and so on are destroyed. Otherwise the old keycloak in memory database might be reused or you might not see your changed data.
  2. Start build and start keycloak using docker: docker-compose up --build.
  3. After the keycloak has been started, the main class DataSetupMain in our data-setup module should be started automatically by the docker-entrypoint.sh in the Dockerfile and should add some example data to the keycloak instance. You should see the message The data has been imported in the console if it has been executed successfully.
  4. Now you can open the Keycloak admin console and login with username / password: admin / password. This initial password for the admin user were configured in our docker-compose file.
  5. You should see that the master and an example realm, which was added by the data-setup module automatically, exists currently. For this example realm the hello world mapper is configured: Keycloak screenshot

Now Keycloak is configured. As a next step we want to check the token.

Checking the access token

To check the token, we need to login. To get the tokens using the direct flow (not recommended for production usage, just for easy demo purposes. See this page) execute the following curl command:

curl -d 'client_id=example-realm-client' -d 'username=jdoe' -d 'password=password' -d 'grant_type=password' 'http://localhost:11080/auth/realms/example-realm/protocol/openid-connect/token'

Note that using the direct flow is only possible because we configured keycloak to allow it in the RealmSetup class. Response should be like:

{
  "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p4dQ1-6N0USu161eiyRV5YaAlY",
  "token_type": "Bearer",
  "not-before-policy": 0,
  "session_state": "56ad924f-d38c-4807-be15-86a11eab8c6a",
  "scope": "email profile"
}

Then copy the access_token value and decode it, e.g. by using jwt.io. You'll get something like the following:

  {
     "exp": 1664905189,
     "iat": 1664904889,
     "jti": "e107276d-d975-4222-bdb5-482cf978a998",
     "iss": "http://localhost:11080/auth/realms/example-realm",
     "aud": "account",
     "sub": "4b95166c-4137-4dd6-8a0d-389241ea5c3b",
     "typ": "Bearer",
     "azp": "example-realm-client",
     "session_state": "56ad924f-d38c-4807-be15-86a11eab8c6a",
     "acr": "1",
     "realm_access": {
     "roles": [
        "default-roles-example-realm",
        "offline_access",
        "uma_authorization"
     ]
     },
     "resource_access": {
        "account": {
           "roles": [
           "manage-account",
           "manage-account-links",
           "view-profile"
           ]
        }
     },
     "scope": "email profile",
     "sid": "56ad924f-d38c-4807-be15-86a11eab8c6a",
     "email_verified": false,
     "name": "John Doe",
     "groups": [],
     "preferred_username": "jdoe",
     "given_name": "John",
     "family_name": "Doe",
     "example": {
        "message": "hello world"
     }
  }

The value auf our own Hello World Token mapper got added to the token because the message 'hello world' appears in the example.message field.

Acknowledgements

Links