From f5f290f1c26ae9d152517a427c9a85020ec9229c Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 16 Jun 2021 10:56:42 +0200 Subject: [PATCH] feat: update the public api instead of profileTransform it is AuthHandler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Co-authored-by: Johan Haals Co-authored-by: Patrik Oldsberg Signed-off-by: blam --- .../src/providers/google/provider.test.ts | 10 ++++--- .../src/providers/google/provider.ts | 26 +++++++++---------- plugins/auth-backend/src/providers/types.ts | 10 ++++--- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/plugins/auth-backend/src/providers/google/provider.test.ts b/plugins/auth-backend/src/providers/google/provider.test.ts index a8b5b92b5e..45df5bd319 100644 --- a/plugins/auth-backend/src/providers/google/provider.test.ts +++ b/plugins/auth-backend/src/providers/google/provider.test.ts @@ -42,10 +42,12 @@ describe('createGoogleProvider', () => { logger: getVoidLogger(), catalogIdentityClient: (catalogIdentityClient as unknown) as CatalogIdentityClient, tokenIssuer: (tokenIssuer as unknown) as TokenIssuer, - profileTransform: async ({ fullProfile }) => ({ - email: fullProfile.emails![0]!.value, - displayName: fullProfile.displayName, - picture: 'http://google.com/lols', + authHandler: async ({ fullProfile }) => ({ + profile: { + email: fullProfile.emails![0]!.value, + displayName: fullProfile.displayName, + picture: 'http://google.com/lols', + }, }), clientId: 'mock', clientSecret: 'mock', diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index f2feb7155b..ace0c8e132 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -40,7 +40,7 @@ import { } from '../../lib/passport'; import { AuthProviderFactory, - ProfileTransform, + AuthHandler, RedirectInfo, SignInResolver, } from '../types'; @@ -52,7 +52,7 @@ type PrivateInfo = { type Options = OAuthProviderOptions & { signInResolver?: SignInResolver; - profileTransform: ProfileTransform; + authHandler: AuthHandler; tokenIssuer: TokenIssuer; catalogIdentityClient: CatalogIdentityClient; logger: Logger; @@ -61,14 +61,14 @@ type Options = OAuthProviderOptions & { export class GoogleAuthProvider implements OAuthHandlers { private readonly _strategy: GoogleStrategy; private readonly signInResolver?: SignInResolver; - private readonly profileTransform: ProfileTransform; + private readonly authHandler: AuthHandler; private readonly tokenIssuer: TokenIssuer; private readonly catalogIdentityClient: CatalogIdentityClient; private readonly logger: Logger; constructor(options: Options) { this.signInResolver = options.signInResolver; - this.profileTransform = options.profileTransform; + this.authHandler = options.authHandler; this.tokenIssuer = options.tokenIssuer; this.catalogIdentityClient = options.catalogIdentityClient; this.logger = options.logger; @@ -146,7 +146,7 @@ export class GoogleAuthProvider implements OAuthHandlers { } private async handleResult(result: OAuthResult) { - const profile = await this.profileTransform(result); + const { profile } = await this.authHandler(result); const response: OAuthResponse = { providerInfo: { @@ -235,7 +235,7 @@ export type GoogleProviderOptions = { * The profile transformation function used to verify and convert the auth response * into the profile that will be presented to the user. */ - profileTransform?: ProfileTransform; + authHandler?: AuthHandler; /** * Configure sign-in for this provider, without it the provider can not be used to sign users in. @@ -272,13 +272,11 @@ export const createGoogleProvider = ( tokenIssuer, }); - let profileTransform: ProfileTransform = async ({ - fullProfile, - params, - }) => makeProfileInfo(fullProfile, params.id_token); - if (options?.profileTransform) { - profileTransform = options.profileTransform; - } + const authHandler: AuthHandler = options?.authHandler + ? options.authHandler + : async ({ fullProfile, params }) => ({ + profile: makeProfileInfo(fullProfile, params.id_token), + }); const signInResolverFn = options?.signIn?.resolver ?? googleDefaultSignInResolver; @@ -295,7 +293,7 @@ export const createGoogleProvider = ( clientSecret, callbackUrl, signInResolver, - profileTransform, + authHandler, tokenIssuer, catalogIdentityClient, logger, diff --git a/plugins/auth-backend/src/providers/types.ts b/plugins/auth-backend/src/providers/types.ts index 305ff7e776..e4bbaa091c 100644 --- a/plugins/auth-backend/src/providers/types.ts +++ b/plugins/auth-backend/src/providers/types.ts @@ -219,14 +219,16 @@ export type SignInResolver = ( }, ) => Promise; +export type AuthHandlerResult = { profile: ProfileInfo }; + /** - * A transformation function called every time the user authenticates using the provider. + * The AuthHandler function is called every time the user authenticates using the provider. * - * The transform should return a profile that represents the session for the user in the frontend. + * The handler should return a profile that represents the session for the user in the frontend. * * Throwing an error in the function will cause the authentication to fail, making it * possible to use this function as a way to limit access to a certain group of users. */ -export type ProfileTransform = ( +export type AuthHandler = ( input: AuthResult, -) => Promise; +) => Promise;