"use strict";
exports.id = 0;
exports.ids = [0];
exports.modules = {

/***/ 1000:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "$K": () => (/* binding */ authHelper),
/* harmony export */   "Oj": () => (/* binding */ getBearerToken)
/* harmony export */ });
/* unused harmony export AuthHelper */
/* harmony import */ var _tokenStorage__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7084);
/* harmony import */ var _errorCode__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5373);
/* harmony import */ var next_config__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4558);
/* harmony import */ var next_config__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(next_config__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var uuid__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(6555);
/* harmony import */ var jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(9344);
/* harmony import */ var jsonwebtoken__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var _vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(4216);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([uuid__WEBPACK_IMPORTED_MODULE_3__]);
uuid__WEBPACK_IMPORTED_MODULE_3__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];






class AuthHelper {
    constructor(tokenStorage){
        this.tokenStorage = tokenStorage;
        const { serverRuntimeConfig  } = next_config__WEBPACK_IMPORTED_MODULE_2___default()();
        this.tokenSecret = serverRuntimeConfig.tokenSecret;
        this.refreshTokenSecret = serverRuntimeConfig.refreshTokenSecret;
    }
    async checkAuth() {
        // check auth type
        return await _vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_5__/* ["default"].getAuthType */ .Z.getAuthType();
    }
    async login(combination) {
        const { username , password  } = combination;
        // get api token & profile from vulcan api server
        const authType = await this.checkAuth();
        const token = await _vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_5__/* ["default"].getInitToken */ .Z.getInitToken({
            type: authType,
            username,
            password
        });
        const apiToken = `${authType} ${token}`;
        const profile = await _vulcanSQLAdapter__WEBPACK_IMPORTED_MODULE_5__/* ["default"].getUserProfile */ .Z.getUserProfile({
            apiToken
        });
        // create a session in storage
        const session = (0,uuid__WEBPACK_IMPORTED_MODULE_3__.v1)();
        const accessToken = jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__.sign({
            session,
            profile
        }, this.tokenSecret);
        const refreshToken = jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__.sign({
            session,
            profile
        }, this.refreshTokenSecret);
        await this.tokenStorage.setToken(session, apiToken);
        return {
            accessToken,
            refreshToken,
            profile
        };
    }
    async auth(accessToken) {
        const authType = await this.checkAuth();
        // if user not setup auth, return a guest user
        if (authType === null) {
            return {
                profile: null,
                session: null,
                apiToken: null
            };
        }
        try {
            if (!accessToken) {
                throw _errorCode__WEBPACK_IMPORTED_MODULE_1__/* .errorCode.UNAUTHORIZED_REQUEST */ .u.UNAUTHORIZED_REQUEST;
            }
            const { session , profile  } = jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__.verify(accessToken, this.tokenSecret);
            const apiToken = await this.tokenStorage.getToken(session);
            if (!apiToken) {
                throw _errorCode__WEBPACK_IMPORTED_MODULE_1__/* .errorCode.UNAUTHORIZED_REQUEST */ .u.UNAUTHORIZED_REQUEST;
            }
            return {
                apiToken,
                session,
                profile
            };
        } catch (error) {
            console.log(error);
            throw _errorCode__WEBPACK_IMPORTED_MODULE_1__/* .errorCode.UNAUTHORIZED_REQUEST */ .u.UNAUTHORIZED_REQUEST;
        }
    }
    async refreshToken(refreshToken) {
        const { session , profile  } = jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__.verify(refreshToken, this.refreshTokenSecret);
        const newAccessToken = jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__.sign({
            session,
            profile
        }, this.tokenSecret);
        const newRefreshToken = jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__.sign({
            session,
            profile
        }, this.refreshTokenSecret);
        return {
            accessToken: newAccessToken,
            refreshToken: newRefreshToken,
            profile
        };
    }
    async logout(accessToken) {
        try {
            const { session  } = jsonwebtoken__WEBPACK_IMPORTED_MODULE_4__.verify(accessToken, this.tokenSecret);
            await this.tokenStorage.delToken(session);
        } catch (error) {
            console.log(error);
            throw _errorCode__WEBPACK_IMPORTED_MODULE_1__/* .errorCode.UNAUTHORIZED_REQUEST */ .u.UNAUTHORIZED_REQUEST;
        }
    }
}
const getBearerToken = (headers)=>{
    return headers["authorization"].replace("Bearer ", "");
};
const authHelper = new AuthHelper(new _tokenStorage__WEBPACK_IMPORTED_MODULE_0__/* .FileTokenStorage */ .z());

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

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

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "z": () => (/* binding */ FileTokenStorage)
/* harmony export */ });
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7147);
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(fs__WEBPACK_IMPORTED_MODULE_0__);

class FileTokenStorage {
    tokens = {};
    filePath = "./.tokens.json";
    getToken = async (key)=>{
        this.tokens = JSON.parse(fs__WEBPACK_IMPORTED_MODULE_0__.readFileSync(this.filePath, "utf-8"));
        return this.tokens[key];
    };
    setToken = async (key, token)=>{
        this.tokens[key] = token;
        this.writeFile();
    };
    delToken = async (key)=>{
        delete this.tokens[key];
        this.writeFile();
    };
    writeFile = async ()=>{
        fs__WEBPACK_IMPORTED_MODULE_0__.writeFileSync(this.filePath, JSON.stringify(this.tokens));
    };
}


/***/ })

};
;