FIX: salesforce oauth tye

This commit is contained in:
Gabriel Rosa
2022-09-08 15:11:54 -03:00
parent 35c0520750
commit 311a4e4b93
3 changed files with 21 additions and 32 deletions
+19 -30
View File
@@ -8,7 +8,7 @@ import { PackTheMetadata } from 'src/utils/ PackTheMetadata';
@ApiTags('oauth') @ApiTags('oauth')
@Controller('oauth') @Controller('oauth')
export class OauthController { export class OauthController {
redirectUrl: URL; redirectUrl: string;
constructor( constructor(
private connectionService: ConnectionClientService, private connectionService: ConnectionClientService,
@@ -17,19 +17,13 @@ export class OauthController {
switch (process.env.ENV) { switch (process.env.ENV) {
case 'dev': case 'dev':
case 'stg': case 'stg':
this.redirectUrl = new URL( this.redirectUrl = `https://app.${process.env.ENV}.dadosfera.ai/coletix/auth-callback`;
`https://app.${process.env.ENV}.dadosfera.ai/coletix/auth-callback`,
);
break; break;
case 'prd': case 'prd':
this.redirectUrl = new URL( this.redirectUrl = `https://app.dadosfera.ai/coletix/auth-callback`;
`https://app.dadosfera.ai/coletix/auth-callback`,
);
break; break;
default: default:
this.redirectUrl = new URL( this.redirectUrl = `http://localhost:4200/coletix/auth-callback`;
`http://localhost:4200/coletix/auth-callback`,
);
} }
} }
@Get('hubspot') @Get('hubspot')
@@ -112,27 +106,25 @@ export class OauthController {
async callback(@Req() req) { async callback(@Req() req) {
const { error, state } = req.query; const { error, state } = req.query;
const url = new URL(this.redirectUrl);
if (error) { if (error) {
this.logger.error(error); this.logger.error(error);
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar'); url.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set( url.searchParams.set(
'error_description', 'error_description',
`Credenciais inválidas. Erro: [${error}]`, `Credenciais inválidas. Erro: [${error}]`,
); );
return { url: this.redirectUrl.href }; return { url: url.href };
} }
const { authInfo } = req; const { authInfo } = req;
if (!authInfo) { if (!authInfo) {
this.logger.error('No authInfo', req); this.logger.error('No authInfo', req);
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar'); url.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set( url.searchParams.set('error_description', `Credenciais inválidas.`);
'error_description', return { url: url.href };
`Credenciais inválidas.`,
);
return { url: this.redirectUrl.href };
} }
const { accessToken: access_token, refreshToken: refresh_token } = authInfo; const { accessToken: access_token, refreshToken: refresh_token } = authInfo;
@@ -142,12 +134,12 @@ export class OauthController {
connectionInfo = jwt.verify(state as string, process.env.JWT_PRIVATE_KEY); connectionInfo = jwt.verify(state as string, process.env.JWT_PRIVATE_KEY);
} catch (error) { } catch (error) {
this.logger.error('Invalid state JWT', { state, req, error }); this.logger.error('Invalid state JWT', { state, req, error });
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar'); url.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set( url.searchParams.set(
'error_description', 'error_description',
'Ocorreu um erro ao autorizá-lo. Por favor tente novamente', 'Ocorreu um erro ao autorizá-lo. Por favor tente novamente',
); );
return { url: this.redirectUrl.href }; return { url: url.href };
} }
connectionInfo.properties.access_token = access_token; connectionInfo.properties.access_token = access_token;
@@ -160,19 +152,16 @@ export class OauthController {
connectionInfo, connectionInfo,
PackTheMetadata({ customer_id, customer_name, user_id }), PackTheMetadata({ customer_id, customer_name, user_id }),
); );
this.redirectUrl.searchParams.set( url.searchParams.set('connection_id', response.connection.id);
'connection_id', return { url: url.href };
response.connection.id,
);
return { url: this.redirectUrl.href };
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
this.redirectUrl.searchParams.set('error', 'Erro ao autorizar'); url.searchParams.set('error', 'Erro ao autorizar');
this.redirectUrl.searchParams.set( url.searchParams.set(
'error_description', 'error_description',
'Ocorreu um erro interno ao autorizá-lo. Por favor tente novamente mais tarde', 'Ocorreu um erro interno ao autorizá-lo. Por favor tente novamente mais tarde',
); );
return { url: this.redirectUrl.href }; return { url: url.href };
} }
} }
} }
@@ -45,7 +45,7 @@ export class SalesforceStrategy extends PassportStrategy(
options.state = this.oauthService.generateStateFromRequest( options.state = this.oauthService.generateStateFromRequest(
options.state, options.state,
req, req,
'file', 'application',
); );
super.authenticate(req, options); super.authenticate(req, options);
} }
+1 -1
View File
File diff suppressed because one or more lines are too long