From ec9e1d2103135334ccbbd0fad47b666234fe40e2 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Thu, 7 Mar 2024 09:38:10 +0100 Subject: [PATCH] feat: migrate non cokie endpoints Signed-off-by: Camila Belo --- plugins/techdocs-backend/src/plugin.ts | 3 ++ .../techdocs-backend/src/service/router.ts | 42 ++++++++++++++----- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/plugins/techdocs-backend/src/plugin.ts b/plugins/techdocs-backend/src/plugin.ts index b576d6577c..e78339cfa6 100644 --- a/plugins/techdocs-backend/src/plugin.ts +++ b/plugins/techdocs-backend/src/plugin.ts @@ -73,6 +73,7 @@ export const techdocsPlugin = createBackendPlugin({ discovery: coreServices.discovery, cache: coreServices.cache, httpAuth: coreServices.httpAuth, + auth: coreServices.auth, }, async init({ config, @@ -82,6 +83,7 @@ export const techdocsPlugin = createBackendPlugin({ discovery, cache, httpAuth, + auth, }) { const winstonLogger = loggerToWinstonLogger(logger); // Preparers are responsible for fetching source files for documentation. @@ -124,6 +126,7 @@ export const techdocsPlugin = createBackendPlugin({ config, discovery, httpAuth, + auth, }), ); diff --git a/plugins/techdocs-backend/src/service/router.ts b/plugins/techdocs-backend/src/service/router.ts index 10388cea28..e3b1cd5dea 100644 --- a/plugins/techdocs-backend/src/service/router.ts +++ b/plugins/techdocs-backend/src/service/router.ts @@ -38,7 +38,7 @@ import { createCacheMiddleware, TechDocsCache } from '../cache'; import { CachedEntityLoader } from './CachedEntityLoader'; import { DefaultDocsBuildStrategy } from './DefaultDocsBuildStrategy'; import * as winston from 'winston'; -import { HttpAuthService } from '@backstage/backend-plugin-api'; +import { AuthService, HttpAuthService } from '@backstage/backend-plugin-api'; /** * Required dependencies for running TechDocs in the "out-of-the-box" @@ -59,6 +59,7 @@ export type OutOfTheBoxDeploymentOptions = { buildLogTransport?: winston.transport; catalogClient?: CatalogClient; httpAuth?: HttpAuthService; + auth?: AuthService; }; /** @@ -77,6 +78,7 @@ export type RecommendedDeploymentOptions = { buildLogTransport?: winston.transport; catalogClient?: CatalogClient; httpAuth?: HttpAuthService; + auth?: AuthService; }; /** @@ -111,7 +113,7 @@ export async function createRouter( const router = Router(); const { publisher, config, logger, discovery } = options; - const { httpAuth } = createLegacyAuthAdapters(options); + const { auth, httpAuth } = createLegacyAuthAdapters(options); const catalogClient = options.catalogClient ?? new CatalogClient({ discoveryApi: discovery }); @@ -147,7 +149,13 @@ export async function createRouter( router.get('/metadata/techdocs/:namespace/:kind/:name', async (req, res) => { const { kind, namespace, name } = req.params; const entityName = { kind, namespace, name }; - const token = getBearerToken(req.headers.authorization); + + const credentials = await httpAuth.credentials(req); + + const { token } = await auth.getPluginRequestToken({ + onBehalfOf: credentials, + targetPluginId: 'catalog', + }); // Verify that the related entity exists and the current user has permission to view it. const entity = await entityLoader.load(entityName, token); @@ -180,7 +188,13 @@ export async function createRouter( router.get('/metadata/entity/:namespace/:kind/:name', async (req, res) => { const { kind, namespace, name } = req.params; const entityName = { kind, namespace, name }; - const token = getBearerToken(req.headers.authorization); + + const credentials = await httpAuth.credentials(req); + + const { token } = await auth.getPluginRequestToken({ + onBehalfOf: credentials, + targetPluginId: 'catalog', + }); const entity = await entityLoader.load(entityName, token); @@ -212,7 +226,13 @@ export async function createRouter( // If a build is required, responds with a success when finished router.get('/sync/:namespace/:kind/:name', async (req, res) => { const { kind, namespace, name } = req.params; - const token = getBearerToken(req.headers.authorization); + + const credentials = await httpAuth.credentials(req); + + const { token } = await auth.getPluginRequestToken({ + onBehalfOf: credentials, + targetPluginId: 'catalog', + }); const entity = await entityLoader.load({ kind, namespace, name }, token); @@ -271,7 +291,13 @@ export async function createRouter( async (req, _res, next) => { const { kind, namespace, name } = req.params; const entityName = { kind, namespace, name }; - const token = getBearerToken(req.headers.authorization); + + const credentials = await httpAuth.credentials(req); + + const { token } = await auth.getPluginRequestToken({ + onBehalfOf: credentials, + targetPluginId: 'catalog', + }); const entity = await entityLoader.load(entityName, token); @@ -303,10 +329,6 @@ export async function createRouter( return router; } -function getBearerToken(header?: string): string | undefined { - return header?.match(/(?:Bearer)\s+(\S+)/i)?.[1]; -} - /** * Create an event-stream response that emits the events 'log', 'error', and 'finish'. *