1

How do I add additional header properties to my JWT using the JWTBearerTokenExchange Class? (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Auth_JWTBearerTokenExchange.htm#apex_class_Auth_JWTBearerTokenExchange)

The token I need to sign requires a an additional property "kid" (Key ID) but I don't think there is a method to add this?

1

kid(Key ID) is a custom claim, you can add your custom claim to the JWT using .setAdditionalClaims . JWT Claims are basically Key-value pairs like Map, so you have to create a map and provide your values. Here your kid is a private-claim

        Auth.JWT jwt = new Auth.JWT();
        jwt.setSub('user@salesforce.com'); 
        jwt.setAud('https://login.salesforce.com'); 
        jwt.setIss('3MVG99OxTyEMCQ3gNp2PjkqeZKxnmAiG1xV4oHh9AKL_rSK.BoSVPGZHQ​ukXnVjzRgSuQqGn75NL7yfkQcyy7');

        //Additional claims to set scope
        Map<String, Object> claims = new Map<String, Object>();
        claims.put('kid', 'myKeyId');

        jwt.setAdditionalClaims(claims);

        //Create the object that signs the JWT bearer token
        Auth.JWS jws = new Auth.JWS(jwt, 'CertFromCertKeyManagement');

        //Get the resulting JWS in case debugging is required
        String token = jws.getCompactSerialization();

        //Set the token endpoint that the JWT bearer token is posted to
        String tokenEndpoint = 'https://login.salesforce.com/services/oauth2/token';

        //POST the JWT bearer token
        Auth.JWTBearerTokenExchange bearer = new Auth.JWTBearerTokenExchange(tokenEndpoint, jws);

        //Get the access token
        String accessToken = bearer.getAccessToken();
  • kid is an optional header. I know we can use setAdditionalClaims for additional claims but there doesn't seem to be a method to add a header. – Kurusu Feb 12 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.