Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 763 #811

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
@@ -1,6 +1,8 @@
const signObj = require('./sign');
module.exports = {
verify: require('./verify'),
sign: require('./sign'),
sign: signObj.sign,
supported_algorithms: signObj.SUPPORTED_ALGS,
JsonWebTokenError: require('./lib/JsonWebTokenError'),
NotBeforeError: require('./lib/NotBeforeError'),
TokenExpiredError: require('./lib/TokenExpiredError'),
Expand All @@ -9,4 +11,4 @@ module.exports = {
Object.defineProperty(module.exports, 'decode', {
enumerable: false,
value: require('./decode'),
});
});
26 changes: 17 additions & 9 deletions sign.js
Expand Up @@ -15,9 +15,9 @@ if (PS_SUPPORTED) {
}

var sign_options_schema = {
expiresIn: { isValid: function(value) { return isInteger(value) || (isString(value) && value); }, message: '"expiresIn" should be a number of seconds or string representing a timespan' },
notBefore: { isValid: function(value) { return isInteger(value) || (isString(value) && value); }, message: '"notBefore" should be a number of seconds or string representing a timespan' },
audience: { isValid: function(value) { return isString(value) || Array.isArray(value); }, message: '"audience" must be a string or array' },
expiresIn: { isValid: function (value) { return isInteger(value) || (isString(value) && value); }, message: '"expiresIn" should be a number of seconds or string representing a timespan' },
notBefore: { isValid: function (value) { return isInteger(value) || (isString(value) && value); }, message: '"notBefore" should be a number of seconds or string representing a timespan' },
audience: { isValid: function (value) { return isString(value) || Array.isArray(value); }, message: '"audience" must be a string or array' },
algorithm: { isValid: includes.bind(null, SUPPORTED_ALGS), message: '"algorithm" must be a valid string enum value' },
header: { isValid: isPlainObject, message: '"header" must be an object' },
encoding: { isValid: isString, message: '"encoding" must be a string' },
Expand All @@ -40,7 +40,7 @@ function validate(schema, allowUnknown, object, parameterName) {
throw new Error('Expected "' + parameterName + '" to be a plain object.');
}
Object.keys(object)
.forEach(function(key) {
.forEach(function (key) {
var validator = schema[key];
if (!validator) {
if (!allowUnknown) {
Expand Down Expand Up @@ -79,7 +79,7 @@ var options_for_objects = [
'jwtid',
];

module.exports = function (payload, secretOrPrivateKey, options, callback) {
function sign(payload, secretOrPrivateKey, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
Expand All @@ -88,7 +88,8 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
}

var isObjectPayload = typeof payload === 'object' &&
!Buffer.isBuffer(payload);
!Buffer.isBuffer(payload);


var header = Object.assign({
alg: options.algorithm || 'HS256',
Expand Down Expand Up @@ -117,15 +118,15 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
return failure(error);
}
if (!options.mutatePayload) {
payload = Object.assign({},payload);
payload = Object.assign({}, payload);
}
} else {
var invalid_options = options_for_objects.filter(function (opt) {
return typeof options[opt] !== 'undefined';
});

if (invalid_options.length > 0) {
return failure(new Error('invalid ' + invalid_options.join(',') + ' option for ' + (typeof payload ) + ' payload'));
return failure(new Error('invalid ' + invalid_options.join(',') + ' option for ' + (typeof payload) + ' payload'));
}
}

Expand Down Expand Up @@ -201,6 +202,13 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
callback(null, signature);
});
} else {
return jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding});
return jws.sign({ header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding });
}
};



module.exports = {
sign,
SUPPORTED_ALGS,
}