-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from panoratech/connections-auth
feat: start auth core
- Loading branch information
Showing
21 changed files
with
1,032 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,24 @@ | ||
ENV=dev | ||
PROD_DISTRIBUTION=managed # could be self-host if you want to run it locally | ||
DATABASE_URL= | ||
JWT_SECRET="SECRET" | ||
POSTGRES_HOST= | ||
POSTGRES_HOST= | ||
|
||
# Sentry for logging errors | ||
SENTRY_DSN=your_sentry_dsn_here | ||
|
||
# INTEGRATIONS PROVIDER CREDENTIALS FOR PANORA | ||
# CRM | ||
HUBSPOT_CLIENT_ID= | ||
HUBSPOT_CLIENT_SECRET= | ||
ZOHOCRM_CLIENT_ID= | ||
ZOHOCRM_CLIENT_SECRET= | ||
PIPEDRIVE_CLIENT_ID= | ||
PIPEDRIVE_CLIENT_SECRET= | ||
FRESHSALES_CLIENT_ID= | ||
FRESHSALES_CLIENT_SECRET= | ||
ZENDESK_CLIENT_ID= | ||
ZENDESK_CLIENT_SECRET= | ||
|
||
|
||
OAUTH_REDIRECT_BASE='http://localhost:3000' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/api/src/@core/connections/connections.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ConnectionsController } from './connections.controller'; | ||
import { ConnectionsService } from './services/connections.service'; | ||
|
||
describe('ConnectionsController', () => { | ||
let controller: ConnectionsController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ConnectionsController], | ||
providers: [ConnectionsService], | ||
}).compile(); | ||
|
||
controller = module.get<ConnectionsController>(ConnectionsController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
packages/api/src/@core/connections/connections.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Controller, Get, Query, Res } from '@nestjs/common'; | ||
import { ConnectionsService } from './services/connections.service'; | ||
import { Response } from 'express'; // Importing the Express Response type for type checking | ||
|
||
@Controller('connections') | ||
export class ConnectionsController { | ||
constructor(private readonly connectionsService: ConnectionsService) {} | ||
|
||
@Get('oauth/crm/callback') | ||
handleCRMCallback( | ||
@Res() res: Response, | ||
@Query('projectId') projectId: string, | ||
@Query('linkedUserId') linkedUserId: string, | ||
@Query('providerName') providerName: string, | ||
@Query('returnUrl') returnUrl: string, | ||
@Query('code') code: string, | ||
@Query('accountURL') zohoAccountURL?: string, | ||
) { | ||
//TODO; ADD VERIFICATION OF PARAMS | ||
|
||
this.connectionsService.handleCRMCallBack( | ||
projectId, | ||
linkedUserId, | ||
providerName, | ||
code, | ||
zohoAccountURL, | ||
); | ||
res.redirect(returnUrl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConnectionsService } from './services/connections.service'; | ||
import { ConnectionsController } from './connections.controller'; | ||
import { CrmConnectionsService } from './services/crm/crm-connection.service'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Module({ | ||
controllers: [ConnectionsController], | ||
providers: [ConnectionsService, CrmConnectionsService, PrismaService], | ||
}) | ||
export class ConnectionsModule {} |
24 changes: 24 additions & 0 deletions
24
packages/api/src/@core/connections/dto/create-connection.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class BaseConnectionDto { | ||
customerId: string; | ||
providerName: string; | ||
} | ||
|
||
interface OAuth { | ||
projectId: string; | ||
returnUrl: string; | ||
} | ||
|
||
interface ApiKey { | ||
apiKey: string; | ||
} | ||
|
||
interface AccessKeys { | ||
accessKeyId: string; | ||
secretAccessKey: string; | ||
} | ||
|
||
export type CreateConnectionDto<T> = BaseConnectionDto & T; | ||
|
||
export type CreateConnectionDtoOauth = CreateConnectionDto<OAuth>; | ||
export type CreateConnectionDtoApiKey = CreateConnectionDto<ApiKey>; | ||
export type CreateConnectionDtoAccessKeys = CreateConnectionDto<AccessKeys>; |
1 change: 1 addition & 0 deletions
1
packages/api/src/@core/connections/entities/connection.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Connection {} |
18 changes: 18 additions & 0 deletions
18
packages/api/src/@core/connections/services/connections.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ConnectionsService } from './connections.service'; | ||
|
||
describe('ConnectionsService', () => { | ||
let service: ConnectionsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ConnectionsService], | ||
}).compile(); | ||
|
||
service = module.get<ConnectionsService>(ConnectionsService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
125 changes: 125 additions & 0 deletions
125
packages/api/src/@core/connections/services/connections.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CrmConnectionsService } from './crm/crm-connection.service'; | ||
import { NotFoundError } from 'src/@core/utils/errors'; | ||
|
||
@Injectable() | ||
export class ConnectionsService { | ||
//STEP 1:[FRONTEND STEP] | ||
//create a frontend SDK snippet in which an authorization embedded link is set up so when users click | ||
// on it to grant access => they grant US the access and then when confirmed | ||
/*const authUrl = | ||
'https://app.hubspot.com/oauth/authorize' + | ||
`?client_id=${encodeURIComponent(CLIENT_ID)}` + | ||
`&scope=${encodeURIComponent(SCOPES)}` + | ||
`&redirect_uri=${encodeURIComponent(REDIRECT_URI)}`;*/ //oauth/callback | ||
|
||
// oauth server calls this redirect callback | ||
// WE WOULD HAVE CREATED A DEV ACCOUNT IN THE 5 CRMs (Panora dev account) | ||
// we catch the tmp token and swap it against oauth2 server for access/refresh tokens | ||
// to perform actions on his behalf | ||
// this call pass 1. integrationID 2. CustomerId 3. Panora Api Key | ||
constructor(private crmConnectionService: CrmConnectionsService) {} | ||
|
||
async handleCRMCallBack( | ||
projectId: string, | ||
linkedUserId: string, | ||
providerName: string, | ||
code: string, | ||
zohoAccountURL?: string, | ||
) { | ||
try { | ||
switch (providerName) { | ||
case 'hubspot': | ||
if (!code) { | ||
throw new NotFoundError('no hubspot code found'); | ||
} | ||
return this.crmConnectionService.handleHubspotCallback( | ||
linkedUserId, | ||
projectId, | ||
code, | ||
); | ||
case 'zoho': | ||
if (!code || !zohoAccountURL) { | ||
throw new NotFoundError('no zoho code/ zoho AccountURL found'); | ||
} | ||
return this.crmConnectionService.handleZohoCallback( | ||
linkedUserId, | ||
projectId, | ||
code, | ||
zohoAccountURL, | ||
); | ||
case 'pipedrive': | ||
if (!code) { | ||
throw new NotFoundError('no pipedrive code found'); | ||
} | ||
return this.crmConnectionService.handlePipedriveCallback( | ||
linkedUserId, | ||
projectId, | ||
code, | ||
); | ||
case 'freshsales': | ||
//todo: LATER | ||
break; | ||
case 'zendesk': | ||
if (!code) { | ||
throw new NotFoundError('no zendesk code found'); | ||
} | ||
return this.crmConnectionService.handleZendeskCallback( | ||
linkedUserId, | ||
projectId, | ||
code, | ||
); | ||
default: | ||
return; | ||
} | ||
} catch (error) { | ||
if (error instanceof NotFoundError) { | ||
console.log(error); | ||
} | ||
return error; | ||
} | ||
} | ||
|
||
async handleCRMTokensRefresh( | ||
connectionId: bigint, | ||
providerId: string, | ||
refresh_token: string, | ||
account_url?: string, | ||
) { | ||
try { | ||
switch (providerId) { | ||
case 'hubspot': | ||
return this.crmConnectionService.handleHubspotTokenRefresh( | ||
connectionId, | ||
refresh_token, | ||
); | ||
case 'zoho': | ||
return this.crmConnectionService.handleZohoTokenRefresh( | ||
connectionId, | ||
refresh_token, | ||
account_url, | ||
); | ||
case 'pipedrive': | ||
return this.crmConnectionService.handlePipedriveTokenRefresh( | ||
connectionId, | ||
refresh_token, | ||
); | ||
case 'freshsales': | ||
//todo: LATER | ||
break; | ||
case 'zendesk': | ||
return this.crmConnectionService.handleZendeskTokenRefresh( | ||
connectionId, | ||
refresh_token, | ||
); | ||
default: | ||
return; | ||
} | ||
} catch (error) { | ||
if (error instanceof NotFoundError) { | ||
console.log(error); | ||
} | ||
return error; | ||
} | ||
} | ||
} |
Oops, something went wrong.