Created
November 9, 2018 13:28
-
-
Save fungiboletus/b4a247daeec774628fd6a8d2a8bc13c9 to your computer and use it in GitHub Desktop.
JWT.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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