Skip to content

Commit cf59102

Browse files
authored
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
1 parent ca838a0 commit cf59102

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

server/api/externalapi.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@ class ExternalAPI {
3232
this.fetch = fetch;
3333
}
3434

35-
this.baseUrl = baseUrl;
36-
this.params = params;
35+
const url = new URL(baseUrl);
36+
3737
this.defaultHeaders = {
3838
'Content-Type': 'application/json',
3939
Accept: 'application/json',
40+
...((url.username || url.password) && {
41+
Authorization: `Basic ${Buffer.from(
42+
`${url.username}:${url.password}`
43+
).toString('base64')}`,
44+
}),
4045
...options.headers,
4146
};
47+
48+
if (url.username || url.password) {
49+
url.username = '';
50+
url.password = '';
51+
baseUrl = url.toString();
52+
}
53+
54+
this.baseUrl = baseUrl;
55+
this.params = params;
4256
this.cache = options.nodeCache;
4357
}
4458

0 commit comments

Comments
 (0)