From 5ed8e5d05cbbdab15a48d60af0b01243bde421fb Mon Sep 17 00:00:00 2001 From: Chris Simmons Date: Sat, 22 Aug 2020 12:26:06 +1200 Subject: [PATCH] simplified and cleaned up duplicate code --- .../src/providers/microsoft/provider.ts | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/plugins/auth-backend/src/providers/microsoft/provider.ts b/plugins/auth-backend/src/providers/microsoft/provider.ts index 92f7a10dd8..5997d8e1a8 100644 --- a/plugins/auth-backend/src/providers/microsoft/provider.ts +++ b/plugins/auth-backend/src/providers/microsoft/provider.ts @@ -58,15 +58,13 @@ export class MicrosoftAuthProvider implements OAuthProviderHandlers { accessToken: string, params: any, rawProfile: any, - photoURL?: any, + photoURL: any, ): OAuthResponse { let passportProfile: passport.Profile = rawProfile; - if (photoURL) { - passportProfile = { - ...passportProfile, - photos: [{ value: photoURL }], - }; - } + passportProfile = { + ...passportProfile, + photos: [{ value: photoURL }], + }; const profile = makeProfileInfo(passportProfile, params.id_token); const providerInfo = { @@ -99,18 +97,8 @@ export class MicrosoftAuthProvider implements OAuthProviderHandlers { rawProfile: passport.Profile, done: PassportDoneCallback, ) => { - got - .get('https://graph.microsoft.com/v1.0/me/photos/48x48/$value', { - encoding: 'binary', - responseType: 'buffer', - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }) - .then(photoData => { - const photoURL = `data:image/jpeg;base64,${Buffer.from( - photoData.body, - ).toString('base64')}`; + this.getUserPhoto(accessToken) + .then(photoURL => { const authResponse = MicrosoftAuthProvider.transformAuthResponse( accessToken, params, @@ -120,15 +108,7 @@ export class MicrosoftAuthProvider implements OAuthProviderHandlers { done(undefined, authResponse, { refreshToken }); }) .catch(error => { - console.log( - `Error retrieving user photo from Microsoft Graph API: ${error}`, - ); - const authResponse = MicrosoftAuthProvider.transformAuthResponse( - accessToken, - params, - rawProfile, - ); - done(undefined, authResponse, { refreshToken }); + throw new Error(`Error processing auth response: ${error}`); }); }, ); @@ -201,8 +181,9 @@ export class MicrosoftAuthProvider implements OAuthProviderHandlers { }) .catch(error => { console.log( - `Error retrieving user photo from Microsoft Graph API: ${error}`, + `Could not retrieve user profile photo from Microsoft Graph API: ${error}`, ); + // User profile photo is optional, ignore errors and resolve undefined resolve(); }); });