otplib 12.0.0-1 → 12.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -388
- package/core.d.ts +1 -0
- package/core.js +23 -0
- package/index.d.ts +1 -0
- package/index.js +23 -0
- package/package.json +16 -98
- package/v11.d.ts +1 -0
- package/v11.js +23 -0
- package/authenticator/authenticator.d.ts +0 -145
- package/authenticator/index.d.ts +0 -1
- package/authenticator/index.js +0 -92
- package/authenticator-async/authenticator.d.ts +0 -34
- package/authenticator-async/index.d.ts +0 -1
- package/authenticator-async/index.js +0 -62
- package/core/index.d.ts +0 -3
- package/core/index.js +0 -41
- package/core-async/index.d.ts +0 -11
- package/core-async/index.js +0 -62
- package/hotp/hotp.d.ts +0 -128
- package/hotp/index.d.ts +0 -2
- package/hotp/index.js +0 -224
- package/hotp/utils.d.ts +0 -208
- package/hotp-async/hotp.d.ts +0 -33
- package/hotp-async/index.d.ts +0 -1
- package/hotp-async/index.js +0 -55
- package/plugin-base32-enc-dec/index.d.ts +0 -9
- package/plugin-base32-enc-dec/index.js +0 -28
- package/plugin-crypto/index.d.ts +0 -4
- package/plugin-crypto/index.js +0 -26
- package/plugin-crypto-async-ronomon/index.d.ts +0 -4
- package/plugin-crypto-async-ronomon/index.js +0 -34
- package/plugin-crypto-js/index.d.ts +0 -4
- package/plugin-crypto-js/index.js +0 -831
- package/plugin-thirty-two/index.d.ts +0 -9
- package/plugin-thirty-two/index.js +0 -24
- package/preset-browser/buffer.js +0 -1
- package/preset-browser/index.d.ts +0 -4
- package/preset-browser/index.js +0 -9
- package/preset-browser/index.js.map +0 -1
- package/preset-default/index.d.ts +0 -4
- package/preset-default/index.js +0 -31
- package/preset-default-async/index.d.ts +0 -4
- package/preset-default-async/index.js +0 -31
- package/preset-v11/index.js +0 -134
- package/totp/index.d.ts +0 -1
- package/totp/index.js +0 -202
- package/totp/totp.d.ts +0 -201
- package/totp-async/index.d.ts +0 -1
- package/totp-async/index.js +0 -98
- package/totp-async/totp.d.ts +0 -46
package/totp/index.js
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* otplib-totp
|
|
3
|
-
*
|
|
4
|
-
* @author Gerald Yeo <contact@fusedthought.com>
|
|
5
|
-
* @version: 12.0.0-1
|
|
6
|
-
* @license: MIT
|
|
7
|
-
**/
|
|
8
|
-
'use strict';
|
|
9
|
-
|
|
10
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
11
|
-
|
|
12
|
-
var otplibHotp = require('../hotp');
|
|
13
|
-
|
|
14
|
-
function parseWindowBounds(win) {
|
|
15
|
-
if (typeof win === 'number') {
|
|
16
|
-
return [Math.abs(win), Math.abs(win)];
|
|
17
|
-
}
|
|
18
|
-
if (Array.isArray(win)) {
|
|
19
|
-
const [past, future] = win;
|
|
20
|
-
if (typeof past === 'number' && typeof future === 'number') {
|
|
21
|
-
return [Math.abs(past), Math.abs(future)];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
throw new Error('Expecting options.window to be an number or [number, number].');
|
|
25
|
-
}
|
|
26
|
-
function totpOptionsValidator(options) {
|
|
27
|
-
otplibHotp.hotpOptionsValidator(options);
|
|
28
|
-
parseWindowBounds(options.window);
|
|
29
|
-
if (typeof options.epoch !== 'number') {
|
|
30
|
-
throw new Error('Expecting options.epoch to be a number.');
|
|
31
|
-
}
|
|
32
|
-
if (typeof options.step !== 'number') {
|
|
33
|
-
throw new Error('Expecting options.step to be a number.');
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
const totpPadSecret = (secret, encoding, minLength) => {
|
|
37
|
-
const currentLength = secret.length;
|
|
38
|
-
const hexSecret = Buffer.from(secret, encoding).toString('hex');
|
|
39
|
-
if (currentLength < minLength) {
|
|
40
|
-
const newSecret = new Array(minLength - currentLength + 1).join(hexSecret);
|
|
41
|
-
return Buffer.from(newSecret, 'hex').slice(0, minLength).toString('hex');
|
|
42
|
-
}
|
|
43
|
-
return hexSecret;
|
|
44
|
-
};
|
|
45
|
-
const totpCreateHmacKey = (algorithm, secret, encoding) => {
|
|
46
|
-
switch (algorithm) {
|
|
47
|
-
case otplibHotp.HashAlgorithms.SHA1:
|
|
48
|
-
return totpPadSecret(secret, encoding, 20);
|
|
49
|
-
case otplibHotp.HashAlgorithms.SHA256:
|
|
50
|
-
return totpPadSecret(secret, encoding, 32);
|
|
51
|
-
case otplibHotp.HashAlgorithms.SHA512:
|
|
52
|
-
return totpPadSecret(secret, encoding, 64);
|
|
53
|
-
default:
|
|
54
|
-
throw new Error(`Expecting algorithm to be one of ${otplibHotp.HASH_ALGORITHMS.join(', ')}. Received ${algorithm}.`);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
function totpDefaultOptions() {
|
|
58
|
-
const options = {
|
|
59
|
-
algorithm: otplibHotp.HashAlgorithms.SHA1,
|
|
60
|
-
createDigest: otplibHotp.createDigestPlaceholder,
|
|
61
|
-
createHmacKey: totpCreateHmacKey,
|
|
62
|
-
digits: 6,
|
|
63
|
-
encoding: otplibHotp.KeyEncodings.ASCII,
|
|
64
|
-
epoch: Date.now(),
|
|
65
|
-
step: 30,
|
|
66
|
-
window: 0
|
|
67
|
-
};
|
|
68
|
-
return options;
|
|
69
|
-
}
|
|
70
|
-
function totpOptions(opt) {
|
|
71
|
-
const options = { ...totpDefaultOptions(),
|
|
72
|
-
...opt
|
|
73
|
-
};
|
|
74
|
-
totpOptionsValidator(options);
|
|
75
|
-
return Object.freeze(options);
|
|
76
|
-
}
|
|
77
|
-
function totpCounter(epoch, step) {
|
|
78
|
-
return Math.floor(epoch / step / 1000);
|
|
79
|
-
}
|
|
80
|
-
function totpToken(secret, options) {
|
|
81
|
-
const counter = totpCounter(options.epoch, options.step);
|
|
82
|
-
return otplibHotp.hotpToken(secret, counter, options);
|
|
83
|
-
}
|
|
84
|
-
function totpEpochsInWindow(epoch, direction, deltaPerEpoch, numOfEpoches) {
|
|
85
|
-
const result = [];
|
|
86
|
-
if (numOfEpoches === 0) {
|
|
87
|
-
return result;
|
|
88
|
-
}
|
|
89
|
-
for (let i = 1; i <= numOfEpoches; i++) {
|
|
90
|
-
const delta = direction * i * deltaPerEpoch;
|
|
91
|
-
result.push(epoch + delta);
|
|
92
|
-
}
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
function totpEpochAvailable(epoch, step, win) {
|
|
96
|
-
const bounds = parseWindowBounds(win);
|
|
97
|
-
const delta = step * 1000;
|
|
98
|
-
return {
|
|
99
|
-
current: epoch,
|
|
100
|
-
past: totpEpochsInWindow(epoch, -1, delta, bounds[0]),
|
|
101
|
-
future: totpEpochsInWindow(epoch, 1, delta, bounds[1])
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
function totpCheck(token, secret, options) {
|
|
105
|
-
if (!otplibHotp.isTokenValid(token)) {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
const systemToken = totpToken(secret, options);
|
|
109
|
-
return token === systemToken;
|
|
110
|
-
}
|
|
111
|
-
function totpCheckByEpoch(epochs, token, secret, options) {
|
|
112
|
-
let position = null;
|
|
113
|
-
epochs.some((epoch, idx) => {
|
|
114
|
-
if (totpCheck(token, secret, { ...options,
|
|
115
|
-
epoch
|
|
116
|
-
})) {
|
|
117
|
-
position = idx + 1;
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
return false;
|
|
121
|
-
});
|
|
122
|
-
return position;
|
|
123
|
-
}
|
|
124
|
-
function totpCheckWithWindow(token, secret, options) {
|
|
125
|
-
if (totpCheck(token, secret, options)) {
|
|
126
|
-
return 0;
|
|
127
|
-
}
|
|
128
|
-
const epochs = totpEpochAvailable(options.epoch, options.step, options.window);
|
|
129
|
-
const backward = totpCheckByEpoch(epochs.past, token, secret, options);
|
|
130
|
-
if (backward !== null) {
|
|
131
|
-
return backward * -1;
|
|
132
|
-
}
|
|
133
|
-
return totpCheckByEpoch(epochs.future, token, secret, options);
|
|
134
|
-
}
|
|
135
|
-
function totpTimeUsed(epoch, step) {
|
|
136
|
-
return Math.floor(epoch / 1000) % step;
|
|
137
|
-
}
|
|
138
|
-
function totpTimeRemaining(epoch, step) {
|
|
139
|
-
return step - totpTimeUsed(epoch, step);
|
|
140
|
-
}
|
|
141
|
-
function totpKeyuri(accountName, issuer, secret, options) {
|
|
142
|
-
return otplibHotp.keyuri({
|
|
143
|
-
algorithm: options.algorithm,
|
|
144
|
-
digits: options.digits,
|
|
145
|
-
step: options.step,
|
|
146
|
-
type: otplibHotp.Strategy.TOTP,
|
|
147
|
-
accountName,
|
|
148
|
-
issuer,
|
|
149
|
-
secret
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
class TOTP extends otplibHotp.HOTP {
|
|
153
|
-
create(defaultOptions = {}) {
|
|
154
|
-
return new TOTP(defaultOptions);
|
|
155
|
-
}
|
|
156
|
-
allOptions() {
|
|
157
|
-
return totpOptions(this.options);
|
|
158
|
-
}
|
|
159
|
-
generate(secret) {
|
|
160
|
-
return totpToken(secret, this.allOptions());
|
|
161
|
-
}
|
|
162
|
-
checkDelta(token, secret) {
|
|
163
|
-
return totpCheckWithWindow(token, secret, this.allOptions());
|
|
164
|
-
}
|
|
165
|
-
check(token, secret) {
|
|
166
|
-
const delta = this.checkDelta(token, secret);
|
|
167
|
-
return typeof delta === 'number';
|
|
168
|
-
}
|
|
169
|
-
verify(opts) {
|
|
170
|
-
if (typeof opts !== 'object') {
|
|
171
|
-
throw new Error('Expecting argument 0 of verify to be an object');
|
|
172
|
-
}
|
|
173
|
-
return this.check(opts.token, opts.secret);
|
|
174
|
-
}
|
|
175
|
-
timeRemaining() {
|
|
176
|
-
const options = this.allOptions();
|
|
177
|
-
return totpTimeRemaining(options.epoch, options.step);
|
|
178
|
-
}
|
|
179
|
-
timeUsed() {
|
|
180
|
-
const options = this.allOptions();
|
|
181
|
-
return totpTimeUsed(options.epoch, options.step);
|
|
182
|
-
}
|
|
183
|
-
keyuri(accountName, issuer, secret) {
|
|
184
|
-
return totpKeyuri(accountName, issuer, secret, this.allOptions());
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
exports.TOTP = TOTP;
|
|
189
|
-
exports.totpCheck = totpCheck;
|
|
190
|
-
exports.totpCheckByEpoch = totpCheckByEpoch;
|
|
191
|
-
exports.totpCheckWithWindow = totpCheckWithWindow;
|
|
192
|
-
exports.totpCounter = totpCounter;
|
|
193
|
-
exports.totpCreateHmacKey = totpCreateHmacKey;
|
|
194
|
-
exports.totpDefaultOptions = totpDefaultOptions;
|
|
195
|
-
exports.totpEpochAvailable = totpEpochAvailable;
|
|
196
|
-
exports.totpKeyuri = totpKeyuri;
|
|
197
|
-
exports.totpOptions = totpOptions;
|
|
198
|
-
exports.totpOptionsValidator = totpOptionsValidator;
|
|
199
|
-
exports.totpPadSecret = totpPadSecret;
|
|
200
|
-
exports.totpTimeRemaining = totpTimeRemaining;
|
|
201
|
-
exports.totpTimeUsed = totpTimeUsed;
|
|
202
|
-
exports.totpToken = totpToken;
|
package/totp/totp.d.ts
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import { CreateHmacKey, HOTP, HOTPOptions, KeyEncodings, SecretKey } from '../hotp';
|
|
2
|
-
/**
|
|
3
|
-
* Interface for options used in TOTP.
|
|
4
|
-
*
|
|
5
|
-
* Contains additional options in addition to
|
|
6
|
-
* those within HOTP.
|
|
7
|
-
*/
|
|
8
|
-
export interface TOTPOptions<T = string> extends HOTPOptions<T> {
|
|
9
|
-
/**
|
|
10
|
-
* The starting time since the JavasSript epoch (seconds) (UNIX epoch * 1000).
|
|
11
|
-
*/
|
|
12
|
-
epoch: number;
|
|
13
|
-
/**
|
|
14
|
-
* Time step (seconds).
|
|
15
|
-
*/
|
|
16
|
-
step: number;
|
|
17
|
-
/**
|
|
18
|
-
* How many windows (x * step) past and future do we consider as valid during check.
|
|
19
|
-
*/
|
|
20
|
-
window: number | [number, number];
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Interface for available epoches derived from
|
|
24
|
-
* the current epoch.
|
|
25
|
-
*/
|
|
26
|
-
export interface EpochAvailable {
|
|
27
|
-
current: number;
|
|
28
|
-
future: number[];
|
|
29
|
-
past: number[];
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Validates the given [[TOTPOptions]].
|
|
33
|
-
*/
|
|
34
|
-
export declare function totpOptionsValidator<T extends TOTPOptions<unknown> = TOTPOptions<unknown>>(options: Readonly<Partial<T>>): void;
|
|
35
|
-
/**
|
|
36
|
-
* Pads the secret to the expected minimum length
|
|
37
|
-
* and returns a hex representation of the string.
|
|
38
|
-
*/
|
|
39
|
-
export declare const totpPadSecret: (secret: string, encoding: KeyEncodings, minLength: number) => string;
|
|
40
|
-
/**
|
|
41
|
-
* Takes a TOTP secret and derives the HMAC key
|
|
42
|
-
* for use in token generation.
|
|
43
|
-
*
|
|
44
|
-
* In RFC 6238, the secret / seed length for different algorithms
|
|
45
|
-
* are predefined.
|
|
46
|
-
*
|
|
47
|
-
* - HMAC-SHA1 (20 bytes)
|
|
48
|
-
* - HMAC-SHA256 (32 bytes)
|
|
49
|
-
* - HMAC-SHA512 (64 bytes)
|
|
50
|
-
*
|
|
51
|
-
* @param algorithm - Reference: [[TOTPOptions.algorithm]]
|
|
52
|
-
* @param secret
|
|
53
|
-
* @param encoding - Reference: [[TOTPOptions.encoding]]
|
|
54
|
-
*/
|
|
55
|
-
export declare const totpCreateHmacKey: CreateHmacKey;
|
|
56
|
-
/**
|
|
57
|
-
* Returns a set of default options for TOTP at the current epoch.
|
|
58
|
-
*/
|
|
59
|
-
export declare function totpDefaultOptions<T extends TOTPOptions<unknown> = TOTPOptions<unknown>>(): Partial<T>;
|
|
60
|
-
/**
|
|
61
|
-
* Takes an TOTP Option object and provides presets for
|
|
62
|
-
* some of the missing required TOTP option fields and validates
|
|
63
|
-
* the resultant options.
|
|
64
|
-
*/
|
|
65
|
-
export declare function totpOptions<T extends TOTPOptions<unknown> = TOTPOptions<unknown>>(opt: Partial<T>): Readonly<T>;
|
|
66
|
-
/**
|
|
67
|
-
* Generates the counter based on the current epoch and step.
|
|
68
|
-
* This dynamic counter is used in the HOTP algorithm.
|
|
69
|
-
*
|
|
70
|
-
* @param epoch - Reference: [[TOTPOptions.epoch]]
|
|
71
|
-
* @param step - Reference: [[TOTPOptions.step]]
|
|
72
|
-
*/
|
|
73
|
-
export declare function totpCounter(epoch: number, step: number): number;
|
|
74
|
-
/**
|
|
75
|
-
* Generates a Time-based One-time Token (TOTP)
|
|
76
|
-
*
|
|
77
|
-
* tl;dr: TOTP = HOTP + counter based on current time.
|
|
78
|
-
*
|
|
79
|
-
* **References**
|
|
80
|
-
*
|
|
81
|
-
* - http://tools.ietf.org/html/rfc6238
|
|
82
|
-
* - http://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm
|
|
83
|
-
*
|
|
84
|
-
*/
|
|
85
|
-
export declare function totpToken<T extends TOTPOptions<unknown> = TOTPOptions<unknown>>(secret: SecretKey, options: Readonly<T>): string;
|
|
86
|
-
/**
|
|
87
|
-
* Gets a set of epoches derived from
|
|
88
|
-
* the current epoch and the acceptable window.
|
|
89
|
-
*
|
|
90
|
-
* @param epoch - Reference: [[TOTPOptions.epoch]]
|
|
91
|
-
* @param step - Reference: [[TOTPOptions.step]]
|
|
92
|
-
* @param win - Reference: [[TOTPOptions.window]]
|
|
93
|
-
*/
|
|
94
|
-
export declare function totpEpochAvailable(epoch: number, step: number, win: number | [number, number]): EpochAvailable;
|
|
95
|
-
/**
|
|
96
|
-
* Checks the given token against the system generated token.
|
|
97
|
-
*
|
|
98
|
-
* **Note**: Token is valid only if it is a number string.
|
|
99
|
-
*/
|
|
100
|
-
export declare function totpCheck<T extends TOTPOptions<unknown> = TOTPOptions<unknown>>(token: string, secret: SecretKey, options: Readonly<T>): boolean;
|
|
101
|
-
/**
|
|
102
|
-
* Checks if there is a valid TOTP token in a given list of epoches.
|
|
103
|
-
* Returns the (index + 1) of a valid epoch in the list.
|
|
104
|
-
*
|
|
105
|
-
* @param epochs - List of epochs to check token against
|
|
106
|
-
* @param token - The token to check
|
|
107
|
-
* @param secret - Your secret key.
|
|
108
|
-
* @param options - A TOTPOptions object.
|
|
109
|
-
*/
|
|
110
|
-
export declare function totpCheckByEpoch<T extends TOTPOptions = TOTPOptions>(epochs: number[], token: string, secret: SecretKey, options: Readonly<T>): number | null;
|
|
111
|
-
/**
|
|
112
|
-
* Checks the provided OTP token against system generated token
|
|
113
|
-
* with support for checking past or future x * step windows.
|
|
114
|
-
*
|
|
115
|
-
* Return values:
|
|
116
|
-
*
|
|
117
|
-
* - null = check failed
|
|
118
|
-
* - positive number = token at future x * step
|
|
119
|
-
* - negative number = token at past x * step
|
|
120
|
-
*
|
|
121
|
-
* @param token - The token to check
|
|
122
|
-
* @param secret - Your secret key.
|
|
123
|
-
* @param options - A TOTPOptions object.
|
|
124
|
-
*/
|
|
125
|
-
export declare function totpCheckWithWindow<T extends TOTPOptions = TOTPOptions>(token: string, secret: SecretKey, options: Readonly<T>): number | null;
|
|
126
|
-
/**
|
|
127
|
-
* Calculates the number of seconds used in the current tick for TOTP.
|
|
128
|
-
*
|
|
129
|
-
* The start of a new token: `timeUsed() === 0`
|
|
130
|
-
*
|
|
131
|
-
* @param epoch - Reference: [[TOTPOptions.epoch]]
|
|
132
|
-
* @param step - Reference: [[TOTPOptions.step]]
|
|
133
|
-
*/
|
|
134
|
-
export declare function totpTimeUsed(epoch: number, step: number): number;
|
|
135
|
-
/**
|
|
136
|
-
* Calculates the number of seconds till next tick for TOTP.
|
|
137
|
-
*
|
|
138
|
-
* The start of a new token: `timeRemaining() === step`
|
|
139
|
-
*
|
|
140
|
-
* @param epoch - Reference: [[TOTPOptions.epoch]]
|
|
141
|
-
* @param step - Reference: [[TOTPOptions.step]]
|
|
142
|
-
*/
|
|
143
|
-
export declare function totpTimeRemaining(epoch: number, step: number): number;
|
|
144
|
-
/**
|
|
145
|
-
* Generates a [keyuri](../#keyuri) from options provided
|
|
146
|
-
* and it's type set to TOTP.
|
|
147
|
-
*/
|
|
148
|
-
export declare function totpKeyuri<T extends TOTPOptions<unknown> = TOTPOptions<unknown>>(accountName: string, issuer: string, secret: SecretKey, options: Readonly<T>): string;
|
|
149
|
-
/**
|
|
150
|
-
* A class wrapper containing all TOTP methods.
|
|
151
|
-
*/
|
|
152
|
-
export declare class TOTP<T extends TOTPOptions = TOTPOptions> extends HOTP<T> {
|
|
153
|
-
/**
|
|
154
|
-
* Creates a new instance with all defaultOptions and options reset.
|
|
155
|
-
*/
|
|
156
|
-
create(defaultOptions?: Partial<T>): TOTP<T>;
|
|
157
|
-
/**
|
|
158
|
-
* Returns class options polyfilled with some of
|
|
159
|
-
* the missing required options.
|
|
160
|
-
*
|
|
161
|
-
* Reference: [[totpOptions]]
|
|
162
|
-
*/
|
|
163
|
-
allOptions(): Readonly<T>;
|
|
164
|
-
/**
|
|
165
|
-
* Reference: [[totpToken]]
|
|
166
|
-
*/
|
|
167
|
-
generate(secret: SecretKey): string;
|
|
168
|
-
/**
|
|
169
|
-
* Reference: [[totpCheckWithWindow]]
|
|
170
|
-
*/
|
|
171
|
-
checkDelta(token: string, secret: SecretKey): number | null;
|
|
172
|
-
/**
|
|
173
|
-
* Checks if a given TOTP token matches the generated
|
|
174
|
-
* token at the given epoch (default to current time).
|
|
175
|
-
*
|
|
176
|
-
* This method will return true as long as the token is
|
|
177
|
-
* still within the acceptable time window defined.
|
|
178
|
-
*
|
|
179
|
-
* i.e when [[checkDelta]] returns a number.
|
|
180
|
-
*/
|
|
181
|
-
check(token: string, secret: SecretKey): boolean;
|
|
182
|
-
/**
|
|
183
|
-
* Same as [[check]] but accepts a single object based argument.
|
|
184
|
-
*/
|
|
185
|
-
verify(opts: {
|
|
186
|
-
token: string;
|
|
187
|
-
secret: SecretKey;
|
|
188
|
-
}): boolean;
|
|
189
|
-
/**
|
|
190
|
-
* Reference: [[totpTimeRemaining]]
|
|
191
|
-
*/
|
|
192
|
-
timeRemaining(): number;
|
|
193
|
-
/**
|
|
194
|
-
* Reference: [[totpTimeUsed]]
|
|
195
|
-
*/
|
|
196
|
-
timeUsed(): number;
|
|
197
|
-
/**
|
|
198
|
-
* Reference: [[totpKeyuri]]
|
|
199
|
-
*/
|
|
200
|
-
keyuri(accountName: string, issuer: string, secret: SecretKey): string;
|
|
201
|
-
}
|
package/totp-async/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './totp';
|
package/totp-async/index.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* otplib-totp-async
|
|
3
|
-
*
|
|
4
|
-
* @author Gerald Yeo <contact@fusedthought.com>
|
|
5
|
-
* @version: 12.0.0-1
|
|
6
|
-
* @license: MIT
|
|
7
|
-
**/
|
|
8
|
-
'use strict';
|
|
9
|
-
|
|
10
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
11
|
-
|
|
12
|
-
var otplibTotp = require('../totp');
|
|
13
|
-
var otplibHotpAsync = require('../hotp-async');
|
|
14
|
-
|
|
15
|
-
async function totpDigestAsync(secret, options) {
|
|
16
|
-
const counter = otplibTotp.totpCounter(options.epoch, options.step);
|
|
17
|
-
return otplibHotpAsync.hotpDigestAsync(secret, counter, options);
|
|
18
|
-
}
|
|
19
|
-
async function totpTokenAsync(secret, options) {
|
|
20
|
-
const digest = await totpDigestAsync(secret, options);
|
|
21
|
-
return otplibTotp.totpToken(secret, { ...options,
|
|
22
|
-
digest
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
async function totpCheckAsync(token, secret, options) {
|
|
26
|
-
const digest = await totpDigestAsync(secret, options);
|
|
27
|
-
return otplibTotp.totpCheck(token, secret, { ...options,
|
|
28
|
-
digest
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
async function totpCheckByEpochAsync(epochs, token, secret, options) {
|
|
32
|
-
let position = null;
|
|
33
|
-
const digests = await Promise.all(epochs.map(epoch => totpDigestAsync(secret, { ...options,
|
|
34
|
-
epoch
|
|
35
|
-
})));
|
|
36
|
-
digests.some((digest, idx) => {
|
|
37
|
-
const result = otplibTotp.totpCheck(token, secret, { ...options,
|
|
38
|
-
digest
|
|
39
|
-
});
|
|
40
|
-
if (result) {
|
|
41
|
-
position = idx + 1;
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
return false;
|
|
45
|
-
});
|
|
46
|
-
return position;
|
|
47
|
-
}
|
|
48
|
-
async function totpCheckWithWindowAsync(token, secret, options) {
|
|
49
|
-
const checkZero = await totpCheckAsync(token, secret, options);
|
|
50
|
-
if (checkZero) {
|
|
51
|
-
return 0;
|
|
52
|
-
}
|
|
53
|
-
const epochs = otplibTotp.totpEpochAvailable(options.epoch, options.step, options.window);
|
|
54
|
-
const backward = await totpCheckByEpochAsync(epochs.past, token, secret, options);
|
|
55
|
-
if (backward !== null) {
|
|
56
|
-
return backward * -1;
|
|
57
|
-
}
|
|
58
|
-
return totpCheckByEpochAsync(epochs.future, token, secret, options);
|
|
59
|
-
}
|
|
60
|
-
class TOTPAsync extends otplibHotpAsync.HOTPAsync {
|
|
61
|
-
create(defaultOptions = {}) {
|
|
62
|
-
return new TOTPAsync(defaultOptions);
|
|
63
|
-
}
|
|
64
|
-
allOptions() {
|
|
65
|
-
return otplibTotp.totpOptions(this.options);
|
|
66
|
-
}
|
|
67
|
-
async generate(secret) {
|
|
68
|
-
return totpTokenAsync(secret, this.allOptions());
|
|
69
|
-
}
|
|
70
|
-
async checkDelta(token, secret) {
|
|
71
|
-
return totpCheckWithWindowAsync(token, secret, this.allOptions());
|
|
72
|
-
}
|
|
73
|
-
async check(token, secret) {
|
|
74
|
-
const delta = await this.checkDelta(token, secret);
|
|
75
|
-
return typeof delta === 'number';
|
|
76
|
-
}
|
|
77
|
-
async verify(opts) {
|
|
78
|
-
return this.check(opts.token, opts.secret);
|
|
79
|
-
}
|
|
80
|
-
async timeRemaining() {
|
|
81
|
-
const options = this.allOptions();
|
|
82
|
-
return otplibTotp.totpTimeRemaining(options.epoch, options.step);
|
|
83
|
-
}
|
|
84
|
-
async timeUsed() {
|
|
85
|
-
const options = this.allOptions();
|
|
86
|
-
return otplibTotp.totpTimeUsed(options.epoch, options.step);
|
|
87
|
-
}
|
|
88
|
-
async keyuri(accountName, issuer, secret) {
|
|
89
|
-
return otplibTotp.totpKeyuri(accountName, issuer, secret, this.allOptions());
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
exports.TOTPAsync = TOTPAsync;
|
|
94
|
-
exports.totpCheckAsync = totpCheckAsync;
|
|
95
|
-
exports.totpCheckByEpochAsync = totpCheckByEpochAsync;
|
|
96
|
-
exports.totpCheckWithWindowAsync = totpCheckWithWindowAsync;
|
|
97
|
-
exports.totpDigestAsync = totpDigestAsync;
|
|
98
|
-
exports.totpTokenAsync = totpTokenAsync;
|
package/totp-async/totp.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { HexString, SecretKey } from '../hotp';
|
|
2
|
-
import { TOTPOptions } from '../totp';
|
|
3
|
-
import { HOTPAsync } from '../hotp-async';
|
|
4
|
-
/**
|
|
5
|
-
* Allow TOTPOptions to accept async method options.
|
|
6
|
-
*/
|
|
7
|
-
export declare type TOTPAsyncOptions = TOTPOptions<Promise<string>>;
|
|
8
|
-
/**
|
|
9
|
-
* Generates the digest for TOTP based tokens.
|
|
10
|
-
*
|
|
11
|
-
* Uses [[hotpDigestAsync]].
|
|
12
|
-
*/
|
|
13
|
-
export declare function totpDigestAsync<T extends TOTPAsyncOptions = TOTPAsyncOptions>(secret: SecretKey, options: Readonly<T>): Promise<HexString>;
|
|
14
|
-
/**
|
|
15
|
-
* Async version of [[totpToken]].
|
|
16
|
-
*/
|
|
17
|
-
export declare function totpTokenAsync<T extends TOTPAsyncOptions = TOTPAsyncOptions>(secret: SecretKey, options: Readonly<T>): Promise<string>;
|
|
18
|
-
/**
|
|
19
|
-
* Async version of [[totpCheck]].
|
|
20
|
-
*/
|
|
21
|
-
export declare function totpCheckAsync<T extends TOTPAsyncOptions = TOTPAsyncOptions>(token: string, secret: SecretKey, options: Readonly<T>): Promise<boolean>;
|
|
22
|
-
/**
|
|
23
|
-
* Async version of [[totpCheckByEpoch]].
|
|
24
|
-
*/
|
|
25
|
-
export declare function totpCheckByEpochAsync<T extends TOTPAsyncOptions = TOTPAsyncOptions>(epochs: number[], token: string, secret: SecretKey, options: Readonly<T>): Promise<number | null>;
|
|
26
|
-
/**
|
|
27
|
-
* Async version of [[totpCheckWithWindow]].
|
|
28
|
-
*/
|
|
29
|
-
export declare function totpCheckWithWindowAsync<T extends TOTPAsyncOptions = TOTPAsyncOptions>(token: string, secret: SecretKey, options: Readonly<T>): Promise<number | null>;
|
|
30
|
-
/**
|
|
31
|
-
* Async version of [[TOTP]].
|
|
32
|
-
*/
|
|
33
|
-
export declare class TOTPAsync<T extends TOTPAsyncOptions = TOTPAsyncOptions> extends HOTPAsync<T> {
|
|
34
|
-
create(defaultOptions?: Partial<T>): TOTPAsync<T>;
|
|
35
|
-
allOptions(): Readonly<T>;
|
|
36
|
-
generate(secret: SecretKey): Promise<string>;
|
|
37
|
-
checkDelta(token: string, secret: SecretKey): Promise<number | null>;
|
|
38
|
-
check(token: string, secret: SecretKey): Promise<boolean>;
|
|
39
|
-
verify(opts: {
|
|
40
|
-
token: string;
|
|
41
|
-
secret: SecretKey;
|
|
42
|
-
}): Promise<boolean>;
|
|
43
|
-
timeRemaining(): Promise<number>;
|
|
44
|
-
timeUsed(): Promise<number>;
|
|
45
|
-
keyuri(accountName: string, issuer: string, secret: SecretKey): Promise<string>;
|
|
46
|
-
}
|