wow
This commit is contained in:
+71
-33
@@ -1,18 +1,21 @@
|
||||
var JsonWebTokenError = require('./lib/JsonWebTokenError');
|
||||
var NotBeforeError = require('./lib/NotBeforeError');
|
||||
var TokenExpiredError = require('./lib/TokenExpiredError');
|
||||
var decode = require('./decode');
|
||||
var timespan = require('./lib/timespan');
|
||||
var PS_SUPPORTED = require('./lib/psSupported');
|
||||
var jws = require('jws');
|
||||
const JsonWebTokenError = require('./lib/JsonWebTokenError');
|
||||
const NotBeforeError = require('./lib/NotBeforeError');
|
||||
const TokenExpiredError = require('./lib/TokenExpiredError');
|
||||
const decode = require('./decode');
|
||||
const timespan = require('./lib/timespan');
|
||||
const validateAsymmetricKey = require('./lib/validateAsymmetricKey');
|
||||
const PS_SUPPORTED = require('./lib/psSupported');
|
||||
const jws = require('jws');
|
||||
const {KeyObject, createSecretKey, createPublicKey} = require("crypto");
|
||||
|
||||
var PUB_KEY_ALGS = ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512'];
|
||||
var RSA_KEY_ALGS = ['RS256', 'RS384', 'RS512'];
|
||||
var HS_ALGS = ['HS256', 'HS384', 'HS512'];
|
||||
const PUB_KEY_ALGS = ['RS256', 'RS384', 'RS512'];
|
||||
const EC_KEY_ALGS = ['ES256', 'ES384', 'ES512'];
|
||||
const RSA_KEY_ALGS = ['RS256', 'RS384', 'RS512'];
|
||||
const HS_ALGS = ['HS256', 'HS384', 'HS512'];
|
||||
|
||||
if (PS_SUPPORTED) {
|
||||
PUB_KEY_ALGS.splice(3, 0, 'PS256', 'PS384', 'PS512');
|
||||
RSA_KEY_ALGS.splice(3, 0, 'PS256', 'PS384', 'PS512');
|
||||
PUB_KEY_ALGS.splice(PUB_KEY_ALGS.length, 0, 'PS256', 'PS384', 'PS512');
|
||||
RSA_KEY_ALGS.splice(RSA_KEY_ALGS.length, 0, 'PS256', 'PS384', 'PS512');
|
||||
}
|
||||
|
||||
module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
@@ -28,7 +31,7 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
//clone this object since we are going to mutate it.
|
||||
options = Object.assign({}, options);
|
||||
|
||||
var done;
|
||||
let done;
|
||||
|
||||
if (callback) {
|
||||
done = callback;
|
||||
@@ -47,7 +50,11 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
return done(new JsonWebTokenError('nonce must be a non-empty string'));
|
||||
}
|
||||
|
||||
var clockTimestamp = options.clockTimestamp || Math.floor(Date.now() / 1000);
|
||||
if (options.allowInvalidAsymmetricKeyTypes !== undefined && typeof options.allowInvalidAsymmetricKeyTypes !== 'boolean') {
|
||||
return done(new JsonWebTokenError('allowInvalidAsymmetricKeyTypes must be a boolean'));
|
||||
}
|
||||
|
||||
const clockTimestamp = options.clockTimestamp || Math.floor(Date.now() / 1000);
|
||||
|
||||
if (!jwtString){
|
||||
return done(new JsonWebTokenError('jwt must be provided'));
|
||||
@@ -57,13 +64,13 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
return done(new JsonWebTokenError('jwt must be a string'));
|
||||
}
|
||||
|
||||
var parts = jwtString.split('.');
|
||||
const parts = jwtString.split('.');
|
||||
|
||||
if (parts.length !== 3){
|
||||
return done(new JsonWebTokenError('jwt malformed'));
|
||||
}
|
||||
|
||||
var decodedToken;
|
||||
let decodedToken;
|
||||
|
||||
try {
|
||||
decodedToken = decode(jwtString, { complete: true });
|
||||
@@ -75,8 +82,8 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
return done(new JsonWebTokenError('invalid token'));
|
||||
}
|
||||
|
||||
var header = decodedToken.header;
|
||||
var getSecret;
|
||||
const header = decodedToken.header;
|
||||
let getSecret;
|
||||
|
||||
if(typeof secretOrPublicKey === 'function') {
|
||||
if(!callback) {
|
||||
@@ -96,7 +103,7 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
return done(new JsonWebTokenError('error in secret or public key callback: ' + err.message));
|
||||
}
|
||||
|
||||
var hasSignature = parts[2].trim() !== '';
|
||||
const hasSignature = parts[2].trim() !== '';
|
||||
|
||||
if (!hasSignature && secretOrPublicKey){
|
||||
return done(new JsonWebTokenError('jwt signature is required'));
|
||||
@@ -107,21 +114,52 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
}
|
||||
|
||||
if (!hasSignature && !options.algorithms) {
|
||||
options.algorithms = ['none'];
|
||||
return done(new JsonWebTokenError('please specify "none" in "algorithms" to verify unsigned tokens'));
|
||||
}
|
||||
|
||||
if (secretOrPublicKey != null && !(secretOrPublicKey instanceof KeyObject)) {
|
||||
try {
|
||||
secretOrPublicKey = createPublicKey(secretOrPublicKey);
|
||||
} catch (_) {
|
||||
try {
|
||||
secretOrPublicKey = createSecretKey(typeof secretOrPublicKey === 'string' ? Buffer.from(secretOrPublicKey) : secretOrPublicKey);
|
||||
} catch (_) {
|
||||
return done(new JsonWebTokenError('secretOrPublicKey is not valid key material'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!options.algorithms) {
|
||||
options.algorithms = ~secretOrPublicKey.toString().indexOf('BEGIN CERTIFICATE') ||
|
||||
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ? PUB_KEY_ALGS :
|
||||
~secretOrPublicKey.toString().indexOf('BEGIN RSA PUBLIC KEY') ? RSA_KEY_ALGS : HS_ALGS;
|
||||
|
||||
if (secretOrPublicKey.type === 'secret') {
|
||||
options.algorithms = HS_ALGS;
|
||||
} else if (['rsa', 'rsa-pss'].includes(secretOrPublicKey.asymmetricKeyType)) {
|
||||
options.algorithms = RSA_KEY_ALGS
|
||||
} else if (secretOrPublicKey.asymmetricKeyType === 'ec') {
|
||||
options.algorithms = EC_KEY_ALGS
|
||||
} else {
|
||||
options.algorithms = PUB_KEY_ALGS
|
||||
}
|
||||
}
|
||||
|
||||
if (!~options.algorithms.indexOf(decodedToken.header.alg)) {
|
||||
if (options.algorithms.indexOf(decodedToken.header.alg) === -1) {
|
||||
return done(new JsonWebTokenError('invalid algorithm'));
|
||||
}
|
||||
|
||||
var valid;
|
||||
if (header.alg.startsWith('HS') && secretOrPublicKey.type !== 'secret') {
|
||||
return done(new JsonWebTokenError((`secretOrPublicKey must be a symmetric key when using ${header.alg}`)))
|
||||
} else if (/^(?:RS|PS|ES)/.test(header.alg) && secretOrPublicKey.type !== 'public') {
|
||||
return done(new JsonWebTokenError((`secretOrPublicKey must be an asymmetric key when using ${header.alg}`)))
|
||||
}
|
||||
|
||||
if (!options.allowInvalidAsymmetricKeyTypes) {
|
||||
try {
|
||||
validateAsymmetricKey(header.alg, secretOrPublicKey);
|
||||
} catch (e) {
|
||||
return done(e);
|
||||
}
|
||||
}
|
||||
|
||||
let valid;
|
||||
|
||||
try {
|
||||
valid = jws.verify(jwtString, decodedToken.header.alg, secretOrPublicKey);
|
||||
@@ -133,7 +171,7 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
return done(new JsonWebTokenError('invalid signature'));
|
||||
}
|
||||
|
||||
var payload = decodedToken.payload;
|
||||
const payload = decodedToken.payload;
|
||||
|
||||
if (typeof payload.nbf !== 'undefined' && !options.ignoreNotBefore) {
|
||||
if (typeof payload.nbf !== 'number') {
|
||||
@@ -154,10 +192,10 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
}
|
||||
|
||||
if (options.audience) {
|
||||
var audiences = Array.isArray(options.audience) ? options.audience : [options.audience];
|
||||
var target = Array.isArray(payload.aud) ? payload.aud : [payload.aud];
|
||||
const audiences = Array.isArray(options.audience) ? options.audience : [options.audience];
|
||||
const target = Array.isArray(payload.aud) ? payload.aud : [payload.aud];
|
||||
|
||||
var match = target.some(function (targetAudience) {
|
||||
const match = target.some(function (targetAudience) {
|
||||
return audiences.some(function (audience) {
|
||||
return audience instanceof RegExp ? audience.test(targetAudience) : audience === targetAudience;
|
||||
});
|
||||
@@ -169,7 +207,7 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
}
|
||||
|
||||
if (options.issuer) {
|
||||
var invalid_issuer =
|
||||
const invalid_issuer =
|
||||
(typeof options.issuer === 'string' && payload.iss !== options.issuer) ||
|
||||
(Array.isArray(options.issuer) && options.issuer.indexOf(payload.iss) === -1);
|
||||
|
||||
@@ -201,7 +239,7 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
return done(new JsonWebTokenError('iat required when maxAge is specified'));
|
||||
}
|
||||
|
||||
var maxAgeTimestamp = timespan(options.maxAge, payload.iat);
|
||||
const maxAgeTimestamp = timespan(options.maxAge, payload.iat);
|
||||
if (typeof maxAgeTimestamp === 'undefined') {
|
||||
return done(new JsonWebTokenError('"maxAge" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60'));
|
||||
}
|
||||
@@ -211,7 +249,7 @@ module.exports = function (jwtString, secretOrPublicKey, options, callback) {
|
||||
}
|
||||
|
||||
if (options.complete === true) {
|
||||
var signature = decodedToken.signature;
|
||||
const signature = decodedToken.signature;
|
||||
|
||||
return done(null, {
|
||||
header: header,
|
||||
|
||||
Reference in New Issue
Block a user