"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwoFactorCommand = void 0;
const tslib_1 = require("tslib");
const otpauth_1 = require("otpauth");
const requests_1 = require("../requests");
const shared_1 = require("../shared");
class TwoFactorCommand extends shared_1.AbstractCommand {
    static buildOTP(options) {
        const { secret } = options;
        return new otpauth_1.TOTP({
            issuer: 'PeerTube',
            algorithm: 'SHA1',
            digits: 6,
            period: 30,
            secret
        });
    }
    request(options) {
        const { currentPassword, userId } = options;
        const path = '/api/v1/users/' + userId + '/two-factor/request';
        return (0, requests_1.unwrapBody)(this.postBodyRequest(Object.assign(Object.assign({}, options), { path, fields: { currentPassword }, implicitToken: true, defaultExpectedStatus: 200 })));
    }
    confirmRequest(options) {
        const { userId, requestToken, otpToken } = options;
        const path = '/api/v1/users/' + userId + '/two-factor/confirm-request';
        return this.postBodyRequest(Object.assign(Object.assign({}, options), { path, fields: { requestToken, otpToken }, implicitToken: true, defaultExpectedStatus: 204 }));
    }
    disable(options) {
        const { userId, currentPassword } = options;
        const path = '/api/v1/users/' + userId + '/two-factor/disable';
        return this.postBodyRequest(Object.assign(Object.assign({}, options), { path, fields: { currentPassword }, implicitToken: true, defaultExpectedStatus: 204 }));
    }
    requestAndConfirm(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { userId, currentPassword } = options;
            const { otpRequest } = yield this.request({ userId, currentPassword });
            yield this.confirmRequest({
                userId,
                requestToken: otpRequest.requestToken,
                otpToken: TwoFactorCommand.buildOTP({ secret: otpRequest.secret }).generate()
            });
            return otpRequest;
        });
    }
}
exports.TwoFactorCommand = TwoFactorCommand;
//# sourceMappingURL=two-factor-command.js.map