From 481637b97dbb630abe622ab42a5aaaf7fd0ae49d Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:18:49 +0200 Subject: [PATCH 1/5] feat: add parameter for Bitbucket `ScmAuth` scopes Signed-off-by: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> --- packages/integration-react/report.api.md | 4 ++++ packages/integration-react/src/api/ScmAuth.ts | 20 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/integration-react/report.api.md b/packages/integration-react/report.api.md index c1a2e34907..ee6eb0f40a 100644 --- a/packages/integration-react/report.api.md +++ b/packages/integration-react/report.api.md @@ -55,6 +55,10 @@ export class ScmAuth implements ScmAuthApi { bitbucketAuthApi: OAuthApi, options?: { host?: string; + scopeMapping?: { + default?: string[]; + repoWrite?: string[]; + }; }, ): ScmAuth; static forGithub( diff --git a/packages/integration-react/src/api/ScmAuth.ts b/packages/integration-react/src/api/ScmAuth.ts index 6257915c8c..a751e43ce4 100644 --- a/packages/integration-react/src/api/ScmAuth.ts +++ b/packages/integration-react/src/api/ScmAuth.ts @@ -226,12 +226,28 @@ export class ScmAuth implements ScmAuthApi { bitbucketAuthApi: OAuthApi, options?: { host?: string; + scopeMapping?: { + default?: string[]; + repoWrite?: string[]; + }; }, ): ScmAuth { const host = options?.host ?? 'bitbucket.org'; + const defaultScopes = options?.scopeMapping?.default ?? [ + 'account', + 'team', + 'pullrequest', + 'snippet', + 'issue', + ]; + const repoWriteScopes = options?.scopeMapping?.repoWrite ?? [ + 'pullrequest:write', + 'snippet:write', + 'issue:write', + ]; return new ScmAuth('bitbucket', bitbucketAuthApi, host, { - default: ['account', 'team', 'pullrequest', 'snippet', 'issue'], - repoWrite: ['pullrequest:write', 'snippet:write', 'issue:write'], + default: defaultScopes, + repoWrite: repoWriteScopes, }); } From f69821a0f86da6c6a80d452085192791eb70ae1a Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:23:28 +0200 Subject: [PATCH 2/5] feat: add `ScmAuth` provider for Bitbucket Server This fixes a bug where it used the same scopes as Bitbucket Cloud, even though they are incompatible with Bitbucket Server. Fixes #26803 Signed-off-by: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> --- packages/integration-react/report.api.md | 10 ++++++ packages/integration-react/src/api/ScmAuth.ts | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/packages/integration-react/report.api.md b/packages/integration-react/report.api.md index ee6eb0f40a..8d02c46010 100644 --- a/packages/integration-react/report.api.md +++ b/packages/integration-react/report.api.md @@ -61,6 +61,16 @@ export class ScmAuth implements ScmAuthApi { }; }, ): ScmAuth; + static forBitbucketServer( + bitbucketAuthApi: OAuthApi, + options: { + host: string; + scopeMapping?: { + default?: string[]; + repoWrite?: string[]; + }; + }, + ): ScmAuth; static forGithub( githubAuthApi: OAuthApi, options?: { diff --git a/packages/integration-react/src/api/ScmAuth.ts b/packages/integration-react/src/api/ScmAuth.ts index a751e43ce4..4042088fd5 100644 --- a/packages/integration-react/src/api/ScmAuth.ts +++ b/packages/integration-react/src/api/ScmAuth.ts @@ -251,6 +251,41 @@ export class ScmAuth implements ScmAuthApi { }); } + /** + * Creates a new ScmAuth instance that handles authentication towards Bitbucket Server. + * + * The host option determines which URLs that are handled by this instance. + * + * The default scopes are: + * + * `PUBLIC_REPOS REPOS_READ` + * + * If the additional `repoWrite` permission is requested, these scopes are added: + * + * `REPO_WRITE` + */ + static forBitbucketServer( + bitbucketAuthApi: OAuthApi, + options: { + host: string; + scopeMapping?: { + default?: string[]; + repoWrite?: string[]; + }; + }, + ): ScmAuth { + return this.forBitbucket(bitbucketAuthApi, { + host: options.host, + scopeMapping: { + default: options.scopeMapping?.default ?? [ + 'PUBLIC_REPOS', + 'REPOS_READ', + ], + repoWrite: options.scopeMapping?.repoWrite ?? ['REPO_WRITE'], + }, + }); + } + /** * Merges together multiple ScmAuth instances into one that * routes requests to the correct instance based on the URL. From 549cd588fd9fe365eb256cd3a04a561a96545f4c Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:35:01 +0200 Subject: [PATCH 3/5] doc: improvements for Bitbucket Server auth Previously, it did not mention updating the sign-in page. Also it skipped configuring the ScmAuth provider for non-default hosts, which is always necessary for Bitbucket Server. Signed-off-by: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> --- docs/auth/bitbucketServer/provider.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/auth/bitbucketServer/provider.md b/docs/auth/bitbucketServer/provider.md index b15f0b206a..c41457d09c 100644 --- a/docs/auth/bitbucketServer/provider.md +++ b/docs/auth/bitbucketServer/provider.md @@ -31,7 +31,7 @@ auth: providers: bitbucketServer: development: - host: bitbucket.org + host: bitbucket.example.org clientId: ${AUTH_BITBUCKET_SERVER_CLIENT_ID} clientSecret: ${AUTH_BITBUCKET_SERVER_CLIENT_SECRET} ``` @@ -77,7 +77,14 @@ backend.add( //... ``` -## Adding the provider to the Backstage frontend +## Frontend Configuration -To add the provider to the frontend, add the `bitbucketServerAuthApi` reference and `SignInPage` component as shown -in [Adding the provider to the sign-in page](../index.md#sign-in-configuration). +### Sign-in + +To add the provider to the frontend, add the `bitbucketServerAuthApiRef` reference and +`SignInPage` component as shown in +[Adding the provider to the sign-in page](../index.md#sign-in-configuration). + +### ScmAuth + +For backstage to be able to use the oauth token of the logged in user to access the Bitbucket Server API, you need to add it to list of ScmAuth providers as shown in [Custom ScmAuthApi Implementation](../index.md#custom-scmauthapi-implementation) using the `ScmAuth.forBitbucketServer` method. From a11495aa740b4fc375184c3850ed27b4411cabdb Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:43:37 +0200 Subject: [PATCH 4/5] doc: add changeset for Bitbucket Server ScmAuth changes Signed-off-by: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> --- .changeset/pretty-plants-hammer.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/pretty-plants-hammer.md diff --git a/.changeset/pretty-plants-hammer.md b/.changeset/pretty-plants-hammer.md new file mode 100644 index 0000000000..7cce64d19a --- /dev/null +++ b/.changeset/pretty-plants-hammer.md @@ -0,0 +1,6 @@ +--- +'@backstage/integration-react': minor +--- + +Add ScmAuth provider for Bitbucket Server that uses correct OAuth scopes by default. +Also allows for overriding the default OAuth scopes. From 955a5df0d016a44662d0216c731e93b9dc5331c7 Mon Sep 17 00:00:00 2001 From: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:45:22 +0200 Subject: [PATCH 5/5] Update .changeset/pretty-plants-hammer.md Co-authored-by: Johan Haals Signed-off-by: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com> --- .changeset/pretty-plants-hammer.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.changeset/pretty-plants-hammer.md b/.changeset/pretty-plants-hammer.md index 7cce64d19a..c070f769c4 100644 --- a/.changeset/pretty-plants-hammer.md +++ b/.changeset/pretty-plants-hammer.md @@ -2,5 +2,4 @@ '@backstage/integration-react': minor --- -Add ScmAuth provider for Bitbucket Server that uses correct OAuth scopes by default. -Also allows for overriding the default OAuth scopes. +Added new ScmAuth method `forBitbucketServer` that uses correct OAuth scopes by default. Also updated `forBitbucket` method to allow overriding the default OAuth scopes.