-
Notifications
You must be signed in to change notification settings - Fork 1
/
otp.ts
238 lines (218 loc) · 7.53 KB
/
otp.ts
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
import { byteLength, decode, encode } from "./deps.ts";
import {
calculateHmacDigest,
cleanUserInputFormat,
cleanUserInputFormatAndAddBase32Padding,
extractCodeFromHmacShaDigest,
isBase32,
} from "./util.ts";
/** The values have to follow the naming convention of the WebCrypto API. */
export enum OtpAlgorithm {
SHA1 = "SHA-1",
SHA256 = "SHA-256",
SHA512 = "SHA-512",
}
export interface GenerateSecretOptions {
byteLength?: number;
allowShortSecret?: boolean;
}
export interface FormatCodeOptions {
grouping?: number;
}
export interface GenerateOptions {
movingFactor?: number;
formatCode?: boolean;
grouping?: number;
sideEffects: boolean;
}
export interface GenerateCodeNoSideEffects {
grouping?: number;
}
export interface ValidateOptions {
movingFactor?: number;
sideEffects: boolean;
validateAgainstWindow: boolean;
}
export interface OtpOptions {
digits?: number;
validationWindow?: number;
algorithm?: OtpAlgorithm;
}
export abstract class Otp {
#secret: Uint8Array;
#digits = 6;
public get digits(): number {
return this.#digits;
}
#validationWindow = 0;
public get validationWindow(): number {
return this.#validationWindow;
}
#algorithm = OtpAlgorithm.SHA1;
public get algorithm(): OtpAlgorithm {
return this.#algorithm;
}
/**
* @param secret Secret in unencoded Uint8Array or Base32 encoded string representation.
* @param options Options to configure the number of digits, the size of the validation window and the algorithm.
*/
constructor(
secret: Uint8Array | string,
options?: OtpOptions,
) {
if (typeof secret === "string") {
secret = decode(cleanUserInputFormatAndAddBase32Padding(secret));
}
this.#secret = secret;
if (options?.digits !== undefined) this.#digits = options.digits;
if (options?.validationWindow !== undefined) {
this.#validationWindow = options.validationWindow;
}
if (options?.algorithm !== undefined) this.#algorithm = options?.algorithm;
}
/**
* Validates the Base32 encoded secret for it's characters set and possibly checks it length for the length requirement.
* Regarding to the RFC HOTP and therefore TOTP require a secret with at least 16 bytes of length.
* Google Authenticator ignored this requirement in the past and therefore a number of services generate secrets which are shorter than 16 bytes.
* Therefore, the default behavior is to ignore the length.
*
* @param secret
* @param ignoreLength
*/
static validateSecret(secret: string, ignoreLength = true): boolean {
let validated = false;
try {
const paddedSecret = cleanUserInputFormatAndAddBase32Padding(secret);
if (decode(paddedSecret).length !== 0) {
validated = isBase32(paddedSecret);
// Deal with secrets which are too short if secret is okay.
if (validated && !ignoreLength) {
const len = byteLength(paddedSecret);
validated = (len >= 16) ? true : false;
}
}
// deno-lint-ignore no-empty
} catch (_) {}
return validated;
}
/**
* Generates the formatted otp code and causes side effects like incrementing a internal counter if options.sideEffects is set to true (default).
* The code is formatted in a grouping of three digits followed by a space if the amount of digits is dividable by three and a grouping of four otherwise.
* Setting a custom grouping or disabling the formatting is possible.
* this.validate should be used to validate otp codes.
* @param options
*/
abstract generate(options?: GenerateOptions): Promise<string>;
/**
* Generates the formatted otp code.
* The code is formatted in a grouping of three digits followed by a space if the amount of digits is dividable by three and a grouping of four otherwise.
* this.validate or this.validateCodeNoSideEffects should be used validate otp codes.
* @param movingFactor
*/
protected async generateCodeNoSideEffects(
movingFactor: number,
formatCode: boolean,
options?: GenerateCodeNoSideEffects,
): Promise<string> {
const extractedCode = extractCodeFromHmacShaDigest(
await calculateHmacDigest({
movingFactor,
secret: this.#secret,
algorithm: this.#algorithm,
}),
this.#digits,
);
let grouping = options?.grouping;
if (!formatCode) grouping = 0;
return Otp.formatCode(
extractedCode,
this.#digits,
{
grouping,
},
);
}
/**
* Validates the formatted otp code, ignoring spaces and causes side effects like incrementing a internal counter if options.sideEffects is set to true (default).
* @param options
*/
abstract validate(
code: string,
options?: ValidateOptions,
): Promise<boolean>;
/**
* Validates the formatted otp code, ignoring spaces.
* @param movingFactor
*/
protected async validateCodeNoSideEffects(
code: string,
movingFactor: number,
): Promise<boolean> {
return cleanUserInputFormat(code) ===
cleanUserInputFormat(
await this.generateCodeNoSideEffects(movingFactor, false),
);
}
/**
* Groups the digits of the code in groups of three if the amount of digits is dividable by three and groups of four if not.
* Prepends zeros if the amount of digits is less than the digits parameter.
* @param code
* @param minimumDigits
*/
static formatCode(
code: number | string,
minimumDigits: number,
options?: FormatCodeOptions,
): string {
const formattedCharArray = [
...cleanUserInputFormat(code.toString()),
];
const deltaDigits = minimumDigits - formattedCharArray.length;
const zeroFilledArray = [
..."0".repeat(deltaDigits > 0 ? deltaDigits : 0),
...formattedCharArray,
];
const grouping = options?.grouping !== undefined
? options.grouping
: zeroFilledArray.length % 3 === 0
? 3
: 4;
// skip index 0 and last char
return zeroFilledArray.map((v, i, a) =>
v = (i !== (a.length - 1) && (i + 1) % grouping === 0) ? `${v} ` : v
).join("");
}
/**
* Generates a 20 byte random secret with the given length using the secure WebCryptoApi.
* The secret should be at least 16 bytes long but per [HOTP RFC](https://www.rfc-editor.org/rfc/rfc4226#section-4) the recommended length is at least 20 bytes.
* @param options
* @throws RangeError if the optional length is less than 16 bytes and allowedShortSecret is not set to true.
*/
static generateSecret(options?: GenerateSecretOptions): Uint8Array {
const notOptionalOptions = {
...options,
};
if (notOptionalOptions.byteLength === undefined) {
notOptionalOptions.byteLength = 20;
}
if (notOptionalOptions.allowShortSecret === undefined) {
notOptionalOptions.allowShortSecret = false;
}
if (notOptionalOptions.byteLength < 16 && !options?.allowShortSecret) {
throw new RangeError("Secret must be at least 16 bytes long.");
} else {
return crypto.getRandomValues(
new Uint8Array(notOptionalOptions.byteLength),
);
}
}
/**
* Generates a 20 byte random Base32 encoded secret with the given length using the secure WebCryptoApi.
* The secret should be at least 16 bytes long but per [HOTP RFC](https://www.rfc-editor.org/rfc/rfc4226#section-4) the recommended length is at least 20 bytes.
* @param options
* @throws RangeError if options.length is less than 16 bytes and options.allowedShortSecret is not set to true.
*/
static generateBase32Secret(options?: GenerateSecretOptions): string {
return encode(Otp.generateSecret(options));
}
}