FIX: oauth conditionally setting state

This commit is contained in:
Gabriel Rosa
2022-09-08 13:44:44 -03:00
parent 8c6ec8b1b9
commit 35c0520750
7 changed files with 26 additions and 9 deletions
+5 -4
View File
@@ -7,10 +7,11 @@ class PartialConnection extends PartialType(ConnectionDto) {}
@Injectable()
export class OauthService {
generateStateFromRequest(req, type): string {
const plugin: string = req.route.path
.replace('/oauth/', '')
.replace('/callback', '');
generateStateFromRequest(prevState, req, type): string {
if (req.route.path.includes('callback')) {
return prevState;
}
const plugin: string = req.route.path.replace('/oauth/', '');
const { query } = req;
const {
@@ -10,12 +10,14 @@ import { OauthSecrets } from 'src/utils/OauthSecrets';
import { Request } from 'express';
import { ParamsDictionary } from 'express-serve-static-core';
import { ParsedQs } from 'qs';
import { OauthService } from '../oauth.service';
@Injectable()
export class FacebookStrategy extends PassportStrategy(Strategy) {
constructor(
@Inject('OAUTH_SECRETS')
private readonly oauthSecrets: OauthSecrets,
private oauthService: OauthService,
) {
const options: StrategyOption = {
clientID: oauthSecrets.facebook.client_id,
@@ -37,8 +39,11 @@ export class FacebookStrategy extends PassportStrategy(Strategy) {
req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>,
options?: any,
): void {
const { customer_id } = req.query;
options.state = customer_id;
options.state = this.oauthService.generateStateFromRequest(
options.state,
req,
'application',
);
options.scope = 'ads_read ads_management';
super.authenticate(req, options);
}
@@ -52,13 +52,18 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
switch (plugin) {
case 'googleads':
options.state = this.oauthService.generateStateFromRequest(
options.state,
req,
'application',
);
options.scope.push('https://www.googleapis.com/auth/adwords');
break;
case 'google-sheets':
options.state = this.oauthService.generateStateFromRequest(req, 'file');
options.state = this.oauthService.generateStateFromRequest(
options.state,
req,
'file',
);
options.scope.push(
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/spreadsheets.readonly',
@@ -28,6 +28,7 @@ export class HubspotStrategy extends PassportStrategy(Strategy) {
authenticate(req, options) {
options.state = this.oauthService.generateStateFromRequest(
options.state,
req,
'application',
);
@@ -32,6 +32,7 @@ export class MailChimpStrategy extends PassportStrategy(Strategy, 'mailchimp') {
authenticate(@Req() req, options) {
if (!req.route.path.includes('callback'))
options.state = this.oauthService.generateStateFromRequest(
options.state,
req,
'application',
);
@@ -42,7 +42,11 @@ export class SalesforceStrategy extends PassportStrategy(
}
authenticate(req, options) {
options.state = this.oauthService.generateStateFromRequest(req, 'file');
options.state = this.oauthService.generateStateFromRequest(
options.state,
req,
'file',
);
super.authenticate(req, options);
}
}