(() => {
var exports = {};
exports.id = 920;
exports.ids = [920];
exports.modules = {

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

"use strict";
module.exports = require("assert");

/***/ }),

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

"use strict";
module.exports = require("buffer");

/***/ }),

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

"use strict";
module.exports = require("crypto");

/***/ }),

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

"use strict";
module.exports = require("dns");

/***/ }),

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

"use strict";
module.exports = require("events");

/***/ }),

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

"use strict";
module.exports = require("fs");

/***/ }),

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

"use strict";
module.exports = require("http");

/***/ }),

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

"use strict";
module.exports = require("net");

/***/ }),

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

"use strict";
module.exports = require("os");

/***/ }),

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

"use strict";
module.exports = require("path");

/***/ }),

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

"use strict";
module.exports = require("process");

/***/ }),

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

"use strict";
module.exports = require("stream");

/***/ }),

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

"use strict";
module.exports = require("string_decoder");

/***/ }),

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

"use strict";
module.exports = require("tls");

/***/ }),

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

"use strict";
module.exports = require("tty");

/***/ }),

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

"use strict";
module.exports = require("url");

/***/ }),

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

"use strict";
module.exports = require("util");

/***/ }),

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

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handle)
/* harmony export */ });
/* harmony import */ var _cocalc_server_accounts_set_email_address__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(644705);
/* harmony import */ var _cocalc_server_accounts_set_email_address__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_accounts_set_email_address__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var lib_account_get_account__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(559459);
/* harmony import */ var lib_api_get_params__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(26438);
/*
Set email address for an existing account.

You must include both the email address and password.  If there is no password
currently set for the account, you have to set one as part of this request.
*/ 


async function handle(req, res) {
    const account_id = await (0,lib_account_get_account__WEBPACK_IMPORTED_MODULE_1__/* ["default"] */ .Z)(req);
    if (account_id == null) {
        res.json({
            error: "must be signed in"
        });
        return;
    }
    const { email_address , password  } = (0,lib_api_get_params__WEBPACK_IMPORTED_MODULE_2__/* ["default"] */ .Z)(req);
    try {
        await _cocalc_server_accounts_set_email_address__WEBPACK_IMPORTED_MODULE_0___default()(account_id, email_address, password);
        res.json({});
    } catch (err) {
        if (err.message.includes("duplicate key")) {
            err = Error(`The email address "${email_address}" is already in use by another account.`);
        }
        res.json({
            error: err.message
        });
    }
}


/***/ }),

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

"use strict";

/*
Set email address of an account.

The password must also be provided. If the email address is already set in the
database, then password has to be the current correct password. If the email
address is NOT set, then a new email address and password are set.

Also, if user changes email address to one that has some pending actions, carry
those actions out.  (TODO: should probably only do these after email address
is verified.)

Throws an exception if something is wrong.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const password_hash_1 = __importStar(__webpack_require__(987909));
const pool_1 = __importDefault(__webpack_require__(541808));
const check_required_sso_1 = __webpack_require__(234894);
const get_strategies_1 = __importDefault(__webpack_require__(728351));
const misc_1 = __webpack_require__(854006);
const account_creation_actions_1 = __importStar(__webpack_require__(786791));
async function setEmailAddress(account_id, email_address, password) {
    if (!(0, misc_1.isValidUUID)(account_id)) {
        throw Error("account_id is not valid");
    }
    if (!(0, misc_1.is_valid_email_address)(email_address)) {
        throw Error("email address is not valid");
    }
    email_address = email_address.toLowerCase();
    if (!password || password.length < 6) {
        throw Error("password must be at least 6 characters");
    }
    const pool = (0, pool_1.default)();
    const { rows } = await pool.query("SELECT email_address, password_hash FROM accounts WHERE account_id=$1", [account_id]);
    if (rows.length == 0) {
        throw Error("no such account");
    }
    const { password_hash, email_address: old_email_address } = rows[0];
    // if you have an email address that's controlled by an "exclusive" SSO strategy
    // you're not allowed to change your email address
    const strategies = await (0, get_strategies_1.default)();
    const strategy = (0, check_required_sso_1.checkRequiredSSO)({ strategies, email: old_email_address });
    if (strategy != null) {
        // user has no password set, so we can set it – but not the email address
        if (!password_hash) {
            await pool.query("UPDATE accounts SET password_hash=$1,  WHERE account_id=$2", [(0, password_hash_1.default)(password), account_id]);
        }
        throw new Error(`You are not allowed to change your email address`);
    }
    // you're also not allowed to change your email address to one that's covered by an exclusive strategy
    if ((0, check_required_sso_1.checkRequiredSSO)({ strategies, email: email_address }) != null) {
        throw new Error(`You are not allowed to change your email address to this one`);
    }
    if (!password_hash) {
        // setting both the email_address *and* password at once.
        await pool.query("UPDATE accounts SET password_hash=$1, email_address=$2 WHERE account_id=$3", [(0, password_hash_1.default)(password), email_address, account_id]);
        return;
    }
    // Verify that existing password is correct.
    if (!(0, password_hash_1.verifyPassword)(password, password_hash)) {
        throw Error("password is incorrect");
    }
    // Is the email address available?
    if ((await pool.query("SELECT COUNT(*)::INT FROM accounts WHERE email_address=$1", [email_address])).rows[0].count > 0) {
        throw Error(`email address "${email_address}" is already in use by another account`);
    }
    // Set the email address:
    await pool.query("UPDATE accounts SET email_address=$1 WHERE account_id=$2", [
        email_address,
        account_id,
    ]);
    // Do any pending account creation actions for this email.
    await (0, account_creation_actions_1.default)(email_address, account_id);
    await (0, account_creation_actions_1.creationActionsDone)(account_id);
}
exports["default"] = setEmailAddress;
//# sourceMappingURL=set-email-address.js.map

/***/ }),

/***/ 630900:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

var crypto = __webpack_require__(706113);

var rn = function(max) {
  var rnBytes = crypto.randomBytes(2);
  var randomNum = rnBytes.readUInt8(0) * 256 + rnBytes.readUInt8(1);
  return randomNum % max;
}

var digits = '0123456789'
var base62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'
var base30 = '0123456789ABCDFHKLMNPQRSTUVWXYZ'  // 1-9, A-Z exclude(E, G, I, J, O) for human read

exports.generate = function(len, chars) {
  len = len || 16;
  chars = chars || base62;
  var key = '';
  var charsLen = chars.length;
  for (var i=0; i<len; i++) {
    key += chars[rn(charsLen)];
  }
  return key;
};

exports.generateDigits = function(len) {
  len = len || 16;
  return exports.generate(len, digits)
};

exports.generateBase30 = function(len) {
  len = len || 16;
  return exports.generate(len, base30)
}


/***/ })

};
;

// 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,1808,4006,8312,5710,9459,8351,4988,6791], () => (__webpack_exec__(843545)));
module.exports = __webpack_exports__;

})();