Skip to content

Instantly share code, notes, and snippets.

@njh
Created September 21, 2022 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njh/50db188efc1c52bc101d974bee3c6c92 to your computer and use it in GitHub Desktop.
Save njh/50db188efc1c52bc101d974bee3c6c92 to your computer and use it in GitHub Desktop.
Generate Insecure JWT tokens in JavaScript
function base64urlencode(obj) {
if (obj instanceof Buffer) {
buf = obj
} else if (typeof obj === 'string') {
buf = Buffer.from(obj)
} else {
json = JSON.stringify(obj, null, 0)
buf = Buffer.from(json)
}
return buf.toString('base64')
.replace(/\+/g, '-') // 62nd char of encoding
.replace(/\//g, '_') // 63rd char of encoding
.replace(/=*$/,'') // Remove padding
}
header = {
"typ": "JWT",
"alg": "none"
}
now = Math.floor(new Date().getTime() / 1000)
payload = {
"sub": "1234567890",
"name": "John Doe",
"exp": now + 86400,
"iat": now
}
jwt = base64urlencode(header) + '.' + base64urlencode(payload) + '.' + base64urlencode('')
console.log(jwt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment