Skip to content

Instantly share code, notes, and snippets.

@stevehobbsdev
Created May 26, 2017 09:44
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 stevehobbsdev/0a4e956dc6a9459bf8c9a0609c082765 to your computer and use it in GitHub Desktop.
Save stevehobbsdev/0a4e956dc6a9459bf8c9a0609c082765 to your computer and use it in GitHub Desktop.
Example Json Web Token generator in NodeJS
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: "test@test.com",
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())}`)
{
"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