From d3265deba86e8e6dbe653c645a3a98632d5133f0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 7 Aug 2023 17:43:17 +0200 Subject: [PATCH] auth-node: refactor to use plain ProfileTransform Signed-off-by: Patrik Oldsberg --- plugins/auth-node/src/index.ts | 1 + .../oauth/PassportOAuthAuthenticatorHelper.ts | 17 +++++++++-------- .../src/oauth/createOAuthProviderFactory.ts | 19 ++++++++++--------- .../src/oauth/createOAuthRouteHandlers.ts | 9 +++------ plugins/auth-node/src/oauth/types.ts | 9 ++------- plugins/auth-node/src/types.ts | 13 +++++++++++++ 6 files changed, 38 insertions(+), 30 deletions(-) diff --git a/plugins/auth-node/src/index.ts b/plugins/auth-node/src/index.ts index d528a3b3a7..a376c87338 100644 --- a/plugins/auth-node/src/index.ts +++ b/plugins/auth-node/src/index.ts @@ -38,6 +38,7 @@ export type { ClientAuthResponse, CookieConfigurer, ProfileInfo, + ProfileTransform, SignInInfo, SignInResolver, TokenParams, diff --git a/plugins/auth-node/src/oauth/PassportOAuthAuthenticatorHelper.ts b/plugins/auth-node/src/oauth/PassportOAuthAuthenticatorHelper.ts index c7fe81c3ee..420a661da5 100644 --- a/plugins/auth-node/src/oauth/PassportOAuthAuthenticatorHelper.ts +++ b/plugins/auth-node/src/oauth/PassportOAuthAuthenticatorHelper.ts @@ -20,12 +20,12 @@ import { PassportHelpers, PassportProfile, } from '../passport'; +import { ProfileTransform } from '../types'; import { OAuthAuthenticatorAuthenticateInput, OAuthAuthenticatorRefreshInput, OAuthAuthenticatorResult, OAuthAuthenticatorStartInput, - OAuthProfileTransform, } from './types'; /** @public */ @@ -53,13 +53,14 @@ export type PassportOAuthDoneCallback = PassportDoneCallback< /** @public */ export class PassportOAuthAuthenticatorHelper { - static defaultProfileTransform: OAuthProfileTransform = - async input => ({ - profile: PassportHelpers.transformProfile( - input.fullProfile, - input.session.idToken, - ), - }); + static defaultProfileTransform: ProfileTransform< + OAuthAuthenticatorResult + > = async input => ({ + profile: PassportHelpers.transformProfile( + input.fullProfile, + input.session.idToken, + ), + }); static from(strategy: Strategy) { return new PassportOAuthAuthenticatorHelper(strategy); diff --git a/plugins/auth-node/src/oauth/createOAuthProviderFactory.ts b/plugins/auth-node/src/oauth/createOAuthProviderFactory.ts index e08756860a..87fde6a1a2 100644 --- a/plugins/auth-node/src/oauth/createOAuthProviderFactory.ts +++ b/plugins/auth-node/src/oauth/createOAuthProviderFactory.ts @@ -15,22 +15,22 @@ */ import { readDeclarativeSignInResolver } from '../sign-in'; -import { AuthProviderFactory, SignInResolver } from '../types'; +import { + AuthProviderFactory, + ProfileTransform, + SignInResolver, +} from '../types'; import { OAuthEnvironmentHandler } from './OAuthEnvironmentHandler'; import { createOAuthRouteHandlers } from './createOAuthRouteHandlers'; import { OAuthStateTransform } from './state'; -import { - OAuthAuthenticator, - OAuthAuthenticatorResult, - OAuthProfileTransform, -} from './types'; +import { OAuthAuthenticator, OAuthAuthenticatorResult } from './types'; import { SignInResolverFactory } from '../sign-in/createSignInResolverFactory'; /** @public */ export function createOAuthProviderFactory(options: { authenticator: OAuthAuthenticator; stateTransform?: OAuthStateTransform; - profileTransform?: OAuthProfileTransform; + profileTransform?: ProfileTransform>; signInResolver?: SignInResolver>; signInResolverFactories?: { [name in string]: SignInResolverFactory< @@ -43,8 +43,9 @@ export function createOAuthProviderFactory(options: { return OAuthEnvironmentHandler.mapConfig(ctx.config, envConfig => { const signInResolver = options.signInResolver ?? - readDeclarativeSignInResolver(envConfig, { - signInResolverFactories: options.signInResolverFactories, + readDeclarativeSignInResolver({ + config: envConfig, + signInResolverFactories: options.signInResolverFactories ?? {}, }); return createOAuthRouteHandlers({ diff --git a/plugins/auth-node/src/oauth/createOAuthRouteHandlers.ts b/plugins/auth-node/src/oauth/createOAuthRouteHandlers.ts index 72640c8692..6c4819a320 100644 --- a/plugins/auth-node/src/oauth/createOAuthRouteHandlers.ts +++ b/plugins/auth-node/src/oauth/createOAuthRouteHandlers.ts @@ -37,13 +37,10 @@ import { AuthResolverContext, ClientAuthResponse, CookieConfigurer, + ProfileTransform, SignInResolver, } from '../types'; -import { - OAuthAuthenticator, - OAuthAuthenticatorResult, - OAuthProfileTransform, -} from './types'; +import { OAuthAuthenticator, OAuthAuthenticatorResult } from './types'; import { Config } from '@backstage/config'; /** @public */ @@ -56,7 +53,7 @@ export interface OAuthRouteHandlersOptions { config: Config; resolverContext: AuthResolverContext; stateTransform?: OAuthStateTransform; - profileTransform?: OAuthProfileTransform; + profileTransform?: ProfileTransform>; cookieConfigurer?: CookieConfigurer; signInResolver?: SignInResolver>; } diff --git a/plugins/auth-node/src/oauth/types.ts b/plugins/auth-node/src/oauth/types.ts index 4a53beaae6..e6ddf370b6 100644 --- a/plugins/auth-node/src/oauth/types.ts +++ b/plugins/auth-node/src/oauth/types.ts @@ -16,7 +16,7 @@ import { Config } from '@backstage/config'; import { Request } from 'express'; -import { AuthResolverContext, ProfileInfo } from '../types'; +import { ProfileTransform } from '../types'; export interface OAuthSession { accessToken: string; @@ -27,11 +27,6 @@ export interface OAuthSession { refreshToken?: string; } -export type OAuthProfileTransform = ( - result: OAuthAuthenticatorResult, - context: AuthResolverContext, -) => Promise<{ profile: ProfileInfo }>; - export interface OAuthAuthenticatorStartInput { scope: string; state: string; @@ -60,7 +55,7 @@ export interface OAuthAuthenticatorResult { } export interface OAuthAuthenticator { - defaultProfileTransform: OAuthProfileTransform; + defaultProfileTransform: ProfileTransform>; shouldPersistScopes?: boolean; initialize(ctx: { callbackUrl: string; config: Config }): TContext; start( diff --git a/plugins/auth-node/src/types.ts b/plugins/auth-node/src/types.ts index f1d7063700..ccdfb0c39b 100644 --- a/plugins/auth-node/src/types.ts +++ b/plugins/auth-node/src/types.ts @@ -309,6 +309,19 @@ export type SignInResolver = ( context: AuthResolverContext, ) => Promise; +/** + * Describes the function that transforms the result of a successful + * authentication into a {@link ProfileInfo} object. + * + * This function may optionally throw an error in order to reject authentication. + * + * @public + */ +export type ProfileTransform = ( + result: TResult, + context: AuthResolverContext, +) => Promise<{ profile: ProfileInfo }>; + /** * Used to display login information to user, i.e. sidebar popup. *