Simplify the bug fix, by implementing logic in the PassportHelper

Signed-off-by: Olivier Liechti <olivier.liechti@wasabi-tech.com>
This commit is contained in:
Olivier Liechti
2024-07-09 16:29:50 +02:00
parent efda5a301d
commit 55c1a729ac
6 changed files with 22 additions and 34 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/plugin-auth-backend-module-atlassian-provider': patch
'@backstage/plugin-auth-node': patch
---
Fix issues with Atlassian OAuth provider
@@ -16,39 +16,16 @@
import {
createOAuthAuthenticator,
OAuthAuthenticatorResult,
PassportOAuthAuthenticatorHelper,
PassportOAuthDoneCallback,
ProfileInfo,
PassportProfile,
} from '@backstage/plugin-auth-node';
import AtlassianStrategy from 'passport-atlassian-oauth2';
export type AtlassianPassportProfile = {
id: string;
displayName: string;
email: string;
photo: string;
provider: string;
_json: any;
};
/** @public */
export const atlassianAuthenticator = createOAuthAuthenticator({
defaultProfileTransform: async (
input: OAuthAuthenticatorResult<AtlassianPassportProfile>,
) => {
const atlassianProfile = input.fullProfile as AtlassianPassportProfile;
const profile: ProfileInfo = {
displayName: atlassianProfile.displayName,
email: atlassianProfile.email,
picture: atlassianProfile.photo,
};
return {
profile,
};
},
defaultProfileTransform:
PassportOAuthAuthenticatorHelper.defaultProfileTransform,
scopes: {
required: ['offline_access', 'read:me', 'read:jira-work', 'read:jira-user'],
},
@@ -79,7 +56,7 @@ export const atlassianAuthenticator = createOAuthAuthenticator({
accessToken: string,
refreshToken: string,
params: any,
fullProfile: AtlassianPassportProfile,
fullProfile: PassportProfile,
done: PassportOAuthDoneCallback,
) => {
done(
@@ -100,12 +77,10 @@ export const atlassianAuthenticator = createOAuthAuthenticator({
},
async authenticate(input, helper) {
const result = await helper.authenticate(input);
return result as OAuthAuthenticatorResult<AtlassianPassportProfile>;
return helper.authenticate(input);
},
async refresh(input, helper) {
const result = await helper.refresh(input);
return result as OAuthAuthenticatorResult<AtlassianPassportProfile>;
return helper.refresh(input);
},
});
@@ -19,6 +19,5 @@
* @packageDocumentation
*/
export { atlassianAuthenticator } from './authenticator';
export type { AtlassianPassportProfile } from './authenticator';
export { authModuleAtlassianProvider as default } from './module';
export { atlassianSignInResolvers } from './resolvers';
@@ -26,7 +26,6 @@ import {
import { OAuthResult } from '../../lib/oauth';
import { createAuthProviderIntegration } from '../createAuthProviderIntegration';
import { AuthHandler } from '../types';
import { AtlassianPassportProfile } from '@backstage/plugin-auth-backend-module-atlassian-provider';
/**
* Auth provider integration for Atlassian auth
@@ -48,7 +47,7 @@ export const atlassian = createAuthProviderIntegration({
resolver: SignInResolver<OAuthResult>;
};
}) {
return createOAuthProviderFactory<AtlassianPassportProfile>({
return createOAuthProviderFactory({
authenticator: atlassianAuthenticator,
profileTransform: adaptLegacyOAuthHandler(options?.authHandler),
signInResolver: adaptLegacyOAuthSignInResolver(options?.signIn?.resolver),
@@ -42,12 +42,19 @@ export class PassportHelpers {
email = firstEmail.value;
}
// This is the case for Atlassian
if (profile.email) {
email = profile.email;
}
let picture: string | undefined = undefined;
if (profile.avatarUrl) {
picture = profile.avatarUrl;
} else if (profile.photos && profile.photos.length > 0) {
const [firstPhoto] = profile.photos;
picture = firstPhoto.value;
} else if (profile.photo) {
picture = profile.photo; // This is the case for Atlassian
}
let displayName: string | undefined =
+2
View File
@@ -19,6 +19,8 @@ import { Profile } from 'passport';
/** @public */
export type PassportProfile = Profile & {
avatarUrl?: string;
email?: string;
photo?: string;
};
/** @public */