Last active
May 28, 2021 18:07
-
-
Save EarlGeorge/be1dd967904a02cdd1a31b4f79648cd8 to your computer and use it in GitHub Desktop.
JWT Tokens
This file contains hidden or 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
const base64url = require("base64url"); | |
const crypto = require("crypto"); | |
const signatureFunction = crypto.createSign("RSA-SHA256"); | |
const verifyFunction = crypto.createVerify("RSA-SHA256"); | |
const fs = require("fs"); | |
/** | |
* ISSUANCE | |
**/ | |
const headerObj = { | |
alg: "RS256", | |
typ: "JWT", | |
}; | |
const payloadObj = { | |
sub: "1234567890", | |
name: "John Doe", | |
admin: true, | |
iat: 1516239022, | |
}; | |
const headerObjString = JSON.stringify(headerObj); | |
const payloadObjString = JSON.stringify(payloadObj); | |
const base64UrlHeader = base64url(headerObjString); | |
const base64UrlPayload = base64url(payloadObjString); | |
signatureFunction.write(base64UrlHeader + "." + base64UrlPayload); | |
signatureFunction.end(); | |
const PRIV_KEY = fs.readFileSync(__dirname + "/priv_key.pem", "utf8"); | |
const signatureBase64 = signatureFunction.sign(PRIV_KEY, "base64"); | |
const signatureBase64Url = base64url.fromBase64(signatureBase64); | |
console.log(signatureBase64Url); | |
// END ISSUANCE | |
// VERIFICATION | |
const JWT = | |
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.POstGetfAytaZS82wHcjoTyoqhMyxXiWdR7Nn7A29DNSl0EiXLdwJ6xC6AfgZWF1bOsS_TuYI3OG85AmiExREkrS6tDfTQ2B3WXlrr-wp5AokiRbz3_oB4OxG-W9KcEEbDRcZc0nH3L7LzYptiy1PtAylQGxHTWZXtGz4ht0bAecBgmpdgXMguEIcoqPJ1n3pIWk_dUZegpqx0Lka21H6XxUTxiy8OcaarA8zdnPUnV6AmNP3ecFawIFYdvJB_cm-GvpCSbr8G8y_Mllj8f4x9nBH8pQux89_6gUY618iYv7tuPWBFfEbLxtF2pZS6YC1aSfLQxeNe8djT9YjpvRZA"; | |
const jwtParts = JWT.split("."); | |
const headerInBase64UrlFormat = jwtParts[0]; | |
const payloadInBase64UrlFormat = jwtParts[1]; | |
const signatureInBase64UrlFormat = jwtParts[2]; | |
verifyFunction.write(headerInBase64UrlFormat + "." + payloadInBase64UrlFormat); | |
verifyFunction.end(); | |
const jwtSignatureBase64 = base64url.toBase64(signatureInBase64UrlFormat); | |
const PUB_KEY = fs.readFileSync(__dirname + "/pub_key.pem", "utf8"); | |
const signatureIsValid = verifyFunction.verify( | |
PUB_KEY, | |
jwtSignatureBase64, | |
"base64" | |
); | |
console.log(signatureIsValid); |
This file contains hidden or 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
const jwt = require('jsonwebtoken'); | |
const fs = require("fs"); | |
const PUB_KEY = fs.readFileSync(__dirname + "/pub_key.pem", "utf8"); | |
const PRIV_KEY = fs.readFileSync(__dirname + "/priv_key.pem", "utf8"); | |
const payloadObj = { | |
sub: "1234567890", | |
name: "John Doe", | |
admin: true, | |
iat: 1516239022, | |
}; | |
const signedJWT = jwt.sign(payloadObj, PRIV_KEY, { algorithm: "RS256" }); | |
jwt.verify(signedJWT, PUB_KEY, { algorithms: ["RS256"] }, (err, payload) => { | |
console.log(payload); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment