From b68b36b4558c9409e64c977f41530c5afce51a63 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 21 Feb 2024 14:54:37 +0100 Subject: [PATCH] backend-next: add auth backend with github provider for local development Signed-off-by: Patrik Oldsberg --- packages/backend-next/package.json | 2 + .../src/authModuleGithubProvider.ts | 57 +++++++++++++++++++ packages/backend-next/src/index.ts | 3 + yarn.lock | 4 +- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 packages/backend-next/src/authModuleGithubProvider.ts diff --git a/packages/backend-next/package.json b/packages/backend-next/package.json index 98ae4c6ac4..c1f09cd0cb 100644 --- a/packages/backend-next/package.json +++ b/packages/backend-next/package.json @@ -30,6 +30,8 @@ "@backstage/backend-tasks": "workspace:^", "@backstage/plugin-adr-backend": "workspace:^", "@backstage/plugin-app-backend": "workspace:^", + "@backstage/plugin-auth-backend": "workspace:^", + "@backstage/plugin-auth-backend-module-github-provider": "workspace:^", "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-azure-devops-backend": "workspace:^", "@backstage/plugin-badges-backend": "workspace:^", diff --git a/packages/backend-next/src/authModuleGithubProvider.ts b/packages/backend-next/src/authModuleGithubProvider.ts new file mode 100644 index 0000000000..5b71f35567 --- /dev/null +++ b/packages/backend-next/src/authModuleGithubProvider.ts @@ -0,0 +1,57 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { createBackendModule } from '@backstage/backend-plugin-api'; +import { githubAuthenticator } from '@backstage/plugin-auth-backend-module-github-provider'; +import { + authProvidersExtensionPoint, + createOAuthProviderFactory, +} from '@backstage/plugin-auth-node'; + +export default createBackendModule({ + pluginId: 'auth', + moduleId: 'githubProvider', + register(reg) { + reg.registerInit({ + deps: { providers: authProvidersExtensionPoint }, + async init({ providers }) { + providers.registerProvider({ + providerId: 'github', + factory: createOAuthProviderFactory({ + authenticator: githubAuthenticator, + async signInResolver({ result: { fullProfile } }, ctx) { + const userId = fullProfile.username; + if (!userId) { + throw new Error( + `GitHub user profile does not contain a username`, + ); + } + + const userEntityRef = `user:default/${userId}`; + + return ctx.issueToken({ + claims: { + sub: userEntityRef, + ent: [userEntityRef], + }, + }); + }, + }), + }); + }, + }); + }, +}); diff --git a/packages/backend-next/src/index.ts b/packages/backend-next/src/index.ts index 9c61b98ec4..e4dd127208 100644 --- a/packages/backend-next/src/index.ts +++ b/packages/backend-next/src/index.ts @@ -18,6 +18,9 @@ import { createBackend } from '@backstage/backend-defaults'; const backend = createBackend(); +backend.add(import('@backstage/plugin-auth-backend')); +backend.add(import('./authModuleGithubProvider')); + backend.add(import('@backstage/plugin-adr-backend')); backend.add(import('@backstage/plugin-app-backend/alpha')); backend.add(import('@backstage/plugin-azure-devops-backend')); diff --git a/yarn.lock b/yarn.lock index 218faf39a1..3726b83cf2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -27358,6 +27358,8 @@ __metadata: "@backstage/cli": "workspace:^" "@backstage/plugin-adr-backend": "workspace:^" "@backstage/plugin-app-backend": "workspace:^" + "@backstage/plugin-auth-backend": "workspace:^" + "@backstage/plugin-auth-backend-module-github-provider": "workspace:^" "@backstage/plugin-auth-node": "workspace:^" "@backstage/plugin-azure-devops-backend": "workspace:^" "@backstage/plugin-badges-backend": "workspace:^" @@ -32125,7 +32127,7 @@ __metadata: languageName: node linkType: hard -"jose@npm:^4.15.4": +"jose@npm:^4.15.4, jose@npm:^4.6.0": version: 4.15.4 resolution: "jose@npm:4.15.4" checksum: dccad91cb3357f36423774a0b89ad830dd84b31090de65cd139b85488439f16a00f8c59c0773825e8a1adb0dd9d13ad725ad66e6ea33880ecb3959bb99e1ea5b