Skip to content

Commit 462bebd

Browse files
committed
reduce code size
1 parent 3fb0078 commit 462bebd

File tree

4 files changed

+35
-41
lines changed

4 files changed

+35
-41
lines changed

lib/psSupported.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/validateAsymmetricKey.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const allowedAlgorithmsForKeys = {
2-
'rsa': ['RS256', 'PS256', 'RS384', 'PS384', 'RS512', 'PS512'],
2+
'rsa': ['RS256', 'PS256', 'RS384'],
33
};
44

55
module.exports = function(algorithm, key) {

sign.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,50 @@ function validAlgorithm(alg) {
1414
return SUPPORTED_ALGS.includes(alg)
1515
}
1616

17+
const TimespanScheme = { isValid: function(value) { return isInteger(value) || (isString(value) && value); }, message: 'should be a number of seconds or string representing a timespan' }
18+
const StringScheme = { isValid: isString, message: 'must be a string' }
19+
const BooleanScheme = { isValid: isBoolean, message: 'must be a boolean' }
20+
1721
const sign_options_schema = {
18-
expiresIn: { isValid: function(value) { return isInteger(value) || (isString(value) && value); }, message: '"expiresIn" should be a number of seconds or string representing a timespan' },
19-
notBefore: { isValid: function(value) { return isInteger(value) || (isString(value) && value); }, message: '"notBefore" should be a number of seconds or string representing a timespan' },
20-
audience: { isValid: function(value) { return isString(value) || Array.isArray(value); }, message: '"audience" must be a string or array' },
21-
algorithm: { isValid: validAlgorithm, message: '"algorithm" must be a valid string enum value' },
22-
header: { isValid: isPlainObject, message: '"header" must be an object' },
23-
encoding: { isValid: isString, message: '"encoding" must be a string' },
24-
issuer: { isValid: isString, message: '"issuer" must be a string' },
25-
subject: { isValid: isString, message: '"subject" must be a string' },
26-
jwtid: { isValid: isString, message: '"jwtid" must be a string' },
27-
noTimestamp: { isValid: isBoolean, message: '"noTimestamp" must be a boolean' },
28-
keyid: { isValid: isString, message: '"keyid" must be a string' },
29-
mutatePayload: { isValid: isBoolean, message: '"mutatePayload" must be a boolean' },
30-
allowInsecureKeySizes: { isValid: isBoolean, message: '"allowInsecureKeySizes" must be a boolean'},
31-
allowInvalidAsymmetricKeyTypes: { isValid: isBoolean, message: '"allowInvalidAsymmetricKeyTypes" must be a boolean'}
22+
expiresIn: TimespanScheme,
23+
notBefore: TimespanScheme,
24+
audience: { isValid: function(value) { return isString(value) || Array.isArray(value); }, message: 'must be a string or array' },
25+
algorithm: { isValid: validAlgorithm, message: 'must be a valid string enum value' },
26+
header: { isValid: isPlainObject, message: 'must be an object' },
27+
encoding: StringScheme,
28+
issuer: StringScheme,
29+
subject: StringScheme,
30+
jwtid: StringScheme,
31+
noTimestamp: BooleanScheme,
32+
keyid: StringScheme,
33+
mutatePayload: BooleanScheme,
34+
allowInsecureKeySizes: BooleanScheme,
35+
allowInvalidAsymmetricKeyTypes: BooleanScheme
3236
};
3337

38+
const SecondClaim = { isValid: isNumber, message: 'should be a number of seconds' }
3439
const registered_claims_schema = {
35-
iat: { isValid: isNumber, message: '"iat" should be a number of seconds' },
36-
exp: { isValid: isNumber, message: '"exp" should be a number of seconds' },
37-
nbf: { isValid: isNumber, message: '"nbf" should be a number of seconds' }
40+
iat: SecondClaim,
41+
exp: SecondClaim,
42+
nbf: SecondClaim
3843
};
3944

4045
function validate(schema, allowUnknown, object, parameterName) {
4146
if (!isPlainObject(object)) {
4247
throw new Error('Expected "' + parameterName + '" to be a plain object.');
4348
}
44-
Object.keys(object)
45-
.forEach(function(key) {
46-
const validator = schema[key];
47-
if (!validator) {
48-
if (!allowUnknown) {
49-
throw new Error('"' + key + '" is not allowed in "' + parameterName + '"');
50-
}
51-
return;
52-
}
53-
if (!validator.isValid(object[key])) {
54-
throw new Error(validator.message);
49+
for(const key in object) {
50+
const validator = schema[key];
51+
if (!validator) {
52+
if (!allowUnknown) {
53+
throw new Error('"' + key + '" is not allowed in "' + parameterName + '"');
5554
}
56-
});
55+
return;
56+
}
57+
if (!validator.isValid(object[key])) {
58+
throw new Error('"' + key + '" ' + validator.message);
59+
}
60+
}
5761
}
5862

5963
function validateOptions(options) {
@@ -224,7 +228,7 @@ module.exports = function (payload, secretOrPrivateKey, options, callback) {
224228

225229
const encoding = options.encoding || 'utf8';
226230

227-
let signature = jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding});
231+
let signature = jws.sign({header, payload, secret: secretOrPrivateKey, encoding});
228232
// TODO: Remove in favor of the modulus length check before signing once node 15+ is the minimum supported version
229233
if(!options.allowInsecureKeySizes && /^(?:RS|PS)/.test(header.alg) && signature.length < 256) {
230234
throw new Error(`secretOrPrivateKey has a minimum key size of 2048 bits for ${header.alg}`)

verify.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,10 @@ const TokenExpiredError = require('./lib/TokenExpiredError');
44
const decode = require('./decode');
55
const timespan = require('./lib/timespan');
66
const validateAsymmetricKey = require('./lib/validateAsymmetricKey');
7-
const PS_SUPPORTED = true;
87
const jws = require('jws');
98
const {KeyObject, createSecretKey, createPublicKey} = require("crypto");
109

11-
const PUB_KEY_ALGS = ['RS256', 'RS384', 'RS512'];
12-
const EC_KEY_ALGS = ['ES256', 'ES384', 'ES512'];
1310
const RSA_KEY_ALGS = ['RS256', 'RS384', 'RS512'];
14-
const HS_ALGS = ['HS256', 'HS384', 'HS512'];
15-
16-
if (PS_SUPPORTED) {
17-
PUB_KEY_ALGS.splice(PUB_KEY_ALGS.length, 0, 'PS256', 'PS384', 'PS512');
18-
RSA_KEY_ALGS.splice(RSA_KEY_ALGS.length, 0, 'PS256', 'PS384', 'PS512');
19-
}
2011

2112
module.exports = function (jwtString, secretOrPublicKey, options, callback) {
2213
if ((typeof options === 'function') && !callback) {

0 commit comments

Comments
 (0)