File size: 4,521 Bytes
2a1c46d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { CreateDigest, CreateHmacKey, HashAlgorithms, HexString, KeyEncodings, OTP, OTPOptions, SecretKey } from './utils';
/**
 * Interface for options used in HOTP.
 */
export interface HOTPOptions<T = string> extends OTPOptions {
    /**
     * Creates the digest which token is derived from.
     */
    createDigest: CreateDigest<T>;
    /**
     * Formats the secret into a HMAC key, applying transformations (like padding) where needed
     */
    createHmacKey: CreateHmacKey<T>;
    /**
     * The algorithm used for calculating the HMAC.
     */
    algorithm: HashAlgorithms;
    /**
     * **USE WITH CAUTION:** Given the same digest, the same token will be received.
     *
     * This is provided for unique use cases. For example, digest generation might
     * depend on an async API.
     */
    digest?: HexString;
    /**
     * The number of digits a token will have. Usually 6 or 8.
     */
    digits: number;
    /**
     * The encoding that was used on the secret.
     */
    encoding: KeyEncodings;
}
/**
 * Validates the given [[HOTPOptions]]
 */
export declare function hotpOptionsValidator<T extends HOTPOptions<unknown> = HOTPOptions<unknown>>(options: Readonly<Partial<T>>): void;
/**
 * Takes a HOTP secret and derives the HMAC key
 * for use in token generation.
 *
 * @param algorithm - Reference: [[HOTPOptions.algorithm]]
 * @param secret
 * @param encoding - Reference: [[HOTPOptions.encoding]]
 */
export declare const hotpCreateHmacKey: CreateHmacKey;
/**
 * Returns the default options for HOTP
 */
export declare function hotpDefaultOptions<T extends HOTPOptions<unknown> = HOTPOptions<unknown>>(): Partial<T>;
/**
 * Takes an HOTP Option object and provides presets for
 * some of the missing required HOTP option fields and validates
 * the resultant options.
 */
export declare function hotpOptions<T extends HOTPOptions<unknown> = HOTPOptions<unknown>>(opt: Readonly<Partial<T>>): Readonly<T>;
/**
 * Formats a given counter into a string counter.
 */
export declare function hotpCounter(counter: number): HexString;
/**
 * Converts a digest to a token of a specified length.
 */
export declare function hotpDigestToToken(hexDigest: HexString, digits: number): string;
/**
 * Generates a HMAC-based One-time Token (HOTP)
 *
 * **References**
 *
 * -   http://en.wikipedia.org/wiki/HMAC-based_One-time_Password_Algorithm
 * -   http://tools.ietf.org/html/rfc4226
 *
 * **Note**: If options.digest is provided, it will skip digest generation.
 * Use options.digest with caution. Same digest = Same token.
 *
 * @param secret - Your secret key.
 * @param counter - the OTP counter (usually it's an incremental count)
 * @param options - A HOTPOptions object.
 */
export declare function hotpToken<T extends HOTPOptions<unknown> = HOTPOptions<unknown>>(secret: SecretKey, counter: number, options: Readonly<T>): string;
/**
 * Checks the given token against the system generated token.
 *
 * **Note**: Token is valid only if it is a number string
 */
export declare function hotpCheck<T extends HOTPOptions<unknown> = HOTPOptions<unknown>>(token: string, secret: SecretKey, counter: number, options: Readonly<T>): boolean;
/**
 * Generates a [keyuri](../#keyuri) from options provided
 * and it's type set to HOTP.
 */
export declare function hotpKeyuri<T extends HOTPOptions<unknown> = HOTPOptions<unknown>>(accountName: string, issuer: string, secret: SecretKey, counter: number, options: Readonly<T>): string;
/**
 * A class wrapper containing all HOTP methods.
 */
export declare class HOTP<T extends HOTPOptions = HOTPOptions> extends OTP<T> {
    /**
     * Creates a new instance with all defaultOptions and options reset.
     */
    create(defaultOptions?: Partial<T>): HOTP<T>;
    /**
     * Returns class options polyfilled with some of
     * the missing required options.
     *
     * Reference: [[hotpOptions]]
     */
    allOptions(): Readonly<T>;
    /**
     * Reference: [[hotpToken]]
     */
    generate(secret: SecretKey, counter: number): string;
    /**
     * Reference: [[hotpCheck]]
     */
    check(token: string, secret: SecretKey, counter: number): boolean;
    /**
     * Same as [[check]] but accepts a single object based argument.
     */
    verify(opts: {
        token: string;
        secret: SecretKey;
        counter: number;
    }): boolean;
    /**
     * Calls [keyuri](../#keyuri) with class options and type
     * set to HOTP.
     */
    keyuri(accountName: string, issuer: string, secret: SecretKey, counter: number): string;
}