"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateOTPSecret = exports.isOTPValid = void 0;
const tslib_1 = require("tslib");
const otpauth_1 = require("otpauth");
const config_1 = require("../initializers/config");
const constants_1 = require("../initializers/constants");
const peertube_crypto_1 = require("./peertube-crypto");
function isOTPValid(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { token, encryptedSecret } = options;
        const secret = yield (0, peertube_crypto_1.decrypt)(encryptedSecret, config_1.CONFIG.SECRETS.PEERTUBE);
        const totp = new otpauth_1.TOTP(Object.assign(Object.assign({}, baseOTPOptions()), { secret }));
        const delta = totp.validate({
            token,
            window: 1
        });
        if (delta === null)
            return false;
        return true;
    });
}
exports.isOTPValid = isOTPValid;
function generateOTPSecret(email) {
    const totp = new otpauth_1.TOTP(Object.assign(Object.assign({}, baseOTPOptions()), { label: email, secret: new otpauth_1.Secret() }));
    return {
        secret: totp.secret.base32,
        uri: totp.toString()
    };
}
exports.generateOTPSecret = generateOTPSecret;
function baseOTPOptions() {
    return {
        issuer: constants_1.WEBSERVER.HOST,
        algorithm: 'SHA1',
        digits: 6,
        period: 30
    };
}
//# sourceMappingURL=otp.js.map