Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional headers for fetch_jwk #397

Merged
merged 5 commits into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/jwks/remote.ts
@@ -1,9 +1,7 @@
import fetchJwks from '../runtime/fetch_jwks.js'
import { isCloudflareWorkers } from '../runtime/env.js'

import type { KeyLike, JWSHeaderParameters, FlattenedJWSInput, GetKeyFunction } from '../types.d'
import fetchJwks from '../runtime/fetch_jwks.js'
import type { FlattenedJWSInput, GetKeyFunction, JWSHeaderParameters, KeyLike } from '../types.d'
import { JWKSInvalid, JWKSNoMatchingKey } from '../util/errors.js'

import { isJWKSLike, LocalJWKSet } from './local.js'

/**
Expand Down Expand Up @@ -42,6 +40,11 @@ export interface RemoteJWKSetOptions {
* runtime.
*/
agent?: any

/**
* Optional headers to be sent with the HTTP request.
*/
headers?: Record<string, string>
}

class RemoteJWKSet extends LocalJWKSet {
Expand All @@ -57,7 +60,7 @@ class RemoteJWKSet extends LocalJWKSet {

private _pendingFetch?: Promise<unknown>

private _options: Pick<RemoteJWKSetOptions, 'agent'>
private _options: Pick<RemoteJWKSetOptions, 'agent' | 'headers'>

constructor(url: unknown, options?: RemoteJWKSetOptions) {
super({ keys: [] })
Expand All @@ -68,7 +71,7 @@ class RemoteJWKSet extends LocalJWKSet {
throw new TypeError('url must be an instance of URL')
}
this._url = new URL(url.href)
this._options = { agent: options?.agent }
this._options = { agent: options?.agent, headers: options?.headers }
this._timeoutDuration =
typeof options?.timeoutDuration === 'number' ? options?.timeoutDuration : 5000
this._cooldownDuration =
Expand Down
14 changes: 7 additions & 7 deletions src/runtime/node/fetch_jwks.ts
@@ -1,14 +1,13 @@
import * as http from 'http'
import * as https from 'https'
import { once } from 'events'
import type { ClientRequest, IncomingMessage } from 'http'
import * as http from 'http'
import type { RequestOptions } from 'https'

import type { FetchFunction } from '../interfaces.d'
import { JOSEError, JWKSTimeout } from '../../util/errors.js'
import * as https from 'https'
import { concat, decoder } from '../../lib/buffer_utils.js'
import { JOSEError, JWKSTimeout } from '../../util/errors.js'
import type { FetchFunction } from '../interfaces.d'

type AcceptedRequestOptions = Pick<RequestOptions, 'agent'>
type AcceptedRequestOptions = Pick<RequestOptions, 'agent' | 'headers'>

const fetchJwks: FetchFunction = async (
url: URL,
Expand All @@ -27,10 +26,11 @@ const fetchJwks: FetchFunction = async (
throw new TypeError('Unsupported URL protocol.')
}

const { agent } = options
const { agent, headers } = options
const req = get(url.href, {
agent,
timeout,
headers,
})

const [response] = <[IncomingMessage]>(
Expand Down