Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "jwt-encode", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Generate json webtokens in the browser", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
const CryptoJS = require('ts.cryptojs256'); | ||
/** | ||
* Default options for JWT signature | ||
*/ | ||
const defaultHeader = { alg: 'HS256', typ: 'JWT' }; | ||
/** | ||
* Return a base64 URL | ||
@@ -26,7 +31,3 @@ * | ||
function sign (data, secret, options = {}) { | ||
const defaultOptions = { | ||
alg: 'HS256', | ||
typ: 'JWT' | ||
}; | ||
const header = Object.assign(defaultOptions, options); | ||
const header = Object.assign(defaultHeader, options); | ||
if (header.alg !== 'HS256' && header.typ !== 'JWT') { | ||
@@ -36,8 +37,5 @@ throw new Error('jwt-encode only support the HS256 algorithm and the JWT type of hash'); | ||
const stringifiedHeader = CryptoJS.enc.Utf8.parse(JSON.stringify(header)); | ||
const encodedHeader = base64url(stringifiedHeader); | ||
const encodedHeader = encode(header); | ||
const encodedData = encode(data); | ||
const stringifiedData = CryptoJS.enc.Utf8.parse(JSON.stringify(data)); | ||
const encodedData = base64url(stringifiedData); | ||
let signature = `${encodedHeader}.${encodedData}`; | ||
@@ -49,2 +47,13 @@ signature = CryptoJS.HmacSHA256(signature, secret); | ||
/** | ||
* Safely base64url encode a JS Object in a way that is UTF-8 safe | ||
* | ||
* @param {Object} Javascript object payload to be encoded | ||
* @return {string} utf-8 safe base64url encoded payload | ||
*/ | ||
function encode (data) { | ||
const stringifiedData = CryptoJS.enc.Utf8.parse(JSON.stringify(data)); | ||
return base64url(stringifiedData); | ||
} | ||
module.exports = sign; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6753
49