Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/__test__/solid-auth-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,26 @@ describe('fetch', () => {
'https://third-party.com/private-resource',
{
headers: {
entries: () => [['accept', 'text/plain']],
forEach: (iterate) => iterate('text/plain', 'accept'),
},
}
)
expect(resp.status).toBe(200)
})

it('accepts a Headers object when no authentication is needed', async () => {
await saveSession(window.localStorage)(fakeSession)

nock('https://third-party.com')
.get('/public-resource')
.matchHeader('accept', 'text/plain')
.reply(200)

const resp = await instance.fetch(
'https://third-party.com/public-resource',
{
headers: {
forEach: (iterate) => iterate('text/plain', 'accept'),
},
}
)
Expand All @@ -642,7 +661,7 @@ describe('fetch', () => {
const resp = await instance.fetch({
url: 'https://third-party.com/private-resource',
headers: {
entries: () => [['accept', 'text/plain']],
forEach: (iterate) => iterate('text/plain', 'accept'),
},
})
expect(resp.status).toBe(200)
Expand Down
22 changes: 22 additions & 0 deletions src/authn-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export async function authnFetch(
input: RequestInfo,
options?: RequestOptions
): Promise<Response> {
// Copy headers into a modifiable object
if (options) {
const headers = copyHeaders((options: any).headers)
options = { ...options, headers }
}

// If not authenticated, perform a regular fetch
const session = await getSession(storage)
if (!session) {
Expand Down Expand Up @@ -44,3 +50,19 @@ async function shouldShareCredentials(
const requestHost = await getHost(storage)(toUrlString(input))
return requestHost != null && requestHost.requiresAuth
}

function copyHeaders(origHeaders: any) {
const headers = {}
if (origHeaders) {
if (typeof origHeaders.forEach === 'function') {
origHeaders.forEach((value, key) => {
headers[key] = value
})
} else {
for (const key in origHeaders) {
headers[key] = origHeaders[key]
}
}
}
return headers
}
16 changes: 2 additions & 14 deletions src/webid-oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,8 @@ export async function fetchWithCredentials(
input: any,
options?: RequestOptions
): Promise<Response> {
// Create a copy of the headers
const headers = {}
const origHeaders = options ? options.headers : input.headers
if (origHeaders) {
const entries =
typeof origHeaders.entries === 'function'
? origHeaders.entries()
: Object.entries(origHeaders)
for (const [name, value] of entries) {
headers[name] = value
}
}

// Add Authorization header
// Add Authorization header (assuming a modifiable headers object)
const headers: any = (options ? options.headers : input.headers) || {}
const popToken = await PoPToken.issueFor(toUrlString(input), session)
headers.authorization = `Bearer ${popToken}`
return fetch(input, { ...options, credentials: 'include', headers })
Expand Down