Skip to main content

How to: implement a custom claim set converter

Implement this when an authorization server or B2B gateway issues access tokens whose claims don't match the format jEAP Security expects, so the claims expected by jEAP Security must be derived from the claims actually present in the token.

Steps​

  1. Implement a Spring converter that maps a claim set to another claim set, and expose it as a Spring bean:

    @Component("eiamClaimSetConverter")
    public class EiamClaimSetConverter implements Converter<Map<String, Object>, Map<String, Object>> {

    @Override
    public Map<String, Object> convert(Map<String, Object> source) {
    // implementation of the converter
    }

    }
  2. Implement the claim mapping logic. The following example (EiamClaimSetConverter.java in the repository jeap-spring-boot-starters) adapts the claims of an access token issued by an authorization server (called eIAM) to jEAP Security by

    • renaming the role claim to userroles
    • renaming the userExtId claim to ext_id
    • mapping the language claim to the locale claim
    • setting the ctx claim to USER (assuming the authorization server eIAM only manages users (no systems))
    package ch.admin.bit.jeap.security.resource.claimsetconverter;

    import org.springframework.lang.NonNull;

    import java.util.HashMap;
    import java.util.Map;

    public class EiamClaimSetConverter extends AbstractClaimSetConverter {

    @Override
    public Map<String, Object> doConvert(@NonNull Map<String, Object> claims) {
    Map<String, Object> mappedClaims = new HashMap<>(claims);
    renameClaim("role", "userroles", mappedClaims);
    renameClaim("userExtId", "ext_id", mappedClaims);
    mapClaim("language", "locale", mappedClaims,
    language -> language.toString().toUpperCase());
    mappedClaims.put("ctx", "USER");
    return mappedClaims;
    }

    }
  3. Activate the converter by naming its bean in the authorization server or B2B gateway configuration with the claim-set-converter-name property. The following example (application-local.yml in the repository jme-security-oauth2-example) activates the converter from step 2:

    jeap:
    security:
    oauth2:
    resourceserver:
    authorization-server:
    issuer: "http://localhost:8081/jme-security-oauth2-auth-scs"
    jwk-set-uri: "${jeap.security.oauth2.resourceserver.authorization-server.issuer}/.well-known/jwks.json"
    claim-set-converter-name: eiamClaimSetConverter
  4. When using the auth-servers configuration option, configure a claim set converter name separately on each list entry. Each entry can reference a different converter bean, or several entries can reference the same bean:

    jeap:
    security:
    oauth2:
    resourceserver:
    auth-servers:
    - issuer: https://issuer-one.example.ch
    jwk-set-uri: https://issuer-one.example.ch/jwks
    claim-set-converter-name: firstClaimSetConverter
    - issuer: https://issuer-two.example.ch
    jwk-set-uri: https://issuer-two.example.ch/jwks
    claim-set-converter-name: secondClaimSetConverter