1

I'm trying to retrieve the claims, audience, issuer, subject from a JWT in apex, according to this documentation:

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Auth_JWT.htm

However it doesn't work obviously.

I receive a JWT as a string from the front end, I verify it with crypto.verify, and then I want to retrieve at least the audience, issuer, and subject.

When trying this

String aud = jwt.getAud();

I receive this error

Method does not exist or incorrect signature: void getAud() from the type String

How should I do?

1 Answer 1

2

Grab the payload, then deserialize it:

String[] parts = jwt.split('\\.');
Blob payloadDecoding = EncodingUtil.base64Decode(parts[1]); 
Map<String, Object> tokens = (Map<String, Object>)JSON.deserializeUntyped(payloadDecoding.toString());
String audience = (String)tokens.get('aud');

JWTs have three parts, separated by ., the header, payload, and signature. You can decode the first and second parts directly into JSON, while the third is encoded binary data for use to verify a token's authenticity.

1
  • thanks for the solutionn!
    – 2095377
    May 1, 2023 at 12:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .