brazy-auth
Version:
Authentication for Next.js
61 lines (60 loc) • 2.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = parseProviders;
var _merge = require("../../utils/merge");
function parseProviders(params) {
const {
url,
providerId
} = params;
const providers = params.providers.map(({
options,
...rest
}) => {
var _userOptions$id, _userOptions$id2;
const defaultOptions = normalizeProvider(rest);
const userOptions = normalizeProvider(options);
return (0, _merge.merge)(defaultOptions, {
...userOptions,
signinUrl: `${url}/signin/${(_userOptions$id = userOptions === null || userOptions === void 0 ? void 0 : userOptions.id) !== null && _userOptions$id !== void 0 ? _userOptions$id : rest.id}`,
callbackUrl: `${url}/callback/${(_userOptions$id2 = userOptions === null || userOptions === void 0 ? void 0 : userOptions.id) !== null && _userOptions$id2 !== void 0 ? _userOptions$id2 : rest.id}`
});
});
const provider = providers.find(({
id
}) => id === providerId);
return {
providers,
provider
};
}
function normalizeProvider(provider) {
var _normalized$version, _normalized$version2;
if (!provider) return;
const normalized = Object.entries(provider).reduce((acc, [key, value]) => {
if (["authorization", "token", "userinfo"].includes(key) && typeof value === "string") {
var _url$searchParams;
const url = new URL(value);
acc[key] = {
url: `${url.origin}${url.pathname}`,
params: Object.fromEntries((_url$searchParams = url.searchParams) !== null && _url$searchParams !== void 0 ? _url$searchParams : [])
};
} else {
acc[key] = value;
}
return acc;
}, {});
if (normalized.type === "openid" && !((_normalized$version = normalized.version) !== null && _normalized$version !== void 0 && _normalized$version.startsWith("1."))) {
var _normalized$idToken, _normalized$wellKnown;
normalized.idToken = Boolean((_normalized$idToken = normalized.idToken) !== null && _normalized$idToken !== void 0 ? _normalized$idToken : (_normalized$wellKnown = normalized.wellKnown) === null || _normalized$wellKnown === void 0 ? void 0 : _normalized$wellKnown.includes("openid-configuration"));
if (!normalized.checks) normalized.checks = ["state"];
}
if (normalized.type === "oauth" && !((_normalized$version2 = normalized.version) !== null && _normalized$version2 !== void 0 && _normalized$version2.startsWith("1."))) {
var _normalized$idToken2, _normalized$wellKnown2;
normalized.idToken = Boolean((_normalized$idToken2 = normalized.idToken) !== null && _normalized$idToken2 !== void 0 ? _normalized$idToken2 : (_normalized$wellKnown2 = normalized.wellKnown) === null || _normalized$wellKnown2 === void 0 ? void 0 : _normalized$wellKnown2.includes("openid-configuration"));
if (!normalized.checks) normalized.checks = ["state"];
}
return normalized;
}