UNPKG

supertokens-node

Version:
117 lines (116 loc) 5.01 kB
"use strict"; /* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved. * * This software is licensed under the Apache License, Version 2.0 (the * "License") as published by the Apache Software Foundation. * * You may not use this file except in compliance with the License. You may * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.validateAndNormaliseUserInput = validateAndNormaliseUserInput; exports.sendUnauthorisedAccess = sendUnauthorisedAccess; exports.isValidRecipeId = isValidRecipeId; exports.getUserForRecipeId = getUserForRecipeId; exports.validateApiKey = validateApiKey; exports.getApiPathWithDashboardBase = getApiPathWithDashboardBase; const utils_1 = require("../../utils"); const constants_1 = require("./constants"); const logger_1 = require("../../logger"); function validateAndNormaliseUserInput(config) { let override = Object.assign({ functions: (originalImplementation) => originalImplementation, apis: (originalImplementation) => originalImplementation }, (config === undefined ? {} : config.override)); if ((config === null || config === void 0 ? void 0 : config.apiKey) !== undefined && (config === null || config === void 0 ? void 0 : config.admins) !== undefined) { (0, logger_1.logDebugMessage)("User Dashboard: Providing 'admins' has no effect when using an apiKey."); } let admins; if ((config === null || config === void 0 ? void 0 : config.admins) !== undefined) { admins = config.admins.map((email) => (0, utils_1.normaliseEmail)(email)); } return Object.assign(Object.assign({}, config), { override, authMode: config !== undefined && config.apiKey ? "api-key" : "email-password", admins }); } function sendUnauthorisedAccess(res) { (0, utils_1.sendNon200ResponseWithMessage)(res, "Unauthorised access", 401); } function isValidRecipeId(recipeId) { return (recipeId === "emailpassword" || recipeId === "thirdparty" || recipeId === "passwordless" || recipeId === "webauthn"); } async function getUserForRecipeId(stInstance, recipeUserId, recipeId, userContext) { let userResponse = await _getUserForRecipeId(stInstance, recipeUserId, recipeId, userContext); let user = undefined; if (userResponse.user !== undefined) { user = Object.assign(Object.assign({}, userResponse.user), { firstName: "", lastName: "" }); } return { user, recipe: userResponse.recipe, }; } async function _getUserForRecipeId(stInstance, recipeUserId, recipeId, userContext) { let recipe; const user = await stInstance.getRecipeInstanceOrThrow("accountlinking").recipeInterfaceImpl.getUser({ userId: recipeUserId.getAsString(), userContext, }); if (user === undefined) { return { user: undefined, recipe: undefined, }; } const loginMethod = user.loginMethods.find((m) => m.recipeId === recipeId && m.recipeUserId.getAsString() === recipeUserId.getAsString()); if (loginMethod === undefined) { return { user: undefined, recipe: undefined, }; } if (recipeId === "emailpassword") { let emailpasswordRecipe = stInstance.getRecipeInstance("emailpassword"); if (emailpasswordRecipe !== undefined) { recipe = "emailpassword"; } } else if (recipeId === "thirdparty") { let thirdpartyRecipe = stInstance.getRecipeInstance("thirdparty"); if (thirdpartyRecipe !== undefined) { recipe = "thirdparty"; } } else if (recipeId === "passwordless") { let passwordlessRecipe = stInstance.getRecipeInstance("passwordless"); if (passwordlessRecipe !== undefined) { recipe = "passwordless"; } } else if (recipeId === "webauthn") { let webauthnRecipe = stInstance.getRecipeInstance("webauthn"); if (webauthnRecipe !== undefined) { recipe = "webauthn"; } } return { user, recipe, }; } async function validateApiKey(input) { let apiKeyHeaderValue = input.req.getHeaderValue("authorization"); // We receieve the api key as `Bearer API_KEY`, this retrieves just the key apiKeyHeaderValue = apiKeyHeaderValue === null || apiKeyHeaderValue === void 0 ? void 0 : apiKeyHeaderValue.split(" ")[1]; if (apiKeyHeaderValue === undefined) { return false; } return apiKeyHeaderValue === input.config.apiKey; } function getApiPathWithDashboardBase(path) { return constants_1.DASHBOARD_API + path; }