-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
totp.js
191 lines (183 loc) · 5.4 KB
/
totp.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { HOTP } from "./hotp.js";
import { Secret } from "./secret.js";
/**
* TOTP: Time-Based One-Time Password Algorithm.
* @see [RFC 6238](https://tools.ietf.org/html/rfc6238)
*/
class TOTP {
/**
* Default configuration.
* @type {{
* issuer: string,
* label: string,
* issuerInLabel: boolean,
* algorithm: string,
* digits: number,
* period: number
* window: number
* }}
*/
static get defaults() {
return {
issuer: "",
label: "OTPAuth",
issuerInLabel: true,
algorithm: "SHA1",
digits: 6,
period: 30,
window: 1,
};
}
/**
* Creates a TOTP object.
* @param {Object} [config] Configuration options.
* @param {string} [config.issuer=''] Account provider.
* @param {string} [config.label='OTPAuth'] Account label.
* @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
* @param {Secret|string} [config.secret=Secret] Secret key.
* @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
* @param {number} [config.digits=6] Token length.
* @param {number} [config.period=30] Token time-step duration.
*/
constructor({
issuer = TOTP.defaults.issuer,
label = TOTP.defaults.label,
issuerInLabel = TOTP.defaults.issuerInLabel,
secret = new Secret(),
algorithm = TOTP.defaults.algorithm,
digits = TOTP.defaults.digits,
period = TOTP.defaults.period,
} = {}) {
/**
* Account provider.
* @type {string}
*/
this.issuer = issuer;
/**
* Account label.
* @type {string}
*/
this.label = label;
/**
* Include issuer prefix in label.
* @type {boolean}
*/
this.issuerInLabel = issuerInLabel;
/**
* Secret key.
* @type {Secret}
*/
this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
/**
* HMAC hashing algorithm.
* @type {string}
*/
this.algorithm = algorithm.toUpperCase();
/**
* Token length.
* @type {number}
*/
this.digits = digits;
/**
* Token time-step duration.
* @type {number}
*/
this.period = period;
}
/**
* Generates a TOTP token.
* @param {Object} config Configuration options.
* @param {Secret} config.secret Secret key.
* @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
* @param {number} [config.digits=6] Token length.
* @param {number} [config.period=30] Token time-step duration.
* @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
* @returns {string} Token.
*/
static generate({ secret, algorithm, digits, period = TOTP.defaults.period, timestamp = Date.now() }) {
return HOTP.generate({
secret,
algorithm,
digits,
counter: Math.floor(timestamp / 1000 / period),
});
}
/**
* Generates a TOTP token.
* @param {Object} [config] Configuration options.
* @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
* @returns {string} Token.
*/
generate({ timestamp = Date.now() } = {}) {
return TOTP.generate({
secret: this.secret,
algorithm: this.algorithm,
digits: this.digits,
period: this.period,
timestamp,
});
}
/**
* Validates a TOTP token.
* @param {Object} config Configuration options.
* @param {string} config.token Token value.
* @param {Secret} config.secret Secret key.
* @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
* @param {number} config.digits Token length.
* @param {number} [config.period=30] Token time-step duration.
* @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
* @param {number} [config.window=1] Window of counter values to test.
* @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
*/
static validate({ token, secret, algorithm, digits, period = TOTP.defaults.period, timestamp = Date.now(), window }) {
return HOTP.validate({
token,
secret,
algorithm,
digits,
counter: Math.floor(timestamp / 1000 / period),
window,
});
}
/**
* Validates a TOTP token.
* @param {Object} config Configuration options.
* @param {string} config.token Token value.
* @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
* @param {number} [config.window=1] Window of counter values to test.
* @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
*/
validate({ token, timestamp, window }) {
return TOTP.validate({
token,
secret: this.secret,
algorithm: this.algorithm,
digits: this.digits,
period: this.period,
timestamp,
window,
});
}
/**
* Returns a Google Authenticator key URI.
* @returns {string} URI.
*/
toString() {
const e = encodeURIComponent;
return (
"otpauth://totp/" +
`${
this.issuer.length > 0
? this.issuerInLabel
? `${e(this.issuer)}:${e(this.label)}?issuer=${e(this.issuer)}&`
: `${e(this.label)}?issuer=${e(this.issuer)}&`
: `${e(this.label)}?`
}` +
`secret=${e(this.secret.base32)}&` +
`algorithm=${e(this.algorithm)}&` +
`digits=${e(this.digits)}&` +
`period=${e(this.period)}`
);
}
}
export { TOTP };