-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt-auth.js
37 lines (26 loc) · 1012 Bytes
/
jwt-auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const ExtractJwt = require('passport-jwt').ExtractJwt;
const JwtStrategy = require('passport-jwt').Strategy;
module.exports = function (options) {
const service = 'jwt';
const params = {};
params.jwtFromRequest = ExtractJwt.fromAuthHeader();
params.audience = options.audience;
params.issuer = options.issuer;
params.secretOrKey = options.secretOrKey;
const authPlugin = new JwtStrategy(params, function (payload, done) {
this.act({ role: 'auth', prepare: 'jwt_login_data', payload }, done);
});
this.add({ role: 'auth', prepare: 'jwt_login_data' }, (msg, done) => {
this.act({ role: 'user', cmd: 'login', nick: msg.payload.sub, auto: true }, (err, out) => {
if (!out.ok) {
return done(out.why);
}
done(err, out);
});
});
this.act({ role: 'auth', cmd: 'register_service', service, plugin: authPlugin, conf: options });
return {
name: 'jwt-auth'
};
};