Merge branch 'fix/oauth-generic' into feature/passport-oauth

This commit is contained in:
Gabriel Rosa
2022-06-23 11:43:43 -03:00
7 changed files with 1704 additions and 61 deletions
+3 -3
View File
@@ -134,7 +134,7 @@ jobs:
eb deploy
- name: Remove Docker's Trash
continue-on-error: true
if: always()
run: |
docker system prune
docker rmi -f $(docker images -aq)
docker system prune --volumes -a -f
docker system df
+1607 -7
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -24,6 +24,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.112.0",
"@grpc/grpc-js": "^1.5.10",
"@grpc/proto-loader": "^0.6.9",
"@nestjs/common": "^8.4.3",
@@ -35,7 +36,7 @@
"@nestjs/platform-express": "^8.4.3",
"@nestjs/schedule": "^1.1.0",
"@nestjs/swagger": "^5.2.1",
"@victorradael/protospack": "2.5.0-6",
"@victorradael/protospack": "2.5.0",
"axios": "^0.25.0",
"cron-parser": "^4.4.0",
"dotenv": "^14.2.0",
+56 -48
View File
@@ -14,7 +14,7 @@ import {
TestConnectionGetColumnsRequest,
TestConnectionRequest,
} from '@victorradael/protospack';
import axios from 'axios';
import axios, { AxiosRequestConfig } from 'axios';
import { lastValueFrom } from 'rxjs';
import { InputModel } from 'src/modules/inputs/dtos/input.model';
import {
@@ -22,17 +22,23 @@ import {
objectSnakeToCamel,
} from 'src/utils/CaseConverter';
import { mustache } from 'src/utils/mustache';
import { OauthScrets } from 'src/utils/OauthSecrets';
import { URLSearchParams } from 'url';
import { IIdRequest, UpdateInputRequest } from './interfaces';
export class InputsClientService implements OnModuleInit {
private inputService: InputService;
oauthSecrets: OauthScrets;
constructor(
@Inject('INPUTS_PACKAGE') private readonly grpcClient: ClientGrpc,
) {}
onModuleInit() {
async onModuleInit() {
this.inputService =
this.grpcClient.getService<InputService>('InputService');
this.oauthSecrets = new OauthScrets();
await this.oauthSecrets.init();
console.log(this.oauthSecrets);
}
async newCreate(createInputDto: InputNewCreateRequest) {
@@ -201,64 +207,66 @@ export class InputsClientService implements OnModuleInit {
switch (plugin) {
case 'hubspot':
return {
client_id: process.env.HUBSPOT_CLIENT_ID || '',
client_secret: process.env.HUBSPOT_CLIENT_SECRET || '',
client_id: this.oauthSecrets.hubspot.client_id || '',
client_secret: this.oauthSecrets.hubspot.client_secret || '',
};
}
return {};
}
async getInputTokens(input: InputModel) {
const { credentials, options } = input;
const credentialsTokens: any = {};
const { credentials } = input;
let credentialsTokens = {};
switch (credentials.connection_type) {
case 'oauth':
const { oauth } = options;
const { get_tokens_url_params, get_tokens_set_response } = oauth;
const secrets = await this.getAuthSecrets(input.plugin);
if (oauth.content_type === 'application/x-www-form-urlencoded') {
const requestParams = new URLSearchParams();
const get_tokens_url_params_string = mustache(get_tokens_url_params, {
...secrets,
...credentials,
});
let get_tokens_url_params_obj: Record<string, any> = {};
try {
get_tokens_url_params_obj = JSON.parse(
get_tokens_url_params_string,
);
} catch (error) {
throw new HttpException(
'Erro transformando get_tokens_url_params',
400,
);
}
const { redirect_uri } = get_tokens_url_params_obj;
for (const key in get_tokens_url_params_obj) {
const value = get_tokens_url_params_obj[key];
requestParams.append(key, value);
}
const { data } = await axios
.post(options.oauth.get_tokens_url, requestParams)
.catch((err) => {
console.log(err.response.data);
throw new HttpException(
err.response.data.message,
err.response.status,
);
});
for (const key in get_tokens_set_response) {
const responseKey = get_tokens_set_response[key];
credentialsTokens[key] = data[responseKey];
}
credentialsTokens.redirect_uri = redirect_uri;
}
credentialsTokens = await this.getOauthTokens(input);
break;
}
return { ...credentials, ...credentialsTokens };
}
// getOauthTokens(input){
async getOauthTokens(input: InputModel) {
const tokens: Record<string, any> = {};
const { options, credentials } = input;
const { oauth } = options;
const { get_tokens_url_params, get_tokens_set_response } = oauth;
const secrets = await this.getAuthSecrets(input.plugin);
const get_tokens_url_params_string = mustache(get_tokens_url_params, {
...secrets,
...credentials,
});
let get_tokens_url_params_obj: Record<string, any> = {};
try {
get_tokens_url_params_obj = JSON.parse(get_tokens_url_params_string);
} catch (error) {
throw new HttpException('Erro transformando get_tokens_url_params', 400);
}
let params = {};
switch (oauth.content_type) {
case 'application/x-www-form-urlencoded':
params = new URLSearchParams(get_tokens_url_params_obj);
params = params.toString();
break;
default:
break;
}
const axiosRequestConfig: AxiosRequestConfig = {
url: oauth.get_tokens_url,
method: oauth.get_tokens_method || 'POST',
data: params,
headers: { 'content-type': oauth.content_type || 'application/json' },
};
// }
const { data } = await axios(axiosRequestConfig).catch((err) => {
console.log(err.response.data);
throw new HttpException(err.response.data.message, err.response.status);
});
for (const key in get_tokens_set_response) {
const responseKey = get_tokens_set_response[key];
tokens[key] = data[responseKey];
}
const { redirect_uri } = get_tokens_url_params_obj;
tokens.redirect_uri = redirect_uri;
return tokens;
}
async getAvailableEntities(data) {
console.log('InputClientService', 'GetAvailableEntities');
+5 -2
View File
@@ -1,4 +1,4 @@
import { Inject, OnModuleInit } from '@nestjs/common';
import { HttpException, Inject, OnModuleInit } from '@nestjs/common';
import { ClientGrpc, Payload } from '@nestjs/microservices';
import {
PipelineServicesNames,
@@ -49,7 +49,10 @@ export class PipelinesClientService implements OnModuleInit {
})
.then((res) => res)
.catch((err) => {
throw new Error(err);
if (err.details == 'Invalid Request') {
throw new HttpException(err.details, 400);
}
throw new HttpException(err.details, 500);
});
return createPipelineResponse;
+2
View File
@@ -1,4 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Method } from 'axios';
export class InputModel {
category: string;
@@ -14,6 +15,7 @@ export class InputModel {
options?: {
oauth?: {
get_tokens_url: string;
get_tokens_method?: Method;
get_tokens_url_params: string;
get_tokens_set_response: Record<string, string>;
content_type: string;
+29
View File
@@ -0,0 +1,29 @@
import {
SecretsManagerClient,
GetSecretValueCommand,
} from '@aws-sdk/client-secrets-manager';
class OauthSecretsObject {
client_id = '';
client_secret = '';
redirect_uri = '';
}
export class OauthScrets {
hubspot = new OauthSecretsObject();
google = new OauthSecretsObject();
mailchimp = new OauthSecretsObject();
async init() {
const path = process.env.SM_OAUTH_PATH;
const secretsManagerClient = new SecretsManagerClient({});
for (const key in this) {
const getSecretComand = new GetSecretValueCommand({
SecretId: `${path}/${key}`,
});
const res = await secretsManagerClient
.send(getSecretComand)
.catch(() => null);
if (res) this[key] = JSON.parse(res.SecretString);
}
}
}