From 39295c1e1c0c939478a7b62e3298eff2435e0b57 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 8 Apr 2022 14:50:51 +0200 Subject: [PATCH] auth-backend: migrate bitbucket sign-in resolvers Signed-off-by: Patrik Oldsberg --- .../src/providers/bitbucket/provider.ts | 84 ++++++++++++------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/plugins/auth-backend/src/providers/bitbucket/provider.ts b/plugins/auth-backend/src/providers/bitbucket/provider.ts index 7a3e6c9a60..c448c222b4 100644 --- a/plugins/auth-backend/src/providers/bitbucket/provider.ts +++ b/plugins/auth-backend/src/providers/bitbucket/provider.ts @@ -192,38 +192,6 @@ export class BitbucketAuthProvider implements OAuthHandlers { } } -export const bitbucketUsernameSignInResolver: SignInResolver< - BitbucketOAuthResult -> = async (info, ctx) => { - const { result } = info; - - if (!result.fullProfile.username) { - throw new Error('Bitbucket profile contained no Username'); - } - - return ctx.signInWithCatalogUser({ - annotations: { - 'bitbucket.org/username': result.fullProfile.username, - }, - }); -}; - -export const bitbucketUserIdSignInResolver: SignInResolver< - BitbucketOAuthResult -> = async (info, ctx) => { - const { result } = info; - - if (!result.fullProfile.id) { - throw new Error('Bitbucket profile contained no User ID'); - } - - return ctx.signInWithCatalogUser({ - annotations: { - 'bitbucket.org/user-id': result.fullProfile.id, - }, - }); -}; - /** * @deprecated This type has been inlined into the create method and will be removed. */ @@ -300,6 +268,44 @@ export const bitbucket = createAuthProviderIntegration({ }); }); }, + resolvers: { + /** + * Looks up the user by matching their username to the `bitbucket.org/username` annotation. + */ + lookupUsernameAnnotation(): SignInResolver { + return async (info, ctx) => { + const { result } = info; + + if (!result.fullProfile.username) { + throw new Error('Bitbucket profile contained no Username'); + } + + return ctx.signInWithCatalogUser({ + annotations: { + 'bitbucket.org/username': result.fullProfile.username, + }, + }); + }; + }, + /** + * Looks up the user by matching their user ID to the `bitbucket.org/user-id` annotation. + */ + lookupUserIdAnnotation(): SignInResolver { + return async (info, ctx) => { + const { result } = info; + + if (!result.fullProfile.id) { + throw new Error('Bitbucket profile contained no User ID'); + } + + return ctx.signInWithCatalogUser({ + annotations: { + 'bitbucket.org/user-id': result.fullProfile.id, + }, + }); + }; + }, + }, }); /** @@ -307,3 +313,17 @@ export const bitbucket = createAuthProviderIntegration({ * @deprecated Use `providers.bitbucket.create` instead */ export const createBitbucketProvider = bitbucket.create; + +/** + * @public + * @deprecated Use `providers.bitbucket.resolvers.lookupUsernameAnnotation()` instead. + */ +export const bitbucketUsernameSignInResolver = + bitbucket.resolvers.lookupUsernameAnnotation(); + +/** + * @public + * @deprecated Use `providers.bitbucket.resolvers.lookupUserIdAnnotation()` instead. + */ +export const bitbucketUserIdSignInResolver = + bitbucket.resolvers.lookupUserIdAnnotation();