Skip to content

Commit

Permalink
Match SL tenant/product in PATCH/DEL requests
Browse files Browse the repository at this point in the history
  • Loading branch information
niwsa committed Feb 11, 2025
1 parent fb23231 commit ab8b798
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions pages/api/setup/[token]/sso-connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import { oidcMetadataParse, strategyChecker } from '@lib/utils';
import { validateDevelopmentModeLimits } from '@lib/development-mode';
import { defaultHandler } from '@lib/api';

const withSetupLink = (
handler: (req: NextApiRequest, res: NextApiResponse, setupLink: any) => Promise<void>
) => {
return async (req: NextApiRequest, res: NextApiResponse) => {
const { token } = req.query as { token: string };
const { setupLinkController } = await jackson();
const setupLink = await setupLinkController.getByToken(token);
return handler(req, res, setupLink);
};
};

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await defaultHandler(req, res, {
GET: handleGET,
POST: handlePOST,
PATCH: handlePATCH,
DELETE: handleDELETE,
GET: withSetupLink(handleGET),
POST: withSetupLink(handlePOST),
PATCH: withSetupLink(handlePATCH),
DELETE: withSetupLink(handleDELETE),
});
};

const handleGET = async (req: NextApiRequest, res: NextApiResponse) => {
const { token } = req.query as { token: string };
const { connectionAPIController, setupLinkController } = await jackson();

const setupLink = await setupLinkController.getByToken(token);
const handleGET = async (req: NextApiRequest, res: NextApiResponse, setupLink: any) => {
const { connectionAPIController } = await jackson();

const connections = await connectionAPIController.getConnections({
tenant: setupLink.tenant,
Expand Down Expand Up @@ -52,11 +60,8 @@ const handleGET = async (req: NextApiRequest, res: NextApiResponse) => {
res.json(_connections);
};

const handlePOST = async (req: NextApiRequest, res: NextApiResponse) => {
const { token } = req.query as { token: string };
const { connectionAPIController, setupLinkController } = await jackson();

const setupLink = await setupLinkController.getByToken(token);
const handlePOST = async (req: NextApiRequest, res: NextApiResponse, setupLink: any) => {
const { connectionAPIController } = await jackson();

const body = {
...req.body,
Expand All @@ -78,17 +83,27 @@ const handlePOST = async (req: NextApiRequest, res: NextApiResponse) => {
res.status(201).json({ data: null });
};

const handleDELETE = async (req: NextApiRequest, res: NextApiResponse) => {
const handleDELETE = async (req: NextApiRequest, res: NextApiResponse, setupLink: any) => {
const { connectionAPIController } = await jackson();

const { clientID, clientSecret } = req.query as { clientID: string; clientSecret: string };

const connections = await connectionAPIController.getConnections({
clientID,
});

const { tenant, product } = connections[0];

if (tenant !== setupLink.tenant || product !== setupLink.product) {
throw { message: 'Tenant/product mismatch', statusCode: 400 };
}

await connectionAPIController.deleteConnections({ clientID, clientSecret });

res.json({ data: null });
};

const handlePATCH = async (req: NextApiRequest, res: NextApiResponse) => {
const handlePATCH = async (req: NextApiRequest, res: NextApiResponse, setupLink: any) => {
const { connectionAPIController } = await jackson();

const {
Expand All @@ -114,6 +129,10 @@ const handlePATCH = async (req: NextApiRequest, res: NextApiResponse) => {
const { isSAML, isOIDC } = strategyChecker(req);
const { tenant, product, clientSecret } = connections[0];

if (tenant !== setupLink.tenant || product !== setupLink.product) {
throw { message: 'Tenant/product mismatch', statusCode: 400 };
}

const body = {
tenant,
product,
Expand Down

0 comments on commit ab8b798

Please sign in to comment.