-
Notifications
You must be signed in to change notification settings - Fork 2
/
totp.js
269 lines (218 loc) · 6.87 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* totp.js
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import GLib from 'gi://GLib';
import * as Base32 from './base32.js';
// strings will be translated by gettext in the frontend
const _ = x => x;
class Algorithm {
static parse(arg)
{
if (typeof arg != 'string')
return arg;
switch (arg.toUpperCase()) {
case 'SHA1':
case 'SHA-1':
return GLib.ChecksumType.SHA1;
case 'SHA256':
case 'SHA-256':
return GLib.ChecksumType.SHA256;
case 'SHA512':
case 'SHA-512':
return GLib.ChecksumType.SHA512;
default:
throw new Error(_('Invalid algorithm.'));
}
}
static str(arg)
{
switch (arg) {
case GLib.ChecksumType.SHA1:
return 'SHA-1';
case GLib.ChecksumType.SHA256:
return 'SHA-256';
case GLib.ChecksumType.SHA512:
return 'SHA-512';
default:
if (typeof arg == 'string')
return arg;
throw new Error(_('Invalid algorithm.'));
}
}
};
function now()
{
return new Date().getTime() / 1000;
}
function hex_to_bytes(hex)
{
let blob = [];
for (let i = 0; i < hex.length; i += 2)
blob.push(parseInt(hex.substring(i, i + 2), 16));
return new Uint8Array(blob);
}
function bytes_to_hex(blob)
{
let hex = "";
blob.forEach(x => hex += x.toString(16).padStart(2, '0'));
return hex;
}
function splitQuery(query)
{
let entries = query.split('&');
let result = {};
entries.forEach(x => {
let re = /(?<key>[^=]+)=(?<value>.*)/;
let found = x.match(re);
if (found)
result[found.groups.key] = found.groups.value;
});
return result;
}
export default
class TOTP {
constructor({
issuer = '',
name = '',
secret = '',
digits = 6,
period = 30,
algorithm = 'SHA-1',
uri = null
} = {})
{
if (uri) {
let [success,
scheme,
userinfo,
host,
port,
path,
query,
fragment] = GLib.Uri.split(uri, GLib.UriFlags.NON_DNS);
if (!success)
throw new Error(_('Failed to parse URI.'));
if (scheme != 'otpauth')
throw new Error(_('URI scheme should be "otpauth://..."'));
if (host != 'totp')
throw new Error(_('URI host should be "totp"'));
if (port != -1)
throw new Error(_('Unexpected port number in URI.'));
/*
if (userinfo)
console.warn(_('Unexpected userinfo in URI.'));
if (fragment)
console.warn(`Unexpected fragment in URI: ${fragment}`);
*/
let {
issuer = '',
secret = '',
digits = 6,
period = 30,
algorithm = 'SHA-1'
} = splitQuery(query);
this.issuer = issuer;
this.name = path.substring(1);
this.secret = secret;
this.digits = parseInt(digits);
this.period = parseInt(period);
this.algorithm = Algorithm.str(Algorithm.parse(algorithm));
} else {
this.issuer = issuer;
this.name = name;
this.secret = secret;
this.digits = parseInt(digits);
this.period = parseInt(period);
this.algorithm = Algorithm.str(Algorithm.parse(algorithm));
}
}
wipe()
{
this.secret = this.secret.replaceAll(/[^=]/g, 'A');
}
code(time = now())
{
const secret_bytes = Base32.decode(this.secret, false);
const counter = Math.trunc(time / this.period);
const counter_hex = `${counter.toString(16)}`.padStart(16, '0');
const counter_bytes = hex_to_bytes(counter_hex);
const algorithm = Algorithm.parse(this.algorithm);
const hmac_hex = GLib.compute_hmac_for_bytes(algorithm,
secret_bytes,
counter_bytes);
const hmac = hex_to_bytes(hmac_hex);
// extract offset from the lower nibble of the last byte
const offset = hmac.at(-1) & 0xf;
// load the big endian uint32 starting at offset, discard top bit
const view = new DataView(hmac.buffer);
let value = view.getUint32(offset) & 0x7fffffff;
let value_str = '';
if (this.issuer.toUpperCase() == 'STEAM') {
// Steam OTP uses this reversed base-26 encoding.
const steam_digits = "23456789BCDFGHJKMNPQRTVWXY";
for (let i = 0; i < this.digits; ++i) {
value_str += steam_digits[value % 26];
value = Math.trunc(value / 26);
}
} else {
// regular OTP uses decimal
value_str = value.toString();
}
// take the last 'digits' characters of the string representation, pad with zeros
let code = value_str.slice(-this.digits);
this.wipe();
return code.padStart(this.digits, '0');
}
// return code and expiry
code_and_expiry()
{
const t = now();
const code = this.code(t);
const expiry = (Math.trunc(t / this.period) + 1) * this.period;
return [code, expiry];
}
uri()
{
// See https://github.com/google/google-authenticator/wiki/Key-Uri-Format
let query = `secret=${this.secret}`;
this.wipe();
if (this.issuer != '')
query += `&issuer=${this.issuer}`;
if (this.digits != 6)
query += `&digits=${this.digits}`;
if (this.period != 30)
query = query + `&period=${this.period}`;
if (this.algorithm != 'SHA-1') {
// remove the '-' from the algorithm name
const algo_str = this.algorithm.replaceAll(/-/g, '');
query += `&algorithm=${algo_str}`;
}
return GLib.Uri.join(GLib.UriFlags.NON_DNS,
'otpauth',
null, // no user
'totp', // host
-1, // port
'/' + this.name, // path
query,
null);
}
fields()
{
const result = this.fields_non_destructive();
this.wipe();
return result;
}
// return all members as strings, as required by libsecret
fields_non_destructive()
{
return {
issuer: this.issuer,
name: this.name,
secret: this.secret,
digits: this.digits.toString(),
period: this.period.toString(),
algorithm: this.algorithm
};
}
};