Widget Identity
Widget Access Key and Secret Key
Generating JWT for Secure Communication (Node.js)
const jwt = require('jsonwebtoken');
// --- Your Assigned Keys ---
const widgetAccessKey = 'wgt_ak_ae8e70cd2d7ee69c9e739562e'; // Your assigned access key
const widgetSecretKey = 'YOUR_ASSIGNED_SECRET_KEY'; // Your assigned secret key - Keep this secret!
// --- Define JWT Payload (Claims) ---
// The payload must include required standard claims.
// 'iss' must be your accessKey.
// 'exp' and 'nbf' define the token's validity period.
// Timestamps are in seconds (Unix time).
const currentTimeInSeconds = Math.floor(Date.now() / 1000);
const jwtPayload = {
iss: widgetAccessKey, // Issuer: Set to your widget's accessKey
iat: currentTimeInSeconds, // Issued At: Current timestamp
exp: currentTimeInSeconds + (60 * 5), // Expiration Time: e.g., 5 minutes from now
nbf: currentTimeInSeconds - (60 * 1), // Not Before: e.g., 1 minute ago (allows for clock skew)
// You can add other custom claims here if needed by the API
// userId: 'user123',
// orderId: 'ORDER456'
};
// --- Generate and Sign the JWT ---
// jwt.sign creates the Header, Payload, and Signature.
// It uses the payload object, your secret key, and the signing algorithm (default is HS256).
const token = jwt.sign(jwtPayload, widgetSecretKey); // HS256 is the default algorithm if not specified
console.log("Payload used for JWT:", jwtPayload);
console.log("Generated JWT for secure communication:");
console.log(token);
// --- How to use the JWT ---
// You would typically include this generated 'token' in an HTTP header (e.g., 'Authorization: Bearer <token>')
// or within the request body when calling order management APIs that require this authentication.
// --- Verification (Recipient Side - Conceptual) ---
// The recipient (e.g., portal API) would receive the token, extract the payload,
// get the 'iss' (accessKey), look up the corresponding secretKey, and verify the signature
// and validate the claims (exp, nbf, etc.).
/*
jwt.verify(token, widgetSecretKey, (err, decodedPayload) => {
if (err) {
console.error('JWT verification failed:', err.message);
} else {
console.log('JWT is valid. Decoded Payload:', decodedPayload);
// Check decodedPayload.iss matches the expected issuer
// Check other claims as needed
}
});
*/Explanation:
Last updated