Skip to content

Commit

Permalink
✨ Reworked env in server
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Dec 20, 2023
1 parent 2385c57 commit 28724be
Show file tree
Hide file tree
Showing 30 changed files with 477 additions and 233 deletions.
2 changes: 2 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"axios": "^1.5.1",
"bcrypt": "^5.1.1",
"bull": "^4.11.5",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"crypto": "^1.0.1",
"dotenv": "^16.3.1",
"install": "^0.13.0",
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/@core/connections/connections.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CrmConnectionModule } from './crm/crm-connection.module';
import { ConnectionsController } from './connections.controller';
import { LoggerService } from '@@core/logger/logger.service';
import { PrismaService } from '@@core/prisma/prisma.service';
import { WebhookService } from '@@core/webhook/webhook.service';

@Module({
controllers: [ConnectionsController],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { ZohoConnectionService } from './services/zoho/zoho.service';
import { LoggerService } from '@@core/logger/logger.service';
import { WebhookService } from '@@core/webhook/webhook.service';
import { WebhookModule } from '@@core/webhook/webhook.module';
import { EnvironmentService } from '@@core/environment/environment.service';
import { EncryptionService } from '@@core/encryption/encryption.service';

@Module({
imports: [WebhookModule],
Expand All @@ -22,6 +24,8 @@ import { WebhookModule } from '@@core/webhook/webhook.module';
ZohoConnectionService,
LoggerService,
WebhookService,
EnvironmentService,
EncryptionService,
],
exports: [CrmConnectionsService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '@@core/prisma/prisma.service';
import axios from 'axios';
import config from '@@core/utils/config';
import { HubspotOAuthResponse } from '../../types';
import { LoggerService } from '@@core/logger/logger.service';
import {
Action,
NotUniqueRecord,
handleServiceError,
} from '@@core/utils/errors';
import { Action, handleServiceError } from '@@core/utils/errors';
import { v4 as uuidv4 } from 'uuid';
import { decrypt, encrypt } from '@@core/utils/crypto';
import { EnvironmentService } from '@@core/environment/environment.service';
import { EncryptionService } from '@@core/encryption/encryption.service';

@Injectable()
export class HubspotConnectionService {
constructor(private prisma: PrismaService, private logger: LoggerService) {
constructor(
private prisma: PrismaService,
private logger: LoggerService,
private env: EnvironmentService,
private cryptoService: EncryptionService,
) {
this.logger.setContext(HubspotConnectionService.name);
}

Expand All @@ -36,11 +37,11 @@ export class HubspotConnectionService {
if (isNotUnique) return;

//reconstruct the redirect URI that was passed in the frontend it must be the same
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/connections/oauth/callback`;
const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`;
const formData = new URLSearchParams({
grant_type: 'authorization_code',
client_id: config.HUBSPOT_CLIENT_ID,
client_secret: config.HUBSPOT_CLIENT_SECRET,
client_id: this.env.getHubspotAuth().CLIENT_ID,
client_secret: this.env.getHubspotAuth().CLIENT_SECRET,
redirect_uri: REDIRECT_URI,
code: code,
});
Expand All @@ -63,8 +64,8 @@ export class HubspotConnectionService {
id_connection: isNotUnique.id_connection,
},
data: {
access_token: encrypt(data.access_token),
refresh_token: encrypt(data.refresh_token),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + data.expires_in * 1000,
),
Expand All @@ -79,8 +80,8 @@ export class HubspotConnectionService {
id_connection: uuidv4(),
provider_slug: 'hubspot',
token_type: 'oauth',
access_token: encrypt(data.access_token),
refresh_token: encrypt(data.refresh_token),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + data.expires_in * 1000,
),
Expand All @@ -104,13 +105,13 @@ export class HubspotConnectionService {

async handleHubspotTokenRefresh(connectionId: string, refresh_token: string) {
try {
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/connections/oauth/callback`; //tocheck
const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`; //tocheck
const formData = new URLSearchParams({
grant_type: 'refresh_token',
client_id: config.HUBSPOT_CLIENT_ID,
client_secret: config.HUBSPOT_CLIENT_SECRET,
client_id: this.env.getHubspotAuth().CLIENT_ID,
client_secret: this.env.getHubspotAuth().CLIENT_SECRET,
redirect_uri: REDIRECT_URI,
refresh_token: decrypt(refresh_token),
refresh_token: this.cryptoService.decrypt(refresh_token),
});
const res = await axios.post(
'https://api.hubapi.com/oauth/v1/token',
Expand All @@ -127,8 +128,8 @@ export class HubspotConnectionService {
id_connection: connectionId,
},
data: {
access_token: encrypt(data.access_token),
refresh_token: encrypt(data.refresh_token),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + data.expires_in * 1000,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '@@core/prisma/prisma.service';
import axios from 'axios';
import config from '@@core/utils/config';
import { PipeDriveOAuthResponse } from '../../types';
import {
Action,
NotUniqueRecord,
handleServiceError,
} from '@@core/utils/errors';
import { Action, handleServiceError } from '@@core/utils/errors';
import { LoggerService } from '@@core/logger/logger.service';
import { v4 as uuidv4 } from 'uuid';
import { decrypt, encrypt } from '@@core/utils/crypto';
import { EnvironmentService } from '@@core/environment/environment.service';
import { EncryptionService } from '@@core/encryption/encryption.service';

@Injectable()
export class PipedriveConnectionService {
constructor(private prisma: PrismaService, private logger: LoggerService) {
constructor(
private prisma: PrismaService,
private logger: LoggerService,
private env: EnvironmentService,
private cryptoService: EncryptionService,
) {
this.logger.setContext(PipedriveConnectionService.name);
}

Expand All @@ -32,7 +33,7 @@ export class PipedriveConnectionService {
});

//reconstruct the redirect URI that was passed in the frontend it must be the same
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/connections/oauth/callback`;
const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`;

const formData = new URLSearchParams({
grant_type: 'authorization_code',
Expand All @@ -46,7 +47,9 @@ export class PipedriveConnectionService {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic ${Buffer.from(
`${config.PIPEDRIVE_CLIENT_ID}:${config.PIPEDRIVE_CLIENT_SECRET}`,
`${this.env.getPipedriveSecret().CLIENT_ID}:${
this.env.getPipedriveSecret().CLIENT_SECRET
}`,
).toString('base64')}`,
},
},
Expand All @@ -61,8 +64,8 @@ export class PipedriveConnectionService {
id_connection: uuidv4(),
provider_slug: 'pipedrive',
token_type: 'oauth',
access_token: encrypt(data.access_token),
refresh_token: encrypt(data.refresh_token),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + data.expires_in * 1000,
),
Expand All @@ -76,8 +79,8 @@ export class PipedriveConnectionService {
},
},
update: {
access_token: encrypt(data.access_token),
refresh_token: encrypt(data.refresh_token),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + data.expires_in * 1000,
),
Expand All @@ -96,12 +99,12 @@ export class PipedriveConnectionService {
refresh_token: string,
) {
try {
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/connections/oauth/callback`;
const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`;

const formData = new URLSearchParams({
grant_type: 'refresh_token',
redirect_uri: REDIRECT_URI,
refresh_token: decrypt(refresh_token),
refresh_token: this.cryptoService.decrypt(refresh_token),
});
const res = await axios.post(
'https://oauth.pipedrive.com/oauth/token',
Expand All @@ -110,7 +113,9 @@ export class PipedriveConnectionService {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic ${Buffer.from(
`${config.PIPEDRIVE_CLIENT_ID}:${config.PIPEDRIVE_CLIENT_SECRET}`,
`${this.env.getPipedriveSecret().CLIENT_ID}:${
this.env.getPipedriveSecret().CLIENT_SECRET
}`,
).toString('base64')}`,
},
},
Expand All @@ -121,8 +126,8 @@ export class PipedriveConnectionService {
id_connection: connectionId,
},
data: {
access_token: encrypt(data.access_token),
refresh_token: encrypt(data.refresh_token),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + data.expires_in * 1000,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import config from '@@core/utils/config';
import { PrismaService } from '@@core/prisma/prisma.service';
import { ZendeskOAuthResponse } from '../../types';
import {
Action,
NotUniqueRecord,
handleServiceError,
} from '@@core/utils/errors';
import { Action, handleServiceError } from '@@core/utils/errors';
import { LoggerService } from '@@core/logger/logger.service';
import { v4 as uuidv4 } from 'uuid';
import { decrypt, encrypt } from '@@core/utils/crypto';
import { EnvironmentService } from '@@core/environment/environment.service';
import { EncryptionService } from '@@core/encryption/encryption.service';

@Injectable()
export class ZendeskConnectionService {
constructor(private prisma: PrismaService, private logger: LoggerService) {
constructor(
private prisma: PrismaService,
private logger: LoggerService,
private env: EnvironmentService,
private cryptoService: EncryptionService,
) {
this.logger.setContext(ZendeskConnectionService.name);
}
async handleZendeskCallback(
Expand All @@ -31,7 +32,7 @@ export class ZendeskConnectionService {
});

//reconstruct the redirect URI that was passed in the frontend it must be the same
const REDIRECT_URI = `${config.OAUTH_REDIRECT_BASE}/connections/oauth/callback`;
const REDIRECT_URI = `${this.env.getOAuthRredirectBaseUrl()}/connections/oauth/callback`;

const formData = new URLSearchParams({
grant_type: 'authorization_code',
Expand All @@ -45,7 +46,9 @@ export class ZendeskConnectionService {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic ${Buffer.from(
`${config.ZENDESK_CLIENT_ID}:${config.ZENDESK_CLIENT_SECRET}`,
`${this.env.getZendeskSecret().CLIENT_ID}:${
this.env.getZendeskSecret().CLIENT_SECRET
}`,
).toString('base64')}`,
},
},
Expand All @@ -61,8 +64,10 @@ export class ZendeskConnectionService {
id_connection: uuidv4(),
provider_slug: 'zendesk',
token_type: 'oauth',
access_token: encrypt(data.access_token),
refresh_token: data.refresh_token ? encrypt(data.refresh_token) : '',
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: data.refresh_token
? this.cryptoService.encrypt(data.refresh_token)
: '',
expiration_timestamp: data.expires_in
? new Date(new Date().getTime() + data.expires_in * 1000)
: new Date(),
Expand All @@ -76,8 +81,10 @@ export class ZendeskConnectionService {
},
},
update: {
access_token: encrypt(data.access_token),
refresh_token: data.refresh_token ? encrypt(data.refresh_token) : '',
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: data.refresh_token
? this.cryptoService.encrypt(data.refresh_token)
: '',
expiration_timestamp: data.expires_in
? new Date(new Date().getTime() + data.expires_in * 1000)
: new Date(),
Expand All @@ -94,7 +101,7 @@ export class ZendeskConnectionService {
try {
const formData = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: decrypt(refresh_token),
refresh_token: this.cryptoService.decrypt(refresh_token),
});
const res = await axios.post(
'https://api.getbase.com/oauth2/token',
Expand All @@ -103,7 +110,9 @@ export class ZendeskConnectionService {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic ${Buffer.from(
`${config.ZENDESK_CLIENT_ID}:${config.ZENDESK_CLIENT_SECRET}`,
`${this.env.getZendeskSecret().CLIENT_ID}:${
this.env.getZendeskSecret().CLIENT_SECRET
}`,
).toString('base64')}`,
},
},
Expand All @@ -114,8 +123,8 @@ export class ZendeskConnectionService {
id_connection: connectionId,
},
data: {
access_token: encrypt(data.access_token),
refresh_token: encrypt(data.refresh_token),
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + data.expires_in * 1000,
),
Expand Down
Loading

0 comments on commit 28724be

Please sign in to comment.