From 723df8e8cee9c40f56ce692bc40a31de5afc4038 Mon Sep 17 00:00:00 2001 From: Pepijn Schildkamp Date: Tue, 31 Aug 2021 16:41:29 +0200 Subject: [PATCH] processed review comments. Ran prettier, api-report, minor to patch Signed-off-by: Pepijn Schildkamp --- .changeset/strong-ravens-smoke.md | 2 +- plugins/auth-backend/api-report.md | 17 +++++++++++++ .../src/providers/oauth2/index.ts | 2 +- .../src/providers/oauth2/provider.test.ts | 9 +++---- .../src/providers/oauth2/provider.ts | 24 +------------------ 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/.changeset/strong-ravens-smoke.md b/.changeset/strong-ravens-smoke.md index 8579e74d37..478eb1c4c2 100644 --- a/.changeset/strong-ravens-smoke.md +++ b/.changeset/strong-ravens-smoke.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-auth-backend': minor +'@backstage/plugin-auth-backend': patch --- Added signIn and authHandler resolver for oAuth2 provider diff --git a/plugins/auth-backend/api-report.md b/plugins/auth-backend/api-report.md index 6e65342828..6785222fa6 100644 --- a/plugins/auth-backend/api-report.md +++ b/plugins/auth-backend/api-report.md @@ -105,6 +105,13 @@ export const createMicrosoftProvider: ( options?: MicrosoftProviderOptions | undefined, ) => AuthProviderFactory; +// Warning: (ae-missing-release-tag) "createOAuth2Provider" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export const createOAuth2Provider: ( + options?: OAuth2ProviderOptions | undefined, +) => AuthProviderFactory; + // Warning: (ae-missing-release-tag) "createOktaProvider" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -199,6 +206,16 @@ export type MicrosoftProviderOptions = { }; }; +// Warning: (ae-missing-release-tag) "OAuth2ProviderOptions" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type OAuth2ProviderOptions = { + authHandler?: AuthHandler; + signIn?: { + resolver?: SignInResolver; + }; +}; + // Warning: (ae-missing-release-tag) "OAuthAdapter" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) diff --git a/plugins/auth-backend/src/providers/oauth2/index.ts b/plugins/auth-backend/src/providers/oauth2/index.ts index 4e2509735d..d68208a148 100644 --- a/plugins/auth-backend/src/providers/oauth2/index.ts +++ b/plugins/auth-backend/src/providers/oauth2/index.ts @@ -14,5 +14,5 @@ * limitations under the License. */ -export { createOAuth2Provider, oAuth2EmailSignInResolver } from './provider'; +export { createOAuth2Provider } from './provider'; export type { OAuth2ProviderOptions } from './provider'; diff --git a/plugins/auth-backend/src/providers/oauth2/provider.test.ts b/plugins/auth-backend/src/providers/oauth2/provider.test.ts index 773471c42f..7030d91343 100644 --- a/plugins/auth-backend/src/providers/oauth2/provider.test.ts +++ b/plugins/auth-backend/src/providers/oauth2/provider.test.ts @@ -21,10 +21,10 @@ import { getVoidLogger } from '@backstage/backend-common'; import { TokenIssuer } from '../../identity/types'; import { CatalogIdentityClient } from '../../lib/catalog'; -const mockFrameHandler = (jest.spyOn( +const mockFrameHandler = jest.spyOn( helpers, 'executeFrameHandlerStrategy', -) as unknown) as jest.MockedFunction< +) as unknown as jest.MockedFunction< () => Promise<{ result: OAuthResult; privateInfo: any }> >; @@ -40,8 +40,9 @@ describe('createOAuth2Provider', () => { const provider = new OAuth2AuthProvider({ logger: getVoidLogger(), - catalogIdentityClient: (catalogIdentityClient as unknown) as CatalogIdentityClient, - tokenIssuer: (tokenIssuer as unknown) as TokenIssuer, + catalogIdentityClient: + catalogIdentityClient as unknown as CatalogIdentityClient, + tokenIssuer: tokenIssuer as unknown as TokenIssuer, 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 fc315a69ca..36e2f6ed99 100644 --- a/plugins/auth-backend/src/providers/oauth2/provider.ts +++ b/plugins/auth-backend/src/providers/oauth2/provider.ts @@ -42,7 +42,7 @@ import { RedirectInfo, SignInResolver, } from '../types'; -import { CatalogIdentityClient, getEntityClaims } from '../../lib/catalog'; +import { CatalogIdentityClient } from '../../lib/catalog'; import { TokenIssuer } from '../../identity'; import { Logger } from 'winston'; @@ -188,28 +188,6 @@ export class OAuth2AuthProvider implements OAuthHandlers { } } -export const oAuth2EmailSignInResolver: SignInResolver = async ( - info, - ctx, -) => { - const { profile } = info; - - if (!profile.email) { - throw new Error('OAuth2 profile contained no email'); - } - - const entity = await ctx.catalogIdentityClient.findUser({ - annotations: { - 'backstage.io/email': profile.email, - }, - }); - - const claims = getEntityClaims(entity); - const token = await ctx.tokenIssuer.issueToken({ claims }); - - return { id: entity.metadata.name, entity, token }; -}; - export const oAuth2DefaultSignInResolver: SignInResolver = async ( info, ctx,