"use strict";
(() => {
var exports = {};
exports.id = 5774;
exports.ids = [5774,1992];
exports.modules = {

/***/ 770640:
/***/ ((module) => {

module.exports = require("cookies");

/***/ }),

/***/ 922687:
/***/ ((module) => {

module.exports = require("password-hash");

/***/ }),

/***/ 439491:
/***/ ((module) => {

module.exports = require("assert");

/***/ }),

/***/ 714300:
/***/ ((module) => {

module.exports = require("buffer");

/***/ }),

/***/ 706113:
/***/ ((module) => {

module.exports = require("crypto");

/***/ }),

/***/ 809523:
/***/ ((module) => {

module.exports = require("dns");

/***/ }),

/***/ 582361:
/***/ ((module) => {

module.exports = require("events");

/***/ }),

/***/ 657147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 113685:
/***/ ((module) => {

module.exports = require("http");

/***/ }),

/***/ 241808:
/***/ ((module) => {

module.exports = require("net");

/***/ }),

/***/ 822037:
/***/ ((module) => {

module.exports = require("os");

/***/ }),

/***/ 371017:
/***/ ((module) => {

module.exports = require("path");

/***/ }),

/***/ 677282:
/***/ ((module) => {

module.exports = require("process");

/***/ }),

/***/ 12781:
/***/ ((module) => {

module.exports = require("stream");

/***/ }),

/***/ 371576:
/***/ ((module) => {

module.exports = require("string_decoder");

/***/ }),

/***/ 924404:
/***/ ((module) => {

module.exports = require("tls");

/***/ }),

/***/ 76224:
/***/ ((module) => {

module.exports = require("tty");

/***/ }),

/***/ 257310:
/***/ ((module) => {

module.exports = require("url");

/***/ }),

/***/ 473837:
/***/ ((module) => {

module.exports = require("util");

/***/ }),

/***/ 629914:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const pool_1 = __importDefault(__webpack_require__(541808));
// raises an exception if no such account.
async function getAccountId({ email_address, }) {
    const pool = (0, pool_1.default)("medium");
    const { rows } = await pool.query("SELECT account_id FROM accounts WHERE email_address=$1", [email_address]);
    if (rows.length == 0) {
        throw Error(`no account with email address '${email_address}'`);
    }
    const { account_id } = rows[0];
    return account_id;
}
exports["default"] = getAccountId;
//# sourceMappingURL=get.js.map

/***/ }),

/***/ 663825:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const pool_1 = __importDefault(__webpack_require__(541808));
const password_hash_1 = __importDefault(__webpack_require__(987909));
async function setPassword(account_id, password) {
    const pool = (0, pool_1.default)();
    await pool.query("UPDATE accounts SET password_hash=$1 WHERE account_id=$2", [
        (0, password_hash_1.default)(password),
        account_id,
    ]);
}
exports["default"] = setPassword;
//# sourceMappingURL=set-password.js.map

/***/ }),

/***/ 357971:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ redeemPasswordResetAPIEndPoint)
/* harmony export */ });
/* harmony import */ var _cocalc_server_auth_redeem_password_reset__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(723655);
/* harmony import */ var _cocalc_server_auth_redeem_password_reset__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_auth_redeem_password_reset__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _sign_in__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(403408);
/* harmony import */ var lib_api_get_params__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(26438);
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */ /*
Redeeming a password reset works as follows:

1. check that the password reset id is valid still; error if not
2. check that the password is valid; error if not
3. invalidate password reset id by writing that it is used to the database
4. write hash of new password to the database
5. respond success and sign user in.
*/ 


async function redeemPasswordResetAPIEndPoint(req, res) {
    const { password , passwordResetId  } = (0,lib_api_get_params__WEBPACK_IMPORTED_MODULE_2__/* ["default"] */ .Z)(req);
    let account_id;
    try {
        account_id = await _cocalc_server_auth_redeem_password_reset__WEBPACK_IMPORTED_MODULE_0___default()(password, passwordResetId);
    } catch (err) {
        res.json({
            error: `${err.message}`
        });
        return;
    }
    await (0,_sign_in__WEBPACK_IMPORTED_MODULE_1__.signUserIn)(req, res, account_id);
    return;
}


/***/ }),

/***/ 723655:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


/*
1. check that the password reset id is valid still; throw error if not
2. check that the password is valid; throw error if not
3. invalidate password reset id by writing that it is used to the database
4. write hash of new password to the database
5. Return account_id of user who just reset their password.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const pool_1 = __importDefault(__webpack_require__(541808));
const get_1 = __importDefault(__webpack_require__(629914));
const set_password_1 = __importDefault(__webpack_require__(663825));
async function redeemPasswordReset(password, passwordResetId) {
    if (password.length < 6) {
        // won't happen in practice because frontend UI prevents this...
        throw Error("password is too short");
    }
    const pool = (0, pool_1.default)();
    const { rows } = await pool.query("SELECT email_address FROM password_reset WHERE expire > NOW() AND id=$1::UUID", [passwordResetId]);
    if (rows.length == 0) {
        throw Error("Password reset no longer valid.");
    }
    const { email_address } = rows[0];
    await pool.query("UPDATE password_reset SET expire=NOW() WHERE id=$1::UUID", [
        passwordResetId,
    ]);
    const account_id = await (0, get_1.default)({ email_address });
    await (0, set_password_1.default)(account_id, password);
    return account_id;
}
exports["default"] = redeemPasswordReset;
//# sourceMappingURL=redeem-password-reset.js.map

/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [8180,2921,9557,5245,6942,1808,4006,8312,5710,8351,4988,3408], () => (__webpack_exec__(357971)));
module.exports = __webpack_exports__;

})();