Skip to content

Commit

Permalink
fix: boxy stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Jul 23, 2024
1 parent bc37fed commit 82dd47e
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export declare type Response =
}
| {
status: "UNKNOWN_TENANT_ERROR";
}
| {
status: "BOXY_ERROR";
message: string;
};
export default function createOrUpdateThirdPartyConfig(
_: APIInterface,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ var __importDefault =
Object.defineProperty(exports, "__esModule", { value: true });
const multitenancy_1 = __importDefault(require("../../../multitenancy"));
const recipe_1 = __importDefault(require("../../../multitenancy/recipe"));
const normalisedURLDomain_1 = __importDefault(require("../../../../normalisedURLDomain"));
const normalisedURLPath_1 = __importDefault(require("../../../../normalisedURLPath"));
const utils_1 = require("../../../thirdparty/providers/utils");
async function createOrUpdateThirdPartyConfig(_, tenantId, options, userContext) {
var _a;
const requestBody = await options.req.getJSONBody();
Expand Down Expand Up @@ -38,6 +41,60 @@ async function createOrUpdateThirdPartyConfig(_, tenantId, options, userContext)
await new Promise((r) => setTimeout(r, 500)); // 500ms
}
}
if (providerConfig.thirdPartyId.startsWith("boxy-saml")) {
// boxy stuff here
const boxyURL = providerConfig.clients[0].additionalConfig.boxyURL;
const boxyAPIKey = providerConfig.clients[0].additionalConfig.boxyAPIKey;
providerConfig.clients[0].additionalConfig.boxyAPIKey = undefined;
const requestBody = {
name: "",
label: "",
description: "",
tenant: `${tenantId}-providerConfig.thirdPartyId`,
product: "supertokens",
defaultRedirectUrl: providerConfig.clients[0].additionalConfig.redirectURLs[0],
forceAuthn: false,
encodedRawMetadata: "",
redirectUrl: JSON.stringify(providerConfig.clients[0].additionalConfig.redirectURLs),
metadataUrl: providerConfig.clients[0].additionalConfig.samlURL || "",
};
const normalisedDomain = new normalisedURLDomain_1.default(boxyURL);
const normalisedBasePath = new normalisedURLPath_1.default(boxyURL);
const connectionsPath = new normalisedURLPath_1.default("/api/v1/saml/config");
const resp = await utils_1.doPostRequest(
normalisedDomain.getAsStringDangerous() +
normalisedBasePath.getAsStringDangerous() +
connectionsPath.getAsStringDangerous(),
requestBody,
{
Authorization: `Api-Key ${boxyAPIKey}`,
}
);
if (resp.status !== 200) {
if (resp.status === 401) {
return {
status: "BOXY_ERROR",
message: "Invalid API Key",
};
}
return {
status: "BOXY_ERROR",
message: resp.stringResponse,
};
}
if (resp.jsonResponse === undefined) {
throw new Error("should never happen");
}
providerConfig.clients[0].clientId = resp.jsonResponse.clientID;
providerConfig.clients[0].clientSecret = resp.jsonResponse.clientSecret;
const thirdPartyRes = await multitenancy_1.default.createOrUpdateThirdPartyConfig(
tenantId,
providerConfig,
undefined,
userContext
);
return thirdPartyRes;
}
const thirdPartyRes = await multitenancy_1.default.createOrUpdateThirdPartyConfig(
tenantId,
providerConfig,
Expand Down
67 changes: 37 additions & 30 deletions lib/build/recipe/dashboard/api/multitenancy/getThirdPartyConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const multitenancy_1 = __importDefault(require("../../../multitenancy"));
const recipe_1 = __importDefault(require("../../../multitenancy/recipe"));
const configUtils_1 = require("../../../thirdparty/providers/configUtils");
async function getThirdPartyConfig(_, tenantId, options, userContext) {
var _a, _b, _c, _d, _e;
var _a, _b, _c, _d;
let tenantRes = await multitenancy_1.default.getTenant(tenantId, userContext);
if (tenantRes === undefined) {
return {
Expand All @@ -36,11 +36,9 @@ async function getThirdPartyConfig(_, tenantId, options, userContext) {
? void 0
: _a.providers;
const mtRecipe = recipe_1.default.getInstance();
let staticProviders =
(_b = mtRecipe === null || mtRecipe === void 0 ? void 0 : mtRecipe.staticThirdPartyProviders) !== null &&
_b !== void 0
? _b
: [];
let staticProviders = (mtRecipe === null || mtRecipe === void 0 ? void 0 : mtRecipe.staticThirdPartyProviders)
? mtRecipe.staticThirdPartyProviders.map((provider) => Object.assign({}, provider))
: [];
let additionalConfig = undefined;
// filter out providers that is not matching thirdPartyId
providersFromCore = providersFromCore.filter((provider) => provider.thirdPartyId === thirdPartyId);
Expand All @@ -65,8 +63,12 @@ async function getThirdPartyConfig(_, tenantId, options, userContext) {
}
} else if (thirdPartyId === "boxy-saml") {
let boxyURL = options.req.getKeyValueFromQuery("boxyUrl");
let boxyAPIKey = options.req.getKeyValueFromQuery("boxyAPIKey");
if (boxyURL !== undefined) {
additionalConfig = { boxyURL };
if (boxyAPIKey !== undefined) {
additionalConfig = Object.assign(Object.assign({}, additionalConfig), { boxyAPIKey });
}
}
} else if (thirdPartyId === "google-workspaces") {
const hd = options.req.getKeyValueFromQuery("hd");
Expand All @@ -79,12 +81,14 @@ async function getThirdPartyConfig(_, tenantId, options, userContext) {
providersFromCore[0].authorizationEndpoint = undefined;
providersFromCore[0].tokenEndpoint = undefined;
providersFromCore[0].userInfoEndpoint = undefined;
for (let j = 0; j < ((_c = providersFromCore[0].clients) !== null && _c !== void 0 ? _c : []).length; j++) {
providersFromCore[0].clients[j].additionalConfig = Object.assign(
Object.assign({}, providersFromCore[0].clients[j].additionalConfig),
additionalConfig
);
}
providersFromCore[0].clients = ((_b = providersFromCore[0].clients) !== null && _b !== void 0
? _b
: []
).map((client) =>
Object.assign(Object.assign({}, client), {
additionalConfig: Object.assign(Object.assign({}, client.additionalConfig), additionalConfig),
})
);
}
}
// filter out other providers from static
Expand All @@ -111,20 +115,23 @@ async function getThirdPartyConfig(_, tenantId, options, userContext) {
// modify additional config if query param is passed
if (additionalConfig !== undefined) {
// we set these to undefined so that these can be computed using the query param that was provided
staticProviders[0].config.oidcDiscoveryEndpoint = undefined;
staticProviders[0].config.authorizationEndpoint = undefined;
staticProviders[0].config.tokenEndpoint = undefined;
staticProviders[0].config.userInfoEndpoint = undefined;
for (
let j = 0;
j < ((_d = staticProviders[0].config.clients) !== null && _d !== void 0 ? _d : []).length;
j++
) {
staticProviders[0].config.clients[j].additionalConfig = Object.assign(
Object.assign({}, staticProviders[0].config.clients[j].additionalConfig),
additionalConfig
);
}
staticProviders[0] = Object.assign(Object.assign({}, staticProviders[0]), {
config: Object.assign(Object.assign({}, staticProviders[0].config), {
oidcDiscoveryEndpoint: undefined,
authorizationEndpoint: undefined,
tokenEndpoint: undefined,
userInfoEndpoint: undefined,
clients: ((_c = staticProviders[0].config.clients) !== null && _c !== void 0 ? _c : []).map(
(client) =>
Object.assign(Object.assign({}, client), {
additionalConfig: Object.assign(
Object.assign({}, client.additionalConfig),
additionalConfig
),
})
),
}),
});
}
}
let mergedProvidersFromCoreAndStatic = configUtils_1.mergeProvidersFromCoreAndStatic(
Expand Down Expand Up @@ -156,17 +163,17 @@ async function getThirdPartyConfig(_, tenantId, options, userContext) {
for (const provider of mergedProvidersFromCoreAndStatic) {
if (provider.config.thirdPartyId === thirdPartyId) {
let foundCorrectConfig = false;
for (const client of (_e = provider.config.clients) !== null && _e !== void 0 ? _e : []) {
for (const client of (_d = provider.config.clients) !== null && _d !== void 0 ? _d : []) {
try {
const providerInstance = await configUtils_1.findAndCreateProviderInstance(
mergedProvidersFromCoreAndStatic,
thirdPartyId,
client.clientType,
userContext
);
const _f = providerInstance.config,
{ clientId, clientSecret, clientType, scope, additionalConfig, forcePKCE } = _f,
commonConfig = __rest(_f, [
const _e = providerInstance.config,
{ clientId, clientSecret, clientType, scope, additionalConfig, forcePKCE } = _e,
commonConfig = __rest(_e, [
"clientId",
"clientSecret",
"clientType",
Expand Down
8 changes: 8 additions & 0 deletions lib/build/recipe/thirdparty/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,17 @@ export declare type ProviderInput = {
};
export declare type TypeInputSignInAndUp = {
providers?: ProviderInput[];
boxy?: {
url: string;
apiKey: string;
};
};
export declare type TypeNormalisedInputSignInAndUp = {
providers: ProviderInput[];
boxy?: {
url: string;
apiKey: string;
};
};
export declare type TypeInput = {
signInAndUpFeature?: TypeInputSignInAndUp;
Expand Down
1 change: 1 addition & 0 deletions lib/build/recipe/thirdparty/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function validateAndNormaliseSignInAndUpConfig(_, config) {
}
return {
providers: config.providers,
boxy: config.boxy,
};
}
function isFakeEmail(email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import { APIInterface, APIOptions } from "../../types";
import Multitenancy from "../../../multitenancy";
import MultitenancyRecipe from "../../../multitenancy/recipe";
import { UserContext } from "../../../../types";
import NormalisedURLDomain from "../../../../normalisedURLDomain";
import NormalisedURLPath from "../../../../normalisedURLPath";
import { doPostRequest } from "../../../thirdparty/providers/utils";

export type Response =
| {
status: "OK";
createdNew: boolean;
}
| { status: "UNKNOWN_TENANT_ERROR" };
| { status: "UNKNOWN_TENANT_ERROR" }
| { status: "BOXY_ERROR"; message: string };

export default async function createOrUpdateThirdPartyConfig(
_: APIInterface,
Expand Down Expand Up @@ -59,6 +63,70 @@ export default async function createOrUpdateThirdPartyConfig(
}
}

if (providerConfig.thirdPartyId.startsWith("boxy-saml")) {
// boxy stuff here
const boxyURL: string = providerConfig.clients[0].additionalConfig.boxyURL;
const boxyAPIKey: string = providerConfig.clients[0].additionalConfig.boxyAPIKey;

providerConfig.clients[0].additionalConfig.boxyAPIKey = undefined;

const requestBody = {
name: "",
label: "",
description: "",
tenant: `${tenantId}-providerConfig.thirdPartyId`,
product: "supertokens",
defaultRedirectUrl: providerConfig.clients[0].additionalConfig.redirectURLs[0],
forceAuthn: false,
encodedRawMetadata: "",
redirectUrl: JSON.stringify(providerConfig.clients[0].additionalConfig.redirectURLs),
metadataUrl: providerConfig.clients[0].additionalConfig.samlURL || "",
};

const normalisedDomain = new NormalisedURLDomain(boxyURL);
const normalisedBasePath = new NormalisedURLPath(boxyURL);
const connectionsPath = new NormalisedURLPath("/api/v1/saml/config");

const resp = await doPostRequest(
normalisedDomain.getAsStringDangerous() +
normalisedBasePath.getAsStringDangerous() +
connectionsPath.getAsStringDangerous(),
requestBody,
{
Authorization: `Api-Key ${boxyAPIKey}`,
}
);

if (resp.status !== 200) {
if (resp.status === 401) {
return {
status: "BOXY_ERROR",
message: "Invalid API Key",
};
}
return {
status: "BOXY_ERROR",
message: resp.stringResponse,
};
}

if (resp.jsonResponse === undefined) {
throw new Error("should never happen");
}

providerConfig.clients[0].clientId = resp.jsonResponse.clientID;
providerConfig.clients[0].clientSecret = resp.jsonResponse.clientSecret;

const thirdPartyRes = await Multitenancy.createOrUpdateThirdPartyConfig(
tenantId,
providerConfig,
undefined,
userContext
);

return thirdPartyRes;
}

const thirdPartyRes = await Multitenancy.createOrUpdateThirdPartyConfig(
tenantId,
providerConfig,
Expand Down
49 changes: 31 additions & 18 deletions lib/ts/recipe/dashboard/api/multitenancy/getThirdPartyConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ export default async function getThirdPartyConfig(

let providersFromCore = tenantRes?.thirdParty?.providers;
const mtRecipe = MultitenancyRecipe.getInstance();
let staticProviders = mtRecipe?.staticThirdPartyProviders ?? [];
let staticProviders = mtRecipe?.staticThirdPartyProviders
? mtRecipe.staticThirdPartyProviders.map((provider) => ({ ...provider }))
: [];

let additionalConfig = undefined;
let additionalConfig: Record<string, any> | undefined = undefined;

// filter out providers that is not matching thirdPartyId
providersFromCore = providersFromCore.filter((provider) => provider.thirdPartyId === thirdPartyId);
Expand Down Expand Up @@ -88,8 +90,12 @@ export default async function getThirdPartyConfig(
}
} else if (thirdPartyId === "boxy-saml") {
let boxyURL = options.req.getKeyValueFromQuery("boxyUrl");
let boxyAPIKey = options.req.getKeyValueFromQuery("boxyAPIKey");
if (boxyURL !== undefined) {
additionalConfig = { boxyURL };
if (boxyAPIKey !== undefined) {
additionalConfig = { ...additionalConfig, boxyAPIKey };
}
}
} else if (thirdPartyId === "google-workspaces") {
const hd = options.req.getKeyValueFromQuery("hd");
Expand All @@ -104,12 +110,13 @@ export default async function getThirdPartyConfig(
providersFromCore[0].tokenEndpoint = undefined;
providersFromCore[0].userInfoEndpoint = undefined;

for (let j = 0; j < (providersFromCore[0].clients ?? []).length; j++) {
providersFromCore[0].clients![j].additionalConfig = {
...providersFromCore[0].clients![j].additionalConfig,
providersFromCore[0].clients = (providersFromCore[0].clients ?? []).map((client) => ({
...client,
additionalConfig: {
...client.additionalConfig,
...additionalConfig,
};
}
},
}));
}
}

Expand Down Expand Up @@ -140,17 +147,23 @@ export default async function getThirdPartyConfig(
// modify additional config if query param is passed
if (additionalConfig !== undefined) {
// we set these to undefined so that these can be computed using the query param that was provided
staticProviders[0].config.oidcDiscoveryEndpoint = undefined;
staticProviders[0].config.authorizationEndpoint = undefined;
staticProviders[0].config.tokenEndpoint = undefined;
staticProviders[0].config.userInfoEndpoint = undefined;

for (let j = 0; j < (staticProviders[0].config.clients ?? []).length; j++) {
staticProviders[0].config.clients![j].additionalConfig = {
...staticProviders[0].config.clients![j].additionalConfig,
...additionalConfig,
};
}
staticProviders[0] = {
...staticProviders[0],
config: {
...staticProviders[0].config,
oidcDiscoveryEndpoint: undefined,
authorizationEndpoint: undefined,
tokenEndpoint: undefined,
userInfoEndpoint: undefined,
clients: (staticProviders[0].config.clients ?? []).map((client) => ({
...client,
additionalConfig: {
...client.additionalConfig,
...additionalConfig,
},
})),
},
};
}
}

Expand Down
Loading

0 comments on commit 82dd47e

Please sign in to comment.