"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.signUserIn = exports.getAccount = void 0;
/*
Sign in works as follows:

1. Query the database for the account_id and password_hash
   with the given username.

2. Use the password-hash library to determine whether or
   not the given password hashes properly.  If so, create and
   set a secure remember_me http cookie confirming that the
   client is that user and tell user they are now authenticated.
   If not, send an error back.
*/
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const remember_me_1 = require("@cocalc/server/auth/remember-me");
const throttle_1 = require("@cocalc/server/auth/throttle");
const cookies_1 = __importDefault(require("cookies"));
const get_params_1 = __importDefault(require("lib/api/get-params"));
const password_hash_1 = require("password-hash");
// import reCaptcha from "@cocalc/server/auth/recaptcha";
async function signIn(req, res) {
    let { email, password } = (0, get_params_1.default)(req);
    email = email.toLowerCase().trim();
    const check = await (0, throttle_1.signInCheck)(email, req.ip);
    if (check) {
        res.json({ error: check });
        return;
    }
    let account_id;
    try {
        // Don't bother checking reCaptcha for *sign in* for now, since it causes trouble
        // when large classes all sign in from one point.  Also, it's much less important
        // for sign in, than for sign up and payment.
        // await reCaptcha(req);
        account_id = await getAccount(email, password);
    }
    catch (err) {
        res.json({ error: `Problem signing into account -- ${err.message}.` });
        (0, throttle_1.recordFail)(email, req.ip);
        return;
    }
    await signUserIn(req, res, account_id);
}
exports.default = signIn;
async function getAccount(email_address, password) {
    const pool = (0, pool_1.default)();
    const { rows } = await pool.query("SELECT account_id, password_hash FROM accounts WHERE email_address=$1", [email_address]);
    if (rows.length == 0) {
        throw Error(`no account with email address '${email_address}'`);
    }
    const { account_id, password_hash } = rows[0];
    if (!(0, password_hash_1.verify)(password, password_hash)) {
        throw Error(`password for '${email_address}' is incorrect`);
    }
    return account_id;
}
exports.getAccount = getAccount;
async function signUserIn(req, res, account_id) {
    let value, ttl_s;
    try {
        ({ value, ttl_s } = await (0, remember_me_1.createRememberMeCookie)(account_id));
    }
    catch (err) {
        res.json({ error: `Problem creating session cookie -- ${err.message}.` });
        return;
    }
    try {
        const cookies = new cookies_1.default(req, res, { maxAge: ttl_s * 1000 });
        cookies.set(remember_me_1.COOKIE_NAME, value);
    }
    catch (err) {
        res.json({ error: `Problem setting cookie -- ${err.message}.` });
        return;
    }
    res.json({ account_id });
}
exports.signUserIn = signUserIn;
//# sourceMappingURL=sign-in.js.map