-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (38 loc) · 1.24 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
var yub = require('yub'),
passport = require('passport-strategy'),
util = require('util');
function Strategy(options, verify) {
if(typeof options == 'function' || !verify) {
throw new TypeError('YubikeyStrategy requires API keys in options');
}
passport.Strategy.call(this);
this._clientID = options.clientID;
this._secret = options.APISecret
this.name = 'yubikey';
this._verify = verify;
}
util.inherits(Strategy, passport.Strategy);
Strategy.prototype.authenticate = function(req) {
if(!req.body.yubikey || req.body.yubikey.length < 32) {
return this.fail({
message: 'No valid yubikey provided'
}, 400);
}
yub.init(this._clientID, this._secret);
var self = this;
function verified(err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(info); }
self.success(user, info);
}
yub.verify(req.body.yubikey, function(err, data) {
if(data.status === 'OK') {
self._verify(data.identity, verified);
} else {
return self.fail({message: 'Yubikey validation failed'});
}
});
};
exports = module.exports = Strategy;
exports.Strategy = Strategy;