-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhotp.js
More file actions
112 lines (96 loc) · 2.46 KB
/
Copy pathhotp.js
File metadata and controls
112 lines (96 loc) · 2.46 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
103
104
105
106
107
108
109
110
111
112
/* hotp.js
* Copyright (C) 2025 Daniel K. O.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
/*
* See RFC 4226
*
* Test case, from Appendix D:
* secret = "12345678901234567890"
* = base64("MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=")
* = base32("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ")
* everything else is default:
* digits = 6
* counter = 0
* algorithm = SHA-1
*
* URI: otpauth://hotp/Test?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=RFC%204226
*
* Expected output:
*
* counter code
* ---------------
* 0 755224
* 1 287082
* 2 359152
* 3 969429
* 4 338314
* 5 254676
* 6 287922
* 7 162583
* 8 399871
* 9 520489
*/
import * as OTP from './otp.js';
// strings will be translated by gettext in the frontend
const _ = x => x;
export default
class HOTP extends OTP.OTP {
constructor({
issuer = '',
name = '',
secret = '',
digits = 6,
counter = 0,
algorithm = 'SHA-1',
uri = null
} = {})
{
super();
this.type = 'HOTP';
if (uri) {
let {
host = null,
issuer = '',
name = '',
secret = '',
digits = 6,
counter = 0,
algorithm = 'SHA-1'
} = OTP.parseURI(uri);
if (host.toLowerCase() != 'hotp')
throw new Error(_('URI host should be "hotp"'));
this.issuer = issuer;
this.name = name;
this.secret = secret;
this.digits = parseInt(digits);
this.counter = parseInt(counter);
this.algorithm = OTP.normalized_algorithm(algorithm);
} else {
this.issuer = issuer;
this.name = name;
this.secret = secret;
this.digits = parseInt(digits);
this.counter = parseInt(counter);
this.algorithm = OTP.normalized_algorithm(algorithm);
}
}
code(counter = this.counter)
{
return super.code(counter);
}
uri()
{
let args = {};
if (this.counter != 0)
args.counter = this.counter;
return super.uri(args);
}
fields_non_destructive()
{
let result = super.fields_non_destructive();
result.counter = this.counter.toString();
return result;
}
}; // class HOTP