auth-backend: refactor user profile fetch to transform profile outside

This commit is contained in:
Patrik Oldsberg
2021-02-13 17:42:14 +01:00
parent 10e59af4d0
commit b9709efff8
7 changed files with 16 additions and 18 deletions
@@ -190,19 +190,17 @@ type ProviderStrategy = {
export const executeFetchUserProfileStrategy = async (
providerStrategy: passport.Strategy,
accessToken: string,
idToken?: string,
): Promise<ProfileInfo> => {
): Promise<passport.Profile> => {
return new Promise((resolve, reject) => {
const anyStrategy = (providerStrategy as unknown) as ProviderStrategy;
anyStrategy.userProfile(
accessToken,
(error: Error, passportProfile: passport.Profile) => {
(error: Error, rawProfile: passport.Profile) => {
if (error) {
reject(error);
} else {
resolve(rawProfile);
}
const profile = makeProfileInfo(passportProfile, idToken);
resolve(profile);
},
);
});
@@ -114,11 +114,11 @@ export class Auth0AuthProvider implements OAuthHandlers {
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
const rawProfile = await executeFetchUserProfileStrategy(
this._strategy,
accessToken,
params.id_token,
);
const profile = makeProfileInfo(rawProfile, params.id_token);
return this.populateIdentity({
providerInfo: {
@@ -127,11 +127,11 @@ export class GoogleAuthProvider implements OAuthHandlers {
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
const rawProfile = await executeFetchUserProfileStrategy(
this._strategy,
accessToken,
params.id_token,
);
const profile = makeProfileInfo(rawProfile, params.id_token);
return this.populateIdentity({
providerInfo: {
@@ -142,11 +142,11 @@ export class MicrosoftAuthProvider implements OAuthHandlers {
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
const rawProfile = await executeFetchUserProfileStrategy(
this._strategy,
accessToken,
params.id_token,
);
const profile = makeProfileInfo(rawProfile, params.id_token);
const photo = await this.getUserPhoto(accessToken);
if (photo) {
profile.picture = photo;
@@ -124,11 +124,11 @@ export class OAuth2AuthProvider implements OAuthHandlers {
refreshToken: updatedRefreshToken,
} = refreshTokenResponse;
const profile = await executeFetchUserProfileStrategy(
const rawProfile = await executeFetchUserProfileStrategy(
this._strategy,
accessToken,
params.id_token,
);
const profile = makeProfileInfo(rawProfile, params.id_token);
return this.populateIdentity({
providerInfo: {
@@ -134,11 +134,11 @@ export class OktaAuthProvider implements OAuthHandlers {
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
const rawProfile = await executeFetchUserProfileStrategy(
this._strategy,
accessToken,
params.id_token,
);
const profile = makeProfileInfo(rawProfile, params.id_token);
return this.populateIdentity({
providerInfo: {
@@ -114,11 +114,11 @@ export class OneLoginProvider implements OAuthHandlers {
req.scope,
);
const profile = await executeFetchUserProfileStrategy(
const rawProfile = await executeFetchUserProfileStrategy(
this._strategy,
accessToken,
params.id_token,
);
const profile = makeProfileInfo(rawProfile, params.id_token);
return this.populateIdentity({
providerInfo: {