Search code examples
oauth-2.0keycloak

OAuth2 (Keycloak): client_credentials grant type additional claims


I have a scenario wherein there are multiple apps/service that need to access my Resource:

These are the three external services: service1, service2, service3.

Now I am using the client_credentials grant type for this since this is machine to machine and no user is involved.

What I did is to create 1 client for each service in Keycloak. It works fine, however I need to add some more details in the token, in particular, a human readable name for each client as the client id is named using the recommended naming.

I know how to map user claims to the token, but is it possible to have something similar for my scenario wherein there are no users and I only have the clients?


Solution

  • Yes, go to the Realm of your app, then:

    • Go to Clients;
    • Select your client;
    • Select Mappers;
    • click on the button [Create];
    • select Hardcoded Claim as Mapper Type;
    • fill up the details of the claim

    Click [Save].


    Update answer with more detail

    I know how to map user claims to the token, but is it possible to have something similar for my scenario wherein there are no users and I only have the clients?

    Yes, you need to create 'Hardcoded Claim Mapper.

    For that:

    • Select the realm of your app
    • Go to clients
    • Select the appropriate client for your use-case

    (For the OLD Keycloak UI)

    • Go to Mappers
    • Click Create
    • In Mapper type select Hardcoded claim
    • Fill up the details of the claim, accordingly.
    • Click on Save

    (For the NEW Keycloak UI)

    • Go to the tab Client Scopes
    • Click on the client scope <the client ID of your client>-dedicated (e.g., test-dedicated in the picture below)

    enter image description here

    • Click on Configure a new mapper (or Add Mapper > By configuration if you have already created mappers before for this client)

    enter image description here

    • Select Hardcoded claim
    • Fill up the details of the claim, accordingly.
    • Click on Save

    The client will be the client that you are using to authenticate against.