diff --git a/.changeset/itchy-mirrors-greet.md b/.changeset/itchy-mirrors-greet.md new file mode 100644 index 0000000000..3cd0dc6c35 --- /dev/null +++ b/.changeset/itchy-mirrors-greet.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Replace getBearerToken with library function of same diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index b22541bda1..d2bc040879 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -53,6 +53,7 @@ "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/integration": "workspace:^", + "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-catalog-common": "workspace:^", "@backstage/plugin-catalog-node": "workspace:^", "@backstage/plugin-permission-common": "workspace:^", diff --git a/plugins/catalog-backend/src/service/createRouter.ts b/plugins/catalog-backend/src/service/createRouter.ts index 5ac1f2eda7..997cd481a6 100644 --- a/plugins/catalog-backend/src/service/createRouter.ts +++ b/plugins/catalog-backend/src/service/createRouter.ts @@ -53,6 +53,7 @@ import { import type { ApiRouter } from '@backstage/backend-openapi-utils'; import spec from '../schema/openapi.generated'; import { PluginTaskScheduler } from '@backstage/backend-tasks'; +import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node'; /** * Options used by {@link createRouter}. @@ -101,7 +102,7 @@ export async function createRouter( if (refreshService) { router.post('/refresh', async (req, res) => { const refreshOptions: RefreshOptions = req.body; - refreshOptions.authorizationToken = getBearerToken( + refreshOptions.authorizationToken = getBearerTokenFromAuthorizationHeader( req.header('authorization'), ); @@ -122,7 +123,9 @@ export async function createRouter( fields: parseEntityTransformParams(req.query), order: parseEntityOrderParams(req.query), pagination: parseEntityPaginationParams(req.query), - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); // Add a Link header to the next page @@ -140,7 +143,9 @@ export async function createRouter( const { items, pageInfo, totalItems } = await entitiesCatalog.queryEntities({ ...parseQueryEntitiesParams(req.query), - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.json({ @@ -160,7 +165,9 @@ export async function createRouter( const { uid } = req.params; const { entities } = await entitiesCatalog.entities({ filter: basicEntityFilter({ 'metadata.uid': uid }), - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); if (!entities.length) { throw new NotFoundError(`No entity with uid ${uid}`); @@ -170,7 +177,9 @@ export async function createRouter( .delete('/entities/by-uid/:uid', async (req, res) => { const { uid } = req.params; await entitiesCatalog.removeEntityByUid(uid, { - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.status(204).end(); }) @@ -182,7 +191,9 @@ export async function createRouter( 'metadata.namespace': namespace, 'metadata.name': name, }), - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); if (!entities.length) { throw new NotFoundError( @@ -197,14 +208,18 @@ export async function createRouter( const { kind, namespace, name } = req.params; const entityRef = stringifyEntityRef({ kind, namespace, name }); const response = await entitiesCatalog.entityAncestry(entityRef, { - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.status(200).json(response); }, ) .post('/entities/by-refs', async (req, res) => { const request = entitiesBatchRequest(req); - const token = getBearerToken(req.header('authorization')); + const token = getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ); const response = await entitiesCatalog.entitiesBatch({ entityRefs: request.entityRefs, fields: parseEntityTransformParams(req.query, request.fields), @@ -216,7 +231,9 @@ export async function createRouter( const response = await entitiesCatalog.facets({ filter: parseEntityFilterParams(req.query), facets: parseEntityFacetParams(req.query), - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.status(200).json(response); }); @@ -235,13 +252,17 @@ export async function createRouter( } const output = await locationService.createLocation(location, dryRun, { - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.status(201).json(output); }) .get('/locations', async (req, res) => { const locations = await locationService.listLocations({ - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.status(200).json(locations.map(l => ({ data: l }))); }) @@ -249,7 +270,9 @@ export async function createRouter( .get('/locations/:id', async (req, res) => { const { id } = req.params; const output = await locationService.getLocation(id, { - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.status(200).json(output); }) @@ -258,7 +281,9 @@ export async function createRouter( const { id } = req.params; await locationService.deleteLocation(id, { - authorizationToken: getBearerToken(req.header('authorization')), + authorizationToken: getBearerTokenFromAuthorizationHeader( + req.header('authorization'), + ), }); res.status(204).end(); }); @@ -331,13 +356,3 @@ export async function createRouter( router.use(errorHandler()); return router; } - -function getBearerToken( - authorizationHeader: string | undefined, -): string | undefined { - if (typeof authorizationHeader !== 'string') { - return undefined; - } - const matches = authorizationHeader.match(/Bearer\s+(\S+)/i); - return matches?.[1]; -} diff --git a/yarn.lock b/yarn.lock index 712fd10e6f..8415e4b6ae 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5586,6 +5586,7 @@ __metadata: "@backstage/config": "workspace:^" "@backstage/errors": "workspace:^" "@backstage/integration": "workspace:^" + "@backstage/plugin-auth-node": "workspace:^" "@backstage/plugin-catalog-common": "workspace:^" "@backstage/plugin-catalog-node": "workspace:^" "@backstage/plugin-permission-common": "workspace:^"