Skip to content

Commit

Permalink
Support Request objects.
Browse files Browse the repository at this point in the history
Fixes #141
  • Loading branch information
RubenVerborgh committed Nov 4, 2019
1 parent 18eb9f9 commit e8985ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
24 changes: 23 additions & 1 deletion src/__test__/solid-auth-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ describe('fetch', () => {
expect(resp.status).toBe(200)
})

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

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

// skipped because isomorphic-fetch does not support Request as first parameter;
// it works in the browser
it.skip('merges headers in a Request object with the authorization header', async () => {
await saveSession(window.localStorage)(fakeSession)

nock('https://third-party.com')
.get('/private-resource')
.reply(401, '', { 'www-authenticate': 'Bearer scope="openid webid"' })
.get('/private-resource')
.matchHeader('accept', 'text/plain')
.matchHeader('authorization', matchAuthzHeader('https://third-party.com'))
.reply(200)

const resp = await instance.fetch({
url: 'https://third-party.com/private-resource',
headers: {
entries: () => [['accept', 'text/plain']]
}
})
expect(resp.status).toBe(200)
})

it('does not resend with credentials if the www-authenticate header is missing', async () => {
expect.assertions(1)
await saveSession(window.localStorage)(fakeSession)
Expand Down
13 changes: 7 additions & 6 deletions src/webid-oidc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
/* global RequestInfo, Response */
/* global Response */
import * as authorization from 'auth-header'
import RelyingParty from '@solid/oidc-rp'
import PoPToken from '@solid/oidc-rp/lib/PoPToken'
Expand Down Expand Up @@ -216,16 +216,17 @@ export function requiresAuth(resp: Response): boolean {
export async function fetchWithCredentials(
session: webIdOidcSession,
fetch: Function,
input: RequestInfo,
input: any,
options?: RequestOptions
): Promise<Response> {
// Create a copy of the headers
const headers = {}
if (options && options.headers) {
const origHeaders = options ? options.headers : input.headers
if (origHeaders) {
const entries =
typeof options.headers.entries === 'function'
? options.headers.entries()
: Object.entries(options.headers)
typeof origHeaders.entries === 'function'
? origHeaders.entries()
: Object.entries(origHeaders)
for (const [name, value] of entries) {
headers[name] = value
}
Expand Down

0 comments on commit e8985ae

Please sign in to comment.