Repository navigation
Expand file tree
/
Copy pathauth-handler.ts
More file actions
81 lines (70 loc) · 2.91 KB
/
Copy pathauth-handler.ts
File metadata and controls
81 lines (70 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
export interface HttpHeaders {
[key: string]: string;
}
/**
* Pluggable authentication handler for HTTP requests.
*
* - {@link headers} is called before each request to supply additional
* request headers (typically `Authorization`).
* - {@link shouldRetryWithHeaders} is called after every response and
* decides whether the request should be retried with new headers,
* typically in response to a 401 / 403 or a WWW-Authenticate.
* - {@link onSuccessfulRetry}, if defined, is called when a retry
* succeeds, giving the handler a chance to persist the new headers.
*/
export interface AuthenticationHandler {
/** Returns request headers (may include `Authorization`). */
headers: () => Promise<HttpHeaders>;
/**
* Called for every response. Returns new headers if the request
* should be retried, or `undefined` to skip the retry.
*/
shouldRetryWithHeaders: (req: RequestInit, res: Response) => Promise<HttpHeaders | undefined>;
/**
* Called when a retry using the headers from
* {@link shouldRetryWithHeaders} succeeded. Lets the handler persist
* those headers for subsequent requests.
*/
onSuccessfulRetry?: (headers: HttpHeaders) => Promise<void>;
}
/**
* Wraps `fetch` with authentication handling. The returned function
* injects headers from `authHandler.headers()`, retries when
* `authHandler.shouldRetryWithHeaders` returns new headers, and notifies
* via `onSuccessfulRetry` when the retry succeeds.
* Caller-provided headers take precedence on both requests, regardless of casing.
*/
export function createAuthenticatingFetchWithRetry(
fetchImpl: typeof fetch,
authHandler: AuthenticationHandler
): typeof fetch {
async function authFetch(url: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const authHeaders = (await authHandler.headers()) || {};
const mergedInit: RequestInit = {
...(init || {}),
headers: mergeHeaders(authHeaders, init?.headers),
};
let response = await fetchImpl(url, mergedInit);
const updatedHeaders = await authHandler.shouldRetryWithHeaders(mergedInit, response);
if (updatedHeaders) {
const retryInit: RequestInit = {
...(init || {}),
headers: mergeHeaders(updatedHeaders, init?.headers),
};
response = await fetchImpl(url, retryInit);
if (response.ok && authHandler.onSuccessfulRetry) {
await authHandler.onSuccessfulRetry(updatedHeaders);
}
}
return response;
}
// Preserve fetch's own properties so the wrapped function is a drop-in.
Object.setPrototypeOf(authFetch, Object.getPrototypeOf(fetchImpl));
Object.defineProperties(authFetch, Object.getOwnPropertyDescriptors(fetchImpl));
return authFetch;
}
function mergeHeaders(authHeaders: HttpHeaders, callerHeaders?: HeadersInit): Headers {
const headers = new Headers(authHeaders);
new Headers(callerHeaders).forEach((value, name) => headers.set(name, value));
return headers;
}