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 Administrator in 2027: A Step-by-Step Career Roadmap
Want to become a Salesforce Administrator? Get the complete 2026 roadmap skills, certification, salary & career path. Start your…
Salesforce Salesforce Admin Self Study vs Live Training: How to Choose the Right Path
Compare Salesforce Admin self-study and live training by cost, duration, flexibility, mentorship, and career support. Find the…
Salesforce How a Salesforce Admin Certification Shapes Your Career Growth
Wondering if a salesforce admin certification is worth it? See how it shapes hiring, salary, and long term salesforce admin…
Salesforce Salesforce Admin vs Developer Salary: Which Path Actually Pays More?
Salesforce admin vs developer salary: developers earn 15–25% more. See entry, mid and senior pay, why the gap exists, and how…