Skip to content

Commit

Permalink
[Story]:OAuth configuration setup for public api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Nagendra committed Feb 8, 2024
1 parent fa47c38 commit 79f571f
Show file tree
Hide file tree
Showing 23 changed files with 272 additions and 149 deletions.
24 changes: 20 additions & 4 deletions pages/api/oauth2/login/facebook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ import passport from 'passport';
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { oauthKeys } from '@/lib/oauth_middleware';

export const createStrategy = ({ client_id = '', client_secret = '', workspace = '', connector = '' }) => {
export const createStrategy = ({
client_id = '',
client_secret = '',
workspace = '',
connector = '',
oauth_keys = 'private'
}) => {
const strategy = new FacebookStrategy(
{
clientID: client_id as string,
clientSecret: client_secret as string,
authType: 'reauthenticate',
profileFields: ['id', 'displayName', 'photos', 'email'],
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/facebook?workspace=${workspace}&connector=${connector}` // this is the endpoint you registered on hubspot while creating your app. This endpoint would exist on your application for verifying the authentication
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/facebook?workspace=${workspace}&connector=${connector}&oauth_keys=${oauth_keys}` // this is the endpoint you registered on hubspot while creating your app. This endpoint would exist on your application for verifying the authentication
},
async (_accessToken, _refreshToken, profile: any, cb: any) => {
try {
Expand All @@ -40,8 +46,18 @@ router
.use(oauthKeys)

.get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_FACEBOOK_CLIENT_ID,
client_secret: process.env.AUTH_FACEBOOK_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };
const strategy = createStrategy(query);

return passport.authenticate(strategy, {
Expand Down
25 changes: 21 additions & 4 deletions pages/api/oauth2/login/google.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ const saveUser = (user: Profile) => {
});
};

export const createStrategy = ({ client_id = '', client_secret = '', workspace = '', connector = '' }) => {
export const createStrategy = ({
client_id = '',
client_secret = '',
workspace = '',
connector = '',
oauth_keys = 'private'
}) => {
const strategy = new GoogleStrategy(
{
clientID: client_id as string,
clientSecret: client_secret as string,
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/google?workspace=${workspace}&connector=${connector}` // this is the endpoint you registered on hubspot while creating your app. This endpoint would exist on your application for verifying the authentication
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/google?workspace=${workspace}&connector=${connector}&oauth_keys=${oauth_keys}` // this is the endpoint you registered on hubspot while creating your app. This endpoint would exist on your application for verifying the authentication
},
async (_accessToken, _refreshToken, profile: any, cb: any) => {
try {
Expand All @@ -48,8 +54,19 @@ router
.use(oauthKeys)

.get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_GOOGLE_CLIENT_ID,
client_secret: process.env.AUTH_GOOGLE_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };

const strategy = createStrategy(query);

return passport.authenticate(strategy, {
Expand Down
25 changes: 21 additions & 4 deletions pages/api/oauth2/login/hubspot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ const saveUser = (user: Profile) => {
});
};

export const createStrategy = ({ client_id = '', client_secret = '', workspace = '', connector = '' }) => {
export const createStrategy = ({
client_id = '',
client_secret = '',
workspace = '',
connector = '',
oauth_keys = 'private'
}) => {
const strategy = new HubspotStrategy(
{
clientID: client_id as string,
clientSecret: client_secret as string,
user_scope: ['identity.basic', 'identity.email'],
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/hubspot?workspace=${workspace}&connector=${connector}`, // this is the endpoint you registered on hubspot while creating your app. This endpoint would exist on your application for verifying the authentication
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/hubspot?workspace=${workspace}&connector=${connector}&oauth_keys=${oauth_keys}`, // this is the endpoint you registered on hubspot while creating your app. This endpoint would exist on your application for verifying the authentication
passReqToCallback: true
},
async (req, _accessToken, _refreshToken, profile, cb: any) => {
Expand All @@ -46,8 +52,19 @@ export const createStrategy = ({ client_id = '', client_secret = '', workspace =
const router = createRouter();

router.use(oauthKeys).get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_HUBSPOT_CLIENT_ID,
client_secret: process.env.AUTH_HUBSPOT_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };

const strategy = createStrategy(query);

return passport.authenticate(strategy, {
Expand Down
24 changes: 20 additions & 4 deletions pages/api/oauth2/login/slack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ const saveUser = (user) => {
});
};

export const createStrategy = ({ client_id = '', client_secret = '', workspace = '', connector = '' }) => {
export const createStrategy = ({
client_id = '',
client_secret = '',
workspace = '',
connector = '',
oauth_keys = 'private'
}) => {
const strategy = new SlackStrategy(
{
clientID: client_id as string,
clientSecret: client_secret as string,
user_scope: ['identity.basic', 'identity.email'],
scope: ['users.profile:read', 'chat:write', 'channels:read', 'channels:join'], // default,
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/slack?workspace=${workspace}&connector=${connector}`
callbackURL: `${process.env.WEB_URL}/api/oauth2/redirect/slack?workspace=${workspace}&connector=${connector}&oauth_keys=${oauth_keys}`
},
async (accessToken, params, profile, cb: any) => {
try {
Expand All @@ -47,8 +53,18 @@ export const createStrategy = ({ client_id = '', client_secret = '', workspace =
const router = createRouter();

router.use(oauthKeys).get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_SLACK_CLIENT_ID,
client_secret: process.env.AUTH_SLACK_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };
const strategy = createStrategy(query);

return passport.authenticate(strategy, null, {
Expand Down
15 changes: 13 additions & 2 deletions pages/api/oauth2/redirect/facebook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ router
.use(oauthKeys)

.get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_FACEBOOK_CLIENT_ID,
client_secret: process.env.AUTH_FACEBOOK_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };

const strategy = createStrategy(query);

return passport.authenticate(strategy, { session: 'false' }, async (err: any, user: any) => {
Expand Down
15 changes: 13 additions & 2 deletions pages/api/oauth2/redirect/google.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ router
.use(oauthKeys)

.get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_GOOGLE_CLIENT_ID,
client_secret: process.env.AUTH_GOOGLE_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };

const strategy = createStrategy(query);

return passport.authenticate(strategy, { session: 'false' }, async (err: any, user: any) => {
Expand Down
14 changes: 12 additions & 2 deletions pages/api/oauth2/redirect/hubspot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ router
.use(oauthKeys)

.get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_HUBSPOT_CLIENT_ID,
client_secret: process.env.AUTH_HUBSPOT_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };
const strategy = createStrategy(query);

return passport.authenticate(strategy, { session: 'false' }, async (err: any, user: any) => {
Expand Down
24 changes: 17 additions & 7 deletions pages/api/oauth2/redirect/slack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@ router
.use(oauthKeys)

.get(async (req, res, next) => {
let { workspace = '', connector = '' } = req.query;
const query = { ...req.credentials, workspace: workspace, connector };
let { workspace = '', connector = '', oauth_keys = 'private' } = req.query;

let credentials = { ...(req.credentials ?? {}) };

if (oauth_keys === 'public') {
credentials = {
client_id: process.env.AUTH_SLACK_CLIENT_ID,
client_secret: process.env.AUTH_SLACK_CLIENT_SECRET
};
}

const query = { ...credentials, workspace: workspace, connector: connector, oauth_keys: oauth_keys };
const strategy = createStrategy(query);

return passport.authenticate(strategy, { session: 'false' }, async (err: any, user: any) => {
const params = new URLSearchParams({
provider: user?.provider ?? 'slack',
access_token: user?.['_accessToken'] ?? '',
refresh_token: user?._refreshToken ?? '',
id: user.profile.email,
unique_id: user.profile.email,
id: user?.profile?.email ?? '',
unique_id: user?.profile?.email ?? '',
bot_user_id: user?.['_bot_user_id'] ?? '',
email: user.profile.email,
name: user.profile.real_name,
image: user.profile.image_original
email: user?.profile?.email ?? '',
name: user?.profile?.real_name ?? '',
image: user?.profile?.image_original ?? ''
});

res.redirect('/auth/callback?' + params.toString());
Expand Down
5 changes: 2 additions & 3 deletions pages/auth/callback/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const OAuthRedirectPage = () => {
const dispatch = useDispatch<AppDispatch>();

/** Redux store */
const connection_flow = useSelector(
(state: RootState) => state.connectionFlow
);
const connection_flow = useSelector((state: RootState) => state.connectionFlow);
const { flowState: {} = {} } = connection_flow;

const appState = useSelector((state: RootState) => state.appFlow.appState);
Expand Down Expand Up @@ -109,6 +107,7 @@ const OAuthRedirectPage = () => {

export const getOAuthParams = (params: any) => {
const oAuthParams = params || {};

const { provider = '' } = oAuthParams;
switch (provider) {
case 'facebook':
Expand Down
6 changes: 1 addition & 5 deletions pages/spaces/[wid]/connections/destinations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import { NextPageWithLayout } from '@/pages_app';

import SidebarLayout from '@layouts/SidebarLayout';

import {
ConnectionType,
ConnectionsPageTitle,
CreateConnectionTitle
} from '@content/Connections/ConnectionModel';
import { ConnectionType, ConnectionsPageTitle, CreateConnectionTitle } from '@content/Connections/ConnectionModel';
import Connections from '@content/Connections';

import constants from '@constants/index';
Expand Down
6 changes: 1 addition & 5 deletions pages/spaces/[wid]/connections/warehouses/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import { NextPageWithLayout } from '@/pages_app';

import SidebarLayout from '@layouts/SidebarLayout';

import {
ConnectionType,
ConnectionsPageTitle,
CreateConnectionTitle
} from '@content/Connections/ConnectionModel';
import { ConnectionType, ConnectionsPageTitle, CreateConnectionTitle } from '@content/Connections/ConnectionModel';
import Connections from '@content/Connections';

import constants from '@constants/index';
Expand Down
3 changes: 3 additions & 0 deletions src/components/FormInput/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface FormFieldProps extends FormObject {
oauth_error?: string;
control: any;
isConnectorConfigured?: boolean;
isConfigurationRequired?: boolean;
handleOnConfigureButtonClick: (data: any) => void;
}

Expand All @@ -33,6 +34,7 @@ const FormField = ({
selectedConnector,
onClick,
isConnectorConfigured,
isConfigurationRequired,
handleOnConfigureButtonClick,
hasAuthorizedOAuth,
oauth_error,
Expand Down Expand Up @@ -60,6 +62,7 @@ const FormField = ({
label={label}
onClick={onClick}
isConnectorConfigured={isConnectorConfigured}
isConfigurationRequired={isConfigurationRequired}
handleOnConfigureButtonClick={handleOnConfigureButtonClick}
oAuthProvider={oAuthProvider}
oauth_error={oauth_error}
Expand Down
Loading

0 comments on commit 79f571f

Please sign in to comment.