Search code examples
spring-bootkeycloakkeycloak-rest-api

Keycloak set value Claim value to "hardcoded claim"


I have created custom protocol mapper with mapper type "hardcoded claim". Can I set "Claim value" field using keycloak API? I learned API specification and did not found anything. Is there a working example? I am using Springboot restAPI and Keycloak.


Solution

  • For that you can use the endpoint Update the mapper :

    PUT /{realm}/clients/<ID_OF_CLIENT>/protocol-mappers/models/<ROLE_MAPPER_ID>
    

    for example

    curl -X PUT http://$KEYCLOAK_IP/auth/admin/realms/<REALM_NAME>/clients/<ID_OF_CLIENT>/protocol-mappers/models/<MAPPER_ID>
    

    With the payload being the new updated configuration of your Mapper, for instance:

    {"id":"<MAPPER_ID>","name":"Hardcore","protocol":"openid-connect","protocolMapper":"oidc-hardcoded-claim-mapper","consentRequired":false,"config":{"claim.value":"<NEW_HARDCODE_VALUE>","userinfo.token.claim":"true","id.token.claim":"true","access.token.claim":"true","claim.name":"Name","jsonType.label":"String"}}
    

    The ID of Client you can get it via the Admin Console, by going to:

    • your Realm;
    • clients;
    • your client;
    • copy the id from the url (e.g., .../clients/10000d0a-aa00-00aa-00a0-aa0000000000).

    The mapper ID you can get it via the Admin console, by going to:

    • your Realm;
    • clients;
    • your client;
    • Mappers;
    • click on your mapper;
    • the ID will be on the url (e.g., ../mappers/10000d0a-aa00-00aa-00a0-aa0000000000)

    You can get both the ID of the Client and the Mapper by using this endpoint:

    http://$KEYCLOAK_IP/auth/admin/realms/<REALM_NAME>/clients?clientId=<CLIENT_ID>
    

    Extract from the JSON response that you will get the Client and Mapper IDs.