@imajin/rx-otp
Version:
HMAC-based (HOTP) and Time-based (TOTP) One-Time Password manager. Works with Google Authenticator for Two-Factor Authentication.
37 lines (36 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TOTP = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const hotp_1 = require("./hotp");
const validator_1 = require("../schemas/validator");
class TOTP {
}
exports.TOTP = TOTP;
TOTP.generate = (key, options = {}) => (0, rxjs_1.of)(Object.assign(Object.assign({}, options), { key }))
.pipe((0, rxjs_1.mergeMap)((data) => validator_1.Validator.validateDataWithSchemaReference('/rx-otp/schemas/totp-generate.json', data)), (0, operators_1.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
}
})), (0, rxjs_1.mergeMap)((_) => hotp_1.HOTP.generate(_.key, _.options)));
TOTP.verify = (token, key, options = {}) => (0, rxjs_1.of)(Object.assign(Object.assign({}, options), { token, key }))
.pipe((0, rxjs_1.mergeMap)((data) => validator_1.Validator.validateDataWithSchemaReference('/rx-otp/schemas/totp-verify.json', data)), (0, operators_1.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
}
})), (0, rxjs_1.mergeMap)((_) => hotp_1.HOTP.verify(_.token, _.key, _.options)));