From ec8930cb5be1013e9f3040137616a8ae8a8e6c30 Mon Sep 17 00:00:00 2001 From: Tom Opdebeeck Date: Fri, 13 Aug 2021 11:26:39 +0200 Subject: [PATCH] fix(auth-backend): update authhandler & signinresolver Signed-off-by: Tom Opdebeeck --- .../src/providers/gitlab/provider.test.ts | 7 ++++ .../src/providers/gitlab/provider.ts | 39 +++++++++---------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/plugins/auth-backend/src/providers/gitlab/provider.test.ts b/plugins/auth-backend/src/providers/gitlab/provider.test.ts index 814a060e90..e4112e1fff 100644 --- a/plugins/auth-backend/src/providers/gitlab/provider.test.ts +++ b/plugins/auth-backend/src/providers/gitlab/provider.test.ts @@ -132,6 +132,13 @@ describe('GitlabAuthProvider', () => { baseUrl: 'mock', catalogIdentityClient: (catalogIdentityClient as unknown) as CatalogIdentityClient, tokenIssuer: (tokenIssuer as unknown) as TokenIssuer, + authHandler: async ({ fullProfile }) => ({ + profile: { + email: fullProfile.emails![0]!.value, + displayName: fullProfile.displayName, + picture: 'http://gitlab.com/lols', + }, + }), logger: getVoidLogger(), }); for (const test of tests) { diff --git a/plugins/auth-backend/src/providers/gitlab/provider.ts b/plugins/auth-backend/src/providers/gitlab/provider.ts index 63d1260983..b07b8d72cf 100644 --- a/plugins/auth-backend/src/providers/gitlab/provider.ts +++ b/plugins/auth-backend/src/providers/gitlab/provider.ts @@ -58,7 +58,7 @@ type PrivateInfo = { export type GitlabAuthProviderOptions = OAuthProviderOptions & { baseUrl: string; signInResolver?: SignInResolver; - authHandler?: AuthHandler; + authHandler: AuthHandler; tokenIssuer: TokenIssuer; catalogIdentityClient: CatalogIdentityClient; logger: Logger; @@ -106,8 +106,8 @@ export class GitlabAuthProvider implements OAuthHandlers { this.catalogIdentityClient = options.catalogIdentityClient; this.logger = options.logger; this.tokenIssuer = options.tokenIssuer; - this.authHandler = options.authHandler || gitlabDefaultAuthHandler; - this.signInResolver = this.createSignInResolverFn(options); + this.authHandler = options.authHandler; + this.signInResolver = options.signInResolver; this._strategy = new GitlabStrategy( { @@ -210,22 +210,6 @@ export class GitlabAuthProvider implements OAuthHandlers { return response; } - private createSignInResolverFn({ - signInResolver, - catalogIdentityClient, - tokenIssuer, - logger, - }: GitlabAuthProviderOptions): SignInResolver { - const resolver = signInResolver || gitlabDefaultSignInResolver; - - return info => - resolver(info, { - catalogIdentityClient, - tokenIssuer, - logger, - }); - } - private transformResult(result: OAuthResult): OAuthResult { const { fullProfile, ...authResult } = result; @@ -295,13 +279,26 @@ export const createGitlabProvider = ( tokenIssuer, }); + const authHandler: AuthHandler = + options?.authHandler ?? gitlabDefaultAuthHandler; + + const signInResolverFn = + options?.signIn?.resolver ?? gitlabDefaultSignInResolver; + + const signInResolver: SignInResolver = info => + signInResolverFn(info, { + catalogIdentityClient, + tokenIssuer, + logger, + }); + const provider = new GitlabAuthProvider({ clientId, clientSecret, callbackUrl, baseUrl, - authHandler: options?.authHandler, - signInResolver: options?.signIn?.resolver, + authHandler, + signInResolver, catalogIdentityClient, logger, tokenIssuer,