From bacb2cf3a8865f832636dae17373a59cde63d842 Mon Sep 17 00:00:00 2001 From: Raghunandan Date: Thu, 4 Jun 2020 15:12:50 +0200 Subject: [PATCH] review fixes --- .../implementations/auth/google/GoogleAuth.ts | 3 +- .../src/providers/PassportStrategyHelper.ts | 38 ++++++++++++------- .../src/providers/google/provider.ts | 9 ++++- plugins/auth-backend/src/providers/types.ts | 1 - 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts b/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts index d43741a9e8..d65a2c71ee 100644 --- a/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts @@ -24,6 +24,7 @@ import { AccessTokenOptions, ProfileInfoApi, ProfileInfoOptions, + ProfileInfo, } from '../../../definitions/auth'; import { OAuthRequestApi, AuthProvider } from '../../../definitions'; import { SessionManager } from '../../../../lib/AuthSessionManager/types'; @@ -41,7 +42,7 @@ type CreateOptions = { }; export type GoogleAuthResponse = { - profile: any; + profile: ProfileInfo; accessToken: string; idToken: string; scope: string; diff --git a/plugins/auth-backend/src/providers/PassportStrategyHelper.ts b/plugins/auth-backend/src/providers/PassportStrategyHelper.ts index d329210b8e..7b7e467281 100644 --- a/plugins/auth-backend/src/providers/PassportStrategyHelper.ts +++ b/plugins/auth-backend/src/providers/PassportStrategyHelper.ts @@ -135,21 +135,31 @@ export const executeRefreshTokenStrategy = async ( ); } - anyStrategy.userProfile( + resolve({ accessToken, - (error: Error, passportProfile: passport.Profile) => { - if (error) { - reject(error); - } - - const profile = makeProfileInfo(passportProfile, params); - resolve({ - accessToken, - params, - profile, - }); - }, - ); + params, + }); + }, + ); + }); +}; + +export const executeFetchUserProfileStrategy = async ( + providerstrategy: passport.Strategy, + accessToken: string, + params: any, +): Promise => { + return new Promise((resolve, reject) => { + const anyStrategy = providerstrategy as any; + anyStrategy.userProfile( + accessToken, + (error: Error, passportProfile: passport.Profile) => { + if (error) { + reject(error); + } + + const profile = makeProfileInfo(passportProfile, params); + resolve(profile); }, ); }); diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index 761aae1ddf..066e16e330 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -21,6 +21,7 @@ import { executeRedirectStrategy, executeRefreshTokenStrategy, makeProfileInfo, + executeFetchUserProfileStrategy, } from '../PassportStrategyHelper'; import { OAuthProviderHandlers, @@ -81,12 +82,18 @@ export class GoogleAuthProvider implements OAuthProviderHandlers { refreshToken: string, scope: string, ): Promise { - const { accessToken, params, profile } = await executeRefreshTokenStrategy( + const { accessToken, params } = await executeRefreshTokenStrategy( this._strategy, refreshToken, scope, ); + const profile = await executeFetchUserProfileStrategy( + this._strategy, + accessToken, + params, + ); + return { accessToken, idToken: params.id_token, diff --git a/plugins/auth-backend/src/providers/types.ts b/plugins/auth-backend/src/providers/types.ts index a8239b397a..b8252ddc97 100644 --- a/plugins/auth-backend/src/providers/types.ts +++ b/plugins/auth-backend/src/providers/types.ts @@ -87,5 +87,4 @@ export type ProfileInfo = { export type RefreshTokenResponse = { accessToken: string; params: any; - profile: ProfileInfo; };