fumadocs-openapi 12.4.2 → 12.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/css/generated/shared.css +1 -1
- package/dist/.translations/index.d.ts +12 -1
- package/dist/.translations/keys.js +12 -1
- package/dist/generate-file.js +3 -1
- package/dist/index.browser.d.ts +2 -2
- package/dist/index.d.ts +3 -2
- package/dist/operation.js +2 -2
- package/dist/packages/shared-api/dist/codegen.js +1 -3
- package/dist/packages/shared-api/dist/components/schema/client.js +1 -1
- package/dist/playground/auth.d.ts +84 -30
- package/dist/playground/auth.js +250 -150
- package/dist/playground/fetcher.js +11 -6
- package/dist/playground/index.d.ts +2 -2
- package/dist/playground/index.js +2 -2
- package/dist/playground/use-playground.d.ts +3 -3
- package/dist/playground/use-playground.js +46 -31
- package/dist/requests/generators/python.d.ts +0 -1
- package/dist/requests/generators/python.js +1 -1
- package/dist/server/index.d.ts +2 -9
- package/dist/server/index.js +0 -4
- package/dist/server/proxy.d.ts +5 -4
- package/dist/server/proxy.js +105 -25
- package/dist/types/openapi.d.ts +7 -2
- package/dist/types.d.ts +1 -0
- package/dist/ui/operation/index.js +48 -27
- package/dist/ui/operation/usage-tabs.js +251 -34
- package/dist/ui/playground/client.js +8 -4
- package/dist/ui/playground/components/oauth-panel.js +150 -39
- package/dist/ui/playground/components/request-panel.js +18 -3
- package/dist/ui/playground/components/response-panel.js +53 -90
- package/dist/utils/create-page.d.ts +1 -1
- package/dist/utils/pages/to-static-data.d.ts +12 -0
- package/dist/utils/use-server.js +1 -1
- package/package.json +7 -6
- package/dist/packages/shared-api/dist/components/accordion.js +0 -63
- package/dist/ui/operation/request-tabs.js +0 -92
- package/dist/ui/operation/response-tabs.js +0 -53
package/dist/playground/auth.js
CHANGED
|
@@ -1,41 +1,44 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
import { useServer } from "../utils/use-server.js";
|
|
2
3
|
import { useQuery } from "../packages/shared-api/dist/utils/use-query.js";
|
|
3
4
|
import { useOperation } from "../operation.js";
|
|
5
|
+
import { proxied } from "./fetcher.js";
|
|
4
6
|
import { useOpenAPI } from "../utils/create-page.js";
|
|
5
|
-
import { createContext, use, useCallback, useEffect,
|
|
7
|
+
import { createContext, use, useCallback, useEffect, useMemo, useState, useSyncExternalStore } from "react";
|
|
6
8
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
-
import { useListener } from "@fumari/stf";
|
|
9
|
+
import { useListener, useStf } from "@fumari/stf";
|
|
8
10
|
import { arrayStartsWith, objectGet } from "@fumari/stf/lib/utils";
|
|
9
11
|
//#region src/playground/auth.tsx
|
|
10
|
-
const pendingOAuthKey = "fumadocs-openapi-oauth";
|
|
11
12
|
const pendingFlowKey = "fumadocs-openapi-flow";
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
if (stored) return JSON.parse(stored);
|
|
15
|
-
}
|
|
16
|
-
function leavePage(url, redirectUrl, flow) {
|
|
17
|
-
if (flow) sessionStorage.setItem(pendingFlowKey, JSON.stringify(flow));
|
|
13
|
+
function leavePage(url, flow, redirectUrl) {
|
|
14
|
+
sessionStorage.setItem(pendingFlowKey, JSON.stringify(flow));
|
|
18
15
|
if (redirectUrl) document.cookie = `fumadocs-openapi-oauth=${encodeURIComponent(window.location.pathname)}; path=/`;
|
|
19
16
|
window.location.replace(url);
|
|
20
17
|
}
|
|
21
18
|
/**
|
|
22
|
-
* Run an OAuth flow and resolve its token.
|
|
19
|
+
* Run an OAuth flow and resolve its token, `openIdConnect` schemes run the flows of their OpenID Connect configuration.
|
|
23
20
|
*
|
|
24
21
|
* `implicit` and `authorizationCode` leave the page instead, see `redirect`.
|
|
25
22
|
*/
|
|
26
|
-
async function requestOAuthToken(scheme, type, { schemeId, scopes, clientId, clientSecret, username, password, clientAuth, serverUrl, redirectUrl, origin, redirect }) {
|
|
27
|
-
const flows = scheme.flows
|
|
23
|
+
async function requestOAuthToken(scheme, type, { schemeId, scopes, clientId = "", clientSecret = "", username = "", password = "", clientAuth = "header", serverUrl, redirectUrl, proxyUrl, origin, redirect, onUserCode, signal }) {
|
|
24
|
+
const { flows, error } = scheme.type === "openIdConnect" ? await discoverOAuthFlows(scheme.openIdConnectUrl, serverUrl) : { flows: scheme.type === "oauth2" && scheme.flows || {} };
|
|
25
|
+
if (!flows) throw error;
|
|
26
|
+
const via = clientSecret || type === "deviceAuthorization" ? proxyUrl : void 0;
|
|
27
|
+
const endpoint = (url) => proxied(new URL(url, serverUrl).href, via);
|
|
28
28
|
const scope = scopes.join(" ");
|
|
29
|
+
const client = {
|
|
30
|
+
client_id: clientId,
|
|
31
|
+
client_secret: clientSecret,
|
|
32
|
+
client_auth: clientAuth
|
|
33
|
+
};
|
|
29
34
|
if (type === "implicit" || type === "authorizationCode") {
|
|
30
35
|
const flow = flows[type];
|
|
31
36
|
if (!flow) return;
|
|
32
37
|
const data = {
|
|
38
|
+
...client,
|
|
33
39
|
state: crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(4)).join(""),
|
|
34
|
-
client_id: clientId,
|
|
35
|
-
client_secret: clientSecret,
|
|
36
|
-
client_auth: clientAuth,
|
|
37
40
|
redirect_uri: new URL(redirectUrl ?? window.location.pathname, window.location.origin).href,
|
|
38
|
-
token_url: "tokenUrl" in flow ?
|
|
41
|
+
token_url: "tokenUrl" in flow ? endpoint(flow.tokenUrl) : void 0
|
|
39
42
|
};
|
|
40
43
|
const url = new URL(flow.authorizationUrl, serverUrl);
|
|
41
44
|
url.searchParams.set("response_type", type === "implicit" ? "token" : "code");
|
|
@@ -43,32 +46,59 @@ async function requestOAuthToken(scheme, type, { schemeId, scopes, clientId, cli
|
|
|
43
46
|
url.searchParams.set("redirect_uri", data.redirect_uri);
|
|
44
47
|
url.searchParams.set("scope", scope);
|
|
45
48
|
url.searchParams.set("state", data.state);
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
origin
|
|
52
|
-
};
|
|
53
|
-
sessionStorage.setItem(pendingOAuthKey, JSON.stringify(pending));
|
|
54
|
-
leavePage(url, redirectUrl);
|
|
49
|
+
if (type === "authorizationCode") {
|
|
50
|
+
data.code_verifier = base64Url(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32)));
|
|
51
|
+
const challenge = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(data.code_verifier));
|
|
52
|
+
url.searchParams.set("code_challenge", base64Url(new Uint8Array(challenge)));
|
|
53
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
55
54
|
}
|
|
55
|
+
if (redirect) redirect(url, data);
|
|
56
|
+
else leavePage(url, {
|
|
57
|
+
scheme: schemeId,
|
|
58
|
+
origin,
|
|
59
|
+
data,
|
|
60
|
+
oauth: true
|
|
61
|
+
}, redirectUrl);
|
|
56
62
|
return;
|
|
57
63
|
}
|
|
58
|
-
if (type !== "password" && type !== "clientCredentials") return;
|
|
59
64
|
const flow = flows[type];
|
|
60
|
-
if (!flow) return;
|
|
65
|
+
if (!flow?.tokenUrl) return;
|
|
66
|
+
const tokenUrl = endpoint(flow.tokenUrl);
|
|
67
|
+
if (type === "deviceAuthorization") {
|
|
68
|
+
const device = await postForm(endpoint(flows.deviceAuthorization.deviceAuthorizationUrl), new URLSearchParams({ scope }), client, signal);
|
|
69
|
+
onUserCode?.({
|
|
70
|
+
userCode: device.user_code,
|
|
71
|
+
verificationUri: device.verification_uri,
|
|
72
|
+
verificationUriComplete: device.verification_uri_complete
|
|
73
|
+
});
|
|
74
|
+
const body = new URLSearchParams({
|
|
75
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
76
|
+
device_code: device.device_code
|
|
77
|
+
});
|
|
78
|
+
const expiresAt = Date.now() + device.expires_in * 1e3;
|
|
79
|
+
let interval = (device.interval ?? 5) * 1e3;
|
|
80
|
+
while (Date.now() < expiresAt) {
|
|
81
|
+
await new Promise((resolve) => {
|
|
82
|
+
setTimeout(resolve, interval);
|
|
83
|
+
signal?.addEventListener("abort", resolve);
|
|
84
|
+
});
|
|
85
|
+
try {
|
|
86
|
+
return await fetchToken(tokenUrl, body, client, signal);
|
|
87
|
+
} catch (e) {
|
|
88
|
+
const code = e instanceof Error ? e.cause : void 0;
|
|
89
|
+
if (code === "slow_down") interval += 5e3;
|
|
90
|
+
else if (code !== "authorization_pending") throw e;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
throw new Error("The device code has expired.");
|
|
94
|
+
}
|
|
61
95
|
const body = new URLSearchParams({ scope });
|
|
62
96
|
if (type === "password") {
|
|
63
97
|
body.set("grant_type", "password");
|
|
64
98
|
body.set("username", username);
|
|
65
99
|
body.set("password", password);
|
|
66
100
|
} else body.set("grant_type", "client_credentials");
|
|
67
|
-
return fetchToken(
|
|
68
|
-
client_id: clientId,
|
|
69
|
-
client_secret: clientSecret,
|
|
70
|
-
client_auth: clientAuth
|
|
71
|
-
});
|
|
101
|
+
return fetchToken(tokenUrl, body, client, signal);
|
|
72
102
|
}
|
|
73
103
|
/**
|
|
74
104
|
* Finish an `implicit` or `authorizationCode` flow of `requestOAuthToken()` from the URL it returned to.
|
|
@@ -84,98 +114,97 @@ function finishOAuthFlow(url, data) {
|
|
|
84
114
|
code,
|
|
85
115
|
redirect_uri: flow.redirect_uri
|
|
86
116
|
});
|
|
117
|
+
if (flow.code_verifier) body.set("code_verifier", flow.code_verifier);
|
|
87
118
|
return fetchToken(flow.token_url, body, flow);
|
|
88
119
|
}
|
|
89
120
|
const hash = new URLSearchParams(url.hash.slice(1));
|
|
90
121
|
const token = hash.get("access_token");
|
|
91
122
|
if (token && hash.get("state") === flow.state) return `${hash.get("token_type") ?? "Bearer"} ${token}`;
|
|
92
123
|
}
|
|
93
|
-
async function
|
|
124
|
+
async function postForm(url, body, { client_id, client_secret, client_auth }, signal) {
|
|
94
125
|
const headers = { "Content-Type": "application/x-www-form-urlencoded" };
|
|
95
126
|
if (client_auth === "header" && client_secret) headers.Authorization = `Basic ${btoa(`${encodeURIComponent(client_id)}:${encodeURIComponent(client_secret)}`)}`;
|
|
96
127
|
else {
|
|
97
128
|
if (client_id) body.set("client_id", client_id);
|
|
98
129
|
if (client_secret) body.set("client_secret", client_secret);
|
|
99
130
|
}
|
|
100
|
-
const res = await fetch(
|
|
131
|
+
const res = await fetch(url, {
|
|
101
132
|
method: "POST",
|
|
102
133
|
headers,
|
|
103
|
-
body
|
|
134
|
+
body,
|
|
135
|
+
signal
|
|
104
136
|
});
|
|
105
|
-
|
|
106
|
-
|
|
137
|
+
const text = await res.text();
|
|
138
|
+
let data;
|
|
139
|
+
try {
|
|
140
|
+
data = JSON.parse(text);
|
|
141
|
+
} catch {}
|
|
142
|
+
if (res.ok && data && !data.error) return data;
|
|
143
|
+
let message = text;
|
|
144
|
+
if (data?.error) message = data.error_description ? `${data.error}: ${data.error_description}` : data.error;
|
|
145
|
+
throw new Error(message, { cause: data?.error });
|
|
146
|
+
}
|
|
147
|
+
async function fetchToken(url, body, client, signal) {
|
|
148
|
+
const { access_token, token_type = "Bearer" } = await postForm(url, body, client, signal);
|
|
107
149
|
return `${token_type} ${access_token}`;
|
|
108
150
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const ctx = use(AuthContext);
|
|
112
|
-
if (!ctx) throw new Error("Component must be used under <OpenAPIProvider />");
|
|
113
|
-
return ctx;
|
|
151
|
+
function base64Url(bytes) {
|
|
152
|
+
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
114
153
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
154
|
+
const discovered = /* @__PURE__ */ new Map();
|
|
155
|
+
/**
|
|
156
|
+
* The OAuth 2.0 flows of an OpenID Connect configuration, cached.
|
|
157
|
+
*/
|
|
158
|
+
function discoverOAuthFlows(configUrl, serverUrl) {
|
|
159
|
+
const url = new URL(configUrl, serverUrl).href;
|
|
160
|
+
let result = discovered.get(url);
|
|
161
|
+
if (result) return result;
|
|
162
|
+
result = fetch(url).then(async (res) => {
|
|
163
|
+
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
|
|
164
|
+
const config = await res.json();
|
|
165
|
+
const resolve = (endpoint) => endpoint && new URL(endpoint, url).href;
|
|
166
|
+
const authorizationUrl = resolve(config.authorization_endpoint);
|
|
167
|
+
const tokenUrl = resolve(config.token_endpoint);
|
|
168
|
+
const deviceAuthorizationUrl = resolve(config.device_authorization_endpoint);
|
|
169
|
+
const grants = new Set(config.grant_types_supported ?? ["authorization_code", "implicit"]);
|
|
170
|
+
const scopes = {};
|
|
171
|
+
for (const scope of config.scopes_supported ?? []) scopes[scope] = "";
|
|
172
|
+
const flows = {};
|
|
173
|
+
if (authorizationUrl && tokenUrl && grants.has("authorization_code")) flows.authorizationCode = {
|
|
174
|
+
authorizationUrl,
|
|
175
|
+
tokenUrl,
|
|
176
|
+
scopes
|
|
130
177
|
};
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const token = await result;
|
|
135
|
-
const info = typeof result === "string" ? {
|
|
136
|
-
type: "implicit",
|
|
137
|
-
client_id: flow.client_id,
|
|
138
|
-
token
|
|
139
|
-
} : {
|
|
140
|
-
type: "authorization_code",
|
|
141
|
-
client_id: flow.client_id,
|
|
142
|
-
client_secret: flow.client_secret,
|
|
143
|
-
client_auth: flow.client_auth,
|
|
144
|
-
token
|
|
178
|
+
if (authorizationUrl && grants.has("implicit")) flows.implicit = {
|
|
179
|
+
authorizationUrl,
|
|
180
|
+
scopes
|
|
145
181
|
};
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
returnQuery.start(flow, result);
|
|
161
|
-
});
|
|
162
|
-
useEffect(() => onMount(), []);
|
|
163
|
-
return /* @__PURE__ */ jsx(AuthContext, {
|
|
164
|
-
value: useMemo(() => ({
|
|
165
|
-
store,
|
|
166
|
-
isLoading: returnQuery.isLoading,
|
|
167
|
-
error: returnQuery.error,
|
|
168
|
-
origin,
|
|
169
|
-
subscribe
|
|
170
|
-
}), [
|
|
171
|
-
store,
|
|
172
|
-
returnQuery.isLoading,
|
|
173
|
-
returnQuery.error,
|
|
174
|
-
origin,
|
|
175
|
-
subscribe
|
|
176
|
-
]),
|
|
177
|
-
children
|
|
182
|
+
if (tokenUrl && grants.has("password")) flows.password = {
|
|
183
|
+
tokenUrl,
|
|
184
|
+
scopes
|
|
185
|
+
};
|
|
186
|
+
if (tokenUrl && grants.has("client_credentials")) flows.clientCredentials = {
|
|
187
|
+
tokenUrl,
|
|
188
|
+
scopes
|
|
189
|
+
};
|
|
190
|
+
if (deviceAuthorizationUrl && tokenUrl && grants.has("urn:ietf:params:oauth:grant-type:device_code")) flows.deviceAuthorization = {
|
|
191
|
+
deviceAuthorizationUrl,
|
|
192
|
+
tokenUrl,
|
|
193
|
+
scopes
|
|
194
|
+
};
|
|
195
|
+
return { flows };
|
|
178
196
|
});
|
|
197
|
+
result = result.catch((error) => ({ error }));
|
|
198
|
+
discovered.set(url, result);
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The OAuth 2.0 flows of an OpenID Connect configuration, or the error of discovering them, a relative URL resolves against the selected server.
|
|
203
|
+
*
|
|
204
|
+
* It suspends while discovering them.
|
|
205
|
+
*/
|
|
206
|
+
function useOpenIdConnectFlows(configUrl) {
|
|
207
|
+
return use(discoverOAuthFlows(configUrl, useServer().resolveUrl()));
|
|
179
208
|
}
|
|
180
209
|
/**
|
|
181
210
|
* Leave the page for a flow of field, like signing in, with the returned function.
|
|
@@ -185,26 +214,109 @@ function PlaygroundAuthProvider({ children }) {
|
|
|
185
214
|
function useAuthRedirect(field, onReturn) {
|
|
186
215
|
const { oauthRedirectUrl } = useOpenAPI();
|
|
187
216
|
const { method, path } = useOperation();
|
|
217
|
+
const { returned } = usePlaygroundAuth();
|
|
188
218
|
const origin = `${method} ${path}`;
|
|
189
|
-
|
|
190
|
-
if (!onReturn) return;
|
|
191
|
-
|
|
192
|
-
if (
|
|
193
|
-
sessionStorage.removeItem(pendingFlowKey);
|
|
194
|
-
const url = new URL(window.location.href);
|
|
219
|
+
useEffect(() => {
|
|
220
|
+
if (!onReturn || !returned || returned.oauth) return;
|
|
221
|
+
if (returned.scheme !== field.schemeId || returned.origin !== origin) return;
|
|
222
|
+
if (!endReturnedFlow(returned)) return;
|
|
195
223
|
window.history.replaceState(null, "", window.location.pathname);
|
|
196
|
-
onReturn(url,
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
224
|
+
onReturn(returned.url, returned.data);
|
|
225
|
+
}, [
|
|
226
|
+
returned,
|
|
227
|
+
onReturn,
|
|
228
|
+
field.schemeId,
|
|
229
|
+
origin
|
|
230
|
+
]);
|
|
231
|
+
return (url, data) => leavePage(url, {
|
|
200
232
|
scheme: field.schemeId,
|
|
201
233
|
origin,
|
|
202
234
|
data
|
|
235
|
+
}, oauthRedirectUrl);
|
|
236
|
+
}
|
|
237
|
+
/** the flow that returned to the page, taken out of `sessionStorage` once read, until it is handled */
|
|
238
|
+
let returnedFlow;
|
|
239
|
+
const subscribeNever = () => () => {};
|
|
240
|
+
function getReturnedFlow() {
|
|
241
|
+
if (returnedFlow === void 0) {
|
|
242
|
+
const stored = sessionStorage.getItem(pendingFlowKey);
|
|
243
|
+
sessionStorage.removeItem(pendingFlowKey);
|
|
244
|
+
returnedFlow = stored ? {
|
|
245
|
+
...JSON.parse(stored),
|
|
246
|
+
url: new URL(window.location.href)
|
|
247
|
+
} : null;
|
|
248
|
+
}
|
|
249
|
+
return returnedFlow ?? void 0;
|
|
250
|
+
}
|
|
251
|
+
/** the returned flow is handled, `false` when it was already handled */
|
|
252
|
+
function endReturnedFlow(flow) {
|
|
253
|
+
if (returnedFlow !== flow) return false;
|
|
254
|
+
returnedFlow = null;
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
/** returned OAuth flows the page is finishing */
|
|
258
|
+
const finishing = /* @__PURE__ */ new WeakSet();
|
|
259
|
+
const AuthContext = createContext(null);
|
|
260
|
+
/**
|
|
261
|
+
* The auth of page: the values of security schemes, and the flow that returned to it.
|
|
262
|
+
*/
|
|
263
|
+
function usePlaygroundAuth() {
|
|
264
|
+
const ctx = use(AuthContext);
|
|
265
|
+
if (!ctx) throw new Error("Component must be used under <OpenAPIProvider />");
|
|
266
|
+
return ctx;
|
|
267
|
+
}
|
|
268
|
+
function PlaygroundAuthProvider({ children }) {
|
|
269
|
+
const stf = useStf({ defaultValues: { auth: {} } });
|
|
270
|
+
const returned = useSyncExternalStore(subscribeNever, getReturnedFlow, () => void 0);
|
|
271
|
+
const query = useQuery(async (flow) => {
|
|
272
|
+
const result = finishOAuthFlow(flow.url, flow.data);
|
|
273
|
+
if (result !== void 0) window.history.replaceState(null, "", window.location.pathname);
|
|
274
|
+
try {
|
|
275
|
+
const token = await result;
|
|
276
|
+
if (token === void 0) return;
|
|
277
|
+
stf.dataEngine.update(["auth", flow.scheme], token);
|
|
278
|
+
const { client_id, client_secret, client_auth } = flow.data;
|
|
279
|
+
const info = typeof result === "string" ? {
|
|
280
|
+
type: "implicit",
|
|
281
|
+
client_id,
|
|
282
|
+
token
|
|
283
|
+
} : {
|
|
284
|
+
type: "authorization_code",
|
|
285
|
+
client_id,
|
|
286
|
+
client_secret,
|
|
287
|
+
client_auth,
|
|
288
|
+
token
|
|
289
|
+
};
|
|
290
|
+
return { [flow.scheme]: info };
|
|
291
|
+
} finally {
|
|
292
|
+
endReturnedFlow(flow);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
if (returned?.oauth && !finishing.has(returned)) {
|
|
296
|
+
finishing.add(returned);
|
|
297
|
+
query.start(returned);
|
|
298
|
+
}
|
|
299
|
+
return /* @__PURE__ */ jsx(AuthContext, {
|
|
300
|
+
value: useMemo(() => ({
|
|
301
|
+
stf,
|
|
302
|
+
returned,
|
|
303
|
+
origin: returned?.origin,
|
|
304
|
+
isLoading: query.isLoading,
|
|
305
|
+
error: query.error,
|
|
306
|
+
store: query.data ?? {}
|
|
307
|
+
}), [
|
|
308
|
+
stf,
|
|
309
|
+
returned,
|
|
310
|
+
query.isLoading,
|
|
311
|
+
query.error,
|
|
312
|
+
query.data
|
|
313
|
+
]),
|
|
314
|
+
children
|
|
203
315
|
});
|
|
204
316
|
}
|
|
205
317
|
const builtinProviders = [
|
|
206
318
|
{
|
|
207
|
-
on: ({ scheme }) => scheme.type === "http" && scheme.scheme === "basic",
|
|
319
|
+
on: ({ scheme }) => scheme.type === "http" && scheme.scheme?.toLowerCase() === "basic",
|
|
208
320
|
render: ({ field, Value }) => /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(Value, {
|
|
209
321
|
name: "username",
|
|
210
322
|
type: "basic",
|
|
@@ -234,6 +346,11 @@ const builtinProviders = [
|
|
|
234
346
|
if (scheme.type === "apiKey") data[scheme.in][scheme.name] = value;
|
|
235
347
|
}
|
|
236
348
|
},
|
|
349
|
+
{
|
|
350
|
+
on: ({ scheme }) => scheme.type === "mutualTLS",
|
|
351
|
+
render: () => null,
|
|
352
|
+
onRequest() {}
|
|
353
|
+
},
|
|
237
354
|
{
|
|
238
355
|
render: ({ field, Value }) => /* @__PURE__ */ jsx(Value, {
|
|
239
356
|
name: "Authorization",
|
|
@@ -256,8 +373,9 @@ function findProvider(providers = [], item) {
|
|
|
256
373
|
}
|
|
257
374
|
function getDefaultValue(scheme) {
|
|
258
375
|
if (scheme.type === "apiKey") return "";
|
|
376
|
+
if (scheme.type === "mutualTLS") return null;
|
|
259
377
|
if (scheme.type !== "http" || !scheme.scheme) return "Bearer ";
|
|
260
|
-
if (scheme.scheme === "basic") return {
|
|
378
|
+
if (scheme.scheme.toLowerCase() === "basic") return {
|
|
261
379
|
username: "",
|
|
262
380
|
password: ""
|
|
263
381
|
};
|
|
@@ -267,7 +385,7 @@ function getDefaultValue(scheme) {
|
|
|
267
385
|
* The auth fields of an API playground: the values it persists, and the request data they encode into.
|
|
268
386
|
*/
|
|
269
387
|
function useAuthFields(engine, { operation, providers, transform } = {}) {
|
|
270
|
-
const
|
|
388
|
+
const page = usePlaygroundAuth().stf.dataEngine;
|
|
271
389
|
const { storageKeyPrefix, doc } = useOpenAPI();
|
|
272
390
|
const { dereferenced, resolve } = doc;
|
|
273
391
|
const schemes = dereferenced.components?.securitySchemes;
|
|
@@ -318,57 +436,39 @@ function useAuthFields(engine, { operation, providers, transform } = {}) {
|
|
|
318
436
|
transform
|
|
319
437
|
]);
|
|
320
438
|
useListener({
|
|
321
|
-
stf:
|
|
439
|
+
stf: page,
|
|
322
440
|
onUpdate(key) {
|
|
323
441
|
for (const field of fields) {
|
|
324
442
|
if (!arrayStartsWith(key, field.fieldName) && !arrayStartsWith(field.fieldName, key)) continue;
|
|
325
|
-
const value =
|
|
326
|
-
if (value != null)
|
|
443
|
+
const value = page.get(field.fieldName);
|
|
444
|
+
if (value != null) sessionStorage.setItem(field.storageKey, JSON.stringify(value));
|
|
327
445
|
}
|
|
328
446
|
}
|
|
329
447
|
});
|
|
330
|
-
const onToken = useEffectEvent((schemeId, token) => {
|
|
331
|
-
const field = fields.find((field) => field.schemeId === schemeId);
|
|
332
|
-
if (field) {
|
|
333
|
-
engine.update(field.fieldName, token);
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
const idx = requirements.findIndex((req) => req.some((item) => item.id === schemeId));
|
|
337
|
-
if (idx !== -1) {
|
|
338
|
-
localStorage.setItem(`${storageKeyPrefix}auth-${schemeId}`, JSON.stringify(token));
|
|
339
|
-
select(idx);
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
useEffect(() => subscribe(onToken), [subscribe]);
|
|
343
448
|
return {
|
|
344
449
|
requirements,
|
|
345
450
|
selected,
|
|
346
451
|
select,
|
|
347
452
|
fields,
|
|
348
453
|
mapValues: useCallback((values) => {
|
|
349
|
-
const cloned = structuredClone(
|
|
454
|
+
const cloned = structuredClone({
|
|
455
|
+
...values,
|
|
456
|
+
auth: page.get(["auth"])
|
|
457
|
+
});
|
|
350
458
|
const data = cloned;
|
|
351
459
|
for (const field of fields) field.provider.onRequest(data, objectGet(data, field.fieldName), field);
|
|
352
460
|
delete data.auth;
|
|
353
461
|
return cloned;
|
|
354
|
-
}, [fields]),
|
|
462
|
+
}, [page, fields]),
|
|
355
463
|
init: useCallback(() => {
|
|
356
|
-
for (const field of fields) {
|
|
357
|
-
const stored =
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
engine.init(field.fieldName, field.defaultValue);
|
|
366
|
-
}
|
|
367
|
-
return () => {
|
|
368
|
-
for (const field of fields) engine.delete(field.fieldName);
|
|
369
|
-
};
|
|
370
|
-
}, [engine, fields])
|
|
464
|
+
for (const field of fields) page.init(field.fieldName, () => {
|
|
465
|
+
const stored = sessionStorage.getItem(field.storageKey);
|
|
466
|
+
const parsed = stored ? JSON.parse(stored) : void 0;
|
|
467
|
+
return typeof parsed === typeof field.defaultValue ? parsed : field.defaultValue;
|
|
468
|
+
});
|
|
469
|
+
return () => {};
|
|
470
|
+
}, [page, fields])
|
|
371
471
|
};
|
|
372
472
|
}
|
|
373
473
|
//#endregion
|
|
374
|
-
export { PlaygroundAuthProvider, finishOAuthFlow,
|
|
474
|
+
export { PlaygroundAuthProvider, finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, useOpenIdConnectFlows, usePlaygroundAuth };
|
|
@@ -2,9 +2,18 @@ import { resolveMediaAdapter } from "../requests/media/resolve-adapter.js";
|
|
|
2
2
|
import { require_fast_content_type_parse } from "../node_modules/.pnpm/fast-content-type-parse@4.0.0/node_modules/fast-content-type-parse/index.js";
|
|
3
3
|
//#region src/playground/fetcher.ts
|
|
4
4
|
var import_fast_content_type_parse = require_fast_content_type_parse();
|
|
5
|
+
/**
|
|
6
|
+
* The URL requesting `url` through the proxy of `createProxy()`, `url` itself without a proxy.
|
|
7
|
+
*/
|
|
8
|
+
function proxied(url, proxyUrl) {
|
|
9
|
+
if (!proxyUrl) return url;
|
|
10
|
+
const out = new URL(proxyUrl, document.baseURI);
|
|
11
|
+
out.searchParams.append("url", url);
|
|
12
|
+
return out.href;
|
|
13
|
+
}
|
|
5
14
|
function createBrowserFetcher(adapters, { proxyUrl, proxyForwardCookie = true, requestTimeout = 10, onRequestInit } = {}) {
|
|
6
15
|
return { async fetch(url, data) {
|
|
7
|
-
|
|
16
|
+
const requestUrl = new URL(proxied(url, proxyUrl), document.baseURI);
|
|
8
17
|
let requestInit = {
|
|
9
18
|
method: data.method.toUpperCase(),
|
|
10
19
|
cache: "no-cache",
|
|
@@ -15,10 +24,6 @@ function createBrowserFetcher(adapters, { proxyUrl, proxyForwardCookie = true, r
|
|
|
15
24
|
const param = data.header[key];
|
|
16
25
|
headers.append(key, param.value);
|
|
17
26
|
}
|
|
18
|
-
if (proxyUrl) {
|
|
19
|
-
requestUrl = new URL(proxyUrl, document.baseURI);
|
|
20
|
-
requestUrl.searchParams.append("url", url);
|
|
21
|
-
}
|
|
22
27
|
if (data.bodyMediaType && data.body) {
|
|
23
28
|
const adapter = resolveMediaAdapter(data.bodyMediaType, adapters);
|
|
24
29
|
if (!adapter) return {
|
|
@@ -86,4 +91,4 @@ function getFileName(headers) {
|
|
|
86
91
|
return name?.replace(/^.*[/\\]/, "") || void 0;
|
|
87
92
|
}
|
|
88
93
|
//#endregion
|
|
89
|
-
export { createBrowserFetcher };
|
|
94
|
+
export { createBrowserFetcher, proxied };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { BrowserFetcherOptions, FetchErrorResult, FetchResponseResult, FetchResult, Fetcher, createBrowserFetcher } from "./fetcher.js";
|
|
2
|
-
import { AuthField, AuthPanelProps, AuthProvider, AuthRenderProps, AuthRequirement, OAuthFlowType, finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, usePlaygroundAuth } from "./auth.js";
|
|
2
|
+
import { AuthField, AuthPanelProps, AuthProvider, AuthRenderProps, AuthRequirement, DeviceUserCode, OAuthFlowType, finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, useOpenIdConnectFlows, usePlaygroundAuth } from "./auth.js";
|
|
3
3
|
import { OAuthInput, Playground, PlaygroundAuth, PlaygroundOptions, PlaygroundResponse, RequestBodyInfo, usePlayground } from "./use-playground.js";
|
|
4
|
-
export { type AuthField, type AuthPanelProps, type AuthProvider, type AuthRenderProps, type AuthRequirement, type BrowserFetcherOptions, type FetchErrorResult, type FetchResponseResult, type FetchResult, type Fetcher, type OAuthFlowType, type OAuthInput, type Playground, type PlaygroundAuth, type PlaygroundOptions, type PlaygroundResponse, type RequestBodyInfo, createBrowserFetcher, finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, usePlayground, usePlaygroundAuth };
|
|
4
|
+
export { type AuthField, type AuthPanelProps, type AuthProvider, type AuthRenderProps, type AuthRequirement, type BrowserFetcherOptions, type DeviceUserCode, type FetchErrorResult, type FetchResponseResult, type FetchResult, type Fetcher, type OAuthFlowType, type OAuthInput, type Playground, type PlaygroundAuth, type PlaygroundOptions, type PlaygroundResponse, type RequestBodyInfo, createBrowserFetcher, finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, useOpenIdConnectFlows, usePlayground, usePlaygroundAuth };
|
package/dist/playground/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, usePlaygroundAuth } from "./auth.js";
|
|
2
1
|
import { createBrowserFetcher } from "./fetcher.js";
|
|
2
|
+
import { finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, useOpenIdConnectFlows, usePlaygroundAuth } from "./auth.js";
|
|
3
3
|
import { usePlayground } from "./use-playground.js";
|
|
4
|
-
export { createBrowserFetcher, finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, usePlayground, usePlaygroundAuth };
|
|
4
|
+
export { createBrowserFetcher, finishOAuthFlow, requestOAuthToken, useAuthFields, useAuthRedirect, useOpenIdConnectFlows, usePlayground, usePlaygroundAuth };
|
|
@@ -24,15 +24,15 @@ export interface PlaygroundAuth {
|
|
|
24
24
|
/** the fields of the selected requirement */
|
|
25
25
|
fields: AuthField[];
|
|
26
26
|
/**
|
|
27
|
-
* Obtain the access token of an OAuth 2.0 field, and write it into the field.
|
|
27
|
+
* Obtain the access token of an OAuth 2.0 or OpenID Connect field, and write it into the field.
|
|
28
28
|
*
|
|
29
|
-
* `implicit` and `authorizationCode` leave the page without a token,
|
|
29
|
+
* `implicit` and `authorizationCode` leave the page without a token, the page finishes them when they return, and `flowReturned` is set.
|
|
30
30
|
*/
|
|
31
31
|
authorize: (field: AuthField, input: OAuthInput) => Promise<string | undefined>;
|
|
32
32
|
/** a flow started here has returned to the page */
|
|
33
33
|
flowReturned: boolean;
|
|
34
34
|
}
|
|
35
|
-
export interface OAuthInput extends
|
|
35
|
+
export interface OAuthInput extends Pick<OAuthFlowInput, 'clientId' | 'clientSecret' | 'username' | 'password' | 'clientAuth' | 'onUserCode' | 'signal'> {
|
|
36
36
|
type: OAuthFlowType;
|
|
37
37
|
/** defaults to the scopes required by the operation */
|
|
38
38
|
scopes?: string[];
|