mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-19 01:59:07 +00:00
FIX: google strategy custom scopes
This commit is contained in:
+2
-2
@@ -32,10 +32,10 @@ import { CatalogService } from './modules/catalog/catalog.service';
|
|||||||
import { OauthController } from './modules/oauth/oauth.controller';
|
import { OauthController } from './modules/oauth/oauth.controller';
|
||||||
import { getOauthSecrets } from './utils/OauthSecrets';
|
import { getOauthSecrets } from './utils/OauthSecrets';
|
||||||
import { HubspotStrategy } from './modules/oauth/passport-strategies/hubspot-strategy';
|
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 { FacebookStrategy } from './modules/oauth/passport-strategies/facebook-strategy';
|
||||||
import { AuthenticationGuard } from './authentication/authentication.guard';
|
import { AuthenticationGuard } from './authentication/authentication.guard';
|
||||||
import { APP_GUARD } from '@nestjs/core';
|
import { APP_GUARD } from '@nestjs/core';
|
||||||
|
import { GoogleStrategy } from './modules/oauth/passport-strategies/google-strategy';
|
||||||
|
|
||||||
const ducClient = new DucClient();
|
const ducClient = new DucClient();
|
||||||
const inputClient = new InputsClientConfiguration();
|
const inputClient = new InputsClientConfiguration();
|
||||||
@@ -74,8 +74,8 @@ const transformationClient = new TransformationsClientConfiguration();
|
|||||||
},
|
},
|
||||||
//OAUTH Passport Strategies
|
//OAUTH Passport Strategies
|
||||||
HubspotStrategy,
|
HubspotStrategy,
|
||||||
GoogleStrategy,
|
|
||||||
FacebookStrategy,
|
FacebookStrategy,
|
||||||
|
GoogleStrategy,
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot({
|
ConfigModule.forRoot({
|
||||||
|
|||||||
@@ -1,11 +1,4 @@
|
|||||||
import {
|
import { Controller, Get, Redirect, Req, UseGuards } from '@nestjs/common';
|
||||||
BadRequestException,
|
|
||||||
Controller,
|
|
||||||
Get,
|
|
||||||
Redirect,
|
|
||||||
Req,
|
|
||||||
UseGuards,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { ApiTags } from '@nestjs/swagger';
|
import { ApiTags } from '@nestjs/swagger';
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
import { InputsClientService } from 'src/clients/inputs/client.service';
|
import { InputsClientService } from 'src/clients/inputs/client.service';
|
||||||
@@ -54,7 +47,32 @@ export class OauthController {
|
|||||||
@UseGuards(AuthGuard('google'))
|
@UseGuards(AuthGuard('google'))
|
||||||
@Redirect()
|
@Redirect()
|
||||||
async oauthGoogleCallback(@Req() req) {
|
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')
|
@Get('facebook')
|
||||||
@@ -70,15 +88,22 @@ export class OauthController {
|
|||||||
return await this.callback(req, 'facebook');
|
return await this.callback(req, 'facebook');
|
||||||
}
|
}
|
||||||
|
|
||||||
async callback(@Req() req, plugin) {
|
async callback(@Req() req: Request, plugin) {
|
||||||
const { authInfo } = req;
|
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 { accessToken: access_token, refreshToken: refresh_token } = authInfo;
|
||||||
const { state: customer_id } = request.query;
|
|
||||||
|
|
||||||
const response = await this.inputService.create({
|
const response = await this.inputService.create({
|
||||||
info: { customer_id, customer: 'customer', user_id: 'empty' },
|
info: { customer_id, customer: 'customer', user_id: 'empty' },
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Profile, Strategy, StrategyOptions } from 'passport-google-oauth20';
|
import { Profile, Strategy, StrategyOptions } from 'passport-google-oauth20';
|
||||||
import { PassportStrategy } from '@nestjs/passport';
|
import { PassportStrategy } from '@nestjs/passport';
|
||||||
import { Inject, Injectable, Req } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import { OauthSecrets } from 'src/utils/OauthSecrets';
|
import { OauthSecrets } from 'src/utils/OauthSecrets';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -12,12 +12,7 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
|
|||||||
const options: StrategyOptions = {
|
const options: StrategyOptions = {
|
||||||
clientID: oauthSecrets.google.client_id,
|
clientID: oauthSecrets.google.client_id,
|
||||||
clientSecret: oauthSecrets.google.client_secret,
|
clientSecret: oauthSecrets.google.client_secret,
|
||||||
scope: [
|
callbackURL: '/oauth/googleads/callback',
|
||||||
'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,
|
|
||||||
};
|
};
|
||||||
const verify = (
|
const verify = (
|
||||||
accessToken: string,
|
accessToken: string,
|
||||||
@@ -32,16 +27,21 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
|
|||||||
super(options, verify);
|
super(options, verify);
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticate(@Req() req, options) {
|
authenticate(req, options) {
|
||||||
|
const path: string = req.route.path;
|
||||||
const { customer_id } = req.query;
|
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.state = customer_id;
|
||||||
options.accessType = 'offline';
|
options.accessType = 'offline';
|
||||||
options.prompt = 'consent';
|
options.prompt = 'consent';
|
||||||
super.authenticate(req, options);
|
super.authenticate(req, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
async validate(...args) {
|
|
||||||
console.log('validate');
|
|
||||||
console.log(args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user