Skip to content

Commit 98a7a09

Browse files
committedJul 24, 2017
Merge branch 'pr-113-jwtOptions'
Added jsonWebTokenOptions to options, which allows one to pass additional options to node-jsonwebtoken verifier. This way there won't be a need to change passport-jwt for any other custom option used in the verifier. Closes #113
2 parents c1dccf8 + 4a7416c commit 98a7a09

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ extracted from the request or verified.
4242
* `ignoreExpiration`: if true do not validate the expiration of the token.
4343
* `passReqToCallback`: If true the request will be passed to the verify
4444
callback. i.e. verify(request, jwt_payload, done_callback).
45+
* `jsonWebTokenOptions`: passport-jwt is verifying the token using [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken).
46+
Pass here an options object for any other option you can pass the jsonwebtoken verifier. (i.e maxAge)
4547

4648
`verify` is a function with the parameters `verify(jwt_payload, done)`
4749

‎lib/helpers/assign.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// note: This is a polyfill to Object.assign to support old nodejs versions (0.10 / 0.12) where
2+
// Object.assign doesn't exist.
3+
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
4+
module.exports = function(target, varArgs) {
5+
if (target == null) { // TypeError if undefined or null
6+
throw new TypeError('Cannot convert undefined or null to object');
7+
}
8+
9+
var to = Object(target);
10+
11+
for (var index = 1; index < arguments.length; index++) {
12+
var nextSource = arguments[index];
13+
14+
if (nextSource != null) { // Skip over if undefined or null
15+
for (var nextKey in nextSource) {
16+
// Avoid bugs when hasOwnProperty is shadowed
17+
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
18+
to[nextKey] = nextSource[nextKey];
19+
}
20+
}
21+
}
22+
}
23+
return to;
24+
};

‎lib/strategy.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var passport = require('passport-strategy')
22
, auth_hdr = require('./auth_header')
33
, util = require('util')
4-
, url = require('url');
4+
, url = require('url')
5+
, assign = require('./helpers/assign.js');
56

67

78

@@ -40,23 +41,16 @@ function JwtStrategy(options, verify) {
4041
}
4142

4243
this._passReqToCallback = options.passReqToCallback;
43-
this._verifOpts = {};
44-
45-
if (options.issuer) {
46-
this._verifOpts.issuer = options.issuer;
47-
}
48-
49-
if (options.audience) {
50-
this._verifOpts.audience = options.audience;
51-
}
52-
53-
if (options.algorithms) {
54-
this._verifOpts.algorithms = options.algorithms;
55-
}
56-
57-
if (options.ignoreExpiration !== undefined) {
58-
this._verifOpts.ignoreExpiration = options.ignoreExpiration;
59-
}
44+
var jsonWebTokenOptions = options.jsonWebTokenOptions || {};
45+
//for backwards compatibility, still allowing you to pass
46+
//audience / issuer / algorithms / ignoreExpiration
47+
//on the options.
48+
this._verifOpts = assign({}, jsonWebTokenOptions, {
49+
audience: options.audience,
50+
issuer: options.issuer,
51+
algorithms: options.algorithms,
52+
ignoreExpiration: !!options.ignoreExpiration
53+
});
6054

6155
}
6256
util.inherits(JwtStrategy, passport.Strategy);

‎test/strategy-validation-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ describe('Strategy', function() {
1818
options.secretOrKey = 'secret';
1919
options.algorithms = ["HS256", "HS384"];
2020
options.ignoreExpiration = false;
21+
options.jsonWebTokenOptions = {
22+
clockTolerance: 10,
23+
maxAge: "1h",
24+
};
2125
options.jwtFromRequest = extract_jwt.fromAuthHeader();
2226
strategy = new Strategy(options, verifyStub);
2327

@@ -61,6 +65,16 @@ describe('Strategy', function() {
6165
expect(Strategy.JwtVerifier.args[0][2].ignoreExpiration).to.be.false;
6266
});
6367

68+
it('should call with the right maxAge option', function() {
69+
expect(Strategy.JwtVerifier.args[0][2]).to.be.an.object;
70+
expect(Strategy.JwtVerifier.args[0][2].maxAge).to.equal('1h');
71+
});
72+
73+
it('should call with the right clockTolerance option', function() {
74+
expect(Strategy.JwtVerifier.args[0][2]).to.be.an.object;
75+
expect(Strategy.JwtVerifier.args[0][2].clockTolerance).to.equal(10);
76+
});
77+
6478
});
6579

6680

0 commit comments

Comments
 (0)
Please sign in to comment.