Skip to content

Commit e53b509

Browse files
committed
Support Headers objects.
Fixes #141
1 parent 897fc5e commit e53b509

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ describe('fetch', () => {
586586
expect(resp.status).toBe(200)
587587
})
588588

589-
it('merges request headers with the authorization header', async () => {
589+
it('merges an object of request headers with the authorization header', async () => {
590590
await saveSession(window.localStorage)(fakeSession)
591591

592592
nock('https://third-party.com')
@@ -606,6 +606,28 @@ describe('fetch', () => {
606606
expect(resp.status).toBe(200)
607607
})
608608

609+
it('merges a Header object with the authorization header', async () => {
610+
await saveSession(window.localStorage)(fakeSession)
611+
612+
nock('https://third-party.com')
613+
.get('/private-resource')
614+
.reply(401, '', { 'www-authenticate': 'Bearer scope="openid webid"' })
615+
.get('/private-resource')
616+
.matchHeader('accept', 'text/plain')
617+
.matchHeader('authorization', matchAuthzHeader('https://third-party.com'))
618+
.reply(200)
619+
620+
const resp = await instance.fetch(
621+
'https://third-party.com/private-resource',
622+
{
623+
headers: {
624+
entries: () => [['accept', 'text/plain']]
625+
}
626+
}
627+
)
628+
expect(resp.status).toBe(200)
629+
})
630+
609631
it('does not resend with credentials if the www-authenticate header is missing', async () => {
610632
expect.assertions(1)
611633
await saveSession(window.localStorage)(fakeSession)

src/webid-oidc.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,20 @@ export async function fetchWithCredentials(
219219
input: RequestInfo,
220220
options?: RequestOptions
221221
): Promise<Response> {
222-
const popToken = await PoPToken.issueFor(toUrlString(input), session)
223-
const authenticatedOptions = {
224-
...options,
225-
credentials: 'include',
226-
headers: {
227-
...(options && options.headers ? options.headers : {}),
228-
authorization: `Bearer ${popToken}`
222+
// Create a copy of the headers
223+
const headers = {}
224+
if (options && options.headers) {
225+
const entries =
226+
typeof options.headers.entries === 'function'
227+
? options.headers.entries()
228+
: Object.entries(options.headers)
229+
for (const [name, value] of entries) {
230+
headers[name] = value
229231
}
230232
}
231-
return fetch(input, authenticatedOptions)
233+
234+
// Add Authorization header
235+
const popToken = await PoPToken.issueFor(toUrlString(input), session)
236+
headers.authorization = `Bearer ${popToken}`
237+
return fetch(input, { ...options, credentials: 'include', headers })
232238
}

0 commit comments

Comments
 (0)