bwh-auth 0.4.0 → 0.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/README.md +10 -0
- package/dist/index.cjs +66 -38
- package/dist/index.d.cts +44 -2
- package/dist/index.d.ts +44 -2
- package/dist/index.js +66 -38
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -231,6 +231,16 @@ It talks to the auth package's credential service (`oauth_server.credentials`) t
|
|
|
231
231
|
- **Requests** send the session cookie and `X-CSRF-TOKEN`. A 401 or 419 response becomes "sign in again".
|
|
232
232
|
- **While the server reports issuing unavailable** (OAuth switched off), the create forms are hidden, and revoke and delete still work.
|
|
233
233
|
- **Token lifetimes** are ISO-8601 durations from the server, displayed as text (`PT4H` shows as "4 hours").
|
|
234
|
+
- **Your own HTTP client:** pass `request` to send every credential request through it, so it gets the same session-expiry handling as the rest of the app. `request(method, url, body)` resolves with `{ ok: true, status, payload }` (the parsed JSON; the component reads its `data` when present) or `{ ok: false, status, message }`, and the message is shown. With `request`, sending the CSRF token is the client's job; without it, the built-in `fetch` is used as above.
|
|
235
|
+
- **Your own form controls:** besides the required components, `components` may include `Textarea`, `Checkbox` (`{ id, checked, onCheckedChange }`) and `RadioGroup` (`{ name, value, onValueChange, options: [{ value, label }] }`). Each one you omit is rendered as the native element.
|
|
236
|
+
|
|
237
|
+
```tsx
|
|
238
|
+
<ApiCredentialsSection
|
|
239
|
+
indexUrl="/account/api-credentials"
|
|
240
|
+
components={{ ...components, Textarea, Checkbox: AppCheckbox, RadioGroup: AppRadioGroup }}
|
|
241
|
+
request={(method, url, body) => myClient.send(method, url, body)}
|
|
242
|
+
/>
|
|
243
|
+
```
|
|
234
244
|
|
|
235
245
|
## Endpoint Defaults
|
|
236
246
|
|
package/dist/index.cjs
CHANGED
|
@@ -1001,7 +1001,7 @@ function relyingApplicationsFrom(value) {
|
|
|
1001
1001
|
// src/api-credentials-section.tsx
|
|
1002
1002
|
var React5 = __toESM(require("react"), 1);
|
|
1003
1003
|
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
1004
|
-
function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, onSuccess, onError }) {
|
|
1004
|
+
function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, request, onSuccess, onError }) {
|
|
1005
1005
|
const { Card, CardContent, CardDescription, CardHeader, CardTitle } = resolveAuthComponents(components);
|
|
1006
1006
|
const [index, setIndex] = React5.useState(null);
|
|
1007
1007
|
const [issued, setIssued] = React5.useState([]);
|
|
@@ -1015,13 +1015,13 @@ function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, on
|
|
|
1015
1015
|
const onErrorRef = React5.useRef(onError);
|
|
1016
1016
|
onErrorRef.current = onError;
|
|
1017
1017
|
const generation = React5.useRef(0);
|
|
1018
|
-
const context = React5.useRef({ indexUrl, csrfToken });
|
|
1019
|
-
context.current = { indexUrl, csrfToken };
|
|
1018
|
+
const context = React5.useRef({ indexUrl, csrfToken, request });
|
|
1019
|
+
context.current = { indexUrl, csrfToken, request };
|
|
1020
1020
|
const reload = React5.useCallback(async () => {
|
|
1021
1021
|
generation.current += 1;
|
|
1022
1022
|
const mine = generation.current;
|
|
1023
|
-
const { indexUrl: url, csrfToken: token } = context.current;
|
|
1024
|
-
const result = await credentialRequest("GET", url, void 0, token);
|
|
1023
|
+
const { indexUrl: url, csrfToken: token, request: send } = context.current;
|
|
1024
|
+
const result = await credentialRequest("GET", url, void 0, token, send);
|
|
1025
1025
|
if (mine !== generation.current) {
|
|
1026
1026
|
return;
|
|
1027
1027
|
}
|
|
@@ -1042,7 +1042,7 @@ function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, on
|
|
|
1042
1042
|
(current) => current === null ? current : list === "tokens" ? { ...current, tokens: current.tokens.filter((token) => token.id !== id) } : { ...current, apps: current.apps.filter((app) => app.id !== id) }
|
|
1043
1043
|
);
|
|
1044
1044
|
}, []);
|
|
1045
|
-
const shared = { components, csrfToken, onError, onSuccess, removeLocally };
|
|
1045
|
+
const shared = { components, csrfToken, request, onError, onSuccess, removeLocally };
|
|
1046
1046
|
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(Card, { children: [
|
|
1047
1047
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(CardHeader, { children: [
|
|
1048
1048
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CardTitle, { children: "API access" }),
|
|
@@ -1075,8 +1075,8 @@ function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, on
|
|
|
1075
1075
|
] }) })
|
|
1076
1076
|
] });
|
|
1077
1077
|
}
|
|
1078
|
-
function TokenSection({ index, components, csrfToken, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1079
|
-
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
1078
|
+
function TokenSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1079
|
+
const { Button, Input, Label, RadioGroup } = resolveAuthComponents(components);
|
|
1080
1080
|
const [name, setName] = React5.useState("");
|
|
1081
1081
|
const [scopes, setScopes] = React5.useState([]);
|
|
1082
1082
|
const [lifetime, setLifetime] = React5.useState(index.token_lifetimes[0] ?? "");
|
|
@@ -1088,7 +1088,7 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1088
1088
|
return;
|
|
1089
1089
|
}
|
|
1090
1090
|
setBusy(true);
|
|
1091
|
-
const result = await credentialRequest("POST", index.issue_token_href, { name, scopes, lifetime }, csrfToken);
|
|
1091
|
+
const result = await credentialRequest("POST", index.issue_token_href, { name, scopes, lifetime }, csrfToken, request);
|
|
1092
1092
|
setBusy(false);
|
|
1093
1093
|
if (!result.ok) {
|
|
1094
1094
|
setError(result.message);
|
|
@@ -1108,7 +1108,7 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1108
1108
|
return;
|
|
1109
1109
|
}
|
|
1110
1110
|
setPending((current) => [...current, token.id]);
|
|
1111
|
-
const result = await credentialRequest("DELETE", token.revoke_href, void 0, csrfToken);
|
|
1111
|
+
const result = await credentialRequest("DELETE", token.revoke_href, void 0, csrfToken, request);
|
|
1112
1112
|
if (!result.ok) {
|
|
1113
1113
|
setPending((current) => current.filter((id) => id !== token.id));
|
|
1114
1114
|
setError(result.message);
|
|
@@ -1128,10 +1128,18 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1128
1128
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Label, { htmlFor: "api-token-name", children: "Token name" }),
|
|
1129
1129
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Input, { id: "api-token-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1130
1130
|
] }),
|
|
1131
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ScopePicker, { idPrefix: "api-token-scope", scopes: index.scopes, selected: scopes, onChange: setScopes }),
|
|
1131
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ScopePicker, { idPrefix: "api-token-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
1132
1132
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1133
1133
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("legend", { children: "Expires after" }),
|
|
1134
|
-
|
|
1134
|
+
RadioGroup ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1135
|
+
RadioGroup,
|
|
1136
|
+
{
|
|
1137
|
+
name: "api-token-lifetime",
|
|
1138
|
+
value: lifetime,
|
|
1139
|
+
onValueChange: setLifetime,
|
|
1140
|
+
options: index.token_lifetimes.map((spec) => ({ value: spec, label: describeDuration(spec) }))
|
|
1141
|
+
}
|
|
1142
|
+
) : index.token_lifetimes.map((spec) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { children: [
|
|
1135
1143
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { type: "radio", name: "api-token-lifetime", checked: lifetime === spec, onChange: () => setLifetime(spec) }),
|
|
1136
1144
|
" ",
|
|
1137
1145
|
describeDuration(spec)
|
|
@@ -1153,8 +1161,8 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1153
1161
|
] }, token.id)) })
|
|
1154
1162
|
] });
|
|
1155
1163
|
}
|
|
1156
|
-
function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1157
|
-
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
1164
|
+
function AppSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1165
|
+
const { Button, Input, Label, Textarea, RadioGroup } = resolveAuthComponents(components);
|
|
1158
1166
|
const [name, setName] = React5.useState("");
|
|
1159
1167
|
const [redirects, setRedirects] = React5.useState("");
|
|
1160
1168
|
const [confidential, setConfidential] = React5.useState(true);
|
|
@@ -1172,7 +1180,8 @@ function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess,
|
|
|
1172
1180
|
"POST",
|
|
1173
1181
|
index.register_app_href,
|
|
1174
1182
|
{ name, redirect_uris: redirectUris, confidential, scopes },
|
|
1175
|
-
csrfToken
|
|
1183
|
+
csrfToken,
|
|
1184
|
+
request
|
|
1176
1185
|
);
|
|
1177
1186
|
setBusy(false);
|
|
1178
1187
|
if (!result.ok) {
|
|
@@ -1194,7 +1203,7 @@ function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess,
|
|
|
1194
1203
|
return;
|
|
1195
1204
|
}
|
|
1196
1205
|
setPending((current) => [...current, app.id]);
|
|
1197
|
-
const result = await credentialRequest("DELETE", app.delete_href, void 0, csrfToken);
|
|
1206
|
+
const result = await credentialRequest("DELETE", app.delete_href, void 0, csrfToken, request);
|
|
1198
1207
|
if (!result.ok) {
|
|
1199
1208
|
setPending((current) => current.filter((id) => id !== app.id));
|
|
1200
1209
|
setError(result.message);
|
|
@@ -1216,20 +1225,33 @@ function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess,
|
|
|
1216
1225
|
] }),
|
|
1217
1226
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1218
1227
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Label, { htmlFor: "oauth-app-redirects", children: "Redirect URIs (one per line)" }),
|
|
1219
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("textarea", { id: "oauth-app-redirects", rows: 3, value: redirects, onChange: (event) => setRedirects(event.target.value) })
|
|
1228
|
+
Textarea ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Textarea, { id: "oauth-app-redirects", rows: 3, value: redirects, onChange: (event) => setRedirects(event.target.value) }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("textarea", { id: "oauth-app-redirects", rows: 3, value: redirects, onChange: (event) => setRedirects(event.target.value) })
|
|
1220
1229
|
] }),
|
|
1221
1230
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1222
1231
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("legend", { children: "Client type" }),
|
|
1223
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1232
|
+
RadioGroup ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1233
|
+
RadioGroup,
|
|
1234
|
+
{
|
|
1235
|
+
name: "oauth-app-type",
|
|
1236
|
+
value: confidential ? "confidential" : "public",
|
|
1237
|
+
onValueChange: (value) => setConfidential(value === "confidential"),
|
|
1238
|
+
options: [
|
|
1239
|
+
{ value: "confidential", label: "Confidential: the app keeps a client secret on its server" },
|
|
1240
|
+
{ value: "public", label: "Public: no secret, PKCE only" }
|
|
1241
|
+
]
|
|
1242
|
+
}
|
|
1243
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
1244
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { children: [
|
|
1245
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { type: "radio", name: "oauth-app-type", checked: confidential, onChange: () => setConfidential(true) }),
|
|
1246
|
+
" Confidential: the app keeps a client secret on its server"
|
|
1247
|
+
] }),
|
|
1248
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { children: [
|
|
1249
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { type: "radio", name: "oauth-app-type", checked: !confidential, onChange: () => setConfidential(false) }),
|
|
1250
|
+
" Public: no secret, PKCE only"
|
|
1251
|
+
] })
|
|
1230
1252
|
] })
|
|
1231
1253
|
] }),
|
|
1232
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ScopePicker, { idPrefix: "oauth-app-scope", scopes: index.scopes, selected: scopes, onChange: setScopes }),
|
|
1254
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ScopePicker, { idPrefix: "oauth-app-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
1233
1255
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Button, { type: "submit", disabled: busy || name.trim() === "" || redirectUris.length === 0 || scopes.length === 0, children: "Register OAuth app" }) })
|
|
1234
1256
|
] }),
|
|
1235
1257
|
error !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "alert", children: error }),
|
|
@@ -1253,22 +1275,17 @@ function ScopePicker({
|
|
|
1253
1275
|
idPrefix,
|
|
1254
1276
|
scopes,
|
|
1255
1277
|
selected,
|
|
1256
|
-
onChange
|
|
1278
|
+
onChange,
|
|
1279
|
+
components
|
|
1257
1280
|
}) {
|
|
1281
|
+
const { Checkbox } = resolveAuthComponents(components);
|
|
1282
|
+
const toggle = (scopeId, checked) => onChange(checked ? [...selected, scopeId] : selected.filter((value) => value !== scopeId));
|
|
1258
1283
|
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0, minWidth: 0 }, children: [
|
|
1259
1284
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("legend", { children: "Permissions" }),
|
|
1260
1285
|
scopes.map((scope) => {
|
|
1261
1286
|
const id = `${idPrefix}-${scope.id}`;
|
|
1262
1287
|
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { htmlFor: id, style: { overflowWrap: "anywhere" }, children: [
|
|
1263
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1264
|
-
"input",
|
|
1265
|
-
{
|
|
1266
|
-
id,
|
|
1267
|
-
type: "checkbox",
|
|
1268
|
-
checked: selected.includes(scope.id),
|
|
1269
|
-
onChange: (event) => onChange(event.target.checked ? [...selected, scope.id] : selected.filter((value) => value !== scope.id))
|
|
1270
|
-
}
|
|
1271
|
-
),
|
|
1288
|
+
Checkbox ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Checkbox, { id, checked: selected.includes(scope.id), onCheckedChange: (checked) => toggle(scope.id, checked) }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { id, type: "checkbox", checked: selected.includes(scope.id), onChange: (event) => toggle(scope.id, event.target.checked) }),
|
|
1272
1289
|
" ",
|
|
1273
1290
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("code", { children: scope.id }),
|
|
1274
1291
|
" ",
|
|
@@ -1338,7 +1355,16 @@ function describeDuration(spec) {
|
|
|
1338
1355
|
const parts = match.slice(1).map((value, position) => value ? `${Number(value)} ${units[position]}${Number(value) === 1 ? "" : "s"}` : null).filter((part) => part !== null);
|
|
1339
1356
|
return parts.length > 0 ? parts.join(" ") : spec;
|
|
1340
1357
|
}
|
|
1341
|
-
async function credentialRequest(method, url, body, csrfToken) {
|
|
1358
|
+
async function credentialRequest(method, url, body, csrfToken, request) {
|
|
1359
|
+
if (request !== void 0) {
|
|
1360
|
+
let result;
|
|
1361
|
+
try {
|
|
1362
|
+
result = await request(method, url, body);
|
|
1363
|
+
} catch {
|
|
1364
|
+
return { ok: false, status: 0, message: "The server could not be reached. Check the connection and try again." };
|
|
1365
|
+
}
|
|
1366
|
+
return result.ok ? { ok: true, status: result.status, data: unwrapData(result.payload) } : result;
|
|
1367
|
+
}
|
|
1342
1368
|
let response;
|
|
1343
1369
|
try {
|
|
1344
1370
|
response = await fetch(url, {
|
|
@@ -1356,11 +1382,13 @@ async function credentialRequest(method, url, body, csrfToken) {
|
|
|
1356
1382
|
}
|
|
1357
1383
|
const payload = await response.json().catch(() => null);
|
|
1358
1384
|
if (response.ok) {
|
|
1359
|
-
|
|
1360
|
-
return { ok: true, status: response.status, data };
|
|
1385
|
+
return { ok: true, status: response.status, data: unwrapData(payload) };
|
|
1361
1386
|
}
|
|
1362
1387
|
return { ok: false, status: response.status, message: refusalMessage(response.status, payload) };
|
|
1363
1388
|
}
|
|
1389
|
+
function unwrapData(payload) {
|
|
1390
|
+
return typeof payload === "object" && payload !== null && "data" in payload ? payload.data : payload;
|
|
1391
|
+
}
|
|
1364
1392
|
function refusalMessage(status, payload) {
|
|
1365
1393
|
if (status === 401 || status === 419) {
|
|
1366
1394
|
return "Your session has expired. Reload the page and sign in again.";
|
package/dist/index.d.cts
CHANGED
|
@@ -61,6 +61,24 @@ type AuthButtonComponentProps = React.ButtonHTMLAttributes<HTMLButtonElement> &
|
|
|
61
61
|
type AuthInputComponentProps = React.InputHTMLAttributes<HTMLInputElement>;
|
|
62
62
|
type AuthLabelComponentProps = React.LabelHTMLAttributes<HTMLLabelElement>;
|
|
63
63
|
type AuthContainerComponentProps = React.HTMLAttributes<HTMLDivElement>;
|
|
64
|
+
type AuthTextareaComponentProps = React.TextareaHTMLAttributes<HTMLTextAreaElement>;
|
|
65
|
+
/** A controlled checkbox: the app's own, e.g. a design-system `Checkbox` with `onCheckedChange`. */
|
|
66
|
+
interface AuthCheckboxComponentProps {
|
|
67
|
+
id: string;
|
|
68
|
+
checked: boolean;
|
|
69
|
+
onCheckedChange: (checked: boolean) => void;
|
|
70
|
+
}
|
|
71
|
+
interface AuthRadioGroupOption {
|
|
72
|
+
value: string;
|
|
73
|
+
label: React.ReactNode;
|
|
74
|
+
}
|
|
75
|
+
/** A controlled set of radio options, rendered however the app renders radio groups. */
|
|
76
|
+
interface AuthRadioGroupComponentProps {
|
|
77
|
+
name: string;
|
|
78
|
+
value: string;
|
|
79
|
+
onValueChange: (value: string) => void;
|
|
80
|
+
options: AuthRadioGroupOption[];
|
|
81
|
+
}
|
|
64
82
|
interface AuthComponentSet {
|
|
65
83
|
Button: React.ComponentType<AuthButtonComponentProps>;
|
|
66
84
|
Card: React.ComponentType<AuthContainerComponentProps>;
|
|
@@ -70,6 +88,10 @@ interface AuthComponentSet {
|
|
|
70
88
|
CardTitle: React.ComponentType<AuthContainerComponentProps>;
|
|
71
89
|
Input: React.ComponentType<AuthInputComponentProps>;
|
|
72
90
|
Label: React.ComponentType<AuthLabelComponentProps>;
|
|
91
|
+
/** Optional; native elements are rendered when omitted. */
|
|
92
|
+
Textarea?: React.ComponentType<AuthTextareaComponentProps>;
|
|
93
|
+
Checkbox?: React.ComponentType<AuthCheckboxComponentProps>;
|
|
94
|
+
RadioGroup?: React.ComponentType<AuthRadioGroupComponentProps>;
|
|
73
95
|
}
|
|
74
96
|
type AuthComponents = AuthComponentSet;
|
|
75
97
|
type AuthComponentOverrides = Partial<AuthComponentSet>;
|
|
@@ -260,6 +282,24 @@ interface ApiCredentialEndpointLink {
|
|
|
260
282
|
label: string;
|
|
261
283
|
url: string;
|
|
262
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* The outcome of one credential request: on success the parsed JSON body
|
|
287
|
+
* (the component reads its `data` when present), on failure the message to show.
|
|
288
|
+
*/
|
|
289
|
+
type ApiCredentialRequestResult = {
|
|
290
|
+
ok: true;
|
|
291
|
+
status: number;
|
|
292
|
+
payload: unknown;
|
|
293
|
+
} | {
|
|
294
|
+
ok: false;
|
|
295
|
+
status: number;
|
|
296
|
+
message: string;
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Performs one credential request in place of the built-in `fetch`, so an app
|
|
300
|
+
* can route it through its own client (session-expiry redirect, CSRF, retries).
|
|
301
|
+
*/
|
|
302
|
+
type ApiCredentialRequest = (method: 'GET' | 'POST' | 'DELETE', url: string, body?: object) => Promise<ApiCredentialRequestResult>;
|
|
263
303
|
interface ApiCredentialsSectionProps {
|
|
264
304
|
/** The credential service's index URL (GET). */
|
|
265
305
|
indexUrl: string;
|
|
@@ -267,6 +307,8 @@ interface ApiCredentialsSectionProps {
|
|
|
267
307
|
/** Values a connector needs: the OpenAPI document, API base, OAuth authorize/token URLs, MCP URL. */
|
|
268
308
|
links?: ApiCredentialEndpointLink[];
|
|
269
309
|
csrfToken?: string;
|
|
310
|
+
/** Replaces the built-in `fetch` for every credential request; `csrfToken` is then the app's to send. */
|
|
311
|
+
request?: ApiCredentialRequest;
|
|
270
312
|
onSuccess?: (message: string) => void;
|
|
271
313
|
onError?: (field: string, message: string) => void;
|
|
272
314
|
}
|
|
@@ -279,8 +321,8 @@ interface ApiCredentialsSectionProps {
|
|
|
279
321
|
* in this component's memory; afterwards only the lists are reloaded, so it is
|
|
280
322
|
* never part of page props or browser history.
|
|
281
323
|
*/
|
|
282
|
-
declare function ApiCredentialsSection({ indexUrl, components, links, csrfToken, onSuccess, onError }: ApiCredentialsSectionProps): React.JSX.Element;
|
|
324
|
+
declare function ApiCredentialsSection({ indexUrl, components, links, csrfToken, request, onSuccess, onError }: ApiCredentialsSectionProps): React.JSX.Element;
|
|
283
325
|
/** PT4H → "4 hours", P30D → "30 days", P1Y → "1 year". Unknown shapes are shown as given. */
|
|
284
326
|
declare function describeDuration(spec: string): string;
|
|
285
327
|
|
|
286
|
-
export { type ApiCredentialApp, type ApiCredentialEndpointLink, type ApiCredentialIndex, type ApiCredentialScope, type ApiCredentialToken, ApiCredentialsSection, type ApiCredentialsSectionProps, type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthSignupField, type AuthSignupValues, type AuthValidationErrors, ChangePasswordForm, type IssuedApiCredential, LoginForm, type LoginFormProps, type LoginRememberMeCheckboxProps, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, type RelyingApplication, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, describeDuration, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey, relyingApplicationsFrom, safeApplicationHref };
|
|
328
|
+
export { type ApiCredentialApp, type ApiCredentialEndpointLink, type ApiCredentialIndex, type ApiCredentialRequest, type ApiCredentialRequestResult, type ApiCredentialScope, type ApiCredentialToken, ApiCredentialsSection, type ApiCredentialsSectionProps, type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthCheckboxComponentProps, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthRadioGroupComponentProps, type AuthRadioGroupOption, type AuthSignupField, type AuthSignupValues, type AuthTextareaComponentProps, type AuthValidationErrors, ChangePasswordForm, type IssuedApiCredential, LoginForm, type LoginFormProps, type LoginRememberMeCheckboxProps, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, type RelyingApplication, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, describeDuration, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey, relyingApplicationsFrom, safeApplicationHref };
|
package/dist/index.d.ts
CHANGED
|
@@ -61,6 +61,24 @@ type AuthButtonComponentProps = React.ButtonHTMLAttributes<HTMLButtonElement> &
|
|
|
61
61
|
type AuthInputComponentProps = React.InputHTMLAttributes<HTMLInputElement>;
|
|
62
62
|
type AuthLabelComponentProps = React.LabelHTMLAttributes<HTMLLabelElement>;
|
|
63
63
|
type AuthContainerComponentProps = React.HTMLAttributes<HTMLDivElement>;
|
|
64
|
+
type AuthTextareaComponentProps = React.TextareaHTMLAttributes<HTMLTextAreaElement>;
|
|
65
|
+
/** A controlled checkbox: the app's own, e.g. a design-system `Checkbox` with `onCheckedChange`. */
|
|
66
|
+
interface AuthCheckboxComponentProps {
|
|
67
|
+
id: string;
|
|
68
|
+
checked: boolean;
|
|
69
|
+
onCheckedChange: (checked: boolean) => void;
|
|
70
|
+
}
|
|
71
|
+
interface AuthRadioGroupOption {
|
|
72
|
+
value: string;
|
|
73
|
+
label: React.ReactNode;
|
|
74
|
+
}
|
|
75
|
+
/** A controlled set of radio options, rendered however the app renders radio groups. */
|
|
76
|
+
interface AuthRadioGroupComponentProps {
|
|
77
|
+
name: string;
|
|
78
|
+
value: string;
|
|
79
|
+
onValueChange: (value: string) => void;
|
|
80
|
+
options: AuthRadioGroupOption[];
|
|
81
|
+
}
|
|
64
82
|
interface AuthComponentSet {
|
|
65
83
|
Button: React.ComponentType<AuthButtonComponentProps>;
|
|
66
84
|
Card: React.ComponentType<AuthContainerComponentProps>;
|
|
@@ -70,6 +88,10 @@ interface AuthComponentSet {
|
|
|
70
88
|
CardTitle: React.ComponentType<AuthContainerComponentProps>;
|
|
71
89
|
Input: React.ComponentType<AuthInputComponentProps>;
|
|
72
90
|
Label: React.ComponentType<AuthLabelComponentProps>;
|
|
91
|
+
/** Optional; native elements are rendered when omitted. */
|
|
92
|
+
Textarea?: React.ComponentType<AuthTextareaComponentProps>;
|
|
93
|
+
Checkbox?: React.ComponentType<AuthCheckboxComponentProps>;
|
|
94
|
+
RadioGroup?: React.ComponentType<AuthRadioGroupComponentProps>;
|
|
73
95
|
}
|
|
74
96
|
type AuthComponents = AuthComponentSet;
|
|
75
97
|
type AuthComponentOverrides = Partial<AuthComponentSet>;
|
|
@@ -260,6 +282,24 @@ interface ApiCredentialEndpointLink {
|
|
|
260
282
|
label: string;
|
|
261
283
|
url: string;
|
|
262
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* The outcome of one credential request: on success the parsed JSON body
|
|
287
|
+
* (the component reads its `data` when present), on failure the message to show.
|
|
288
|
+
*/
|
|
289
|
+
type ApiCredentialRequestResult = {
|
|
290
|
+
ok: true;
|
|
291
|
+
status: number;
|
|
292
|
+
payload: unknown;
|
|
293
|
+
} | {
|
|
294
|
+
ok: false;
|
|
295
|
+
status: number;
|
|
296
|
+
message: string;
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Performs one credential request in place of the built-in `fetch`, so an app
|
|
300
|
+
* can route it through its own client (session-expiry redirect, CSRF, retries).
|
|
301
|
+
*/
|
|
302
|
+
type ApiCredentialRequest = (method: 'GET' | 'POST' | 'DELETE', url: string, body?: object) => Promise<ApiCredentialRequestResult>;
|
|
263
303
|
interface ApiCredentialsSectionProps {
|
|
264
304
|
/** The credential service's index URL (GET). */
|
|
265
305
|
indexUrl: string;
|
|
@@ -267,6 +307,8 @@ interface ApiCredentialsSectionProps {
|
|
|
267
307
|
/** Values a connector needs: the OpenAPI document, API base, OAuth authorize/token URLs, MCP URL. */
|
|
268
308
|
links?: ApiCredentialEndpointLink[];
|
|
269
309
|
csrfToken?: string;
|
|
310
|
+
/** Replaces the built-in `fetch` for every credential request; `csrfToken` is then the app's to send. */
|
|
311
|
+
request?: ApiCredentialRequest;
|
|
270
312
|
onSuccess?: (message: string) => void;
|
|
271
313
|
onError?: (field: string, message: string) => void;
|
|
272
314
|
}
|
|
@@ -279,8 +321,8 @@ interface ApiCredentialsSectionProps {
|
|
|
279
321
|
* in this component's memory; afterwards only the lists are reloaded, so it is
|
|
280
322
|
* never part of page props or browser history.
|
|
281
323
|
*/
|
|
282
|
-
declare function ApiCredentialsSection({ indexUrl, components, links, csrfToken, onSuccess, onError }: ApiCredentialsSectionProps): React.JSX.Element;
|
|
324
|
+
declare function ApiCredentialsSection({ indexUrl, components, links, csrfToken, request, onSuccess, onError }: ApiCredentialsSectionProps): React.JSX.Element;
|
|
283
325
|
/** PT4H → "4 hours", P30D → "30 days", P1Y → "1 year". Unknown shapes are shown as given. */
|
|
284
326
|
declare function describeDuration(spec: string): string;
|
|
285
327
|
|
|
286
|
-
export { type ApiCredentialApp, type ApiCredentialEndpointLink, type ApiCredentialIndex, type ApiCredentialScope, type ApiCredentialToken, ApiCredentialsSection, type ApiCredentialsSectionProps, type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthSignupField, type AuthSignupValues, type AuthValidationErrors, ChangePasswordForm, type IssuedApiCredential, LoginForm, type LoginFormProps, type LoginRememberMeCheckboxProps, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, type RelyingApplication, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, describeDuration, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey, relyingApplicationsFrom, safeApplicationHref };
|
|
328
|
+
export { type ApiCredentialApp, type ApiCredentialEndpointLink, type ApiCredentialIndex, type ApiCredentialRequest, type ApiCredentialRequestResult, type ApiCredentialScope, type ApiCredentialToken, ApiCredentialsSection, type ApiCredentialsSectionProps, type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthCheckboxComponentProps, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthRadioGroupComponentProps, type AuthRadioGroupOption, type AuthSignupField, type AuthSignupValues, type AuthTextareaComponentProps, type AuthValidationErrors, ChangePasswordForm, type IssuedApiCredential, LoginForm, type LoginFormProps, type LoginRememberMeCheckboxProps, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, type RelyingApplication, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, describeDuration, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey, relyingApplicationsFrom, safeApplicationHref };
|
package/dist/index.js
CHANGED
|
@@ -946,7 +946,7 @@ function relyingApplicationsFrom(value) {
|
|
|
946
946
|
// src/api-credentials-section.tsx
|
|
947
947
|
import * as React5 from "react";
|
|
948
948
|
import { Fragment, jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
949
|
-
function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, onSuccess, onError }) {
|
|
949
|
+
function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, request, onSuccess, onError }) {
|
|
950
950
|
const { Card, CardContent, CardDescription, CardHeader, CardTitle } = resolveAuthComponents(components);
|
|
951
951
|
const [index, setIndex] = React5.useState(null);
|
|
952
952
|
const [issued, setIssued] = React5.useState([]);
|
|
@@ -960,13 +960,13 @@ function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, on
|
|
|
960
960
|
const onErrorRef = React5.useRef(onError);
|
|
961
961
|
onErrorRef.current = onError;
|
|
962
962
|
const generation = React5.useRef(0);
|
|
963
|
-
const context = React5.useRef({ indexUrl, csrfToken });
|
|
964
|
-
context.current = { indexUrl, csrfToken };
|
|
963
|
+
const context = React5.useRef({ indexUrl, csrfToken, request });
|
|
964
|
+
context.current = { indexUrl, csrfToken, request };
|
|
965
965
|
const reload = React5.useCallback(async () => {
|
|
966
966
|
generation.current += 1;
|
|
967
967
|
const mine = generation.current;
|
|
968
|
-
const { indexUrl: url, csrfToken: token } = context.current;
|
|
969
|
-
const result = await credentialRequest("GET", url, void 0, token);
|
|
968
|
+
const { indexUrl: url, csrfToken: token, request: send } = context.current;
|
|
969
|
+
const result = await credentialRequest("GET", url, void 0, token, send);
|
|
970
970
|
if (mine !== generation.current) {
|
|
971
971
|
return;
|
|
972
972
|
}
|
|
@@ -987,7 +987,7 @@ function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, on
|
|
|
987
987
|
(current) => current === null ? current : list === "tokens" ? { ...current, tokens: current.tokens.filter((token) => token.id !== id) } : { ...current, apps: current.apps.filter((app) => app.id !== id) }
|
|
988
988
|
);
|
|
989
989
|
}, []);
|
|
990
|
-
const shared = { components, csrfToken, onError, onSuccess, removeLocally };
|
|
990
|
+
const shared = { components, csrfToken, request, onError, onSuccess, removeLocally };
|
|
991
991
|
return /* @__PURE__ */ jsxs4(Card, { children: [
|
|
992
992
|
/* @__PURE__ */ jsxs4(CardHeader, { children: [
|
|
993
993
|
/* @__PURE__ */ jsx4(CardTitle, { children: "API access" }),
|
|
@@ -1020,8 +1020,8 @@ function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, on
|
|
|
1020
1020
|
] }) })
|
|
1021
1021
|
] });
|
|
1022
1022
|
}
|
|
1023
|
-
function TokenSection({ index, components, csrfToken, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1024
|
-
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
1023
|
+
function TokenSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1024
|
+
const { Button, Input, Label, RadioGroup } = resolveAuthComponents(components);
|
|
1025
1025
|
const [name, setName] = React5.useState("");
|
|
1026
1026
|
const [scopes, setScopes] = React5.useState([]);
|
|
1027
1027
|
const [lifetime, setLifetime] = React5.useState(index.token_lifetimes[0] ?? "");
|
|
@@ -1033,7 +1033,7 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1033
1033
|
return;
|
|
1034
1034
|
}
|
|
1035
1035
|
setBusy(true);
|
|
1036
|
-
const result = await credentialRequest("POST", index.issue_token_href, { name, scopes, lifetime }, csrfToken);
|
|
1036
|
+
const result = await credentialRequest("POST", index.issue_token_href, { name, scopes, lifetime }, csrfToken, request);
|
|
1037
1037
|
setBusy(false);
|
|
1038
1038
|
if (!result.ok) {
|
|
1039
1039
|
setError(result.message);
|
|
@@ -1053,7 +1053,7 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1053
1053
|
return;
|
|
1054
1054
|
}
|
|
1055
1055
|
setPending((current) => [...current, token.id]);
|
|
1056
|
-
const result = await credentialRequest("DELETE", token.revoke_href, void 0, csrfToken);
|
|
1056
|
+
const result = await credentialRequest("DELETE", token.revoke_href, void 0, csrfToken, request);
|
|
1057
1057
|
if (!result.ok) {
|
|
1058
1058
|
setPending((current) => current.filter((id) => id !== token.id));
|
|
1059
1059
|
setError(result.message);
|
|
@@ -1073,10 +1073,18 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1073
1073
|
/* @__PURE__ */ jsx4(Label, { htmlFor: "api-token-name", children: "Token name" }),
|
|
1074
1074
|
/* @__PURE__ */ jsx4(Input, { id: "api-token-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1075
1075
|
] }),
|
|
1076
|
-
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "api-token-scope", scopes: index.scopes, selected: scopes, onChange: setScopes }),
|
|
1076
|
+
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "api-token-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
1077
1077
|
/* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1078
1078
|
/* @__PURE__ */ jsx4("legend", { children: "Expires after" }),
|
|
1079
|
-
|
|
1079
|
+
RadioGroup ? /* @__PURE__ */ jsx4(
|
|
1080
|
+
RadioGroup,
|
|
1081
|
+
{
|
|
1082
|
+
name: "api-token-lifetime",
|
|
1083
|
+
value: lifetime,
|
|
1084
|
+
onValueChange: setLifetime,
|
|
1085
|
+
options: index.token_lifetimes.map((spec) => ({ value: spec, label: describeDuration(spec) }))
|
|
1086
|
+
}
|
|
1087
|
+
) : index.token_lifetimes.map((spec) => /* @__PURE__ */ jsxs4("label", { children: [
|
|
1080
1088
|
/* @__PURE__ */ jsx4("input", { type: "radio", name: "api-token-lifetime", checked: lifetime === spec, onChange: () => setLifetime(spec) }),
|
|
1081
1089
|
" ",
|
|
1082
1090
|
describeDuration(spec)
|
|
@@ -1098,8 +1106,8 @@ function TokenSection({ index, components, csrfToken, onIssued, reload, onSucces
|
|
|
1098
1106
|
] }, token.id)) })
|
|
1099
1107
|
] });
|
|
1100
1108
|
}
|
|
1101
|
-
function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1102
|
-
const { Button, Input, Label } = resolveAuthComponents(components);
|
|
1109
|
+
function AppSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1110
|
+
const { Button, Input, Label, Textarea, RadioGroup } = resolveAuthComponents(components);
|
|
1103
1111
|
const [name, setName] = React5.useState("");
|
|
1104
1112
|
const [redirects, setRedirects] = React5.useState("");
|
|
1105
1113
|
const [confidential, setConfidential] = React5.useState(true);
|
|
@@ -1117,7 +1125,8 @@ function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess,
|
|
|
1117
1125
|
"POST",
|
|
1118
1126
|
index.register_app_href,
|
|
1119
1127
|
{ name, redirect_uris: redirectUris, confidential, scopes },
|
|
1120
|
-
csrfToken
|
|
1128
|
+
csrfToken,
|
|
1129
|
+
request
|
|
1121
1130
|
);
|
|
1122
1131
|
setBusy(false);
|
|
1123
1132
|
if (!result.ok) {
|
|
@@ -1139,7 +1148,7 @@ function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess,
|
|
|
1139
1148
|
return;
|
|
1140
1149
|
}
|
|
1141
1150
|
setPending((current) => [...current, app.id]);
|
|
1142
|
-
const result = await credentialRequest("DELETE", app.delete_href, void 0, csrfToken);
|
|
1151
|
+
const result = await credentialRequest("DELETE", app.delete_href, void 0, csrfToken, request);
|
|
1143
1152
|
if (!result.ok) {
|
|
1144
1153
|
setPending((current) => current.filter((id) => id !== app.id));
|
|
1145
1154
|
setError(result.message);
|
|
@@ -1161,20 +1170,33 @@ function AppSection({ index, components, csrfToken, onIssued, reload, onSuccess,
|
|
|
1161
1170
|
] }),
|
|
1162
1171
|
/* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1163
1172
|
/* @__PURE__ */ jsx4(Label, { htmlFor: "oauth-app-redirects", children: "Redirect URIs (one per line)" }),
|
|
1164
|
-
/* @__PURE__ */ jsx4("textarea", { id: "oauth-app-redirects", rows: 3, value: redirects, onChange: (event) => setRedirects(event.target.value) })
|
|
1173
|
+
Textarea ? /* @__PURE__ */ jsx4(Textarea, { id: "oauth-app-redirects", rows: 3, value: redirects, onChange: (event) => setRedirects(event.target.value) }) : /* @__PURE__ */ jsx4("textarea", { id: "oauth-app-redirects", rows: 3, value: redirects, onChange: (event) => setRedirects(event.target.value) })
|
|
1165
1174
|
] }),
|
|
1166
1175
|
/* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1167
1176
|
/* @__PURE__ */ jsx4("legend", { children: "Client type" }),
|
|
1168
|
-
/* @__PURE__ */
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1177
|
+
RadioGroup ? /* @__PURE__ */ jsx4(
|
|
1178
|
+
RadioGroup,
|
|
1179
|
+
{
|
|
1180
|
+
name: "oauth-app-type",
|
|
1181
|
+
value: confidential ? "confidential" : "public",
|
|
1182
|
+
onValueChange: (value) => setConfidential(value === "confidential"),
|
|
1183
|
+
options: [
|
|
1184
|
+
{ value: "confidential", label: "Confidential: the app keeps a client secret on its server" },
|
|
1185
|
+
{ value: "public", label: "Public: no secret, PKCE only" }
|
|
1186
|
+
]
|
|
1187
|
+
}
|
|
1188
|
+
) : /* @__PURE__ */ jsxs4(Fragment, { children: [
|
|
1189
|
+
/* @__PURE__ */ jsxs4("label", { children: [
|
|
1190
|
+
/* @__PURE__ */ jsx4("input", { type: "radio", name: "oauth-app-type", checked: confidential, onChange: () => setConfidential(true) }),
|
|
1191
|
+
" Confidential: the app keeps a client secret on its server"
|
|
1192
|
+
] }),
|
|
1193
|
+
/* @__PURE__ */ jsxs4("label", { children: [
|
|
1194
|
+
/* @__PURE__ */ jsx4("input", { type: "radio", name: "oauth-app-type", checked: !confidential, onChange: () => setConfidential(false) }),
|
|
1195
|
+
" Public: no secret, PKCE only"
|
|
1196
|
+
] })
|
|
1175
1197
|
] })
|
|
1176
1198
|
] }),
|
|
1177
|
-
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "oauth-app-scope", scopes: index.scopes, selected: scopes, onChange: setScopes }),
|
|
1199
|
+
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "oauth-app-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
1178
1200
|
/* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(Button, { type: "submit", disabled: busy || name.trim() === "" || redirectUris.length === 0 || scopes.length === 0, children: "Register OAuth app" }) })
|
|
1179
1201
|
] }),
|
|
1180
1202
|
error !== null && /* @__PURE__ */ jsx4("p", { role: "alert", children: error }),
|
|
@@ -1198,22 +1220,17 @@ function ScopePicker({
|
|
|
1198
1220
|
idPrefix,
|
|
1199
1221
|
scopes,
|
|
1200
1222
|
selected,
|
|
1201
|
-
onChange
|
|
1223
|
+
onChange,
|
|
1224
|
+
components
|
|
1202
1225
|
}) {
|
|
1226
|
+
const { Checkbox } = resolveAuthComponents(components);
|
|
1227
|
+
const toggle = (scopeId, checked) => onChange(checked ? [...selected, scopeId] : selected.filter((value) => value !== scopeId));
|
|
1203
1228
|
return /* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0, minWidth: 0 }, children: [
|
|
1204
1229
|
/* @__PURE__ */ jsx4("legend", { children: "Permissions" }),
|
|
1205
1230
|
scopes.map((scope) => {
|
|
1206
1231
|
const id = `${idPrefix}-${scope.id}`;
|
|
1207
1232
|
return /* @__PURE__ */ jsxs4("label", { htmlFor: id, style: { overflowWrap: "anywhere" }, children: [
|
|
1208
|
-
/* @__PURE__ */ jsx4(
|
|
1209
|
-
"input",
|
|
1210
|
-
{
|
|
1211
|
-
id,
|
|
1212
|
-
type: "checkbox",
|
|
1213
|
-
checked: selected.includes(scope.id),
|
|
1214
|
-
onChange: (event) => onChange(event.target.checked ? [...selected, scope.id] : selected.filter((value) => value !== scope.id))
|
|
1215
|
-
}
|
|
1216
|
-
),
|
|
1233
|
+
Checkbox ? /* @__PURE__ */ jsx4(Checkbox, { id, checked: selected.includes(scope.id), onCheckedChange: (checked) => toggle(scope.id, checked) }) : /* @__PURE__ */ jsx4("input", { id, type: "checkbox", checked: selected.includes(scope.id), onChange: (event) => toggle(scope.id, event.target.checked) }),
|
|
1217
1234
|
" ",
|
|
1218
1235
|
/* @__PURE__ */ jsx4("code", { children: scope.id }),
|
|
1219
1236
|
" ",
|
|
@@ -1283,7 +1300,16 @@ function describeDuration(spec) {
|
|
|
1283
1300
|
const parts = match.slice(1).map((value, position) => value ? `${Number(value)} ${units[position]}${Number(value) === 1 ? "" : "s"}` : null).filter((part) => part !== null);
|
|
1284
1301
|
return parts.length > 0 ? parts.join(" ") : spec;
|
|
1285
1302
|
}
|
|
1286
|
-
async function credentialRequest(method, url, body, csrfToken) {
|
|
1303
|
+
async function credentialRequest(method, url, body, csrfToken, request) {
|
|
1304
|
+
if (request !== void 0) {
|
|
1305
|
+
let result;
|
|
1306
|
+
try {
|
|
1307
|
+
result = await request(method, url, body);
|
|
1308
|
+
} catch {
|
|
1309
|
+
return { ok: false, status: 0, message: "The server could not be reached. Check the connection and try again." };
|
|
1310
|
+
}
|
|
1311
|
+
return result.ok ? { ok: true, status: result.status, data: unwrapData(result.payload) } : result;
|
|
1312
|
+
}
|
|
1287
1313
|
let response;
|
|
1288
1314
|
try {
|
|
1289
1315
|
response = await fetch(url, {
|
|
@@ -1301,11 +1327,13 @@ async function credentialRequest(method, url, body, csrfToken) {
|
|
|
1301
1327
|
}
|
|
1302
1328
|
const payload = await response.json().catch(() => null);
|
|
1303
1329
|
if (response.ok) {
|
|
1304
|
-
|
|
1305
|
-
return { ok: true, status: response.status, data };
|
|
1330
|
+
return { ok: true, status: response.status, data: unwrapData(payload) };
|
|
1306
1331
|
}
|
|
1307
1332
|
return { ok: false, status: response.status, message: refusalMessage(response.status, payload) };
|
|
1308
1333
|
}
|
|
1334
|
+
function unwrapData(payload) {
|
|
1335
|
+
return typeof payload === "object" && payload !== null && "data" in payload ? payload.data : payload;
|
|
1336
|
+
}
|
|
1309
1337
|
function refusalMessage(status, payload) {
|
|
1310
1338
|
if (status === 401 || status === 419) {
|
|
1311
1339
|
return "Your session has expired. Reload the page and sign in again.";
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bwh-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Shared React auth UI and browser WebAuthn helpers for BWH applications.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@11.
|
|
7
|
+
"packageManager": "pnpm@12.11.2",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=22"
|
|
10
|
+
},
|
|
8
11
|
"repository": {
|
|
9
12
|
"type": "git",
|
|
10
13
|
"url": "git+https://github.com/bherila/auth-react.git"
|