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?


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();

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


        //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.