Skip to content

Commit bbd3606

Browse files
committed
requestProperty support for nested properties
This reverts the behavior to what v6 used to do.
1 parent 3c1d5cf commit bbd3606

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

package-lock.json

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
"dependencies": {
3636
"@types/jsonwebtoken": "^9",
3737
"express-unless": "^2.1.3",
38-
"jsonwebtoken": "^9.0.0"
38+
"jsonwebtoken": "^9.0.0",
39+
"lodash.set": "^4.3.2"
3940
},
4041
"devDependencies": {
42+
"@types/lodash": "^4.14.191",
43+
"@types/lodash.set": "^4.3.7",
4144
"@types/mocha": "^9.1.0",
4245
"@typescript-eslint/eslint-plugin": "^5.15.0",
4346
"@typescript-eslint/parser": "^5.15.0",

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import * as jwt from 'jsonwebtoken';
1+
import jwt from 'jsonwebtoken';
22
import * as express from 'express';
33
import { unless } from 'express-unless';
4+
import set from 'lodash.set';
5+
46
import { UnauthorizedError } from './errors/UnauthorizedError';
57

68
/**
@@ -123,7 +125,7 @@ export const expressjwt = (options: Params) => {
123125
.map(header => header.trim().toLowerCase())
124126
.includes('authorization');
125127
if (hasAuthInAccessControl) {
126-
return next();
128+
return setImmediate(next);
127129
}
128130
}
129131

@@ -185,10 +187,10 @@ export const expressjwt = (options: Params) => {
185187
}
186188

187189
const request = req as Request<jwt.JwtPayload | string>;
188-
request[requestProperty] = decodedToken.payload;
189-
next();
190+
set(request, requestProperty, decodedToken.payload);
191+
setImmediate(next);
190192
} catch (err) {
191-
return next(err);
193+
setImmediate(next, err);
192194
}
193195
};
194196

test/jwt.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as jwt from 'jsonwebtoken';
33
import * as express from 'express';
44
import { expressjwt, UnauthorizedError, Request, GetVerificationKey } from '../src';
5-
import * as assert from 'assert';
5+
import assert from 'assert';
66

77

88
describe('failure tests', function () {
@@ -289,6 +289,21 @@ describe('work tests', function () {
289289
});
290290
});
291291

292+
it('should work with custom and nested request property', function (done) {
293+
const secret = 'shhhhhh';
294+
const token = jwt.sign({ foo: 'bar' }, secret);
295+
const req = {} as Request;
296+
const res = {} as express.Response;
297+
const requestProperty = 'auth.payload';
298+
299+
req.headers = {};
300+
req.headers.authorization = 'Bearer ' + token;
301+
expressjwt({ secret: secret, algorithms: ['HS256'], requestProperty })(req, res, function () {
302+
assert.equal(req.auth.payload.foo, 'bar');
303+
done();
304+
});
305+
});
306+
292307
it('should work if authorization header is valid with a buffer secret', function (done) {
293308
const secret = Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'base64');
294309
const token = jwt.sign({ foo: 'bar' }, secret);

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"outDir": "./dist",
44
"allowJs": true,
55
"target": "es5",
6-
"declaration": true
6+
"declaration": true,
7+
"esModuleInterop": true
78
},
89
"include": [
910
"./src/**/*"
1011
]
11-
}
12+
}

0 commit comments

Comments
 (0)