From 843a0a158c89e9945bbc64776c2abc91bd661bc0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 11 Jan 2023 11:48:59 +0100 Subject: [PATCH] backend-plugin-api: added new core identity service Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .changeset/flat-toes-repair.md | 5 ++ .changeset/forty-chairs-judge.md | 5 ++ .changeset/moody-waves-cover.md | 5 ++ packages/backend-app-api/api-report.md | 12 +++++ packages/backend-app-api/package.json | 1 + .../identity/identityFactory.ts | 50 +++++++++++++++++++ .../implementations/identity/index.ts | 18 +++++++ .../src/services/implementations/index.ts | 1 + .../backend-defaults/src/CreateBackend.ts | 12 +++-- packages/backend-plugin-api/api-report.md | 5 ++ packages/backend-plugin-api/package.json | 1 + .../services/definitions/IdentityService.ts | 20 ++++++++ .../src/services/definitions/coreServices.ts | 9 ++++ .../src/services/definitions/index.ts | 1 + yarn.lock | 2 + 15 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 .changeset/flat-toes-repair.md create mode 100644 .changeset/forty-chairs-judge.md create mode 100644 .changeset/moody-waves-cover.md create mode 100644 packages/backend-app-api/src/services/implementations/identity/identityFactory.ts create mode 100644 packages/backend-app-api/src/services/implementations/identity/index.ts create mode 100644 packages/backend-plugin-api/src/services/definitions/IdentityService.ts diff --git a/.changeset/flat-toes-repair.md b/.changeset/flat-toes-repair.md new file mode 100644 index 0000000000..67b752d04d --- /dev/null +++ b/.changeset/flat-toes-repair.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-defaults': patch +--- + +Added factory for the new core identity service to the set of default service factories. diff --git a/.changeset/forty-chairs-judge.md b/.changeset/forty-chairs-judge.md new file mode 100644 index 0000000000..ebee8631f9 --- /dev/null +++ b/.changeset/forty-chairs-judge.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-app-api': patch +--- + +Added service factory for the new core identity service. diff --git a/.changeset/moody-waves-cover.md b/.changeset/moody-waves-cover.md new file mode 100644 index 0000000000..1833cb1ffa --- /dev/null +++ b/.changeset/moody-waves-cover.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-plugin-api': patch +--- + +Added new core identity service. diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md index cc84309a2f..a8f077b760 100644 --- a/packages/backend-app-api/api-report.md +++ b/packages/backend-app-api/api-report.md @@ -15,6 +15,7 @@ import { ExtensionPoint } from '@backstage/backend-plugin-api'; import { HelmetOptions } from 'helmet'; import * as http from 'http'; import { HttpRouterService } from '@backstage/backend-plugin-api'; +import { IdentityService } from '@backstage/backend-plugin-api'; import { LifecycleService } from '@backstage/backend-plugin-api'; import { LoggerService } from '@backstage/backend-plugin-api'; import { PermissionsService } from '@backstage/backend-plugin-api'; @@ -126,6 +127,17 @@ export type HttpServerOptions = { }; }; +// @public (undocumented) +export const identityFactory: ( + options?: IdentityFactoryOptions | undefined, +) => ServiceFactory; + +// @public +export type IdentityFactoryOptions = { + issuer?: string; + algorithms?: string[]; +}; + // @public export const lifecycleFactory: ( options?: undefined, diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json index 2193fe8f3b..a6ecb5f455 100644 --- a/packages/backend-app-api/package.json +++ b/packages/backend-app-api/package.json @@ -38,6 +38,7 @@ "@backstage/backend-tasks": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", + "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-permission-node": "workspace:^", "@types/cors": "^2.8.6", "@types/express": "^4.17.6", diff --git a/packages/backend-app-api/src/services/implementations/identity/identityFactory.ts b/packages/backend-app-api/src/services/implementations/identity/identityFactory.ts new file mode 100644 index 0000000000..f554927930 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/identity/identityFactory.ts @@ -0,0 +1,50 @@ +/* + * Copyright 2022 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 { + coreServices, + createServiceFactory, +} from '@backstage/backend-plugin-api'; +import { DefaultIdentityClient } from '@backstage/plugin-auth-node'; + +/** + * An identity client options object which allows extra configurations + * + * @public + */ +export type IdentityFactoryOptions = { + issuer?: string; + + /** JWS "alg" (Algorithm) Header Parameter values. Defaults to an array containing just ES256. + * More info on supported algorithms: https://github.com/panva/jose */ + algorithms?: string[]; +}; + +/** @public */ +export const identityFactory = createServiceFactory({ + service: coreServices.identity, + deps: { + config: coreServices.config, + discovery: coreServices.discovery, + tokenManager: coreServices.tokenManager, + }, + + async factory({}, options?: IdentityFactoryOptions) { + return async ({ discovery }) => { + return DefaultIdentityClient.create({ discovery, ...options }); + }; + }, +}); diff --git a/packages/backend-app-api/src/services/implementations/identity/index.ts b/packages/backend-app-api/src/services/implementations/identity/index.ts new file mode 100644 index 0000000000..9382232620 --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/identity/index.ts @@ -0,0 +1,18 @@ +/* + * Copyright 2023 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. + */ + +export { identityFactory } from './identityFactory'; +export type { IdentityFactoryOptions } from './identityFactory'; diff --git a/packages/backend-app-api/src/services/implementations/index.ts b/packages/backend-app-api/src/services/implementations/index.ts index 8b9d0c0679..57042951d9 100644 --- a/packages/backend-app-api/src/services/implementations/index.ts +++ b/packages/backend-app-api/src/services/implementations/index.ts @@ -19,6 +19,7 @@ export * from './config'; export * from './database'; export * from './discovery'; export * from './httpRouter'; +export * from './identity'; export * from './lifecycle'; export * from './logger'; export * from './permissions'; diff --git a/packages/backend-defaults/src/CreateBackend.ts b/packages/backend-defaults/src/CreateBackend.ts index bef80e4f2b..fe82fb671c 100644 --- a/packages/backend-defaults/src/CreateBackend.ts +++ b/packages/backend-defaults/src/CreateBackend.ts @@ -31,6 +31,7 @@ import { schedulerFactory, tokenManagerFactory, urlReaderFactory, + identityFactory, } from '@backstage/backend-app-api'; import { ServiceFactoryOrFunction } from '@backstage/backend-plugin-api'; @@ -39,16 +40,17 @@ export const defaultServiceFactories = [ configFactory(), databaseFactory(), discoveryFactory(), + httpRouterFactory(), + identityFactory(), + lifecycleFactory(), loggerFactory(), - rootLoggerFactory(), permissionsFactory(), + rootHttpRouterFactory(), + rootLifecycleFactory(), + rootLoggerFactory(), schedulerFactory(), tokenManagerFactory(), urlReaderFactory(), - httpRouterFactory(), - rootHttpRouterFactory(), - lifecycleFactory(), - rootLifecycleFactory(), ]; /** diff --git a/packages/backend-plugin-api/api-report.md b/packages/backend-plugin-api/api-report.md index 5c689866c7..213815f782 100644 --- a/packages/backend-plugin-api/api-report.md +++ b/packages/backend-plugin-api/api-report.md @@ -7,6 +7,7 @@ import { Config } from '@backstage/config'; import { Handler } from 'express'; +import { IdentityApi } from '@backstage/plugin-auth-node'; import { JsonValue } from '@backstage/types'; import { Knex } from 'knex'; import { PermissionEvaluator } from '@backstage/plugin-permission-common'; @@ -108,6 +109,7 @@ export namespace coreServices { const scheduler: ServiceRef; const tokenManager: ServiceRef; const urlReader: ServiceRef; + const identity: ServiceRef; } // @public @@ -182,6 +184,9 @@ export interface HttpRouterService { use(handler: Handler): void; } +// @public (undocumented) +export interface IdentityService extends IdentityApi {} + // @public (undocumented) export interface LifecycleService { addShutdownHook(options: LifecycleServiceShutdownHook): void; diff --git a/packages/backend-plugin-api/package.json b/packages/backend-plugin-api/package.json index 1f56836a9a..40187564ce 100644 --- a/packages/backend-plugin-api/package.json +++ b/packages/backend-plugin-api/package.json @@ -35,6 +35,7 @@ "dependencies": { "@backstage/backend-tasks": "workspace:^", "@backstage/config": "workspace:^", + "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-permission-common": "workspace:^", "@backstage/types": "workspace:^", "@types/express": "^4.17.6", diff --git a/packages/backend-plugin-api/src/services/definitions/IdentityService.ts b/packages/backend-plugin-api/src/services/definitions/IdentityService.ts new file mode 100644 index 0000000000..330aaf6447 --- /dev/null +++ b/packages/backend-plugin-api/src/services/definitions/IdentityService.ts @@ -0,0 +1,20 @@ +/* + * Copyright 2020 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 { IdentityApi } from '@backstage/plugin-auth-node'; + +/** @public */ +export interface IdentityService extends IdentityApi {} diff --git a/packages/backend-plugin-api/src/services/definitions/coreServices.ts b/packages/backend-plugin-api/src/services/definitions/coreServices.ts index 7de92d6d01..7dad94cebf 100644 --- a/packages/backend-plugin-api/src/services/definitions/coreServices.ts +++ b/packages/backend-plugin-api/src/services/definitions/coreServices.ts @@ -156,4 +156,13 @@ export namespace coreServices { export const urlReader = createServiceRef< import('./UrlReaderService').UrlReaderService >({ id: 'core.urlReader' }); + + /** + * The service reference for the plugin scoped {@link IdentityService}. + * + * @public + */ + export const identity = createServiceRef< + import('./IdentityService').IdentityService + >({ id: 'core.identity' }); } diff --git a/packages/backend-plugin-api/src/services/definitions/index.ts b/packages/backend-plugin-api/src/services/definitions/index.ts index 99b3cbd06d..c5af0967bd 100644 --- a/packages/backend-plugin-api/src/services/definitions/index.ts +++ b/packages/backend-plugin-api/src/services/definitions/index.ts @@ -49,3 +49,4 @@ export type { SearchResponseFile, UrlReaderService, } from './UrlReaderService'; +export type { IdentityService } from './IdentityService'; diff --git a/yarn.lock b/yarn.lock index 6bf178b13d..de235b173c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3383,6 +3383,7 @@ __metadata: "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" "@backstage/errors": "workspace:^" + "@backstage/plugin-auth-node": "workspace:^" "@backstage/plugin-permission-node": "workspace:^" "@types/compression": ^1.7.0 "@types/cors": ^2.8.6 @@ -3516,6 +3517,7 @@ __metadata: "@backstage/backend-tasks": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" + "@backstage/plugin-auth-node": "workspace:^" "@backstage/plugin-permission-common": "workspace:^" "@backstage/types": "workspace:^" "@types/express": ^4.17.6