Create a JSON Web Token
Last update: May 14, 2024
- Topics:
- Adaptive Forms
CREATED FOR:
- Beginner
- Intermediate
- Developer
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties. JWT.io libraries were used in this sample to generate the JWT.
The service credentials that you have downloaded in the previous step contains the private key in the PKCS#1 format.To extract the private key from this string we have used BouncyCastle libraries. The crypto libraires that are part of java do not support PKCS#1 format.
The following code was used to generate the JWT:
public String getJWTToken()
{
Security.addProvider(new BouncyCastleProvider());
RSAPrivateKey privateKey = null;
GetServiceCredentials getCredentials = new GetServiceCredentials();
try
{
long now = System.currentTimeMillis();
Long expirationTime = now + TimeUnit.MINUTES.toMillis(5);
// get the private key string from the service credentials
String privateKeyString = getCredentials.getPRIVATE_KEY();
//The JWT signature algorithm we use to sign the token
SignatureAlgorithm sa = SignatureAlgorithm.RS256;
Reader targetReader = new StringReader(privateKeyString);
// PEMParser removes the unnecessary headers and decodes the underlying Base64 PEM data into a binary format.
PEMParser pemParser = new PEMParser(targetReader);
// tores the result generated by the pEMParser
Object object = pemParser.readObject();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
KeyPair kp = converter.getKeyPair((PEMKeyPair) object);
privateKey = (RSAPrivateKey) kp.getPrivate();
//Let's set the JWT Claims
Map < String, Object > jwtClaims = new HashMap < String, Object > ();
jwtClaims.put("iss", getCredentials.getORG_ID());
jwtClaims.put("sub", getCredentials.getTECH_ACCT());
jwtClaims.put("exp", expirationTime);
jwtClaims.put("aud", "https://" + getCredentials.getIMS_ENDPOINT() + "/c/" + getCredentials.getCLIENT_ID());
String metascopes[] = new String[] { getCredentials.getMETASCOPE_ID() };
for (String metascope: metascopes)
{
jwtClaims.put("https://" + getCredentials.getIMS_ENDPOINT() + "/s/" + metascope, java.lang.Boolean.TRUE);
}
// Create the final JWT token
String jwtToken = Jwts.builder().setClaims(jwtClaims).signWith(sa, privateKey).compact();
System.out.println("Got JWT Token " + jwtToken);
pemParser.close();
return jwtToken;
} catch (IOException e) {
System.out.println("The error is " + e.getMessage());
}
return null;
}
Previous pageCreate Service Credentials
Next pageCreate Access Token
Experience Manager
- Overview
- Playlists
- Introduction to AEM as a Cloud Service
- Experience Cloud integrations
- Underlying Technology
- Edge Delivery Services
- Cloud Manager
- Local Development Environment Setup
- Developing
- Debugging AEM
- AEM APIs
- Content Delivery
- Caching
- Accessing AEM
- Authentication
- Advanced Networking
- Security
- AEM Eventing
- Migration
- Content Transfer Tool
- Bulk Import of assets
- Moving to AEM as a Cloud Service
- Cloud Acceleration Manager
- Content Fragments
- Forms
- Developing for Forms as a Cloud Service
- 1 - Getting started
- 2 - Install IntelliJ
- 3 - Setup Git
- 4 - Sync IntelliJ with AEM
- 5 - Build a form
- 6 - Custom Submit Handler
- 7 - Registering servlet using resource type
- 8 - Enable Forms Portal Components
- 9 - Include Cloud Services and FDM
- 10 - Context aware cloud configuration
- 11 - Push to Cloud Manager
- 12 - Deploy to development environment
- 13 - Updating maven archetype
- Create Adaptive Form
- Custom submit service with headless form
- Create address block component
- Create clickable image component
- AEM Forms and Analytics
- Creating Countries Dropdown Component
- Creating Button Variations
- Using vertical tabs
- Using output and forms service
- Document Generation in AEM Forms CS
- Using Forms Document Services API
- Document Generation using Batch API
- PDF Manipulation in Forms CS
- Integrate with Marketo
- Store Form Submissions with Blob Index Tags
- Prefill core component based form
- Azure Portal Storage
- Save and Resume form filling
- Create Review Workflow
- Acrobat Sign with AEM Forms
- Integrate with Microsoft Power Automate
- Integrate with Microsoft Dynamics
- Integrate with Salesforce
- Store form submissions in one drive and sharepoint
- Developing for Forms as a Cloud Service
- Asset Compute Extensibility
- Multi-step Tutorials
- Expert Resources