Skip to content

Instantly share code, notes, and snippets.

@tiagorangel2011
Last active April 23, 2024 17:58
Show Gist options
  • Save tiagorangel2011/8db4ee6b9ef07624a66719861455332a to your computer and use it in GitHub Desktop.
Save tiagorangel2011/8db4ee6b9ef07624a66719861455332a to your computer and use it in GitHub Desktop.
JWT decoder used for Authflow
// 💡 Tip: need to decode with node.js? Use the @authflow-js/verify npm module
function decryptJWT(e) {
function b64(e) {
return decodeURIComponent(Array.prototype.map.call(atob(e), function (e) {
return "%" + ("00" + e.charCodeAt(0).toString(16)).slice(-2)
}).join(""))
}
try {
const r = e
.split(".")[0]
.replace("-", "+")
.replace("_", "/"),
b64
c = (JSON.parse(b64(r)), e.split(".")[1].replace("-", "+").replace("_", "/"));
return JSON.parse(b64(c))
} catch (e) {
throw new Error("Invalid JWT")
}
}
@cmdwm
Copy link

cmdwm commented Mar 15, 2023

Possibly the encoding algorithm used in that JWT is different than the one the function tests for, apologies!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment