Skip to content

Commit 2855ece

Browse files
committed
feat: uses create(de)Cipheriv instead of deprecated methods
BREAKING CHANGE: min node is 12.14.x, upgrades internal dependencies and hardens minimum shared secret requirements. Introduces new token format of <version><iv><payload>, so that in the future breaking changes are easier to mitigate. For back-compatibility introduces ability to specify legacy secret with a length of 24, which is supported by createCipher/createDecipher.
1 parent 976c9dd commit 2855ece

19 files changed

+3189
-2923
lines changed

.eslintrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"extends": "makeomatic",
3-
"parser": "babel-eslint",
43
"rules": {
54
"object-curly-newline": 0
6-
}
5+
}
76
}

.mdeprc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"node": "10.16.0"
2+
"node": "12.14.1",
3+
"auto_compose": true,
4+
"with_local_compose": true
35
}

.mocharc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": "@babel/register",
3+
"timeout": 10000,
4+
"bail": true
5+
}

.releaserc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"branch": "master",
2+
"branches": ["master"],
33
"analyzeCommits": {
44
"preset": "angular",
55
"releaseRules": [

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const tokenManager = new TokenManager({
4040
},
4141
encrypt: {
4242
algorithm: 'aes256',
43-
sharedSecret: Buffer.from('incredibly-long-secret'),
43+
sharedSecret: Buffer.from('incredibly-long-secret-ooooohooo'),
4444
},
4545
});
4646
```

package.json

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,42 @@
3030
},
3131
"homepage": "https://github.com/makeomatic/ms-token#readme",
3232
"peerDependencies": {
33-
"ioredis": "3.x.x || 4.x.x"
33+
"ioredis": "4.x.x"
3434
},
3535
"engine": {
36-
"node": ">= 8.9.0"
36+
"node": ">= 12.14.0"
3737
},
3838
"devDependencies": {
39-
"@babel/cli": "^7.4.4",
40-
"@babel/core": "^7.4.5",
41-
"@babel/plugin-proposal-class-properties": "^7.4.4",
42-
"@babel/plugin-proposal-object-rest-spread": "^7.4.4",
43-
"@babel/plugin-transform-strict-mode": "^7.2.0",
44-
"@babel/register": "^7.4.4",
45-
"@makeomatic/deploy": "^8.4.4",
46-
"babel-eslint": "^10.0.2",
47-
"babel-plugin-istanbul": "^5.1.4",
48-
"codecov": "^3.5.0",
49-
"cross-env": "^5.2.0",
50-
"eslint": "^6.0.1",
51-
"eslint-config-makeomatic": "^3.0.0",
52-
"eslint-plugin-import": "^2.18.0",
53-
"eslint-plugin-mocha": "^5.3.0",
39+
"@babel/cli": "^7.8.3",
40+
"@babel/core": "^7.8.3",
41+
"@babel/plugin-proposal-class-properties": "^7.8.3",
42+
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
43+
"@babel/plugin-transform-strict-mode": "^7.8.3",
44+
"@babel/register": "^7.8.3",
45+
"@makeomatic/deploy": "^10.0.1",
46+
"babel-plugin-istanbul": "^6.0.0",
47+
"codecov": "^3.6.2",
48+
"cross-env": "^6.0.3",
49+
"eslint": "^6.8.0",
50+
"eslint-config-makeomatic": "^4.0.0",
51+
"eslint-plugin-import": "^2.20.0",
52+
"eslint-plugin-mocha": "^6.2.2",
5453
"eslint-plugin-promise": "^4.2.1",
55-
"ioredis": "^4.10.0",
56-
"mocha": "^6.1.4",
57-
"nyc": "^14.1.1",
58-
"rimraf": "^2.6.3"
54+
"ioredis": "^4.14.1",
55+
"mocha": "^7.0.0",
56+
"nyc": "^15.0.0",
57+
"rimraf": "^3.0.0"
5958
},
6059
"dependencies": {
61-
"@hapi/joi": "^15.0.0",
62-
"base64-url": "^2.2.2",
63-
"chance": "^1.0.18",
60+
"@hapi/joi": "^17.1.0",
61+
"base64-url": "^2.3.3",
62+
"chance": "^1.1.4",
6463
"get-value": "^3.0.1",
65-
"glob": "^7.1.4",
64+
"glob": "^7.1.6",
6665
"is": "^3.3.0",
6766
"lodash.compact": "^3.0.1",
6867
"lodash.omit": "^4.5.0",
69-
"uuid": "^3.3.2"
68+
"uuid": "^3.4.0"
7069
},
7170
"files": [
7271
"src/",

src/actions/create.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ const schema = Joi
2323
.min(0)
2424
.max(Joi.ref('ttl')),
2525
Joi.boolean()
26-
.only(true)
26+
.valid(true)
2727
),
2828

2929
metadata: Joi.any(),
3030

31+
legacy: Joi.boolean().default(false),
32+
3133
secret: Joi.alternatives()
3234
.try(
3335
Joi.boolean(),
3436
Joi.object({
3537
type: Joi.string()
36-
.only(['alphabet', 'number', 'uuid'])
38+
.valid('alphabet', 'number', 'uuid')
3739
.required(),
3840

3941
alphabet: Joi.any()
@@ -45,16 +47,16 @@ const schema = Joi
4547

4648
length: Joi.any()
4749
.when('type', {
48-
is: Joi.string().only(['alphabet', 'number']),
50+
is: Joi.string().valid('alphabet', 'number'),
4951
then: Joi.number().integer().min(1).required(),
5052
otherwise: Joi.forbidden(),
5153
}),
5254

5355
encrypt: Joi.boolean()
5456
.when('type', {
5557
is: 'uuid',
56-
then: Joi.default(true),
57-
otherwise: Joi.default(false),
58+
then: Joi.any().default(true),
59+
otherwise: Joi.any().default(false),
5860
}),
5961
})
6062
)
@@ -63,7 +65,7 @@ const schema = Joi
6365
regenerate: Joi.boolean()
6466
.when('secret', {
6567
is: false,
66-
then: Joi.only(false),
68+
then: Joi.valid(false),
6769
otherwise: Joi.optional(),
6870
}),
6971
})
@@ -96,7 +98,7 @@ function getSecret(_secret) {
9698
module.exports = async function create(args) {
9799
const opts = Joi.attempt(args, schema);
98100

99-
const { action, id, ttl, metadata } = opts;
101+
const { action, id, ttl, metadata, legacy } = opts;
100102
const throttle = getThrottle(opts.throttle, ttl);
101103
const uid = opts.regenerate ? uuid.v4() : false;
102104
const secret = getSecret(opts.secret);
@@ -125,7 +127,7 @@ module.exports = async function create(args) {
125127

126128
if (secret) {
127129
settings.secret = secret;
128-
output.secret = crypto.secret(this.encrypt, secret, { id, action, uid });
130+
output.secret = await crypto.secret(this.encrypt, secret, { id, action, uid }, legacy);
129131
}
130132

131133
await this.backend.create(settings, output);

src/actions/info.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const schema = Joi.alternatives()
4242
.required(),
4343

4444
encrypt: Joi.bool()
45-
.only(true)
45+
.valid(true)
4646
.required(),
4747
}),
4848

@@ -59,7 +59,7 @@ const schema = Joi.alternatives()
5959
.required(),
6060

6161
encrypt: Joi.bool()
62-
.only(false)
62+
.valid(false)
6363
.required(),
6464
})
6565
);

src/actions/regenerate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const schema = Joi.alternatives()
1818
);
1919

2020
// helper function used to generate new secret
21-
const generateSecret = encrypt => (id, action, uid, secret) => (
21+
const generateSecret = (encrypt) => async (id, action, uid, secret) => (
2222
crypto.secret(encrypt, secret, { id, action, uid })
2323
);
2424

src/actions/verify.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ const optsSchema = Joi.object({
3333
.default({}),
3434
});
3535

36-
/**
37-
* Enriches error and includes args into it
38-
* @param {Error} e
39-
*/
40-
function enrichError(e) {
41-
e.args = this;
42-
throw e;
43-
}
44-
4536
/**
4637
* Parses input options
4738
* @param {Function}
@@ -83,12 +74,13 @@ function assertControlOptions(args, opts) {
8374
* @param {Object} [_opts={}]
8475
* @return {Promise}
8576
*/
86-
module.exports = async function create(_args, _opts = {}) {
77+
module.exports = async function verify(_args, _opts = {}) {
8778
const { args, opts } = parseInput(this.decrypt, _args, _opts);
8879
assertControlOptions(args, opts);
8980
try {
9081
return await this.backend.verify(args, opts);
9182
} catch (e) {
92-
return enrichError.call(args, e);
83+
e.args = args;
84+
throw e;
9385
}
9486
};

0 commit comments

Comments
 (0)