mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-10 09:28:36 +00:00
110 lines
2.7 KiB
TypeScript
110 lines
2.7 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import * as Sentry from '@sentry/node';
|
|
import axios from 'axios';
|
|
import { readFileSync } from 'fs';
|
|
import { IntegrationAuth, Integration } from '../../models';
|
|
import { INTEGRATION_SET, INTEGRATION_OPTIONS } from '../../variables';
|
|
import { IntegrationService } from '../../services';
|
|
import { getApps, revokeAccess } from '../../integrations';
|
|
|
|
export const getIntegrationOptions = async (
|
|
req: Request,
|
|
res: Response
|
|
) => {
|
|
return res.status(200).send({
|
|
integrationOptions: INTEGRATION_OPTIONS
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Perform OAuth2 code-token exchange as part of integration [integration] for workspace with id [workspaceId]
|
|
* @param req
|
|
* @param res
|
|
* @returns
|
|
*/
|
|
export const oAuthExchange = async (
|
|
req: Request,
|
|
res: Response
|
|
) => {
|
|
try {
|
|
const { workspaceId, code, integration } = req.body;
|
|
|
|
if (!INTEGRATION_SET.has(integration))
|
|
throw new Error('Failed to validate integration');
|
|
|
|
const environments = req.membership.workspace?.environments || [];
|
|
if(environments.length === 0){
|
|
throw new Error("Failed to get environments")
|
|
}
|
|
|
|
await IntegrationService.handleOAuthExchange({
|
|
workspaceId,
|
|
integration,
|
|
code,
|
|
environment: environments[0].slug,
|
|
});
|
|
} catch (err) {
|
|
Sentry.setUser(null);
|
|
Sentry.captureException(err);
|
|
return res.status(400).send({
|
|
message: 'Failed to get OAuth2 code-token exchange'
|
|
});
|
|
}
|
|
|
|
return res.status(200).send({
|
|
message: 'Successfully enabled integration authorization'
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Return list of applications allowed for integration with integration authorization id [integrationAuthId]
|
|
* @param req
|
|
* @param res
|
|
* @returns
|
|
*/
|
|
export const getIntegrationAuthApps = async (req: Request, res: Response) => {
|
|
let apps;
|
|
try {
|
|
apps = await getApps({
|
|
integrationAuth: req.integrationAuth,
|
|
accessToken: req.accessToken
|
|
});
|
|
} catch (err) {
|
|
Sentry.setUser(null);
|
|
Sentry.captureException(err);
|
|
return res.status(400).send({
|
|
message: 'Failed to get integration authorization applications'
|
|
});
|
|
}
|
|
|
|
return res.status(200).send({
|
|
apps
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Delete integration authorization with id [integrationAuthId]
|
|
* @param req
|
|
* @param res
|
|
* @returns
|
|
*/
|
|
export const deleteIntegrationAuth = async (req: Request, res: Response) => {
|
|
try {
|
|
const { integrationAuthId } = req.params;
|
|
|
|
await revokeAccess({
|
|
integrationAuth: req.integrationAuth,
|
|
accessToken: req.accessToken
|
|
});
|
|
} catch (err) {
|
|
Sentry.setUser(null);
|
|
Sentry.captureException(err);
|
|
return res.status(400).send({
|
|
message: 'Failed to delete integration authorization'
|
|
});
|
|
}
|
|
|
|
return res.status(200).send({
|
|
message: 'Successfully deleted integration authorization'
|
|
});
|
|
} |