mirror of
https://github.com/hanzoai/node-jsonwebtoken.git
synced 2026-09-24 17:00:48 +00:00
Merge branch 'simov-fix-headers-option'
This commit is contained in:
@@ -30,7 +30,7 @@ JWT.decode = function (jwt, options) {
|
||||
header: decoded.header,
|
||||
payload: payload,
|
||||
signature: decoded.signature
|
||||
}
|
||||
};
|
||||
}
|
||||
return payload;
|
||||
};
|
||||
@@ -38,7 +38,7 @@ JWT.decode = function (jwt, options) {
|
||||
JWT.sign = function(payload, secretOrPrivateKey, options) {
|
||||
options = options || {};
|
||||
|
||||
var header = ((typeof options.headers === 'object') && options.headers) || {};
|
||||
var header = {};
|
||||
|
||||
if (typeof payload === 'object') {
|
||||
header.typ = 'JWT';
|
||||
@@ -46,9 +46,9 @@ JWT.sign = function(payload, secretOrPrivateKey, options) {
|
||||
|
||||
header.alg = options.algorithm || 'HS256';
|
||||
|
||||
if (options.header) {
|
||||
Object.keys(options.header).forEach(function (k) {
|
||||
header[k] = options.header[k];
|
||||
if (options.headers) {
|
||||
Object.keys(options.headers).forEach(function (k) {
|
||||
header[k] = options.headers[k];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
var jwt = require('../index');
|
||||
var expect = require('chai').expect;
|
||||
|
||||
describe('set headers', function() {
|
||||
|
||||
it('should add the header', function () {
|
||||
var token = jwt.sign({foo: 123}, '123', { headers: { foo: 'bar' } });
|
||||
var decoded = jwt.decode(token, {complete: true});
|
||||
expect(decoded.header.foo).to.equal('bar');
|
||||
});
|
||||
|
||||
it('should allow overriding headers', function () {
|
||||
var token = jwt.sign({foo: 123}, '123', { headers: { alg: 'HS512' } });
|
||||
var decoded = jwt.decode(token, {complete: true});
|
||||
expect(decoded.header.alg).to.equal('HS512');
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user