File size: 568 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import totp from 'totp-generator';

/**
 * Client to generate TOTP codes, used for 2FA on WordPress.com.
 */
export class TOTPClient {
	private secret: string;

	/**
	 * Constructs an instance of the TOTP client.
	 *
	 * @param {string} secret Secret value used to generate TOTP codes.
	 */
	constructor( secret: string ) {
		this.secret = secret;
	}

	/**
	 * Returns the TOTP code.
	 *
	 * @returns {string} TOTP code valid for the epoch for which it was generated.
	 */
	getToken(): string {
		return totp( this.secret, { timestamp: Date.now() } ).toString();
	}
}