fix: support auth in badge plugin

Signed-off-by: Erik Larsson <erik.larsson@schibsted.com>
This commit is contained in:
Erik Larsson
2021-03-21 08:24:34 +01:00
parent dcb5d5e373
commit d0b4ebf22c
4 changed files with 41 additions and 7 deletions
+16 -2
View File
@@ -46,7 +46,12 @@ export async function createRouter(
router.get('/entity/:namespace/:kind/:name/badge-specs', async (req, res) => {
const { namespace, kind, name } = req.params;
const entity = await catalog.getEntityByName({ namespace, kind, name });
const entity = await catalog.getEntityByName(
{ namespace, kind, name },
{
token: getBearerToken(req.headers.authorization),
},
);
if (!entity) {
throw new NotFoundError(
`No ${kind} entity in ${namespace} named "${name}"`,
@@ -81,7 +86,12 @@ export async function createRouter(
'/entity/:namespace/:kind/:name/badge/:badgeId',
async (req, res) => {
const { namespace, kind, name, badgeId } = req.params;
const entity = await catalog.getEntityByName({ namespace, kind, name });
const entity = await catalog.getEntityByName(
{ namespace, kind, name },
{
token: getBearerToken(req.headers.authorization),
},
);
if (!entity) {
throw new NotFoundError(
`No ${kind} entity in ${namespace} named "${name}"`,
@@ -134,3 +144,7 @@ async function getBadgeUrl(
const baseUrl = await options.discovery.getExternalBaseUrl('badges');
return `${baseUrl}/entity/${namespace}/${kind}/${name}/badge/${badgeId}`;
}
function getBearerToken(header?: string): string | undefined {
return header?.match(/Bearer\s+(\S+)/i)?.[1];
}