forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonService.ts
230 lines (207 loc) · 7.69 KB
/
JsonService.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import { ErrorResponse, ErrorTimeout } from "./errors";
import type { ExtraHeader } from "./OidcClientSettings";
import { Logger } from "./utils";
import { ErrorDPoPNonce } from "./errors/ErrorDPoPNonce";
/**
* @internal
*/
export type JwtHandler = (text: string) => Promise<Record<string, unknown>>;
/**
* @internal
*/
export interface GetJsonOpts {
token?: string;
credentials?: RequestCredentials;
timeoutInSeconds?: number;
}
/**
* @internal
*/
export interface PostFormOpts {
body: URLSearchParams;
basicAuth?: string;
timeoutInSeconds?: number;
initCredentials?: "same-origin" | "include" | "omit";
extraHeaders?: Record<string, ExtraHeader>;
}
/**
* @internal
*/
export class JsonService {
private readonly _logger = new Logger("JsonService");
private _contentTypes: string[] = [];
public constructor(
additionalContentTypes: string[] = [],
private _jwtHandler: JwtHandler | null = null,
private _extraHeaders: Record<string, ExtraHeader> = {},
) {
this._contentTypes.push(...additionalContentTypes, "application/json");
if (_jwtHandler) {
this._contentTypes.push("application/jwt");
}
}
protected async fetchWithTimeout(input: RequestInfo, init: RequestInit & { timeoutInSeconds?: number } = {}) {
const { timeoutInSeconds, ...initFetch } = init;
if (!timeoutInSeconds) {
return await fetch(input, initFetch);
}
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutInSeconds * 1000);
try {
const response = await fetch(input, {
...init,
signal: controller.signal,
});
return response;
}
catch (err) {
if (err instanceof DOMException && err.name === "AbortError") {
throw new ErrorTimeout("Network timed out");
}
throw err;
}
finally {
clearTimeout(timeoutId);
}
}
public async getJson(url: string, {
token,
credentials,
timeoutInSeconds,
}: GetJsonOpts = {}): Promise<Record<string, unknown>> {
const logger = this._logger.create("getJson");
const headers: HeadersInit = {
"Accept": this._contentTypes.join(", "),
};
if (token) {
logger.debug("token passed, setting Authorization header");
headers["Authorization"] = "Bearer " + token;
}
this._appendExtraHeaders(headers);
let response: Response;
try {
logger.debug("url:", url);
response = await this.fetchWithTimeout(url, { method: "GET", headers, timeoutInSeconds, credentials });
}
catch (err) {
logger.error("Network Error");
throw err;
}
logger.debug("HTTP response received, status", response.status);
const contentType = response.headers.get("Content-Type");
if (contentType && !this._contentTypes.find(item => contentType.startsWith(item))) {
logger.throw(new Error(`Invalid response Content-Type: ${(contentType ?? "undefined")}, from URL: ${url}`));
}
if (response.ok && this._jwtHandler && contentType?.startsWith("application/jwt")) {
return await this._jwtHandler(await response.text());
}
let json: Record<string, unknown>;
try {
json = await response.json();
}
catch (err) {
logger.error("Error parsing JSON response", err);
if (response.ok) throw err;
throw new Error(`${response.statusText} (${response.status})`);
}
if (!response.ok) {
logger.error("Error from server:", json);
if (json.error) {
throw new ErrorResponse(json);
}
throw new Error(`${response.statusText} (${response.status}): ${JSON.stringify(json)}`);
}
return json;
}
public async postForm(url: string, {
body,
basicAuth,
timeoutInSeconds,
initCredentials,
extraHeaders,
}: PostFormOpts): Promise<Record<string, unknown>> {
const logger = this._logger.create("postForm");
const headers: HeadersInit = {
"Accept": this._contentTypes.join(", "),
"Content-Type": "application/x-www-form-urlencoded",
...extraHeaders,
};
if (basicAuth !== undefined) {
headers["Authorization"] = "Basic " + basicAuth;
}
this._appendExtraHeaders(headers);
let response: Response;
try {
logger.debug("url:", url);
response = await this.fetchWithTimeout(url, { method: "POST", headers, body, timeoutInSeconds, credentials: initCredentials });
}
catch (err) {
logger.error("Network error");
throw err;
}
logger.debug("HTTP response received, status", response.status);
const contentType = response.headers.get("Content-Type");
if (contentType && !this._contentTypes.find(item => contentType.startsWith(item))) {
throw new Error(`Invalid response Content-Type: ${(contentType ?? "undefined")}, from URL: ${url}`);
}
const responseText = await response.text();
let json: Record<string, unknown> = {};
if (responseText) {
try {
json = JSON.parse(responseText);
}
catch (err) {
logger.error("Error parsing JSON response", err);
if (response.ok) throw err;
throw new Error(`${response.statusText} (${response.status})`);
}
}
if (!response.ok) {
logger.error("Error from server:", json);
if (response.headers.has("dpop-nonce")) {
const nonce = response.headers.get("dpop-nonce") as string;
throw new ErrorDPoPNonce(nonce, `${JSON.stringify(json)}`);
}
if (json.error) {
throw new ErrorResponse(json, body);
}
throw new Error(`${response.statusText} (${response.status}): ${JSON.stringify(json)}`);
}
return json;
}
private _appendExtraHeaders(
headers: Record<string, string>,
): void {
const logger = this._logger.create("appendExtraHeaders");
const customKeys = Object.keys(this._extraHeaders);
const protectedHeaders = [
"accept",
"content-type",
];
const preventOverride = [
"authorization",
];
if (customKeys.length === 0) {
return;
}
customKeys.forEach((headerName) => {
if (protectedHeaders.includes(headerName.toLocaleLowerCase())) {
logger.warn("Protected header could not be set", headerName, protectedHeaders);
return;
}
if (preventOverride.includes(headerName.toLocaleLowerCase()) &&
Object.keys(headers).includes(headerName)) {
logger.warn("Header could not be overridden", headerName, preventOverride);
return;
}
const content = (typeof this._extraHeaders[headerName] === "function") ?
(this._extraHeaders[headerName] as ()=>string)() :
this._extraHeaders[headerName];
if (content && content !== "") {
headers[headerName] = content as string;
}
});
}
}