UNPKG

@imajin/rx-otp

Version:

HMAC-based (HOTP) and Time-based (TOTP) One-Time Password manager. Works with Google Authenticator for Two-Factor Authentication.

33 lines (32 loc) 1.37 kB
import { mergeMap, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { HOTP } from './hotp'; import { Validator } from '../schemas/validator'; export class TOTP { } TOTP.generate = (key, options = {}) => of(Object.assign(Object.assign({}, options), { key })) .pipe(mergeMap((data) => Validator.validateDataWithSchemaReference('/rx-otp/schemas/totp-generate.json', data)), map((_) => ({ key: _.key, options: { key_format: _.key_format, counter: Math.floor((_.timestamp / 1000) / _.time), code_digits: _.code_digits, add_checksum: _.add_checksum, truncation_offset: _.truncation_offset, algorithm: _.algorithm } })), mergeMap((_) => HOTP.generate(_.key, _.options))); TOTP.verify = (token, key, options = {}) => of(Object.assign(Object.assign({}, options), { token, key })) .pipe(mergeMap((data) => Validator.validateDataWithSchemaReference('/rx-otp/schemas/totp-verify.json', data)), map((_) => ({ token: _.token, key: _.key, options: { key_format: _.key_format, window: _.window, counter: Math.floor((_.timestamp / 1000) / _.time), add_checksum: _.add_checksum, truncation_offset: _.truncation_offset, algorithm: _.algorithm, previous_otp_allowed: true } })), mergeMap((_) => HOTP.verify(_.token, _.key, _.options)));