"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Auth = void 0;
const tslib_1 = require("tslib");
const bcrypt_1 = tslib_1.__importDefault(require("bcrypt"));
const serialization_1 = require("../helpers/data/serialization");
const crypto_1 = require("../helpers/security/crypto");
/*@ts-ignore*/
var { __ΩSerializable } = require('../types');
const __ΩResetToken = ['T', 'token', 'data', 'generationTime', 'verifier', 'ResetToken', '"c!P&4"e"!4#\'4$&4%Mw&y'];
class Auth {
    static hashPassword(password, rounds = 10) {
        return bcrypt_1.default.hash(password, rounds);
    }
    static verifyHash(password, hash) {
        return bcrypt_1.default.compare(password, hash);
    }
    static async generateResetToken(data) {
        const generationTime = Date.now();
        const serializedData = (0, serialization_1.toJson)(data);
        const randomBuf = await (0, crypto_1.randomBytes)(16);
        const bufSize = 4 + 16 + serializedData.length;
        const tokenBuf = Buffer.alloc(bufSize);
        tokenBuf.writeUInt32BE(Math.floor(generationTime / 1000), 0);
        randomBuf.copy(tokenBuf, 4);
        tokenBuf.write(serializedData, 20);
        return {
            data,
            generationTime,
            verifier: randomBuf.toString('base64'),
            token: tokenBuf.toString('base64')
        };
    }
    static async decodeResetToken(token) {
        const tokenBuf = Buffer.from('token', 'base64');
        const generationTime = tokenBuf.readUInt32BE(0) * 1000;
        const randomBuf = tokenBuf.slice(4, 20);
        const serializedData = tokenBuf.slice(20).toString('utf8');
        const data = (serialization_1.fromJson.Ω = [['!']], (0, serialization_1.fromJson)(serializedData));
        return {
            token,
            data,
            generationTime,
            verifier: randomBuf.toString('base64')
        };
    }
}
exports.Auth = Auth;
Auth.__type = ['password', 'rounds', 'hashPassword', 'hash', 'verifyHash', () => __ΩSerializable, 'data', () => __ΩResetToken, 'generateResetToken', 'token', () => __ΩResetToken, 'decodeResetToken', 'Auth', 'P&2!"2"&`0#sP&2!&2$)`0%sPn&2\'n(`0)sP&2*"o+"`0,s5w-'];
//# sourceMappingURL=provider.js.map