Skip to content

Commit

Permalink
✨ New providers oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Apr 12, 2024
1 parent 0699ffb commit 2edc145
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 105 deletions.
10 changes: 6 additions & 4 deletions packages/api/scripts/oauthConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type ${providerUpper}OAuthResponse = {
access_token: string;
refresh_token: string;
expires_in: string;
token_type: string;
};
@Injectable()
Expand Down Expand Up @@ -166,14 +167,15 @@ export class ${providerUpper}ConnectionService implements I${verticalUpper}Conne
async handleTokenRefresh(opts: RefreshParams) {
try {
const { connectionId, refreshToken, projectId } = opts;
const formData = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
});
const CREDENTIALS = (await this.cService.getCredentials(
projectId,
this.type,
)) as OAuth2AuthData;
const formData = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
});
const res = await axios.post(
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import {
IAccountingConnectionService,
} from '../../types';
import { ServiceRegistry } from '../registry.service';
import { AuthStrategy } from '@panora/shared';
import { AuthStrategy, providersConfig } from '@panora/shared';
import { OAuth2AuthData, providerToType } from '@panora/shared';
import { ConnectionsStrategiesService } from '@@core/connections-strategies/connections-strategies.service';

export type FreeagentOAuthResponse = {
access_token: string;
refresh_token: string;
expires_at: string;
expires_in: string | number;
refresh_token_expires_in: number;
token_type: string;
};

@Injectable()
Expand Down Expand Up @@ -60,18 +62,22 @@ export class FreeagentConnectionService
)) as OAuth2AuthData;

const formData = new URLSearchParams({
client_id: CREDENTIALS.CLIENT_ID,
client_secret: CREDENTIALS.CLIENT_SECRET,
redirect_uri: REDIRECT_URI,
code: code,
grant_type: 'authorization_code',
});
const subdomain = 'panora';
const res = await axios.post('', formData.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
const res = await axios.post(
'https://api.freeagent.com/v2/token_endpoint',
formData.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic ${Buffer.from(
`${CREDENTIALS.CLIENT_ID}:${CREDENTIALS.CLIENT_SECRET}`,
).toString('base64')}`,
},
},
});
);
const data: FreeagentOAuthResponse = res.data;
this.logger.log(
'OAuth credentials : freeagent ticketing ' + JSON.stringify(data),
Expand All @@ -88,9 +94,9 @@ export class FreeagentConnectionService
data: {
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
account_url: '',
account_url: providersConfig['accounting']['freshagent'].apiUrl,
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
status: 'valid',
created_at: new Date(),
Expand All @@ -104,11 +110,11 @@ export class FreeagentConnectionService
provider_slug: 'freeagent',
vertical: 'accounting',
token_type: 'oauth',
account_url: '',
account_url: providersConfig['accounting']['freshagent'].apiUrl,
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
status: 'valid',
created_at: new Date(),
Expand Down Expand Up @@ -139,12 +145,18 @@ export class FreeagentConnectionService
this.type,
)) as OAuth2AuthData;

const res = await axios.post('', formData.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic Q1JFREVOVElBTFMuQ0xJRU5UX0lEfTokewogICAgICAgICAgICAgICAgICBDUkVERU5USUFMUy5DTElFTlRfU0VDUkVUCiAgICAgICAgICAgICAgfQ==`,
const res = await axios.post(
'https://api.freeagent.com/v2/token_endpoint',
formData.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic ${Buffer.from(
`${CREDENTIALS.CLIENT_ID}:${CREDENTIALS.CLIENT_SECRET}`,
).toString('base64')}`,
},
},
});
);
const data: FreeagentOAuthResponse = res.data;
await this.prisma.connections.update({
where: {
Expand All @@ -154,7 +166,7 @@ export class FreeagentConnectionService
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import {
IAccountingConnectionService,
} from '../../types';
import { ServiceRegistry } from '../registry.service';
import { AuthStrategy } from '@panora/shared';
import { AuthStrategy, providersConfig } from '@panora/shared';
import { OAuth2AuthData, providerToType } from '@panora/shared';
import { ConnectionsStrategiesService } from '@@core/connections-strategies/connections-strategies.service';

export type FreshbooksOAuthResponse = {
access_token: string;
refresh_token: string;
expires_at: string;
expires_in: string;
created_at: number;
token_type: string;
};

@Injectable()
Expand Down Expand Up @@ -66,12 +68,15 @@ export class FreshbooksConnectionService
code: code,
grant_type: 'authorization_code',
});
const subdomain = 'panora';
const res = await axios.post('', formData.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
const res = await axios.post(
'https://api.freshbooks.com/auth/oauth/token',
formData.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
},
},
});
);
const data: FreshbooksOAuthResponse = res.data;
this.logger.log(
'OAuth credentials : freshbooks ticketing ' + JSON.stringify(data),
Expand All @@ -88,9 +93,9 @@ export class FreshbooksConnectionService
data: {
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
account_url: '',
account_url: providersConfig['accounting']['freshbooks'].apiUrl,
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
status: 'valid',
created_at: new Date(),
Expand All @@ -104,11 +109,11 @@ export class FreshbooksConnectionService
provider_slug: 'freshbooks',
vertical: 'accounting',
token_type: 'oauth',
account_url: '',
account_url: providersConfig['accounting']['freshbooks'].apiUrl,
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
status: 'valid',
created_at: new Date(),
Expand All @@ -135,21 +140,28 @@ export class FreshbooksConnectionService
async handleTokenRefresh(opts: RefreshParams) {
try {
const { connectionId, refreshToken, projectId } = opts;
const formData = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
});

const CREDENTIALS = (await this.cService.getCredentials(
projectId,
this.type,
)) as OAuth2AuthData;

const res = await axios.post('', formData.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic Q1JFREVOVElBTFMuQ0xJRU5UX0lEfTokewogICAgICAgICAgICAgICAgICBDUkVERU5USUFMUy5DTElFTlRfU0VDUkVUCiAgICAgICAgICAgICAgfQ==`,
},
const formData = new URLSearchParams({
client_id: CREDENTIALS.CLIENT_ID,
client_secret: CREDENTIALS.CLIENT_SECRET,
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
});

const res = await axios.post(
'https://api.freshbooks.com/auth/oauth/token',
formData.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
},
},
);
const data: FreshbooksOAuthResponse = res.data;
await this.prisma.connections.update({
where: {
Expand All @@ -159,7 +171,7 @@ export class FreshbooksConnectionService
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import {
IAccountingConnectionService,
} from '../../types';
import { ServiceRegistry } from '../registry.service';
import { AuthStrategy } from '@panora/shared';
import { AuthStrategy, providersConfig } from '@panora/shared';
import { OAuth2AuthData, providerToType } from '@panora/shared';
import { ConnectionsStrategiesService } from '@@core/connections-strategies/connections-strategies.service';

export type MoneybirdOAuthResponse = {
access_token: string;
refresh_token: string;
expires_at: string;
expires_in: string;
token_type: string;
scope: string;
created_at: number;
};

@Injectable()
Expand Down Expand Up @@ -66,12 +69,15 @@ export class MoneybirdConnectionService
code: code,
grant_type: 'authorization_code',
});
const subdomain = 'panora';
const res = await axios.post('', formData.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
const res = await axios.post(
' https://moneybird.com/oauth/token',
formData.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
},
},
});
);
const data: MoneybirdOAuthResponse = res.data;
this.logger.log(
'OAuth credentials : moneybird ticketing ' + JSON.stringify(data),
Expand All @@ -88,9 +94,9 @@ export class MoneybirdConnectionService
data: {
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
account_url: '',
account_url: providersConfig['accounting']['moneybird'].apiUrl,
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
status: 'valid',
created_at: new Date(),
Expand All @@ -104,11 +110,11 @@ export class MoneybirdConnectionService
provider_slug: 'moneybird',
vertical: 'accounting',
token_type: 'oauth',
account_url: '',
account_url: providersConfig['accounting']['moneybird'].apiUrl,
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
status: 'valid',
created_at: new Date(),
Expand All @@ -130,21 +136,25 @@ export class MoneybirdConnectionService
async handleTokenRefresh(opts: RefreshParams) {
try {
const { connectionId, refreshToken, projectId } = opts;
const formData = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
});
const CREDENTIALS = (await this.cService.getCredentials(
projectId,
this.type,
)) as OAuth2AuthData;

const res = await axios.post('', formData.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic Q1JFREVOVElBTFMuQ0xJRU5UX0lEfTokewogICAgICAgICAgICAgICAgICBDUkVERU5USUFMUy5DTElFTlRfU0VDUkVUCiAgICAgICAgICAgICAgfQ==`,
},
const formData = new URLSearchParams({
client_id: CREDENTIALS.CLIENT_ID,
client_SECRET: CREDENTIALS.CLIENT_SECRET,
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
});
const res = await axios.post(
'https://moneybird.com/oauth/token',
formData.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
},
},
);
const data: MoneybirdOAuthResponse = res.data;
await this.prisma.connections.update({
where: {
Expand All @@ -154,7 +164,7 @@ export class MoneybirdConnectionService
access_token: this.cryptoService.encrypt(data.access_token),
refresh_token: this.cryptoService.encrypt(data.refresh_token),
expiration_timestamp: new Date(
new Date().getTime() + Number(data.expires_at) * 1000,
new Date().getTime() + Number(data.expires_in) * 1000,
),
},
});
Expand Down
Loading

0 comments on commit 2edc145

Please sign in to comment.