From cf59102ef91fa0e907cc6369b0fe60b503c823ca Mon Sep 17 00:00:00 2001 From: Fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Sun, 3 Nov 2024 14:35:20 +0800 Subject: [PATCH] fix(externalapi): extract basic auth and pass it through header (#1062) This commit adds extraction of basic authentication credentials from the URL and then pass the credentials as the `Authorization` header. And then credentials are removed from the URL before being passed to fetch. This is done because fetch request cannot be constructed using a URL with credentials fix #1027 --- server/api/externalapi.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/server/api/externalapi.ts b/server/api/externalapi.ts index 0dfddefc9..0dc1f967d 100644 --- a/server/api/externalapi.ts +++ b/server/api/externalapi.ts @@ -32,13 +32,27 @@ class ExternalAPI { this.fetch = fetch; } - this.baseUrl = baseUrl; - this.params = params; + const url = new URL(baseUrl); + this.defaultHeaders = { 'Content-Type': 'application/json', Accept: 'application/json', + ...((url.username || url.password) && { + Authorization: `Basic ${Buffer.from( + `${url.username}:${url.password}` + ).toString('base64')}`, + }), ...options.headers, }; + + if (url.username || url.password) { + url.username = ''; + url.password = ''; + baseUrl = url.toString(); + } + + this.baseUrl = baseUrl; + this.params = params; this.cache = options.nodeCache; }