Skip to content

Instantly share code, notes, and snippets.

@ku
Created September 1, 2017 01:14
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 ku/88238f54a28f95e49e0a7c5005e93df3 to your computer and use it in GitHub Desktop.
Save ku/88238f54a28f95e49e0a7c5005e93df3 to your computer and use it in GitHub Desktop.
pure json react native friendly JWT
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