feat: add allow static cookie auth

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-03-05 15:59:24 +01:00
parent 580dcf8f09
commit 19d00fe372
2 changed files with 29 additions and 1 deletions
+16 -1
View File
@@ -72,8 +72,17 @@ export const techdocsPlugin = createBackendPlugin({
http: coreServices.httpRouter,
discovery: coreServices.discovery,
cache: coreServices.cache,
httpAuth: coreServices.httpAuth,
},
async init({ config, logger, urlReader, http, discovery, cache }) {
async init({
config,
logger,
urlReader,
http,
discovery,
cache,
httpAuth,
}) {
const winstonLogger = loggerToWinstonLogger(logger);
// Preparers are responsible for fetching source files for documentation.
const preparers = await Preparers.fromConfig(config, {
@@ -114,8 +123,14 @@ export const techdocsPlugin = createBackendPlugin({
publisher,
config,
discovery,
httpAuth,
}),
);
http.addAuthPolicy({
path: '/static',
allow: 'user-cookie',
});
},
});
},
@@ -16,6 +16,7 @@
import {
PluginEndpointDiscovery,
PluginCacheManager,
createLegacyAuthAdapters,
} from '@backstage/backend-common';
import { CatalogClient } from '@backstage/catalog-client';
import { stringifyEntityRef } from '@backstage/catalog-model';
@@ -37,6 +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';
/**
* Required dependencies for running TechDocs in the "out-of-the-box"
@@ -56,6 +58,7 @@ export type OutOfTheBoxDeploymentOptions = {
docsBuildStrategy?: DocsBuildStrategy;
buildLogTransport?: winston.transport;
catalogClient?: CatalogClient;
httpAuth?: HttpAuthService;
};
/**
@@ -73,6 +76,7 @@ export type RecommendedDeploymentOptions = {
docsBuildStrategy?: DocsBuildStrategy;
buildLogTransport?: winston.transport;
catalogClient?: CatalogClient;
httpAuth?: HttpAuthService;
};
/**
@@ -106,6 +110,9 @@ export async function createRouter(
): Promise<express.Router> {
const router = Router();
const { publisher, config, logger, discovery } = options;
const { httpAuth } = createLegacyAuthAdapters(options);
const catalogClient =
options.catalogClient ?? new CatalogClient({ discoveryApi: discovery });
const docsBuildStrategy =
@@ -287,6 +294,12 @@ export async function createRouter(
// Route middleware which serves files from the storage set in the publisher.
router.use('/static/docs', publisher.docsRouter());
// Endpoint that sets the cookie for the user
router.get('/cookie', async (req, res) => {
const { expiresAt } = await httpAuth.issueUserCookie(req);
res.json({ expiresAt: expiresAt.toISOString() });
});
return router;
}