UNPKG

3.77 kBJavaScriptView Raw
1var passport = require('passport-strategy')
2 , auth_hdr = require('./auth_header')
3 , util = require('util')
4 , url = require('url');
5
6
7
8/**
9 * Strategy constructor
10 *
11 * @param options
12 * secretOrKey: (REQUIRED) String or buffer containing the secret or PEM-encoded public key
13 * jwtFromRequest: (REQUIRED) Function that accepts a reqeust as the only parameter and returns the either JWT as a string or null
14 * issuer: If defined issuer will be verified against this value
15 * audience: If defined audience will be verified against this value
16 * algorithms: List of strings with the names of the allowed algorithms. For instance, ["HS256", "HS384"].
17 * ignoreExpiration: if true do not validate the expiration of the token.
18 * passReqToCallback: If true the, the verify callback will be called with args (request, jwt_payload, done_callback).
19 * @param verify - Verify callback with args (jwt_payload, done_callback) if passReqToCallback is false,
20 * (request, jwt_payload, done_callback) if true.
21 */
22function JwtStrategy(options, verify) {
23
24 passport.Strategy.call(this);
25 this.name = 'jwt';
26
27 this._secretOrKey = options.secretOrKey;
28 if (!this._secretOrKey) {
29 throw new TypeError('JwtStrategy requires a secret or key');
30 }
31
32 this._verify = verify;
33 if (!this._verify) {
34 throw new TypeError('JwtStrategy requires a verify callback');
35 }
36
37 this._jwtFromRequest = options.jwtFromRequest;
38 if (!this._jwtFromRequest) {
39 throw new TypeError('JwtStrategy requires a function to retrieve jwt from requests (see option jwtFromRequest)');
40 }
41
42 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 != null) {
58 this._verifOpts.ignoreExpiration = options.ignoreExpiration;
59 }
60
61};
62util.inherits(JwtStrategy, passport.Strategy);
63
64
65
66/**
67 * Allow for injection of JWT Verifier.
68 *
69 * This improves testability by allowing tests to cleanly isolate failures in the JWT Verification
70 * process from failures in the passport related mechanics of authentication.
71 *
72 * Note that this should only be replaced in tests.
73 */
74JwtStrategy.JwtVerifier = require('./verify_jwt');
75
76
77
78/**
79 * Authenticate request based on JWT obtained from header or post body
80 */
81JwtStrategy.prototype.authenticate = function(req, options) {
82 var self = this;
83
84 var token = self._jwtFromRequest(req);
85
86 if (!token) {
87 return self.fail(new Error("No auth token"));
88 }
89
90 // Verify the JWT
91 JwtStrategy.JwtVerifier(token, this._secretOrKey, this._verifOpts, function(jwt_err, payload) {
92 if (jwt_err) {
93 return self.fail(jwt_err);
94 } else {
95 // Pass the parsed token to the user
96 var verified = function(err, user, info) {
97 if(err) {
98 return self.error(err);
99 } else if (!user) {
100 return self.fail(info);
101 } else {
102 return self.success(user, info);
103 }
104 };
105
106 try {
107 if (self._passReqToCallback) {
108 self._verify(req, payload, verified);
109 } else {
110 self._verify(payload, verified);
111 }
112 } catch(ex) {
113 self.error(ex);
114 }
115 }
116 });
117};
118
119
120
121/**
122 * Export the Jwt Strategy
123 */
124 module.exports = JwtStrategy;