UNPKG

next-auth

Version:

Authentication for Next.js

86 lines (70 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getAuthorizationUrl; var _client = require("./client"); var _clientLegacy = require("./client-legacy"); var checks = _interopRequireWildcard(require("./checks")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } async function getAuthorizationUrl({ options, query }) { var _provider$version; const { logger, provider } = options; let params = {}; if (typeof provider.authorization === "string") { const parsedUrl = new URL(provider.authorization); const parsedParams = Object.fromEntries(parsedUrl.searchParams); params = { ...params, ...parsedParams }; } else { var _provider$authorizati; params = { ...params, ...((_provider$authorizati = provider.authorization) === null || _provider$authorizati === void 0 ? void 0 : _provider$authorizati.params) }; } params = { ...params, ...query }; if ((_provider$version = provider.version) !== null && _provider$version !== void 0 && _provider$version.startsWith("1.")) { var _provider$authorizati2; const client = (0, _clientLegacy.oAuth1Client)(options); const tokens = await client.getOAuthRequestToken(params); const url = `${(_provider$authorizati2 = provider.authorization) === null || _provider$authorizati2 === void 0 ? void 0 : _provider$authorizati2.url}?${new URLSearchParams({ oauth_token: tokens.oauth_token, oauth_token_secret: tokens.oauth_token_secret, ...tokens.params })}`; _clientLegacy.oAuth1TokenStore.set(tokens.oauth_token, tokens.oauth_token_secret); logger.debug("GET_AUTHORIZATION_URL", { url, provider }); return { redirect: url }; } const client = await (0, _client.openidClient)(options); const authorizationParams = params; const cookies = []; await checks.state.create(options, cookies, authorizationParams); await checks.pkce.create(options, cookies, authorizationParams); await checks.nonce.create(options, cookies, authorizationParams); const url = client.authorizationUrl(authorizationParams); logger.debug("GET_AUTHORIZATION_URL", { url, cookies, provider }); return { redirect: url, cookies }; }