mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-28 11:30:04 +00:00
Better error handling on oauth
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import { Controller, Get, Redirect, Req, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { Request } from 'express';
|
||||
import { ConnectionClientService } from '../connection/client.service';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import DadosferaLogger from '@dadosfera/dadosfera-logs/dist';
|
||||
|
||||
@ApiTags('oauth')
|
||||
@Controller('oauth')
|
||||
export class OauthController {
|
||||
frontendRedirectUri = '';
|
||||
redirectUrl: URL;
|
||||
|
||||
constructor(
|
||||
private connectionService: ConnectionClientService,
|
||||
private logger: DadosferaLogger,
|
||||
@@ -17,13 +16,19 @@ export class OauthController {
|
||||
switch (process.env.ENV) {
|
||||
case 'dev':
|
||||
case 'stg':
|
||||
this.frontendRedirectUri = `https://app.${process.env.ENV}.dadosfera.ai/coletix/auth-callback`;
|
||||
this.redirectUrl = new URL(
|
||||
`https://app.${process.env.ENV}.dadosfera.ai/coletix/auth-callback`,
|
||||
);
|
||||
break;
|
||||
case 'prd':
|
||||
this.frontendRedirectUri = `https://app.dadosfera.ai/coletix/auth-callback`;
|
||||
this.redirectUrl = new URL(
|
||||
`https://app.dadosfera.ai/coletix/auth-callback`,
|
||||
);
|
||||
break;
|
||||
default:
|
||||
this.frontendRedirectUri = `http://localhost:4200/coletix/auth-callback`;
|
||||
this.redirectUrl = new URL(
|
||||
`http://localhost:4200/coletix/auth-callback`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@Get('hubspot')
|
||||
@@ -104,20 +109,30 @@ export class OauthController {
|
||||
return await this.callback(req);
|
||||
}
|
||||
|
||||
async callback(@Req() req: Request) {
|
||||
async callback(@Req() req) {
|
||||
const { error, state } = req.query;
|
||||
|
||||
if (error)
|
||||
return {
|
||||
url: `${this.frontendRedirectUri}?error=${error}`,
|
||||
};
|
||||
const request: any = req;
|
||||
const { authInfo } = request;
|
||||
if (error) {
|
||||
this.logger.error(error);
|
||||
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
|
||||
this.redirectUrl.searchParams.set(
|
||||
'error_description',
|
||||
`Credenciais inválidas. Erro: [${error}]`,
|
||||
);
|
||||
return { url: this.redirectUrl.href };
|
||||
}
|
||||
|
||||
if (!authInfo)
|
||||
return {
|
||||
url: `${this.frontendRedirectUri}?error=invalid_credentials`,
|
||||
};
|
||||
const { authInfo } = req;
|
||||
|
||||
if (!authInfo) {
|
||||
this.logger.error('No authInfo', req);
|
||||
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
|
||||
this.redirectUrl.searchParams.set(
|
||||
'error_description',
|
||||
`Credenciais inválidas.`,
|
||||
);
|
||||
return { url: this.redirectUrl.href };
|
||||
}
|
||||
|
||||
const { accessToken: access_token, refreshToken: refresh_token } = authInfo;
|
||||
|
||||
@@ -125,9 +140,13 @@ export class OauthController {
|
||||
try {
|
||||
connectionInfo = jwt.verify(state as string, process.env.JWT_PRIVATE_KEY);
|
||||
} catch (error) {
|
||||
return {
|
||||
url: `${this.frontendRedirectUri}?error=invalid_state`,
|
||||
};
|
||||
this.logger.error('Invalid state JWT', { state, req, error });
|
||||
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
|
||||
this.redirectUrl.searchParams.set(
|
||||
'error_description',
|
||||
'Ocorreu um erro ao autorizá-lo. Por favor tente novamente',
|
||||
);
|
||||
return { url: this.redirectUrl.href };
|
||||
}
|
||||
|
||||
connectionInfo.properties.access_token = access_token;
|
||||
@@ -140,14 +159,19 @@ export class OauthController {
|
||||
connectionInfo,
|
||||
{ customer_id, customer_name, user_id },
|
||||
);
|
||||
return {
|
||||
url: `${this.frontendRedirectUri}?connection_id=${response.connection.id}`,
|
||||
};
|
||||
this.redirectUrl.searchParams.set(
|
||||
'connection_id',
|
||||
response.connection.id,
|
||||
);
|
||||
return { url: this.redirectUrl.href };
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
return {
|
||||
url: `${this.frontendRedirectUri}?error=internal`,
|
||||
};
|
||||
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar');
|
||||
this.redirectUrl.searchParams.set(
|
||||
'error_description',
|
||||
'Ocorreu um erro interno ao autorizá-lo. Por favor tente novamente mais tarde',
|
||||
);
|
||||
return { url: this.redirectUrl.href };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user