About this question
Since a JWT token is sent to SFDC and needs to be decoded in Apex class, I tried to decode the JWT token in Apex. But I do not have any idea about how to do that. Can anyone please help me?
Since a JWT token is sent to SFDC and needs to be decoded in Apex class, I tried to decode the JWT token in Apex. But I do not have any idea about how to do that. Can anyone please help me?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on Aug 24, 2023
You can consider using EncodingUtil class and base64decode to convert an encrypted string to a Blob for JWT token decode. After the conversion, you can apply toString to turn it into a JSON string. You will also get some JSON methods in Apex to convert a string to an object.
Ranjana Admin JanBask Expert Latest answer
Answered on Jun 4, 2024
In Apex, you can decode a JWT (JSON Web Token) using Salesforce's Crypto class and the Blob and EncodingUtil classes. Here's a basic example of how you can decode a JWT token in Apex:
public class JWTDecoder {
// Method to decode a JWT token
public static Map decodeJWT(String jwtToken) {
// Split the token into its three parts: header, payload, and signature
List jwtParts = jwtToken.split('\.');
// Decode and parse the payload (second part)
String payloadJson = jwtParts[1];
String payloadDecoded = EncodingUtil.base64Decode(payloadJson).toString();
Map payloadMap = (Map) JSON.deserializeUntyped(payloadDecoded);
return payloadMap;
}
}To use this class, you can simply call the decodeJWT method and pass your JWT token as an argument:
String jwtToken = 'your_jwt_token_here';
Map decodedPayload = JWTDecoder.decodeJWT(jwtToken);
// Access the decoded payload values
String userId = (String) decodedPayload.get('sub');
String username = (String) decodedPayload.get('username');Keep in mind that this is a basic example, and you might need to handle exceptions, verify the token signature, and perform additional validation depending on your specific requirements and the JWT implementation you're working with. Additionally, ensure that you have the necessary permissions and access controls in place when working with sensitive data in JWT tokens.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Become a Salesforce Developer Without Experience
No coding background? Learn how to become a Salesforce Developer step by step - skills to build, projects to create, and how to…
Salesforce How to Become a Salesforce Admin With No Experience
No Salesforce Admin experience? Learn how to build real skills, create projects, and land an entry-level Salesforce Admin job…
Salesforce How Long Does It Take to Become a Salesforce Developer?
Wondering how long to learn Salesforce Developer? Most beginners need 6–12 months. See the study time, stage-by-stage roadmap and…
Salesforce How Long Does It Take to Become a Salesforce Admin?
Wondering how long to learn Salesforce Admin? Most beginners need 3–6 months. See the study time, stage-by-stage roadmap and…