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

/***/ 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");

/***/ }),

/***/ 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");

/***/ }),

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

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "Z": () => (/* binding */ isPost)
/* harmony export */ });
function isPost(req, res) {
    if ((req === null || req === void 0 ? void 0 : req.method) !== "POST") {
        res === null || res === void 0 ? void 0 : res.status(404).json({
            message: "must use a POST request"
        });
        return false;
    }
    return true;
};


/***/ }),

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

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ redeemVerifyEmailAPICall)
/* harmony export */ });
/* harmony import */ var _cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(35012);
/* harmony import */ var _cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var lib_api_is_post__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(447618);
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */ 

async function redeemVerifyEmailAPICall(req, res) {
    if (!(0,lib_api_is_post__WEBPACK_IMPORTED_MODULE_1__/* ["default"] */ .Z)(req, res)) return;
    const { email_address , token  } = req.body;
    try {
        await _cocalc_server_auth_redeem_verify_email__WEBPACK_IMPORTED_MODULE_0___default()(email_address, token);
    } catch (err) {
        res.json({
            error: `${err.message}`
        });
        return;
    }
    res.json({});
    return;
};


/***/ }),

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


/*
"Redeem" an email verification token.  This checks everything is valid, then sets that the email
has been verified.
*/
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isEmailVerified = void 0;
var pool_1 = __importDefault(__webpack_require__(541808));
var misc_1 = __webpack_require__(854006);
function redeemVerifyEmail(email_address, token) {
    return __awaiter(this, void 0, void 0, function () {
        var pool, rows, _a, account_id, email_address_challenge, email_address_verified;
        var _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    if (token.length < 16) {
                        throw Error("token is too short");
                    }
                    if (!(0, misc_1.is_valid_email_address)(email_address)) {
                        throw Error("email_address is not valid");
                    }
                    pool = (0, pool_1.default)();
                    return [4 /*yield*/, pool.query("SELECT account_id, email_address_challenge, email_address_verified FROM accounts WHERE email_address=$1", [email_address])];
                case 1:
                    rows = (_c.sent()).rows;
                    if (rows.length == 0) {
                        throw Error("no account with email address ".concat(email_address));
                    }
                    _a = rows[0], account_id = _a.account_id, email_address_challenge = _a.email_address_challenge, email_address_verified = _a.email_address_verified;
                    if (!(email_address_challenge === null || email_address_challenge === void 0 ? void 0 : email_address_challenge.email)) {
                        if (email_address_verified === null || email_address_verified === void 0 ? void 0 : email_address_verified[email_address]) {
                            // nothing to do.
                            return [2 /*return*/];
                        }
                        throw Error("no email verification configured for ".concat(email_address));
                    }
                    if (email_address_challenge.token != token) {
                        throw Error("tokens do not match");
                    }
                    // we're good, save this in the email_address_verified JSONB record and also delete the challenge
                    return [4 /*yield*/, pool.query("UPDATE accounts SET email_address_challenge=NULL, email_address_verified=$1::JSONB WHERE account_id=$2", [__assign(__assign({}, email_address_verified), (_b = {}, _b[email_address] = new Date(), _b)), account_id])];
                case 2:
                    // we're good, save this in the email_address_verified JSONB record and also delete the challenge
                    _c.sent();
                    return [2 /*return*/];
            }
        });
    });
}
exports["default"] = redeemVerifyEmail;
function isEmailVerified(email_address) {
    return __awaiter(this, void 0, void 0, function () {
        var pool, rows, email_address_verified;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    pool = (0, pool_1.default)();
                    return [4 /*yield*/, pool.query("SELECT email_address_verified FROM accounts WHERE email_address=$1::TEXT", [email_address])];
                case 1:
                    rows = (_a.sent()).rows;
                    if (rows.length == 0)
                        return [2 /*return*/, false];
                    email_address_verified = rows[0].email_address_verified;
                    return [2 /*return*/, !!email_address_verified[email_address]];
            }
        });
    });
}
exports.isEmailVerified = isEmailVerified;
//# sourceMappingURL=redeem-verify-email.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,1808,9738], () => (__webpack_exec__(39973)));
module.exports = __webpack_exports__;

})();