-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notable changes: 1. `expiresInMinutes` and `expiresInSeconds` are deprecated and no longer supported. 2. `notBeforeInMinutes` and `notBeforeInSeconds` are deprecated and no longer supported. 3. options are properly validated. 4. `options.expiresIn`, `options.notBefore`, `options.audience`, `options.issuer`, `options.subject` and `options.jwtid` are mutually exclusive with `payload.exp`, `payload.nbf`, `payload.aud`, `payload.iss`, `payload.sub` and `payload.jti` respectively. 5. `options.algorithm` is properly validated. 6. `options.headers` is renamed to `options.header`.
- Loading branch information
1 parent
e32043b
commit 53c3987
Showing
10 changed files
with
132 additions
and
143 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
var Joi = require('joi'); | ||
var timespan = require('./lib/timespan'); | ||
var xtend = require('xtend'); | ||
var jws = require('jws'); | ||
|
||
var sign_options_schema = Joi.object().keys({ | ||
expiresIn: [Joi.number().integer(), Joi.string()], | ||
notBefore: [Joi.number().integer(), Joi.string()], | ||
audience: [Joi.string(), Joi.array()], | ||
algorithm: Joi.string().valid('RS256','RS384','RS512','ES256','ES384','ES512','HS256','HS384','HS512','none'), | ||
header: Joi.object(), | ||
encoding: Joi.string(), | ||
issuer: Joi.string(), | ||
subject: Joi.string(), | ||
jwtid: Joi.string(), | ||
noTimestamp: Joi.boolean() | ||
}); | ||
|
||
var options_to_payload = { | ||
'audience': 'aud', | ||
'issuer': 'iss', | ||
'subject': 'sub', | ||
'jwtid': 'jti' | ||
}; | ||
|
||
module.exports = function(payload, secretOrPrivateKey, options, callback) { | ||
options = options || {}; | ||
|
||
var header = xtend({ | ||
alg: options.algorithm || 'HS256', | ||
typ: typeof payload === 'object' ? 'JWT' : undefined | ||
}, options.header); | ||
|
||
if (typeof payload === 'undefined') { | ||
throw new Error('payload is required'); | ||
} else if (typeof payload === 'object') { | ||
payload = xtend(payload); | ||
} | ||
|
||
if (typeof payload.exp !== 'undefined' && typeof options.expiresIn !== 'undefined') { | ||
throw new Error('Bad "options.expiresIn" option the payload already has an "exp" property.'); | ||
} | ||
|
||
if (typeof payload.nbf !== 'undefined' && typeof options.notBefore !== 'undefined') { | ||
throw new Error('Bad "options.notBefore" option the payload already has an "nbf" property.'); | ||
} | ||
|
||
var validation_result = sign_options_schema.validate(options); | ||
|
||
if (validation_result.error) { | ||
throw validation_result.error; | ||
} | ||
|
||
var timestamp = payload.iat || Math.floor(Date.now() / 1000); | ||
|
||
if (!options.noTimestamp) { | ||
payload.iat = timestamp; | ||
} else { | ||
delete payload.iat; | ||
} | ||
|
||
if (typeof options.notBefore !== 'undefined') { | ||
payload.nbf = timespan(options.notBefore); | ||
if (typeof payload.nbf === 'undefined') { | ||
throw new Error('"notBefore" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60'); | ||
} | ||
} | ||
|
||
if (typeof options.expiresIn !== 'undefined' && typeof payload === 'object') { | ||
payload.exp = timespan(options.expiresIn); | ||
if (typeof payload.exp === 'undefined') { | ||
throw new Error('"expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60'); | ||
} | ||
} | ||
|
||
Object.keys(options_to_payload).forEach(function (key) { | ||
var claim = options_to_payload[key]; | ||
if (typeof options[key] !== 'undefined' && typeof payload[claim] !== 'undefined') { | ||
throw new Error('Bad "options.' + key + '" option. The payload already has an "' + claim + '" property.'); | ||
} | ||
payload[claim] = options[key]; | ||
}); | ||
|
||
var encoding = options.encoding || 'utf8'; | ||
|
||
if(typeof callback === 'function') { | ||
jws.createSign({ | ||
header: header, | ||
privateKey: secretOrPrivateKey, | ||
payload: JSON.stringify(payload), | ||
encoding: encoding | ||
}) | ||
.once('error', callback) | ||
.once('done', function(signature) { | ||
callback(null, signature); | ||
}); | ||
} else { | ||
return jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding}); | ||
} | ||
}; |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.