var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import * as OTPAuth from "otpauth";
import CaptureSpan from "./Telemetry/CaptureSpan";
/**
 * Utility class for handling TOTP authentication.
 */
export default class TotpAuth {
    /**
     * Generates a random secret key for two-factor authentication.
     * @returns The generated secret key.
     */
    static generateSecret() {
        return new OTPAuth.Secret().base32;
    }
    static getLabel(data) {
        return data.email.toString();
    }
    static getTotp(data) {
        const totp = new OTPAuth.TOTP({
            // Provider or service the account is associated with.
            issuer: "OneUptime",
            // Account identifier.
            label: this.getLabel({
                email: data.email,
            }),
            // Algorithm used for the HMAC function.
            algorithm: "SHA256",
            // Length of the generated tokens.
            digits: 6,
            // Interval of time for which a token is valid, in seconds.
            period: 30,
            // Arbitrary key encoded in Base32 or OTPAuth.Secret instance.
            secret: data.secret,
        });
        return totp;
    }
    /**
     * Verifies if a given token matches the provided secret key.
     * @param data - The data object containing the secret key and token.
     * @returns A boolean indicating whether the token is valid or not.
     */
    static verifyToken(data) {
        const { secret, token, email } = data;
        const totp = this.getTotp({ secret, email });
        const delta = totp.validate({ token, window: 3 });
        return delta !== null;
    }
    /**
     * Generates a URI for the given secret key and user email, which can be used to set up two-factor authentication.
     * @param data - The data object containing the secret key and user email.
     * @returns The generated URI for setting up two-factor authentication.
     */
    static generateUri(data) {
        const { secret, userEmail } = data;
        const totp = this.getTotp({ secret, email: userEmail });
        return totp.toString();
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", String)
], TotpAuth, "generateSecret", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", String)
], TotpAuth, "getLabel", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", OTPAuth.TOTP)
], TotpAuth, "getTotp", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Boolean)
], TotpAuth, "verifyToken", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", String)
], TotpAuth, "generateUri", null);
//# sourceMappingURL=TotpAuth.js.map