Skip to content

Commit

Permalink
feat: support redirectUrls optionally (#574)
Browse files Browse the repository at this point in the history
* fix: support oauth-callback for all integrations

* fix: support all ticketing integration

* feat(wip): support all integrations with redirectUrls

* fix: undefined redirectUrl throwing errors

* fix: trello to support redirectUrls

* fix: redirectUrls with fieldMappings

* fix: redirectUrl Example to not include redirectUrl by default
  • Loading branch information
Nabhag8848 authored May 30, 2024
1 parent 670ebcf commit 055b169
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 35 deletions.
1 change: 1 addition & 0 deletions packages/backend/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,5 @@ export type IntegrationAuthProps = {
tenantSecretToken: string;
response: Response;
request: Request;
redirectUrl?: string;
};
3 changes: 3 additions & 0 deletions packages/backend/helpers/auth/processOAuthResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type IntegrationCreationOutcome = {
response: Response;
tpCustomerId?: string;
statusText?: string;
redirectUrl?: string;
};

/**
Expand Down Expand Up @@ -40,6 +41,7 @@ const processOAuthResult = async ({
status,
tpCustomerId,
statusText,
redirectUrl
}: IntegrationCreationOutcome) => {
error && logError(error);
if (error instanceof Prisma.PrismaClientKnownRequestError) {
Expand All @@ -55,6 +57,7 @@ const processOAuthResult = async ({
integrationName,
tenantId,
tenantSecretToken,
redirectUrl
} as IntegrationStatusSseMessage);

return response.send({
Expand Down
1 change: 1 addition & 0 deletions packages/backend/redis/client/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export interface IntegrationStatusSseMessage {
integrationName: string;
tenantId: string;
tenantSecretToken?: string;
redirectUrl?:string
}
6 changes: 6 additions & 0 deletions packages/backend/routes/v1/chat/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ authRouter.get('/oauth-callback', async (req, res) => {
logInfo('OAuth callback', req.query);
const integrationId = req.query.integrationId as TP_ID; // add TP_ID alias after
const revertPublicKey = req.query.x_revert_public_token as string;
const redirect_url = req.query?.redirect_url;
const redirectUrl = redirect_url ? (redirect_url as string) : undefined;

// generate a token for connection auth and save in redis for 5 mins
const tenantSecretToken = randomUUID();
Expand Down Expand Up @@ -64,6 +66,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
tenantSecretToken,
response: res,
request: req,
redirectUrl,
};

if (req.query.code && req.query.t_id && revertPublicKey) {
Expand All @@ -81,6 +84,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'Not implemented yet',
redirectUrl,
});
}
} else {
Expand All @@ -91,6 +95,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'noop',
redirectUrl,
});
}
} catch (error: any) {
Expand All @@ -103,6 +108,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
tenantId: req.query.t_id as string,
status: false,
statusText: 'Error while getting oauth creds',
redirectUrl,
});
}
});
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/chat/authHandlers/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DiscordAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl,
}: IntegrationAuthProps) {
const botToken = account?.apps[0]?.is_revert_app
? undefined
Expand Down Expand Up @@ -87,6 +88,7 @@ class DiscordAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.id,
redirectUrl,
});
} catch (error: any) {
return processOAuthResult({
Expand All @@ -97,6 +99,7 @@ class DiscordAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl,
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/chat/authHandlers/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SlackAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl
}: IntegrationAuthProps) {
const url = 'https://slack.com/api/oauth.v2.access';
const formData = {
Expand Down Expand Up @@ -100,6 +101,7 @@ class SlackAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.user,
redirectUrl
});
} catch (error: any) {
return processOAuthResult({
Expand All @@ -110,6 +112,7 @@ class SlackAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl
});
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/routes/v1/crm/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ authRouter.get('/oauth-callback', async (req, res) => {
logInfo('OAuth callback', req.query);
const integrationId = req.query.integrationId as CRM_TP_ID;
const revertPublicKey = req.query.x_revert_public_token as string;
const redirect_url = req.query?.redirect_url;
const redirectUrl = redirect_url ? (redirect_url as string) : undefined;

// generate a token for connection auth and save in redis for 5 mins
const tenantSecretToken = randomUUID();
Expand Down Expand Up @@ -69,6 +71,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
tenantSecretToken,
response: res,
request: req,
redirectUrl,
};

if (req.query.code && req.query.t_id && revertPublicKey) {
Expand All @@ -94,6 +97,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'Not implemented yet',
redirectUrl,
});
}
} else {
Expand All @@ -104,6 +108,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'noop',
redirectUrl,
});
}
} catch (error: any) {
Expand All @@ -116,6 +121,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'Error while getting oauth creds',
redirectUrl,
});
}
});
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/crm/authHandlers/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CloseAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl
}: IntegrationAuthProps) {
try {
const formData = {
Expand Down Expand Up @@ -87,6 +88,7 @@ class CloseAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.email,
redirectUrl
});
} catch (error: any) {
return processOAuthResult({
Expand All @@ -97,6 +99,7 @@ class CloseAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/crm/authHandlers/hubspot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class HubspotAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl
}: IntegrationAuthProps) {
try {
const url = 'https://api.hubapi.com/oauth/v1/token';
Expand Down Expand Up @@ -83,6 +84,7 @@ class HubspotAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.user,
redirectUrl
});
} catch (error: any) {
console.log('ERROR', error);
Expand All @@ -94,6 +96,7 @@ class HubspotAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/crm/authHandlers/ms_dynamic_365.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MsDynamicAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl
}: IntegrationAuthProps) {
try {
let orgURL = account?.apps[0]?.is_revert_app
Expand Down Expand Up @@ -107,6 +108,7 @@ class MsDynamicAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.UserId,
redirectUrl
});
} catch (error: any) {
console.log('OAuthERROR', error);
Expand All @@ -118,6 +120,7 @@ class MsDynamicAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/crm/authHandlers/pipedrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PipeDriveAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl,
}: IntegrationAuthProps) {
try {
// Handle the received code
Expand Down Expand Up @@ -96,6 +97,7 @@ class PipeDriveAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.data.email,
redirectUrl,
});
} catch (error) {
console.log('OAuthERROR', error);
Expand All @@ -107,6 +109,7 @@ class PipeDriveAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl,
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/crm/authHandlers/sfdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SfdcAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl
}: IntegrationAuthProps) {
try {
// Handle the received code
Expand Down Expand Up @@ -89,6 +90,7 @@ class SfdcAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.email,
redirectUrl
});
} catch (error: any) {
console.log('OAuthERROR', error);
Expand All @@ -100,6 +102,7 @@ class SfdcAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/crm/authHandlers/zoho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ZohoAuthHandler extends BaseOAuthHandler {
tenantSecretToken,
response,
request,
redirectUrl
}: IntegrationAuthProps) {
try {
const url = `${request?.query.accountURL}/oauth/v2/token`;
Expand Down Expand Up @@ -104,6 +105,7 @@ class ZohoAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data.Email,
redirectUrl
});
}
} catch (error: any) {
Expand All @@ -116,6 +118,7 @@ class ZohoAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl
});
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/routes/v1/ticket/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ authRouter.get('/oauth-callback', async (req, res) => {
logInfo('OAuth callback', req.query);
const integrationId = req.query.integrationId as TP_ID;
const revertPublicKey = req.query.x_revert_public_token as string;
const redirect_url = req.query?.redirect_url;
const redirectUrl = redirect_url ? (redirect_url as string) : undefined;
// generate a token for connection auth and save in redis for 5 mins
const tenantSecretToken = randomUUID();
await redis.setEx(`tenantSecretToken_${req.query.t_id}`, 5 * 60, tenantSecretToken);
Expand Down Expand Up @@ -54,6 +56,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
tenantSecretToken,
response: res,
request: req,
redirectUrl,
};

if (req.query.code && req.query.t_id && revertPublicKey) {
Expand All @@ -77,6 +80,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'Not implemented yet',
redirectUrl,
});
}
}
Expand All @@ -88,6 +92,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'noop',
redirectUrl,
});
} catch (error: any) {
return processOAuthResult({
Expand All @@ -99,6 +104,7 @@ authRouter.get('/oauth-callback', async (req, res) => {
response: res,
tenantId: req.query.t_id as string,
statusText: 'Error while getting oauth creds',
redirectUrl,
});
}
});
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/routes/v1/ticket/authHandlers/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BitbucketAuthHandler extends BaseOAuthHandler {
tenantId,
tenantSecretToken,
response,
redirectUrl,
}: IntegrationAuthProps) {
const formData = {
grant_type: 'authorization_code',
Expand Down Expand Up @@ -103,6 +104,7 @@ class BitbucketAuthHandler extends BaseOAuthHandler {
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
tpCustomerId: info.data?.account_id,
redirectUrl,
});
} catch (error: any) {
return processOAuthResult({
Expand All @@ -113,6 +115,7 @@ class BitbucketAuthHandler extends BaseOAuthHandler {
response,
tenantId: tenantId,
integrationName: mapIntegrationIdToIntegrationName[integrationId],
redirectUrl,
});
}
}
Expand Down
Loading

0 comments on commit 055b169

Please sign in to comment.