user-settings-backend: remove usage of identity service

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-08-21 12:12:14 +02:00
parent dae74b0d05
commit 1b9809927a
4 changed files with 43 additions and 13 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-user-settings-backend': patch
---
Replaced usage of the deprecated identity service with the new HTTP auth service for the new backend system.
+2 -2
View File
@@ -3,9 +3,9 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { DatabaseService } from '@backstage/backend-plugin-api';
import express from 'express';
import { IdentityApi } from '@backstage/plugin-auth-node';
import { PluginDatabaseManager } from '@backstage/backend-common';
import { SignalsService } from '@backstage/plugin-signals-node';
// @public
@@ -14,7 +14,7 @@ export function createRouter(options: RouterOptions): Promise<express.Router>;
// @public (undocumented)
export interface RouterOptions {
// (undocumented)
database: PluginDatabaseManager;
database: DatabaseService;
// (undocumented)
identity: IdentityApi;
// (undocumented)
+10 -4
View File
@@ -18,8 +18,9 @@ import {
coreServices,
createBackendPlugin,
} from '@backstage/backend-plugin-api';
import { createRouter } from './service/router';
import { createRouterInternal } from './service/router';
import { signalsServiceRef } from '@backstage/plugin-signals-node';
import { DatabaseUserSettingsStore } from './database/DatabaseUserSettingsStore';
/**
* The user settings backend plugin.
@@ -32,12 +33,17 @@ export default createBackendPlugin({
env.registerInit({
deps: {
database: coreServices.database,
identity: coreServices.identity,
httpAuth: coreServices.httpAuth,
httpRouter: coreServices.httpRouter,
signals: signalsServiceRef,
},
async init({ database, identity, httpRouter, signals }) {
httpRouter.use(await createRouter({ database, identity, signals }));
async init({ database, httpAuth, httpRouter, signals }) {
const userSettingsStore = await DatabaseUserSettingsStore.create({
database,
});
httpRouter.use(
await createRouterInternal({ userSettingsStore, httpAuth, signals }),
);
},
});
},
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { errorHandler, PluginDatabaseManager } from '@backstage/backend-common';
import { errorHandler } from '@backstage/backend-common';
import { AuthenticationError, InputError } from '@backstage/errors';
import { IdentityApi } from '@backstage/plugin-auth-node';
import express, { Request } from 'express';
@@ -23,12 +23,16 @@ import { DatabaseUserSettingsStore } from '../database/DatabaseUserSettingsStore
import { UserSettingsStore } from '../database/UserSettingsStore';
import { SignalsService } from '@backstage/plugin-signals-node';
import { UserSettingsSignal } from '@backstage/plugin-user-settings-common';
import {
DatabaseService,
HttpAuthService,
} from '@backstage/backend-plugin-api';
/**
* @public
*/
export interface RouterOptions {
database: PluginDatabaseManager;
database: DatabaseService;
identity: IdentityApi;
signals?: SignalsService;
}
@@ -52,11 +56,19 @@ export async function createRouter(
});
}
export async function createRouterInternal(options: {
identity: IdentityApi;
userSettingsStore: UserSettingsStore;
signals?: SignalsService;
}): Promise<express.Router> {
export async function createRouterInternal(
options:
| {
identity: IdentityApi;
userSettingsStore: UserSettingsStore;
signals?: SignalsService;
}
| {
httpAuth: HttpAuthService;
userSettingsStore: UserSettingsStore;
signals?: SignalsService;
},
): Promise<express.Router> {
const router = Router();
router.use(express.json());
@@ -64,6 +76,13 @@ export async function createRouterInternal(options: {
* Helper method to extract the userEntityRef from the request.
*/
const getUserEntityRef = async (req: Request): Promise<string> => {
if ('httpAuth' in options) {
const credentials = await options.httpAuth.credentials(req, {
allow: ['user'],
});
return credentials.principal.userEntityRef;
}
// throws an AuthenticationError in case the token exists but is invalid
const identity = await options.identity.getIdentity({ request: req });
if (!identity) {