Skip to content

Commit f284057

Browse files
Merge pull request #167 from solid/fix/headers-object
Ensure Headers objects are supported
2 parents 1dd5b26 + 14133f3 commit f284057

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

src/__test__/solid-auth-client.spec.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,26 @@ describe('fetch', () => {
619619
'https://third-party.com/private-resource',
620620
{
621621
headers: {
622-
entries: () => [['accept', 'text/plain']],
622+
forEach: (iterate) => iterate('text/plain', 'accept'),
623+
},
624+
}
625+
)
626+
expect(resp.status).toBe(200)
627+
})
628+
629+
it('accepts a Headers object when no authentication is needed', async () => {
630+
await saveSession(window.localStorage)(fakeSession)
631+
632+
nock('https://third-party.com')
633+
.get('/public-resource')
634+
.matchHeader('accept', 'text/plain')
635+
.reply(200)
636+
637+
const resp = await instance.fetch(
638+
'https://third-party.com/public-resource',
639+
{
640+
headers: {
641+
forEach: (iterate) => iterate('text/plain', 'accept'),
623642
},
624643
}
625644
)
@@ -642,7 +661,7 @@ describe('fetch', () => {
642661
const resp = await instance.fetch({
643662
url: 'https://third-party.com/private-resource',
644663
headers: {
645-
entries: () => [['accept', 'text/plain']],
664+
forEach: (iterate) => iterate('text/plain', 'accept'),
646665
},
647666
})
648667
expect(resp.status).toBe(200)

src/authn-fetch.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export async function authnFetch(
1313
input: RequestInfo,
1414
options?: RequestOptions
1515
): Promise<Response> {
16+
// Copy headers into a modifiable object
17+
if (options) {
18+
const headers = copyHeaders((options: any).headers)
19+
options = { ...options, headers }
20+
}
21+
1622
// If not authenticated, perform a regular fetch
1723
const session = await getSession(storage)
1824
if (!session) {
@@ -44,3 +50,19 @@ async function shouldShareCredentials(
4450
const requestHost = await getHost(storage)(toUrlString(input))
4551
return requestHost != null && requestHost.requiresAuth
4652
}
53+
54+
function copyHeaders(origHeaders: any) {
55+
const headers = {}
56+
if (origHeaders) {
57+
if (typeof origHeaders.forEach === 'function') {
58+
origHeaders.forEach((value, key) => {
59+
headers[key] = value
60+
})
61+
} else {
62+
for (const key in origHeaders) {
63+
headers[key] = origHeaders[key]
64+
}
65+
}
66+
}
67+
return headers
68+
}

src/webid-oidc.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,8 @@ export async function fetchWithCredentials(
227227
input: any,
228228
options?: RequestOptions
229229
): Promise<Response> {
230-
// Create a copy of the headers
231-
const headers = {}
232-
const origHeaders = options ? options.headers : input.headers
233-
if (origHeaders) {
234-
const entries =
235-
typeof origHeaders.entries === 'function'
236-
? origHeaders.entries()
237-
: Object.entries(origHeaders)
238-
for (const [name, value] of entries) {
239-
headers[name] = value
240-
}
241-
}
242-
243-
// Add Authorization header
230+
// Add Authorization header (assuming a modifiable headers object)
231+
const headers: any = (options ? options.headers : input.headers) || {}
244232
const popToken = await PoPToken.issueFor(toUrlString(input), session)
245233
headers.authorization = `Bearer ${popToken}`
246234
return fetch(input, { ...options, credentials: 'include', headers })

0 commit comments

Comments
 (0)