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
-
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>> {@Overridepublic Map<String, Object> convert(Map<String, Object> source) {// implementation of the converter}} -
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
roleclaim touserroles - renaming the
userExtIdclaim toext_id - mapping the
languageclaim to thelocaleclaim - setting the
ctxclaim toUSER(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 {@Overridepublic 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;}} - renaming the
-
Activate the converter by naming its bean in the authorization server or B2B gateway configuration with the
claim-set-converter-nameproperty. 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 -
When using the
auth-serversconfiguration 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.chjwk-set-uri: https://issuer-one.example.ch/jwksclaim-set-converter-name: firstClaimSetConverter- issuer: https://issuer-two.example.chjwk-set-uri: https://issuer-two.example.ch/jwksclaim-set-converter-name: secondClaimSetConverter
Related
- Integrating different authorization servers — what a claim set converter is and the
claim-set-converter-nameconfiguration property reference. - Tokens in the Blueprint Microservice — the claims jEAP Security expects in an access token.
- How to: call a secured REST API from Java — the client side of calling a REST API protected by jEAP Security.