FIX: google strategy custom scopes

This commit is contained in:
Gabriel Rosa
2022-06-27 14:02:51 -03:00
parent 06a0ed3299
commit 2f5d940b46
4 changed files with 55 additions and 30 deletions
+2 -2
View File
@@ -32,10 +32,10 @@ import { CatalogService } from './modules/catalog/catalog.service';
import { OauthController } from './modules/oauth/oauth.controller';
import { getOauthSecrets } from './utils/OauthSecrets';
import { HubspotStrategy } from './modules/oauth/passport-strategies/hubspot-strategy';
import { GoogleStrategy } from './modules/oauth/passport-strategies/google-strategy';
import { FacebookStrategy } from './modules/oauth/passport-strategies/facebook-strategy';
import { AuthenticationGuard } from './authentication/authentication.guard';
import { APP_GUARD } from '@nestjs/core';
import { GoogleStrategy } from './modules/oauth/passport-strategies/google-strategy';
const ducClient = new DucClient();
const inputClient = new InputsClientConfiguration();
@@ -74,8 +74,8 @@ const transformationClient = new TransformationsClientConfiguration();
},
//OAUTH Passport Strategies
HubspotStrategy,
GoogleStrategy,
FacebookStrategy,
GoogleStrategy,
],
imports: [
ConfigModule.forRoot({
+39 -14
View File
@@ -1,11 +1,4 @@
import {
BadRequestException,
Controller,
Get,
Redirect,
Req,
UseGuards,
} from '@nestjs/common';
import { Controller, Get, Redirect, Req, UseGuards } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { InputsClientService } from 'src/clients/inputs/client.service';
@@ -54,7 +47,32 @@ export class OauthController {
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleCallback(@Req() req) {
return await this.callback(req, 'google');
return await this.callback(req, 'googleads');
}
@Get('googleads')
@UseGuards(AuthGuard('google'))
async oauthGoogleAds() {
return true;
}
@Get('googleads/callback')
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleAdsCallback(@Req() req) {
return await this.callback(req, 'googleads');
}
@Get('google-sheets')
@UseGuards(AuthGuard('google'))
async oauthGoogleSheets() {
return true;
}
@Get('google-sheets/callback')
@UseGuards(AuthGuard('google'))
@Redirect()
async oauthGoogleSheetsCallback(@Req() req) {
return await this.callback(req, 'google-sheets');
}
@Get('facebook')
@@ -70,15 +88,22 @@ export class OauthController {
return await this.callback(req, 'facebook');
}
async callback(@Req() req, plugin) {
const { authInfo } = req;
async callback(@Req() req: Request, plugin) {
const { error, state: customer_id } = req.query;
const request: Request = req;
if (error)
return {
url: `${this.frontendRedirectUri}?error=${error}`,
};
const request: any = req;
const { authInfo } = request;
if (!authInfo) throw new BadRequestException('Oauth tokens not found');
if (!authInfo)
return {
url: `${this.frontendRedirectUri}?error=invalid_credentials`,
};
const { accessToken: access_token, refreshToken: refresh_token } = authInfo;
const { state: customer_id } = request.query;
const response = await this.inputService.create({
info: { customer_id, customer: 'customer', user_id: 'empty' },
@@ -1,6 +1,6 @@
import { Profile, Strategy, StrategyOptions } from 'passport-google-oauth20';
import { PassportStrategy } from '@nestjs/passport';
import { Inject, Injectable, Req } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { OauthSecrets } from 'src/utils/OauthSecrets';
@Injectable()
@@ -12,12 +12,7 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
const options: StrategyOptions = {
clientID: oauthSecrets.google.client_id,
clientSecret: oauthSecrets.google.client_secret,
scope: [
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/userinfo.email',
],
callbackURL: oauthSecrets.google.redirect_uri,
callbackURL: '/oauth/googleads/callback',
};
const verify = (
accessToken: string,
@@ -32,16 +27,21 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
super(options, verify);
}
authenticate(@Req() req, options) {
authenticate(req, options) {
const path: string = req.route.path;
const { customer_id } = req.query;
const scope: string[] = ['https://www.googleapis.com/auth/userinfo.email'];
if (path.includes('googleads'))
scope.push('https://www.googleapis.com/auth/adwords');
if (path.includes('google-sheets'))
scope.push(
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/spreadsheets.readonly',
);
options.scope = scope;
options.state = customer_id;
options.accessType = 'offline';
options.prompt = 'consent';
super.authenticate(req, options);
}
async validate(...args) {
console.log('validate');
console.log(args);
}
}
+1 -1
View File
File diff suppressed because one or more lines are too long