From dd48e3158286f4c5a9675fe7f8f9188816647083 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Thu, 19 Dec 2024 14:52:54 +0000 Subject: [PATCH] Add ability to provide custom request headers when fetching schedule --- src/__tests__/backends/json/JsonScheduleBackend.test.ts | 8 ++++++++ src/backends/json/JsonScheduleBackend.ts | 4 +++- src/config.ts | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/__tests__/backends/json/JsonScheduleBackend.test.ts b/src/__tests__/backends/json/JsonScheduleBackend.test.ts index 6d6611b..8025621 100644 --- a/src/__tests__/backends/json/JsonScheduleBackend.test.ts +++ b/src/__tests__/backends/json/JsonScheduleBackend.test.ts @@ -18,6 +18,11 @@ function jsonScheduleServer() { const json = getFixture("original_democon.json"); res.end(json); } else if (req.url === "/fosdem/p/matrix") { + if (req.headers.authorization !== "Bearer TOKEN") { + res.writeHead(401); + res.end("Not authorised"); + return; + } res.writeHead(200); const json = getFixture("fosdem_democon.json"); res.end(json); @@ -51,6 +56,9 @@ describe("JsonScheduleBackend", () => { scheduleDefinition: `http://127.0.0.1:${ (serv.address() as AddressInfo).port }/fosdem/p/matrix`, + scheduleRequestHeaders: { + "Authorization": "Bearer TOKEN" + } }, globalConfig ); diff --git a/src/backends/json/JsonScheduleBackend.ts b/src/backends/json/JsonScheduleBackend.ts index 8c3fd8c..674a62c 100644 --- a/src/backends/json/JsonScheduleBackend.ts +++ b/src/backends/json/JsonScheduleBackend.ts @@ -28,12 +28,14 @@ export class JsonScheduleBackend implements IScheduleBackend { let jsonDesc: any; let cached = false; + const cachedSchedulePath = path.join(dataPath, 'cached_schedule.json'); try { if (cfg.scheduleDefinition.startsWith("http")) { + const headers = cfg.scheduleRequestHeaders ?? {}; // Fetch the JSON track over the network - jsonDesc = await fetch(cfg.scheduleDefinition).then(r => r.json()); + jsonDesc = await fetch(cfg.scheduleDefinition, {headers}).then(r => r.json()); } else { // Load the JSON from disk jsonDesc = await readJsonFileAsync(cfg.scheduleDefinition); diff --git a/src/config.ts b/src/config.ts index 27e93a8..b5a2ff0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -133,6 +133,15 @@ export interface IJsonScheduleBackendConfig { * Defaults to original. */ scheduleFormat?: JsonScheduleFormat; + + /** + * Map of request headers to send when requesting the schedule definition. + * Useful for authenticating requests. + * Required for the FOSDEM-format schedules. + * + * Defaults to no headers. + */ + scheduleRequestHeaders?: {[_: string]: string}; } export enum PretalxScheduleFormat {