Created
September 1, 2017 01:14
-
-
Save ku/88238f54a28f95e49e0a7c5005e93df3 to your computer and use it in GitHub Desktop.
pure json react native friendly JWT
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 base64 = require('base-64') | |
const base64url = (s) => base64.encode(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '') | |
const CryptoJS = require( 'crypto-js') | |
const hmacSHA256 = CryptoJS.HmacSHA256 | |
var seed = { | |
sub: "1234567890", | |
name: "John Doe", | |
admin: true | |
} | |
const payload = JSON.stringify(seed) | |
const hs256 = {"alg":"HS256",typ:"JWT"} | |
const header = JSON.stringify(hs256) | |
const signature = hmacSHA256([ | |
base64url(header), | |
base64url(payload), | |
].join('.'), 'secret') | |
console.log( | |
[ | |
base64url(header), | |
base64url(payload), | |
signature.toString(CryptoJS.enc.Base64).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '') | |
].join('.') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment