-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtotp.js
More file actions
102 lines (82 loc) · 2.23 KB
/
Copy pathtotp.js
File metadata and controls
102 lines (82 loc) · 2.23 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* totp.js
* Copyright (C) 2025 Daniel K. O.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
// See RFC 6238
import * as OTP from './otp.js';
// strings will be translated by gettext in the frontend
const _ = x => x;
function now()
{
return new Date().getTime() / 1000;
}
export default
class TOTP extends OTP.OTP {
constructor({
issuer = '',
name = '',
secret = '',
digits = 6,
period = 30,
algorithm = 'SHA-1',
uri = null
} = {})
{
super();
this.type = 'TOTP';
if (uri) {
let {
host = null,
issuer = '',
name = '',
secret = '',
digits = 6,
period = 30,
algorithm = 'SHA-1'
} = OTP.parseURI(uri);
if (host.toLowerCase() != "totp")
throw new Error(_('URI host should be "totp"'));
this.issuer = issuer;
this.name = name;
this.secret = secret;
this.digits = parseInt(digits);
this.period = parseInt(period);
this.algorithm = OTP.normalized_algorithm(algorithm);
} else {
this.issuer = issuer;
this.name = name;
this.secret = secret;
this.digits = parseInt(digits);
this.period = parseInt(period);
this.algorithm = OTP.normalized_algorithm(algorithm);
}
}
code(time = now())
{
const counter = Math.trunc(time / this.period);
return super.code(counter);
}
// 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()
{
let args = {};
if (this.period != 30)
args.period = this.period;
return super.uri(args);
}
// return all members as strings, as required by libsecret
fields_non_destructive()
{
let result = super.fields_non_destructive();
result.period = this.period.toString();
return result;
}
}; // class TOTP