From 2d126853ef4585afc02d1645f525952a29a87e30 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Apr 2022 13:51:55 +0200 Subject: [PATCH] auth-backend: migrate oauth2 provider to use resolver context Signed-off-by: Patrik Oldsberg --- .../src/providers/oauth2/provider.test.ts | 17 +------ .../src/providers/oauth2/provider.ts | 45 ++++--------------- 2 files changed, 10 insertions(+), 52 deletions(-) diff --git a/plugins/auth-backend/src/providers/oauth2/provider.test.ts b/plugins/auth-backend/src/providers/oauth2/provider.test.ts index 7030d91343..23e08dd729 100644 --- a/plugins/auth-backend/src/providers/oauth2/provider.test.ts +++ b/plugins/auth-backend/src/providers/oauth2/provider.test.ts @@ -17,9 +17,7 @@ import { OAuth2AuthProvider } from './provider'; import * as helpers from '../../lib/passport/PassportStrategyHelper'; import { OAuthResult } from '../../lib/oauth'; -import { getVoidLogger } from '@backstage/backend-common'; -import { TokenIssuer } from '../../identity/types'; -import { CatalogIdentityClient } from '../../lib/catalog'; +import { AuthResolverContext } from '../types'; const mockFrameHandler = jest.spyOn( helpers, @@ -30,19 +28,8 @@ const mockFrameHandler = jest.spyOn( describe('createOAuth2Provider', () => { it('should auth', async () => { - const tokenIssuer = { - issueToken: jest.fn(), - listPublicKeys: jest.fn(), - }; - const catalogIdentityClient = { - findUser: jest.fn(), - }; - const provider = new OAuth2AuthProvider({ - 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/oauth2/provider.ts b/plugins/auth-backend/src/providers/oauth2/provider.ts index 5c5df2a748..312efd6d1f 100644 --- a/plugins/auth-backend/src/providers/oauth2/provider.ts +++ b/plugins/auth-backend/src/providers/oauth2/provider.ts @@ -39,12 +39,10 @@ import { import { AuthHandler, AuthProviderFactory, + AuthResolverContext, RedirectInfo, SignInResolver, } from '../types'; -import { CatalogIdentityClient } from '../../lib/catalog'; -import { TokenIssuer } from '../../identity'; -import { Logger } from 'winston'; type PrivateInfo = { refreshToken: string; @@ -53,12 +51,10 @@ type PrivateInfo = { export type OAuth2AuthProviderOptions = OAuthProviderOptions & { signInResolver?: SignInResolver; authHandler: AuthHandler; - tokenIssuer: TokenIssuer; - catalogIdentityClient: CatalogIdentityClient; authorizationUrl: string; tokenUrl: string; scope?: string; - logger: Logger; + resolverContext: AuthResolverContext; includeBasicAuth?: boolean; }; @@ -66,16 +62,12 @@ export class OAuth2AuthProvider implements OAuthHandlers { private readonly _strategy: OAuth2Strategy; 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: OAuth2AuthProviderOptions) { this.signInResolver = options.signInResolver; this.authHandler = options.authHandler; - this.tokenIssuer = options.tokenIssuer; - this.catalogIdentityClient = options.catalogIdentityClient; - this.logger = options.logger; + this.resolverContext = options.resolverContext; this._strategy = new OAuth2Strategy( { @@ -163,12 +155,7 @@ export class OAuth2AuthProvider implements OAuthHandlers { } private async handleResult(result: OAuthResult) { - 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: { @@ -186,7 +173,7 @@ export class OAuth2AuthProvider implements OAuthHandlers { result, profile, }, - context, + this.resolverContext, ); } @@ -216,15 +203,7 @@ export const createOAuth2Provider = (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'); @@ -239,11 +218,6 @@ export const createOAuth2Provider = (options?: { const disableRefresh = envConfig.getOptionalBoolean('disableRefresh') ?? false; - const catalogIdentityClient = new CatalogIdentityClient({ - catalogApi, - tokenManager, - }); - const authHandler: AuthHandler = options?.authHandler ? options.authHandler : async ({ fullProfile, params }) => ({ @@ -253,22 +227,19 @@ export const createOAuth2Provider = (options?: { const provider = new OAuth2AuthProvider({ clientId, clientSecret, - tokenIssuer, - catalogIdentityClient, callbackUrl, signInResolver: options?.signIn?.resolver, authHandler, authorizationUrl, tokenUrl, scope, - logger, includeBasicAuth, + resolverContext, }); return OAuthAdapter.fromConfig(globalConfig, provider, { disableRefresh, providerId, - tokenIssuer, callbackUrl, }); });