Created
May 26, 2017 09:44
-
-
Save stevehobbsdev/0a4e956dc6a9459bf8c9a0609c082765 to your computer and use it in GitHub Desktop.
Example Json Web Token generator in NodeJS
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
const crypto = require('crypto') | |
const base64url = require('base64-url') | |
const encode = input => base64url.encode(input) | |
const header = JSON.stringify({ | |
alg: "HS256", | |
"typ": "JWT" | |
}) | |
const payload = JSON.stringify({ | |
name: "Joe Bloggs", | |
eml: "[email protected]", | |
adm: true, | |
cid: 10 | |
}) | |
const secret = "00}R`nP1E%L$b0." | |
const hmac = crypto.createHmac('sha256', secret) | |
hmac.update(`${encode(header)}.${encode(payload)}`) | |
console.log(`${encode(header)}.${encode(payload)}.${base64url.encode(hmac.digest())}`) |
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
{ | |
"name": "jwt-test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"base64-url": "^1.3.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment