authenticator.js
1.83 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Created by lintry on 2017/5/19.
*/
const Authenticator = function (secret, options) {
if (!(this instanceof Authenticator)) {
return new Authenticator(secret, options)
}
const _ = require('lodash'),
speakeasy = require('speakeasy'),
qr = require('qr-image');
options = options || {};
this.secret = secret;
const TOTP_OPTIONS = this.totp_options = {
secret: this.secret,
encoding: options.encoding || 'base32',
step: options.step || 30,
algorithm: options.algorithm || 'sha512'
};
/**
* 验证token有效性
* @param token
*/
this.verify = function (token) {
return speakeasy.totp.verify(_.merge({}, TOTP_OPTIONS, {token: token}));
};
/**
* 在options.window的范围内验证token的有效性
* @param token
* @param window
*/
this.verifyDelta = function (token, window) {
return speakeasy.totp.verifyDelta(_.merge({}, TOTP_OPTIONS, {token: token, window: window}));
};
/**
* 获取授权定义地址
* @param label
* @param issuer
* @return {string}
*/
this.getOtpAuthURL = function (label, issuer) {
return speakeasy.otpauthURL(_.merge({}, TOTP_OPTIONS, {label: label, issuer: issuer}));
};
/**
* 生成svg的QR图片内容
* @param label
* @param issuer
* @return {{content: *, url: string}}
*/
this.getQR = function (label, issuer) {
let url = this.getOtpAuthURL(label, issuer);
return {
content: qr.imageSync(url, { type: 'svg' }),
url: url
};
};
/**
* 生成新的token
* @return {String}
*/
this.totp = function () {
return speakeasy.totp(_.merge({}, TOTP_OPTIONS));
};
return this;
};
module.exports = Authenticator;