From 46ab31f7433655b2f160a8d2d591c37ae41f483c Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Apr 2022 13:09:56 +0200 Subject: [PATCH] auth-backend: migrate atlassian provider to use resolver context Signed-off-by: Patrik Oldsberg --- .../src/providers/atlassian/provider.test.ts | 17 +------ .../src/providers/atlassian/provider.ts | 45 ++++--------------- 2 files changed, 10 insertions(+), 52 deletions(-) diff --git a/plugins/auth-backend/src/providers/atlassian/provider.test.ts b/plugins/auth-backend/src/providers/atlassian/provider.test.ts index 29241dd17b..c41a18aa22 100644 --- a/plugins/auth-backend/src/providers/atlassian/provider.test.ts +++ b/plugins/auth-backend/src/providers/atlassian/provider.test.ts @@ -16,11 +16,9 @@ import { AtlassianAuthProvider } from './provider'; import * as helpers from '../../lib/passport/PassportStrategyHelper'; -import { getVoidLogger } from '@backstage/backend-common'; -import { TokenIssuer } from '../../identity'; -import { CatalogIdentityClient } from '../../lib/catalog'; import { OAuthResult } from '../../lib/oauth'; import { PassportProfile } from '../../lib/passport/types'; +import { AuthResolverContext } from '../types'; const mockFrameHandler = jest.spyOn( helpers, @@ -28,19 +26,8 @@ const mockFrameHandler = jest.spyOn( ) as unknown as jest.MockedFunction<() => Promise<{ result: OAuthResult }>>; describe('createAtlassianProvider', () => { - const tokenIssuer = { - issueToken: jest.fn(), - listPublicKeys: jest.fn(), - }; - const catalogIdentityClient = { - findUser: jest.fn(), - }; - const provider = new AtlassianAuthProvider({ - logger: getVoidLogger(), - catalogIdentityClient: - catalogIdentityClient as unknown as CatalogIdentityClient, - tokenIssuer: tokenIssuer as unknown as TokenIssuer, + resolverContext: {} as AuthResolverContext, authHandler: async ({ fullProfile }) => ({ profile: { email: fullProfile.emails![0]!.value, diff --git a/plugins/auth-backend/src/providers/atlassian/provider.ts b/plugins/auth-backend/src/providers/atlassian/provider.ts index 91e18c7e10..e45b1dfedd 100644 --- a/plugins/auth-backend/src/providers/atlassian/provider.ts +++ b/plugins/auth-backend/src/providers/atlassian/provider.ts @@ -38,21 +38,17 @@ import { import { AuthHandler, AuthProviderFactory, + AuthResolverContext, RedirectInfo, SignInResolver, } from '../types'; import express from 'express'; -import { TokenIssuer } from '../../identity'; -import { CatalogIdentityClient } from '../../lib/catalog'; -import { Logger } from 'winston'; export type AtlassianAuthProviderOptions = OAuthProviderOptions & { scopes: string; signInResolver?: SignInResolver; authHandler: AuthHandler; - tokenIssuer: TokenIssuer; - catalogIdentityClient: CatalogIdentityClient; - logger: Logger; + resolverContext: AuthResolverContext; }; export const atlassianDefaultAuthHandler: AuthHandler = async ({ @@ -66,14 +62,10 @@ export class AtlassianAuthProvider implements OAuthHandlers { private readonly _strategy: AtlassianStrategy; private readonly signInResolver?: SignInResolver; private readonly authHandler: AuthHandler; - private readonly tokenIssuer: TokenIssuer; - private readonly catalogIdentityClient: CatalogIdentityClient; - private readonly logger: Logger; + private readonly resolverContext: AuthResolverContext; constructor(options: AtlassianAuthProviderOptions) { - this.catalogIdentityClient = options.catalogIdentityClient; - this.logger = options.logger; - this.tokenIssuer = options.tokenIssuer; + this.resolverContext = options.resolverContext; this.authHandler = options.authHandler; this.signInResolver = options.signInResolver; @@ -120,12 +112,7 @@ export class AtlassianAuthProvider implements OAuthHandlers { } private async handleResult(result: OAuthResult): Promise { - const context = { - logger: this.logger, - catalogIdentityClient: this.catalogIdentityClient, - tokenIssuer: this.tokenIssuer, - }; - const { profile } = await this.authHandler(result, context); + const { profile } = await this.authHandler(result, this.resolverContext); const response: OAuthResponse = { providerInfo: { @@ -143,7 +130,7 @@ export class AtlassianAuthProvider implements OAuthHandlers { result, profile, }, - context, + this.resolverContext, ); } @@ -206,15 +193,7 @@ export const createAtlassianProvider = (options?: { resolver: SignInResolver; }; }): AuthProviderFactory => { - return ({ - providerId, - globalConfig, - config, - tokenIssuer, - tokenManager, - catalogApi, - logger, - }) => + return ({ providerId, globalConfig, config, resolverContext }) => OAuthEnvironmentHandler.mapConfig(config, envConfig => { const clientId = envConfig.getString('clientId'); const clientSecret = envConfig.getString('clientSecret'); @@ -224,11 +203,6 @@ export const createAtlassianProvider = (options?: { customCallbackUrl || `${globalConfig.baseUrl}/${providerId}/handler/frame`; - const catalogIdentityClient = new CatalogIdentityClient({ - catalogApi, - tokenManager, - }); - const authHandler: AuthHandler = options?.authHandler ?? atlassianDefaultAuthHandler; @@ -239,14 +213,11 @@ export const createAtlassianProvider = (options?: { callbackUrl, authHandler, signInResolver: options?.signIn?.resolver, - catalogIdentityClient, - logger, - tokenIssuer, + resolverContext, }); return OAuthAdapter.fromConfig(globalConfig, provider, { providerId, - tokenIssuer, callbackUrl, }); });