Skip to content

Commit e8985ae

Browse files
committed
Support Request objects.
Fixes #141
1 parent 18eb9f9 commit e8985ae

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

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

609-
it('merges a Header object with the authorization header', async () => {
609+
it('merges a Headers object with the authorization header', async () => {
610610
await saveSession(window.localStorage)(fakeSession)
611611

612612
nock('https://third-party.com')
@@ -628,6 +628,28 @@ describe('fetch', () => {
628628
expect(resp.status).toBe(200)
629629
})
630630

631+
// skipped because isomorphic-fetch does not support Request as first parameter;
632+
// it works in the browser
633+
it.skip('merges headers in a Request object with the authorization header', async () => {
634+
await saveSession(window.localStorage)(fakeSession)
635+
636+
nock('https://third-party.com')
637+
.get('/private-resource')
638+
.reply(401, '', { 'www-authenticate': 'Bearer scope="openid webid"' })
639+
.get('/private-resource')
640+
.matchHeader('accept', 'text/plain')
641+
.matchHeader('authorization', matchAuthzHeader('https://third-party.com'))
642+
.reply(200)
643+
644+
const resp = await instance.fetch({
645+
url: 'https://third-party.com/private-resource',
646+
headers: {
647+
entries: () => [['accept', 'text/plain']]
648+
}
649+
})
650+
expect(resp.status).toBe(200)
651+
})
652+
631653
it('does not resend with credentials if the www-authenticate header is missing', async () => {
632654
expect.assertions(1)
633655
await saveSession(window.localStorage)(fakeSession)

src/webid-oidc.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
/* global RequestInfo, Response */
2+
/* global Response */
33
import * as authorization from 'auth-header'
44
import RelyingParty from '@solid/oidc-rp'
55
import PoPToken from '@solid/oidc-rp/lib/PoPToken'
@@ -216,16 +216,17 @@ export function requiresAuth(resp: Response): boolean {
216216
export async function fetchWithCredentials(
217217
session: webIdOidcSession,
218218
fetch: Function,
219-
input: RequestInfo,
219+
input: any,
220220
options?: RequestOptions
221221
): Promise<Response> {
222222
// Create a copy of the headers
223223
const headers = {}
224-
if (options && options.headers) {
224+
const origHeaders = options ? options.headers : input.headers
225+
if (origHeaders) {
225226
const entries =
226-
typeof options.headers.entries === 'function'
227-
? options.headers.entries()
228-
: Object.entries(options.headers)
227+
typeof origHeaders.entries === 'function'
228+
? origHeaders.entries()
229+
: Object.entries(origHeaders)
229230
for (const [name, value] of entries) {
230231
headers[name] = value
231232
}

0 commit comments

Comments
 (0)