otpauth.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * OTP secret key.
  3. */
  4. declare class Secret {
  5. /**
  6. * Converts a Latin-1 string to a Secret object.
  7. * @param {string} str Latin-1 string.
  8. * @returns {Secret} Secret object.
  9. */
  10. static fromLatin1(str: string): Secret;
  11. /**
  12. * Converts an UTF-8 string to a Secret object.
  13. * @param {string} str UTF-8 string.
  14. * @returns {Secret} Secret object.
  15. */
  16. static fromUTF8(str: string): Secret;
  17. /**
  18. * Converts a base32 string to a Secret object.
  19. * @param {string} str Base32 string.
  20. * @returns {Secret} Secret object.
  21. */
  22. static fromBase32(str: string): Secret;
  23. /**
  24. * Converts a hexadecimal string to a Secret object.
  25. * @param {string} str Hexadecimal string.
  26. * @returns {Secret} Secret object.
  27. */
  28. static fromHex(str: string): Secret;
  29. /**
  30. * Creates a secret key object.
  31. * @param {Object} [config] Configuration options.
  32. * @param {ArrayBufferLike} [config.buffer] Secret key buffer.
  33. * @param {number} [config.size=20] Number of random bytes to generate, ignored if 'buffer' is provided.
  34. */
  35. constructor({ buffer, size }?: {
  36. buffer?: ArrayBufferLike | undefined;
  37. size?: number | undefined;
  38. } | undefined);
  39. /**
  40. * Secret key.
  41. * @type {Uint8Array}
  42. * @readonly
  43. */
  44. readonly bytes: Uint8Array;
  45. /**
  46. * Secret key buffer.
  47. * @deprecated For backward compatibility, the "bytes" property should be used instead.
  48. * @type {ArrayBufferLike}
  49. */
  50. get buffer(): ArrayBufferLike;
  51. /**
  52. * Latin-1 string representation of secret key.
  53. * @type {string}
  54. */
  55. get latin1(): string;
  56. /**
  57. * UTF-8 string representation of secret key.
  58. * @type {string}
  59. */
  60. get utf8(): string;
  61. /**
  62. * Base32 string representation of secret key.
  63. * @type {string}
  64. */
  65. get base32(): string;
  66. /**
  67. * Hexadecimal string representation of secret key.
  68. * @type {string}
  69. */
  70. get hex(): string;
  71. }
  72. /**
  73. * HOTP: An HMAC-based One-time Password Algorithm.
  74. * @see [RFC 4226](https://tools.ietf.org/html/rfc4226)
  75. */
  76. declare class HOTP {
  77. /**
  78. * Default configuration.
  79. * @type {{
  80. * issuer: string,
  81. * label: string,
  82. * issuerInLabel: boolean,
  83. * algorithm: string,
  84. * digits: number,
  85. * counter: number
  86. * window: number
  87. * }}
  88. */
  89. static get defaults(): {
  90. issuer: string;
  91. label: string;
  92. issuerInLabel: boolean;
  93. algorithm: string;
  94. digits: number;
  95. counter: number;
  96. window: number;
  97. };
  98. /**
  99. * Generates an HOTP token.
  100. * @param {Object} config Configuration options.
  101. * @param {Secret} config.secret Secret key.
  102. * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
  103. * @param {number} [config.digits=6] Token length.
  104. * @param {number} [config.counter=0] Counter value.
  105. * @returns {string} Token.
  106. */
  107. static generate({ secret, algorithm, digits, counter, }: {
  108. secret: Secret;
  109. algorithm?: string | undefined;
  110. digits?: number | undefined;
  111. counter?: number | undefined;
  112. }): string;
  113. /**
  114. * Validates an HOTP token.
  115. * @param {Object} config Configuration options.
  116. * @param {string} config.token Token value.
  117. * @param {Secret} config.secret Secret key.
  118. * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
  119. * @param {number} config.digits Token length.
  120. * @param {number} [config.counter=0] Counter value.
  121. * @param {number} [config.window=1] Window of counter values to test.
  122. * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
  123. */
  124. static validate({ token, secret, algorithm, digits, counter, window, }: {
  125. token: string;
  126. secret: Secret;
  127. algorithm?: string | undefined;
  128. digits: number;
  129. counter?: number | undefined;
  130. window?: number | undefined;
  131. }): number | null;
  132. /**
  133. * Creates an HOTP object.
  134. * @param {Object} [config] Configuration options.
  135. * @param {string} [config.issuer=''] Account provider.
  136. * @param {string} [config.label='OTPAuth'] Account label.
  137. * @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
  138. * @param {Secret|string} [config.secret=Secret] Secret key.
  139. * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
  140. * @param {number} [config.digits=6] Token length.
  141. * @param {number} [config.counter=0] Initial counter value.
  142. */
  143. constructor({ issuer, label, issuerInLabel, secret, algorithm, digits, counter, }?: {
  144. issuer?: string | undefined;
  145. label?: string | undefined;
  146. issuerInLabel?: boolean | undefined;
  147. secret?: string | Secret | undefined;
  148. algorithm?: string | undefined;
  149. digits?: number | undefined;
  150. counter?: number | undefined;
  151. } | undefined);
  152. /**
  153. * Account provider.
  154. * @type {string}
  155. */
  156. issuer: string;
  157. /**
  158. * Account label.
  159. * @type {string}
  160. */
  161. label: string;
  162. /**
  163. * Include issuer prefix in label.
  164. * @type {boolean}
  165. */
  166. issuerInLabel: boolean;
  167. /**
  168. * Secret key.
  169. * @type {Secret}
  170. */
  171. secret: Secret;
  172. /**
  173. * HMAC hashing algorithm.
  174. * @type {string}
  175. */
  176. algorithm: string;
  177. /**
  178. * Token length.
  179. * @type {number}
  180. */
  181. digits: number;
  182. /**
  183. * Initial counter value.
  184. * @type {number}
  185. */
  186. counter: number;
  187. /**
  188. * Generates an HOTP token.
  189. * @param {Object} [config] Configuration options.
  190. * @param {number} [config.counter=this.counter++] Counter value.
  191. * @returns {string} Token.
  192. */
  193. generate({ counter }?: {
  194. counter?: number | undefined;
  195. } | undefined): string;
  196. /**
  197. * Validates an HOTP token.
  198. * @param {Object} config Configuration options.
  199. * @param {string} config.token Token value.
  200. * @param {number} [config.counter=this.counter] Counter value.
  201. * @param {number} [config.window=1] Window of counter values to test.
  202. * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
  203. */
  204. validate({ token, counter, window }: {
  205. token: string;
  206. counter?: number | undefined;
  207. window?: number | undefined;
  208. }): number | null;
  209. /**
  210. * Returns a Google Authenticator key URI.
  211. * @returns {string} URI.
  212. */
  213. toString(): string;
  214. }
  215. /**
  216. * TOTP: Time-Based One-Time Password Algorithm.
  217. * @see [RFC 6238](https://tools.ietf.org/html/rfc6238)
  218. */
  219. declare class TOTP {
  220. /**
  221. * Default configuration.
  222. * @type {{
  223. * issuer: string,
  224. * label: string,
  225. * issuerInLabel: boolean,
  226. * algorithm: string,
  227. * digits: number,
  228. * period: number
  229. * window: number
  230. * }}
  231. */
  232. static get defaults(): {
  233. issuer: string;
  234. label: string;
  235. issuerInLabel: boolean;
  236. algorithm: string;
  237. digits: number;
  238. period: number;
  239. window: number;
  240. };
  241. /**
  242. * Generates a TOTP token.
  243. * @param {Object} config Configuration options.
  244. * @param {Secret} config.secret Secret key.
  245. * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
  246. * @param {number} [config.digits=6] Token length.
  247. * @param {number} [config.period=30] Token time-step duration.
  248. * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
  249. * @returns {string} Token.
  250. */
  251. static generate({ secret, algorithm, digits, period, timestamp }: {
  252. secret: Secret;
  253. algorithm?: string | undefined;
  254. digits?: number | undefined;
  255. period?: number | undefined;
  256. timestamp?: number | undefined;
  257. }): string;
  258. /**
  259. * Validates a TOTP token.
  260. * @param {Object} config Configuration options.
  261. * @param {string} config.token Token value.
  262. * @param {Secret} config.secret Secret key.
  263. * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
  264. * @param {number} config.digits Token length.
  265. * @param {number} [config.period=30] Token time-step duration.
  266. * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
  267. * @param {number} [config.window=1] Window of counter values to test.
  268. * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
  269. */
  270. static validate({ token, secret, algorithm, digits, period, timestamp, window }: {
  271. token: string;
  272. secret: Secret;
  273. algorithm?: string | undefined;
  274. digits: number;
  275. period?: number | undefined;
  276. timestamp?: number | undefined;
  277. window?: number | undefined;
  278. }): number | null;
  279. /**
  280. * Creates a TOTP object.
  281. * @param {Object} [config] Configuration options.
  282. * @param {string} [config.issuer=''] Account provider.
  283. * @param {string} [config.label='OTPAuth'] Account label.
  284. * @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
  285. * @param {Secret|string} [config.secret=Secret] Secret key.
  286. * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
  287. * @param {number} [config.digits=6] Token length.
  288. * @param {number} [config.period=30] Token time-step duration.
  289. */
  290. constructor({ issuer, label, issuerInLabel, secret, algorithm, digits, period, }?: {
  291. issuer?: string | undefined;
  292. label?: string | undefined;
  293. issuerInLabel?: boolean | undefined;
  294. secret?: string | Secret | undefined;
  295. algorithm?: string | undefined;
  296. digits?: number | undefined;
  297. period?: number | undefined;
  298. } | undefined);
  299. /**
  300. * Account provider.
  301. * @type {string}
  302. */
  303. issuer: string;
  304. /**
  305. * Account label.
  306. * @type {string}
  307. */
  308. label: string;
  309. /**
  310. * Include issuer prefix in label.
  311. * @type {boolean}
  312. */
  313. issuerInLabel: boolean;
  314. /**
  315. * Secret key.
  316. * @type {Secret}
  317. */
  318. secret: Secret;
  319. /**
  320. * HMAC hashing algorithm.
  321. * @type {string}
  322. */
  323. algorithm: string;
  324. /**
  325. * Token length.
  326. * @type {number}
  327. */
  328. digits: number;
  329. /**
  330. * Token time-step duration.
  331. * @type {number}
  332. */
  333. period: number;
  334. /**
  335. * Generates a TOTP token.
  336. * @param {Object} [config] Configuration options.
  337. * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
  338. * @returns {string} Token.
  339. */
  340. generate({ timestamp }?: {
  341. timestamp?: number | undefined;
  342. } | undefined): string;
  343. /**
  344. * Validates a TOTP token.
  345. * @param {Object} config Configuration options.
  346. * @param {string} config.token Token value.
  347. * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
  348. * @param {number} [config.window=1] Window of counter values to test.
  349. * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
  350. */
  351. validate({ token, timestamp, window }: {
  352. token: string;
  353. timestamp?: number | undefined;
  354. window?: number | undefined;
  355. }): number | null;
  356. /**
  357. * Returns a Google Authenticator key URI.
  358. * @returns {string} URI.
  359. */
  360. toString(): string;
  361. }
  362. /**
  363. * HOTP/TOTP object/string conversion.
  364. * @see [Key URI Format](https://github.com/google/google-authenticator/wiki/Key-Uri-Format)
  365. */
  366. declare class URI {
  367. /**
  368. * Parses a Google Authenticator key URI and returns an HOTP/TOTP object.
  369. * @param {string} uri Google Authenticator Key URI.
  370. * @returns {HOTP|TOTP} HOTP/TOTP object.
  371. */
  372. static parse(uri: string): HOTP | TOTP;
  373. /**
  374. * Converts an HOTP/TOTP object to a Google Authenticator key URI.
  375. * @param {HOTP|TOTP} otp HOTP/TOTP object.
  376. * @returns {string} Google Authenticator Key URI.
  377. */
  378. static stringify(otp: HOTP | TOTP): string;
  379. }
  380. /**
  381. * Library version.
  382. * @type {string}
  383. */
  384. declare const version: string;
  385. export { HOTP, Secret, TOTP, URI, version };