[catalog] replace getBearerToken with library

Reduce code duplication by replacing `getBearerToken` with
the one from `@backstage/plugin-auth-node`

Signed-off-by: Rickard Dybeck <dybeck@spotify.com>
This commit is contained in:
Rickard Dybeck
2023-05-25 13:53:42 -04:00
parent 7dd8b39a92
commit 77e04a2d55
4 changed files with 45 additions and 23 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Replace getBearerToken with library function of same
+1
View File
@@ -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:^",
@@ -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];
}
+1
View File
@@ -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:^"