Skip to content

Instantly share code, notes, and snippets.

@fungiboletus
Created November 9, 2018 13:28
Show Gist options
  • Save fungiboletus/b4a247daeec774628fd6a8d2a8bc13c9 to your computer and use it in GitHub Desktop.
Save fungiboletus/b4a247daeec774628fd6a8d2a8bc13c9 to your computer and use it in GitHub Desktop.
JWT.js
import { readFileSync } from 'fs';
import jose from 'node-jose';
const privateKeyPath = "./private-key.pem";
let privatekeyPem;
try {
privatekeyPem = readFileSync(privateKeyPath);
} catch (error) {
console.error(error.message);
process.exit(1);
}
let decryptMethod;
const privateKeyStore = jose.JWK.createKeyStore();
privateKeyStore.add(privatekeyPem, 'pem').then((privateKey) => {
decryptMethod = jose.JWE.createDecrypt(privateKey);
}, (reason) => {
console.error(reason.message);
process.exit(1);
});
async function decryptJwt(jwt) {
try {
const decryptedContent = await decryptMethod.decrypt(jwt);
const payload = JSON.parse(decryptedContent.payload);
return payload;
} catch (error) {
console.error(`Unable to decrypt the JWT: ${error.message}`);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment