"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var basic_auth_1 = __importDefault(require("basic-auth"));
var password_hash_1 = require("password-hash");
var lodash_1 = require("lodash");
function isAuthenticated(_a) {
    var e_1, _b;
    var req = _a.req, res = _a.res, path = _a.path, auth = _a.auth;
    if (auth == null) {
        return true; // no authentication needed
    }
    // strip any /'s from beginning of path  (auth path's are assumed relative)
    while (path[0] === "/") {
        path = path.slice(1);
    }
    var authInfo = undefined;
    for (var p in auth) {
        if (path.startsWith(p)) {
            authInfo = auth[p];
            break;
        }
    }
    if (authInfo == null) {
        // don't need auth for this path
        return true;
    }
    if (!(0, lodash_1.isArray)(authInfo)) {
        // do a double check...
        res.statusCode = 401;
        res.end("auth is misconfigured  -- invalid auth field in the public_paths database.");
        return false;
    }
    var credentials = (0, basic_auth_1.default)(req);
    var fail = true;
    if ((credentials === null || credentials === void 0 ? void 0 : credentials.name) && (credentials === null || credentials === void 0 ? void 0 : credentials.pass)) {
        try {
            for (var authInfo_1 = __values(authInfo), authInfo_1_1 = authInfo_1.next(); !authInfo_1_1.done; authInfo_1_1 = authInfo_1.next()) {
                var _c = authInfo_1_1.value, name_1 = _c.name, pass = _c.pass;
                if (name_1 == credentials.name) {
                    if ((0, password_hash_1.verify)(credentials.pass, pass)) {
                        fail = false;
                    }
                    break;
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (authInfo_1_1 && !authInfo_1_1.done && (_b = authInfo_1.return)) _b.call(authInfo_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    if (fail) {
        res.statusCode = 401;
        res.setHeader("WWW-Authenticate", 'Basic realm="cocalc.com"');
        res.end("Access denied");
        return false;
    }
    // access granted
    return true;
}
exports.default = isAuthenticated;
//# sourceMappingURL=authenticate.js.map