bwh-auth 0.3.3 → 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 +36 -0
- package/dist/index.cjs +421 -0
- package/dist/index.d.cts +112 -1
- package/dist/index.d.ts +112 -1
- package/dist/index.js +419 -0
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ The companion Laravel API package is
|
|
|
22
22
|
- `TwoFactorForm`
|
|
23
23
|
- `PasskeyLoginButton`
|
|
24
24
|
- `PasskeySection`
|
|
25
|
+
- `ApiCredentialsSection`
|
|
25
26
|
|
|
26
27
|
## Relying-party helpers
|
|
27
28
|
|
|
@@ -206,6 +207,41 @@ The explicit passkey button and conditional autofill both default to the Laravel
|
|
|
206
207
|
/>
|
|
207
208
|
```
|
|
208
209
|
|
|
210
|
+
## API credentials
|
|
211
|
+
|
|
212
|
+
`ApiCredentialsSection` lets a signed-in person create credentials for apps that use the API:
|
|
213
|
+
- **API tokens**, for connectors that ask for a key;
|
|
214
|
+
- **OAuth apps**, for connectors that run the authorization-code flow.
|
|
215
|
+
|
|
216
|
+
It talks to the auth package's credential service (`oauth_server.credentials`) through its JSON index URL. The server sends every action URL, so the component assembles none.
|
|
217
|
+
|
|
218
|
+
```tsx
|
|
219
|
+
<ApiCredentialsSection
|
|
220
|
+
indexUrl="/account/api-credentials"
|
|
221
|
+
components={components}
|
|
222
|
+
links={[
|
|
223
|
+
{ label: 'OpenAPI document', url: openApiUrl },
|
|
224
|
+
{ label: 'OAuth authorize URL', url: authorizeUrl },
|
|
225
|
+
{ label: 'OAuth token URL', url: tokenUrl },
|
|
226
|
+
]}
|
|
227
|
+
/>
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
- **Each new secret is shown once.** It comes from the creation response and is kept only in component memory. Afterwards only the lists are reloaded, so it never enters page props or history.
|
|
231
|
+
- **Requests** send the session cookie and `X-CSRF-TOKEN`. A 401 or 419 response becomes "sign in again".
|
|
232
|
+
- **While the server reports issuing unavailable** (OAuth switched off), the create forms are hidden, and revoke and delete still work.
|
|
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
|
+
```
|
|
244
|
+
|
|
209
245
|
## Endpoint Defaults
|
|
210
246
|
|
|
211
247
|
Auth forms default to the Laravel package API routes where the Laravel package owns the endpoint:
|
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
ApiCredentialsSection: () => ApiCredentialsSection,
|
|
33
34
|
ChangePasswordForm: () => ChangePasswordForm,
|
|
34
35
|
LoginForm: () => LoginForm,
|
|
35
36
|
PasskeyLoginButton: () => PasskeyLoginButton,
|
|
@@ -41,6 +42,7 @@ __export(index_exports, {
|
|
|
41
42
|
arrayBufferToBase64url: () => arrayBufferToBase64url,
|
|
42
43
|
authenticateWithPasskey: () => authenticateWithPasskey,
|
|
43
44
|
base64urlToArrayBuffer: () => base64urlToArrayBuffer,
|
|
45
|
+
describeDuration: () => describeDuration,
|
|
44
46
|
getCsrfToken: () => getCsrfToken,
|
|
45
47
|
getDefaultPasskeyName: () => getDefaultPasskeyName,
|
|
46
48
|
isAbortError: () => isAbortError,
|
|
@@ -995,8 +997,426 @@ function relyingApplicationsFrom(value) {
|
|
|
995
997
|
return href === null ? [] : [{ key, name: name.trim(), url: href }];
|
|
996
998
|
});
|
|
997
999
|
}
|
|
1000
|
+
|
|
1001
|
+
// src/api-credentials-section.tsx
|
|
1002
|
+
var React5 = __toESM(require("react"), 1);
|
|
1003
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
1004
|
+
function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, request, onSuccess, onError }) {
|
|
1005
|
+
const { Card, CardContent, CardDescription, CardHeader, CardTitle } = resolveAuthComponents(components);
|
|
1006
|
+
const [index, setIndex] = React5.useState(null);
|
|
1007
|
+
const [issued, setIssued] = React5.useState([]);
|
|
1008
|
+
const issuedKey = React5.useRef(0);
|
|
1009
|
+
const addIssued = React5.useCallback((credential) => {
|
|
1010
|
+
issuedKey.current += 1;
|
|
1011
|
+
const key = issuedKey.current;
|
|
1012
|
+
setIssued((current) => [...current, { key, credential }]);
|
|
1013
|
+
}, []);
|
|
1014
|
+
const [loadError, setLoadError] = React5.useState(null);
|
|
1015
|
+
const onErrorRef = React5.useRef(onError);
|
|
1016
|
+
onErrorRef.current = onError;
|
|
1017
|
+
const generation = React5.useRef(0);
|
|
1018
|
+
const context = React5.useRef({ indexUrl, csrfToken, request });
|
|
1019
|
+
context.current = { indexUrl, csrfToken, request };
|
|
1020
|
+
const reload = React5.useCallback(async () => {
|
|
1021
|
+
generation.current += 1;
|
|
1022
|
+
const mine = generation.current;
|
|
1023
|
+
const { indexUrl: url, csrfToken: token, request: send } = context.current;
|
|
1024
|
+
const result = await credentialRequest("GET", url, void 0, token, send);
|
|
1025
|
+
if (mine !== generation.current) {
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
if (result.ok) {
|
|
1029
|
+
setIndex(result.data);
|
|
1030
|
+
setLoadError(null);
|
|
1031
|
+
} else {
|
|
1032
|
+
setLoadError(result.message);
|
|
1033
|
+
onErrorRef.current?.("api-credentials", result.message);
|
|
1034
|
+
}
|
|
1035
|
+
}, []);
|
|
1036
|
+
React5.useEffect(() => {
|
|
1037
|
+
setIndex(null);
|
|
1038
|
+
void reload();
|
|
1039
|
+
}, [indexUrl, csrfToken, reload]);
|
|
1040
|
+
const removeLocally = React5.useCallback((list, id) => {
|
|
1041
|
+
setIndex(
|
|
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
|
+
);
|
|
1044
|
+
}, []);
|
|
1045
|
+
const shared = { components, csrfToken, request, onError, onSuccess, removeLocally };
|
|
1046
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(Card, { children: [
|
|
1047
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(CardHeader, { children: [
|
|
1048
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CardTitle, { children: "API access" }),
|
|
1049
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CardDescription, { children: "Connect apps that use the API. Register an OAuth app for apps that sign you in, or create an API token for apps that ask for a key. Each carries only the permissions you choose, and never more than your own access." })
|
|
1050
|
+
] }),
|
|
1051
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CardContent, { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "grid", gap: "1.5rem", minWidth: 0 }, children: [
|
|
1052
|
+
links.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("dl", { style: { display: "grid", gap: "0.5rem", minWidth: 0 }, children: links.map((link) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { minWidth: 0 }, children: [
|
|
1053
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("dt", { children: link.label }),
|
|
1054
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("dd", { style: { margin: 0, overflowWrap: "anywhere" }, children: [
|
|
1055
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("code", { children: link.url }),
|
|
1056
|
+
" ",
|
|
1057
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopyButton, { text: link.url, label: link.label, components })
|
|
1058
|
+
] })
|
|
1059
|
+
] }, link.label)) }),
|
|
1060
|
+
issued.map(({ key, credential }) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1061
|
+
IssuedNotice,
|
|
1062
|
+
{
|
|
1063
|
+
issued: credential,
|
|
1064
|
+
components,
|
|
1065
|
+
onDismiss: () => setIssued((current) => current.filter((entry) => entry.key !== key))
|
|
1066
|
+
},
|
|
1067
|
+
key
|
|
1068
|
+
)),
|
|
1069
|
+
loadError !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "alert", children: loadError }),
|
|
1070
|
+
index !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
1071
|
+
(index.issue_token_href === null || index.register_app_href === null) && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "status", children: "Creating new credentials is unavailable right now. You can still revoke existing ones." }),
|
|
1072
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(TokenSection, { index, ...shared, onIssued: addIssued, reload }),
|
|
1073
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AppSection, { index, ...shared, onIssued: addIssued, reload })
|
|
1074
|
+
] })
|
|
1075
|
+
] }) })
|
|
1076
|
+
] });
|
|
1077
|
+
}
|
|
1078
|
+
function TokenSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1079
|
+
const { Button, Input, Label, RadioGroup } = resolveAuthComponents(components);
|
|
1080
|
+
const [name, setName] = React5.useState("");
|
|
1081
|
+
const [scopes, setScopes] = React5.useState([]);
|
|
1082
|
+
const [lifetime, setLifetime] = React5.useState(index.token_lifetimes[0] ?? "");
|
|
1083
|
+
const [busy, setBusy] = React5.useState(false);
|
|
1084
|
+
const [error, setError] = React5.useState(null);
|
|
1085
|
+
async function create(event) {
|
|
1086
|
+
event.preventDefault();
|
|
1087
|
+
if (index.issue_token_href === null) {
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
setBusy(true);
|
|
1091
|
+
const result = await credentialRequest("POST", index.issue_token_href, { name, scopes, lifetime }, csrfToken, request);
|
|
1092
|
+
setBusy(false);
|
|
1093
|
+
if (!result.ok) {
|
|
1094
|
+
setError(result.message);
|
|
1095
|
+
onError?.("api-tokens", result.message);
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
setError(null);
|
|
1099
|
+
setName("");
|
|
1100
|
+
setScopes([]);
|
|
1101
|
+
onIssued(result.data);
|
|
1102
|
+
onSuccess?.("API token created.");
|
|
1103
|
+
await reload();
|
|
1104
|
+
}
|
|
1105
|
+
const [pending, setPending] = React5.useState([]);
|
|
1106
|
+
async function revoke(token) {
|
|
1107
|
+
if (pending.includes(token.id)) {
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
setPending((current) => [...current, token.id]);
|
|
1111
|
+
const result = await credentialRequest("DELETE", token.revoke_href, void 0, csrfToken, request);
|
|
1112
|
+
if (!result.ok) {
|
|
1113
|
+
setPending((current) => current.filter((id) => id !== token.id));
|
|
1114
|
+
setError(result.message);
|
|
1115
|
+
onError?.("api-tokens", result.message);
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
setError(null);
|
|
1119
|
+
removeLocally("tokens", token.id);
|
|
1120
|
+
onSuccess?.("API token revoked.");
|
|
1121
|
+
await reload();
|
|
1122
|
+
setPending((current) => current.filter((id) => id !== token.id));
|
|
1123
|
+
}
|
|
1124
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("section", { style: { display: "grid", gap: "0.75rem", minWidth: 0 }, children: [
|
|
1125
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("h3", { children: "API tokens" }),
|
|
1126
|
+
index.issue_token_href !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("form", { onSubmit: create, style: { display: "grid", gap: "0.75rem" }, children: [
|
|
1127
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1128
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Label, { htmlFor: "api-token-name", children: "Token name" }),
|
|
1129
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Input, { id: "api-token-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1130
|
+
] }),
|
|
1131
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ScopePicker, { idPrefix: "api-token-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
1132
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1133
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("legend", { children: "Expires after" }),
|
|
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: [
|
|
1143
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("input", { type: "radio", name: "api-token-lifetime", checked: lifetime === spec, onChange: () => setLifetime(spec) }),
|
|
1144
|
+
" ",
|
|
1145
|
+
describeDuration(spec)
|
|
1146
|
+
] }, spec))
|
|
1147
|
+
] }),
|
|
1148
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Button, { type: "submit", disabled: busy || name.trim() === "" || scopes.length === 0 || lifetime === "", children: "Create API token" }) })
|
|
1149
|
+
] }),
|
|
1150
|
+
error !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "alert", children: error }),
|
|
1151
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("ul", { style: { display: "grid", gap: "0.5rem", listStyle: "none", padding: 0, margin: 0, minWidth: 0 }, children: index.tokens.map((token) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("li", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1152
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("strong", { children: token.name }),
|
|
1153
|
+
" \xB7 ",
|
|
1154
|
+
token.scopes.join(", "),
|
|
1155
|
+
token.expires_at !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
1156
|
+
" \xB7 expires ",
|
|
1157
|
+
new Date(token.expires_at).toLocaleString()
|
|
1158
|
+
] }),
|
|
1159
|
+
" ",
|
|
1160
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Button, { type: "button", disabled: pending.includes(token.id), onClick: () => void revoke(token), children: "Revoke" })
|
|
1161
|
+
] }, token.id)) })
|
|
1162
|
+
] });
|
|
1163
|
+
}
|
|
1164
|
+
function AppSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1165
|
+
const { Button, Input, Label, Textarea, RadioGroup } = resolveAuthComponents(components);
|
|
1166
|
+
const [name, setName] = React5.useState("");
|
|
1167
|
+
const [redirects, setRedirects] = React5.useState("");
|
|
1168
|
+
const [confidential, setConfidential] = React5.useState(true);
|
|
1169
|
+
const [scopes, setScopes] = React5.useState([]);
|
|
1170
|
+
const [busy, setBusy] = React5.useState(false);
|
|
1171
|
+
const [error, setError] = React5.useState(null);
|
|
1172
|
+
const redirectUris = redirects.split("\n").map((line) => line.trim()).filter((line) => line !== "");
|
|
1173
|
+
async function register(event) {
|
|
1174
|
+
event.preventDefault();
|
|
1175
|
+
if (index.register_app_href === null) {
|
|
1176
|
+
return;
|
|
1177
|
+
}
|
|
1178
|
+
setBusy(true);
|
|
1179
|
+
const result = await credentialRequest(
|
|
1180
|
+
"POST",
|
|
1181
|
+
index.register_app_href,
|
|
1182
|
+
{ name, redirect_uris: redirectUris, confidential, scopes },
|
|
1183
|
+
csrfToken,
|
|
1184
|
+
request
|
|
1185
|
+
);
|
|
1186
|
+
setBusy(false);
|
|
1187
|
+
if (!result.ok) {
|
|
1188
|
+
setError(result.message);
|
|
1189
|
+
onError?.("oauth-apps", result.message);
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
setError(null);
|
|
1193
|
+
setName("");
|
|
1194
|
+
setRedirects("");
|
|
1195
|
+
setScopes([]);
|
|
1196
|
+
onIssued(result.data);
|
|
1197
|
+
onSuccess?.("OAuth app registered.");
|
|
1198
|
+
await reload();
|
|
1199
|
+
}
|
|
1200
|
+
const [pending, setPending] = React5.useState([]);
|
|
1201
|
+
async function remove(app) {
|
|
1202
|
+
if (pending.includes(app.id)) {
|
|
1203
|
+
return;
|
|
1204
|
+
}
|
|
1205
|
+
setPending((current) => [...current, app.id]);
|
|
1206
|
+
const result = await credentialRequest("DELETE", app.delete_href, void 0, csrfToken, request);
|
|
1207
|
+
if (!result.ok) {
|
|
1208
|
+
setPending((current) => current.filter((id) => id !== app.id));
|
|
1209
|
+
setError(result.message);
|
|
1210
|
+
onError?.("oauth-apps", result.message);
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
setError(null);
|
|
1214
|
+
removeLocally("apps", app.id);
|
|
1215
|
+
onSuccess?.("OAuth app deleted and its tokens revoked.");
|
|
1216
|
+
await reload();
|
|
1217
|
+
setPending((current) => current.filter((id) => id !== app.id));
|
|
1218
|
+
}
|
|
1219
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("section", { style: { display: "grid", gap: "0.75rem", minWidth: 0 }, children: [
|
|
1220
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("h3", { children: "OAuth apps" }),
|
|
1221
|
+
index.register_app_href !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("form", { onSubmit: register, style: { display: "grid", gap: "0.75rem" }, children: [
|
|
1222
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1223
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Label, { htmlFor: "oauth-app-name", children: "App name" }),
|
|
1224
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Input, { id: "oauth-app-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1225
|
+
] }),
|
|
1226
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1227
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Label, { htmlFor: "oauth-app-redirects", children: "Redirect URIs (one per line)" }),
|
|
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) })
|
|
1229
|
+
] }),
|
|
1230
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1231
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("legend", { children: "Client type" }),
|
|
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
|
+
] })
|
|
1252
|
+
] })
|
|
1253
|
+
] }),
|
|
1254
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ScopePicker, { idPrefix: "oauth-app-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
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" }) })
|
|
1256
|
+
] }),
|
|
1257
|
+
error !== null && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("p", { role: "alert", children: error }),
|
|
1258
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("ul", { style: { display: "grid", gap: "0.5rem", listStyle: "none", padding: 0, margin: 0, minWidth: 0 }, children: index.apps.map((app) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("li", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1259
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("strong", { children: app.name }),
|
|
1260
|
+
" \xB7 client ID ",
|
|
1261
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("code", { children: app.id }),
|
|
1262
|
+
" \xB7 ",
|
|
1263
|
+
app.confidential ? "confidential" : "public",
|
|
1264
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("br", {}),
|
|
1265
|
+
app.redirect_uris.join(", "),
|
|
1266
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("br", {}),
|
|
1267
|
+
"May request: ",
|
|
1268
|
+
app.scopes.join(", "),
|
|
1269
|
+
" ",
|
|
1270
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Button, { type: "button", disabled: pending.includes(app.id), onClick: () => void remove(app), children: "Delete" })
|
|
1271
|
+
] }, app.id)) })
|
|
1272
|
+
] });
|
|
1273
|
+
}
|
|
1274
|
+
function ScopePicker({
|
|
1275
|
+
idPrefix,
|
|
1276
|
+
scopes,
|
|
1277
|
+
selected,
|
|
1278
|
+
onChange,
|
|
1279
|
+
components
|
|
1280
|
+
}) {
|
|
1281
|
+
const { Checkbox } = resolveAuthComponents(components);
|
|
1282
|
+
const toggle = (scopeId, checked) => onChange(checked ? [...selected, scopeId] : selected.filter((value) => value !== scopeId));
|
|
1283
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0, minWidth: 0 }, children: [
|
|
1284
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("legend", { children: "Permissions" }),
|
|
1285
|
+
scopes.map((scope) => {
|
|
1286
|
+
const id = `${idPrefix}-${scope.id}`;
|
|
1287
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("label", { htmlFor: id, style: { overflowWrap: "anywhere" }, children: [
|
|
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) }),
|
|
1289
|
+
" ",
|
|
1290
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("code", { children: scope.id }),
|
|
1291
|
+
" ",
|
|
1292
|
+
scope.description
|
|
1293
|
+
] }, scope.id);
|
|
1294
|
+
})
|
|
1295
|
+
] });
|
|
1296
|
+
}
|
|
1297
|
+
function IssuedNotice({
|
|
1298
|
+
issued,
|
|
1299
|
+
components,
|
|
1300
|
+
onDismiss
|
|
1301
|
+
}) {
|
|
1302
|
+
const { Button } = resolveAuthComponents(components);
|
|
1303
|
+
const values = issued.kind === "token" ? [{ label: "API token", url: issued.token }] : [
|
|
1304
|
+
{ label: "Client ID", url: issued.client_id },
|
|
1305
|
+
...issued.client_secret !== null ? [{ label: "Client secret", url: issued.client_secret }] : []
|
|
1306
|
+
];
|
|
1307
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { role: "status", style: { display: "grid", gap: "0.5rem", minWidth: 0 }, children: [
|
|
1308
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("p", { style: { overflowWrap: "anywhere" }, children: [
|
|
1309
|
+
issued.kind === "token" ? `API token \u201C${issued.name}\u201D created.` : `OAuth app \u201C${issued.name}\u201D registered.`,
|
|
1310
|
+
" Copy it now: it will not be shown again."
|
|
1311
|
+
] }),
|
|
1312
|
+
values.map((value) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1313
|
+
value.label,
|
|
1314
|
+
": ",
|
|
1315
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("code", { children: value.url }),
|
|
1316
|
+
" ",
|
|
1317
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopyButton, { text: value.url, label: value.label, components })
|
|
1318
|
+
] }, value.label)),
|
|
1319
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Button, { type: "button", onClick: onDismiss, children: "Done" }) })
|
|
1320
|
+
] });
|
|
1321
|
+
}
|
|
1322
|
+
function CopyButton({ text, label, components }) {
|
|
1323
|
+
const { Button } = resolveAuthComponents(components);
|
|
1324
|
+
const [state, setState] = React5.useState("idle");
|
|
1325
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
|
|
1326
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1327
|
+
Button,
|
|
1328
|
+
{
|
|
1329
|
+
type: "button",
|
|
1330
|
+
"aria-label": `Copy ${label}`,
|
|
1331
|
+
onClick: async () => {
|
|
1332
|
+
if (typeof navigator === "undefined" || typeof navigator.clipboard?.writeText !== "function") {
|
|
1333
|
+
setState("failed");
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1336
|
+
try {
|
|
1337
|
+
await navigator.clipboard.writeText(text);
|
|
1338
|
+
setState("copied");
|
|
1339
|
+
} catch {
|
|
1340
|
+
setState("failed");
|
|
1341
|
+
}
|
|
1342
|
+
},
|
|
1343
|
+
children: state === "copied" ? "Copied" : "Copy"
|
|
1344
|
+
}
|
|
1345
|
+
),
|
|
1346
|
+
state === "failed" && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { role: "status", children: " Copying is unavailable here; select the text and copy it manually." })
|
|
1347
|
+
] });
|
|
1348
|
+
}
|
|
1349
|
+
function describeDuration(spec) {
|
|
1350
|
+
const match = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?)?$/.exec(spec);
|
|
1351
|
+
if (!match) {
|
|
1352
|
+
return spec;
|
|
1353
|
+
}
|
|
1354
|
+
const units = ["year", "month", "week", "day", "hour", "minute"];
|
|
1355
|
+
const parts = match.slice(1).map((value, position) => value ? `${Number(value)} ${units[position]}${Number(value) === 1 ? "" : "s"}` : null).filter((part) => part !== null);
|
|
1356
|
+
return parts.length > 0 ? parts.join(" ") : spec;
|
|
1357
|
+
}
|
|
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
|
+
}
|
|
1368
|
+
let response;
|
|
1369
|
+
try {
|
|
1370
|
+
response = await fetch(url, {
|
|
1371
|
+
method,
|
|
1372
|
+
credentials: "same-origin",
|
|
1373
|
+
headers: {
|
|
1374
|
+
Accept: "application/json",
|
|
1375
|
+
...body !== void 0 ? { "Content-Type": "application/json" } : {},
|
|
1376
|
+
...method !== "GET" ? { "X-CSRF-TOKEN": getCsrfToken(csrfToken) } : {}
|
|
1377
|
+
},
|
|
1378
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
1379
|
+
});
|
|
1380
|
+
} catch {
|
|
1381
|
+
return { ok: false, status: 0, message: "The server could not be reached. Check the connection and try again." };
|
|
1382
|
+
}
|
|
1383
|
+
const payload = await response.json().catch(() => null);
|
|
1384
|
+
if (response.ok) {
|
|
1385
|
+
return { ok: true, status: response.status, data: unwrapData(payload) };
|
|
1386
|
+
}
|
|
1387
|
+
return { ok: false, status: response.status, message: refusalMessage(response.status, payload) };
|
|
1388
|
+
}
|
|
1389
|
+
function unwrapData(payload) {
|
|
1390
|
+
return typeof payload === "object" && payload !== null && "data" in payload ? payload.data : payload;
|
|
1391
|
+
}
|
|
1392
|
+
function refusalMessage(status, payload) {
|
|
1393
|
+
if (status === 401 || status === 419) {
|
|
1394
|
+
return "Your session has expired. Reload the page and sign in again.";
|
|
1395
|
+
}
|
|
1396
|
+
if (typeof payload === "object" && payload !== null) {
|
|
1397
|
+
const errors = payload.errors;
|
|
1398
|
+
if (errors && typeof errors === "object") {
|
|
1399
|
+
const first = Object.values(errors)[0];
|
|
1400
|
+
if (Array.isArray(first) && typeof first[0] === "string") {
|
|
1401
|
+
return first[0];
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
const message = payload.message;
|
|
1405
|
+
if (typeof message === "string" && message !== "") {
|
|
1406
|
+
return message;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
if (status === 403) {
|
|
1410
|
+
return "You do not have permission to do that.";
|
|
1411
|
+
}
|
|
1412
|
+
if (status === 404) {
|
|
1413
|
+
return "That credential no longer exists, or creating credentials is unavailable right now.";
|
|
1414
|
+
}
|
|
1415
|
+
return "That action could not be completed.";
|
|
1416
|
+
}
|
|
998
1417
|
// Annotate the CommonJS export names for ESM import in node:
|
|
999
1418
|
0 && (module.exports = {
|
|
1419
|
+
ApiCredentialsSection,
|
|
1000
1420
|
ChangePasswordForm,
|
|
1001
1421
|
LoginForm,
|
|
1002
1422
|
PasskeyLoginButton,
|
|
@@ -1008,6 +1428,7 @@ function relyingApplicationsFrom(value) {
|
|
|
1008
1428
|
arrayBufferToBase64url,
|
|
1009
1429
|
authenticateWithPasskey,
|
|
1010
1430
|
base64urlToArrayBuffer,
|
|
1431
|
+
describeDuration,
|
|
1011
1432
|
getCsrfToken,
|
|
1012
1433
|
getDefaultPasskeyName,
|
|
1013
1434
|
isAbortError,
|
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>;
|
|
@@ -214,4 +236,93 @@ declare function isConditionalMediationAvailable(): Promise<boolean>;
|
|
|
214
236
|
declare function authenticateWithPasskey({ endpoints, mediation, signal }?: AuthenticateWithPasskeyOptions): Promise<PasskeyAuthenticationResult>;
|
|
215
237
|
declare function registerPasskey({ endpoints, name, signal }?: RegisterPasskeyOptions): Promise<PasskeyRegistrationResult>;
|
|
216
238
|
|
|
217
|
-
|
|
239
|
+
interface ApiCredentialScope {
|
|
240
|
+
id: string;
|
|
241
|
+
description: string;
|
|
242
|
+
}
|
|
243
|
+
interface ApiCredentialToken {
|
|
244
|
+
id: string;
|
|
245
|
+
name: string;
|
|
246
|
+
scopes: string[];
|
|
247
|
+
created_at: string | null;
|
|
248
|
+
expires_at: string | null;
|
|
249
|
+
revoke_href: string;
|
|
250
|
+
}
|
|
251
|
+
interface ApiCredentialApp {
|
|
252
|
+
id: string;
|
|
253
|
+
name: string;
|
|
254
|
+
confidential: boolean;
|
|
255
|
+
redirect_uris: string[];
|
|
256
|
+
scopes: string[];
|
|
257
|
+
created_at: string | null;
|
|
258
|
+
delete_href: string;
|
|
259
|
+
}
|
|
260
|
+
/** The server's index response (`data`): every URL is finished; this component assembles none. */
|
|
261
|
+
interface ApiCredentialIndex {
|
|
262
|
+
scopes: ApiCredentialScope[];
|
|
263
|
+
token_lifetimes: string[];
|
|
264
|
+
/** Null while issuing is unavailable (the OAuth server is switched off). Revocation stays available. */
|
|
265
|
+
issue_token_href: string | null;
|
|
266
|
+
register_app_href: string | null;
|
|
267
|
+
tokens: ApiCredentialToken[];
|
|
268
|
+
apps: ApiCredentialApp[];
|
|
269
|
+
}
|
|
270
|
+
type IssuedApiCredential = {
|
|
271
|
+
kind: 'token';
|
|
272
|
+
name: string;
|
|
273
|
+
token: string;
|
|
274
|
+
expires_at?: string;
|
|
275
|
+
} | {
|
|
276
|
+
kind: 'app';
|
|
277
|
+
name: string;
|
|
278
|
+
client_id: string;
|
|
279
|
+
client_secret: string | null;
|
|
280
|
+
};
|
|
281
|
+
interface ApiCredentialEndpointLink {
|
|
282
|
+
label: string;
|
|
283
|
+
url: string;
|
|
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>;
|
|
303
|
+
interface ApiCredentialsSectionProps {
|
|
304
|
+
/** The credential service's index URL (GET). */
|
|
305
|
+
indexUrl: string;
|
|
306
|
+
components: AuthComponentInput;
|
|
307
|
+
/** Values a connector needs: the OpenAPI document, API base, OAuth authorize/token URLs, MCP URL. */
|
|
308
|
+
links?: ApiCredentialEndpointLink[];
|
|
309
|
+
csrfToken?: string;
|
|
310
|
+
/** Replaces the built-in `fetch` for every credential request; `csrfToken` is then the app's to send. */
|
|
311
|
+
request?: ApiCredentialRequest;
|
|
312
|
+
onSuccess?: (message: string) => void;
|
|
313
|
+
onError?: (field: string, message: string) => void;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* A person's credentials for apps that use the API: personal API tokens (for
|
|
317
|
+
* connectors that ask for a key) and OAuth apps (for connectors that run the
|
|
318
|
+
* authorization-code flow).
|
|
319
|
+
*
|
|
320
|
+
* Each secret arrives once, in the response that created it, and lives only
|
|
321
|
+
* in this component's memory; afterwards only the lists are reloaded, so it is
|
|
322
|
+
* never part of page props or browser history.
|
|
323
|
+
*/
|
|
324
|
+
declare function ApiCredentialsSection({ indexUrl, components, links, csrfToken, request, onSuccess, onError }: ApiCredentialsSectionProps): React.JSX.Element;
|
|
325
|
+
/** PT4H → "4 hours", P30D → "30 days", P1Y → "1 year". Unknown shapes are shown as given. */
|
|
326
|
+
declare function describeDuration(spec: string): string;
|
|
327
|
+
|
|
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>;
|
|
@@ -214,4 +236,93 @@ declare function isConditionalMediationAvailable(): Promise<boolean>;
|
|
|
214
236
|
declare function authenticateWithPasskey({ endpoints, mediation, signal }?: AuthenticateWithPasskeyOptions): Promise<PasskeyAuthenticationResult>;
|
|
215
237
|
declare function registerPasskey({ endpoints, name, signal }?: RegisterPasskeyOptions): Promise<PasskeyRegistrationResult>;
|
|
216
238
|
|
|
217
|
-
|
|
239
|
+
interface ApiCredentialScope {
|
|
240
|
+
id: string;
|
|
241
|
+
description: string;
|
|
242
|
+
}
|
|
243
|
+
interface ApiCredentialToken {
|
|
244
|
+
id: string;
|
|
245
|
+
name: string;
|
|
246
|
+
scopes: string[];
|
|
247
|
+
created_at: string | null;
|
|
248
|
+
expires_at: string | null;
|
|
249
|
+
revoke_href: string;
|
|
250
|
+
}
|
|
251
|
+
interface ApiCredentialApp {
|
|
252
|
+
id: string;
|
|
253
|
+
name: string;
|
|
254
|
+
confidential: boolean;
|
|
255
|
+
redirect_uris: string[];
|
|
256
|
+
scopes: string[];
|
|
257
|
+
created_at: string | null;
|
|
258
|
+
delete_href: string;
|
|
259
|
+
}
|
|
260
|
+
/** The server's index response (`data`): every URL is finished; this component assembles none. */
|
|
261
|
+
interface ApiCredentialIndex {
|
|
262
|
+
scopes: ApiCredentialScope[];
|
|
263
|
+
token_lifetimes: string[];
|
|
264
|
+
/** Null while issuing is unavailable (the OAuth server is switched off). Revocation stays available. */
|
|
265
|
+
issue_token_href: string | null;
|
|
266
|
+
register_app_href: string | null;
|
|
267
|
+
tokens: ApiCredentialToken[];
|
|
268
|
+
apps: ApiCredentialApp[];
|
|
269
|
+
}
|
|
270
|
+
type IssuedApiCredential = {
|
|
271
|
+
kind: 'token';
|
|
272
|
+
name: string;
|
|
273
|
+
token: string;
|
|
274
|
+
expires_at?: string;
|
|
275
|
+
} | {
|
|
276
|
+
kind: 'app';
|
|
277
|
+
name: string;
|
|
278
|
+
client_id: string;
|
|
279
|
+
client_secret: string | null;
|
|
280
|
+
};
|
|
281
|
+
interface ApiCredentialEndpointLink {
|
|
282
|
+
label: string;
|
|
283
|
+
url: string;
|
|
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>;
|
|
303
|
+
interface ApiCredentialsSectionProps {
|
|
304
|
+
/** The credential service's index URL (GET). */
|
|
305
|
+
indexUrl: string;
|
|
306
|
+
components: AuthComponentInput;
|
|
307
|
+
/** Values a connector needs: the OpenAPI document, API base, OAuth authorize/token URLs, MCP URL. */
|
|
308
|
+
links?: ApiCredentialEndpointLink[];
|
|
309
|
+
csrfToken?: string;
|
|
310
|
+
/** Replaces the built-in `fetch` for every credential request; `csrfToken` is then the app's to send. */
|
|
311
|
+
request?: ApiCredentialRequest;
|
|
312
|
+
onSuccess?: (message: string) => void;
|
|
313
|
+
onError?: (field: string, message: string) => void;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* A person's credentials for apps that use the API: personal API tokens (for
|
|
317
|
+
* connectors that ask for a key) and OAuth apps (for connectors that run the
|
|
318
|
+
* authorization-code flow).
|
|
319
|
+
*
|
|
320
|
+
* Each secret arrives once, in the response that created it, and lives only
|
|
321
|
+
* in this component's memory; afterwards only the lists are reloaded, so it is
|
|
322
|
+
* never part of page props or browser history.
|
|
323
|
+
*/
|
|
324
|
+
declare function ApiCredentialsSection({ indexUrl, components, links, csrfToken, request, onSuccess, onError }: ApiCredentialsSectionProps): React.JSX.Element;
|
|
325
|
+
/** PT4H → "4 hours", P30D → "30 days", P1Y → "1 year". Unknown shapes are shown as given. */
|
|
326
|
+
declare function describeDuration(spec: string): string;
|
|
327
|
+
|
|
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
|
@@ -942,7 +942,425 @@ function relyingApplicationsFrom(value) {
|
|
|
942
942
|
return href === null ? [] : [{ key, name: name.trim(), url: href }];
|
|
943
943
|
});
|
|
944
944
|
}
|
|
945
|
+
|
|
946
|
+
// src/api-credentials-section.tsx
|
|
947
|
+
import * as React5 from "react";
|
|
948
|
+
import { Fragment, jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
949
|
+
function ApiCredentialsSection({ indexUrl, components, links = [], csrfToken, request, onSuccess, onError }) {
|
|
950
|
+
const { Card, CardContent, CardDescription, CardHeader, CardTitle } = resolveAuthComponents(components);
|
|
951
|
+
const [index, setIndex] = React5.useState(null);
|
|
952
|
+
const [issued, setIssued] = React5.useState([]);
|
|
953
|
+
const issuedKey = React5.useRef(0);
|
|
954
|
+
const addIssued = React5.useCallback((credential) => {
|
|
955
|
+
issuedKey.current += 1;
|
|
956
|
+
const key = issuedKey.current;
|
|
957
|
+
setIssued((current) => [...current, { key, credential }]);
|
|
958
|
+
}, []);
|
|
959
|
+
const [loadError, setLoadError] = React5.useState(null);
|
|
960
|
+
const onErrorRef = React5.useRef(onError);
|
|
961
|
+
onErrorRef.current = onError;
|
|
962
|
+
const generation = React5.useRef(0);
|
|
963
|
+
const context = React5.useRef({ indexUrl, csrfToken, request });
|
|
964
|
+
context.current = { indexUrl, csrfToken, request };
|
|
965
|
+
const reload = React5.useCallback(async () => {
|
|
966
|
+
generation.current += 1;
|
|
967
|
+
const mine = generation.current;
|
|
968
|
+
const { indexUrl: url, csrfToken: token, request: send } = context.current;
|
|
969
|
+
const result = await credentialRequest("GET", url, void 0, token, send);
|
|
970
|
+
if (mine !== generation.current) {
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
if (result.ok) {
|
|
974
|
+
setIndex(result.data);
|
|
975
|
+
setLoadError(null);
|
|
976
|
+
} else {
|
|
977
|
+
setLoadError(result.message);
|
|
978
|
+
onErrorRef.current?.("api-credentials", result.message);
|
|
979
|
+
}
|
|
980
|
+
}, []);
|
|
981
|
+
React5.useEffect(() => {
|
|
982
|
+
setIndex(null);
|
|
983
|
+
void reload();
|
|
984
|
+
}, [indexUrl, csrfToken, reload]);
|
|
985
|
+
const removeLocally = React5.useCallback((list, id) => {
|
|
986
|
+
setIndex(
|
|
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
|
+
);
|
|
989
|
+
}, []);
|
|
990
|
+
const shared = { components, csrfToken, request, onError, onSuccess, removeLocally };
|
|
991
|
+
return /* @__PURE__ */ jsxs4(Card, { children: [
|
|
992
|
+
/* @__PURE__ */ jsxs4(CardHeader, { children: [
|
|
993
|
+
/* @__PURE__ */ jsx4(CardTitle, { children: "API access" }),
|
|
994
|
+
/* @__PURE__ */ jsx4(CardDescription, { children: "Connect apps that use the API. Register an OAuth app for apps that sign you in, or create an API token for apps that ask for a key. Each carries only the permissions you choose, and never more than your own access." })
|
|
995
|
+
] }),
|
|
996
|
+
/* @__PURE__ */ jsx4(CardContent, { children: /* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "1.5rem", minWidth: 0 }, children: [
|
|
997
|
+
links.length > 0 && /* @__PURE__ */ jsx4("dl", { style: { display: "grid", gap: "0.5rem", minWidth: 0 }, children: links.map((link) => /* @__PURE__ */ jsxs4("div", { style: { minWidth: 0 }, children: [
|
|
998
|
+
/* @__PURE__ */ jsx4("dt", { children: link.label }),
|
|
999
|
+
/* @__PURE__ */ jsxs4("dd", { style: { margin: 0, overflowWrap: "anywhere" }, children: [
|
|
1000
|
+
/* @__PURE__ */ jsx4("code", { children: link.url }),
|
|
1001
|
+
" ",
|
|
1002
|
+
/* @__PURE__ */ jsx4(CopyButton, { text: link.url, label: link.label, components })
|
|
1003
|
+
] })
|
|
1004
|
+
] }, link.label)) }),
|
|
1005
|
+
issued.map(({ key, credential }) => /* @__PURE__ */ jsx4(
|
|
1006
|
+
IssuedNotice,
|
|
1007
|
+
{
|
|
1008
|
+
issued: credential,
|
|
1009
|
+
components,
|
|
1010
|
+
onDismiss: () => setIssued((current) => current.filter((entry) => entry.key !== key))
|
|
1011
|
+
},
|
|
1012
|
+
key
|
|
1013
|
+
)),
|
|
1014
|
+
loadError !== null && /* @__PURE__ */ jsx4("p", { role: "alert", children: loadError }),
|
|
1015
|
+
index !== null && /* @__PURE__ */ jsxs4(Fragment, { children: [
|
|
1016
|
+
(index.issue_token_href === null || index.register_app_href === null) && /* @__PURE__ */ jsx4("p", { role: "status", children: "Creating new credentials is unavailable right now. You can still revoke existing ones." }),
|
|
1017
|
+
/* @__PURE__ */ jsx4(TokenSection, { index, ...shared, onIssued: addIssued, reload }),
|
|
1018
|
+
/* @__PURE__ */ jsx4(AppSection, { index, ...shared, onIssued: addIssued, reload })
|
|
1019
|
+
] })
|
|
1020
|
+
] }) })
|
|
1021
|
+
] });
|
|
1022
|
+
}
|
|
1023
|
+
function TokenSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1024
|
+
const { Button, Input, Label, RadioGroup } = resolveAuthComponents(components);
|
|
1025
|
+
const [name, setName] = React5.useState("");
|
|
1026
|
+
const [scopes, setScopes] = React5.useState([]);
|
|
1027
|
+
const [lifetime, setLifetime] = React5.useState(index.token_lifetimes[0] ?? "");
|
|
1028
|
+
const [busy, setBusy] = React5.useState(false);
|
|
1029
|
+
const [error, setError] = React5.useState(null);
|
|
1030
|
+
async function create(event) {
|
|
1031
|
+
event.preventDefault();
|
|
1032
|
+
if (index.issue_token_href === null) {
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
setBusy(true);
|
|
1036
|
+
const result = await credentialRequest("POST", index.issue_token_href, { name, scopes, lifetime }, csrfToken, request);
|
|
1037
|
+
setBusy(false);
|
|
1038
|
+
if (!result.ok) {
|
|
1039
|
+
setError(result.message);
|
|
1040
|
+
onError?.("api-tokens", result.message);
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
setError(null);
|
|
1044
|
+
setName("");
|
|
1045
|
+
setScopes([]);
|
|
1046
|
+
onIssued(result.data);
|
|
1047
|
+
onSuccess?.("API token created.");
|
|
1048
|
+
await reload();
|
|
1049
|
+
}
|
|
1050
|
+
const [pending, setPending] = React5.useState([]);
|
|
1051
|
+
async function revoke(token) {
|
|
1052
|
+
if (pending.includes(token.id)) {
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
setPending((current) => [...current, token.id]);
|
|
1056
|
+
const result = await credentialRequest("DELETE", token.revoke_href, void 0, csrfToken, request);
|
|
1057
|
+
if (!result.ok) {
|
|
1058
|
+
setPending((current) => current.filter((id) => id !== token.id));
|
|
1059
|
+
setError(result.message);
|
|
1060
|
+
onError?.("api-tokens", result.message);
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
setError(null);
|
|
1064
|
+
removeLocally("tokens", token.id);
|
|
1065
|
+
onSuccess?.("API token revoked.");
|
|
1066
|
+
await reload();
|
|
1067
|
+
setPending((current) => current.filter((id) => id !== token.id));
|
|
1068
|
+
}
|
|
1069
|
+
return /* @__PURE__ */ jsxs4("section", { style: { display: "grid", gap: "0.75rem", minWidth: 0 }, children: [
|
|
1070
|
+
/* @__PURE__ */ jsx4("h3", { children: "API tokens" }),
|
|
1071
|
+
index.issue_token_href !== null && /* @__PURE__ */ jsxs4("form", { onSubmit: create, style: { display: "grid", gap: "0.75rem" }, children: [
|
|
1072
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1073
|
+
/* @__PURE__ */ jsx4(Label, { htmlFor: "api-token-name", children: "Token name" }),
|
|
1074
|
+
/* @__PURE__ */ jsx4(Input, { id: "api-token-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1075
|
+
] }),
|
|
1076
|
+
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "api-token-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
1077
|
+
/* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1078
|
+
/* @__PURE__ */ jsx4("legend", { children: "Expires after" }),
|
|
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: [
|
|
1088
|
+
/* @__PURE__ */ jsx4("input", { type: "radio", name: "api-token-lifetime", checked: lifetime === spec, onChange: () => setLifetime(spec) }),
|
|
1089
|
+
" ",
|
|
1090
|
+
describeDuration(spec)
|
|
1091
|
+
] }, spec))
|
|
1092
|
+
] }),
|
|
1093
|
+
/* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(Button, { type: "submit", disabled: busy || name.trim() === "" || scopes.length === 0 || lifetime === "", children: "Create API token" }) })
|
|
1094
|
+
] }),
|
|
1095
|
+
error !== null && /* @__PURE__ */ jsx4("p", { role: "alert", children: error }),
|
|
1096
|
+
/* @__PURE__ */ jsx4("ul", { style: { display: "grid", gap: "0.5rem", listStyle: "none", padding: 0, margin: 0, minWidth: 0 }, children: index.tokens.map((token) => /* @__PURE__ */ jsxs4("li", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1097
|
+
/* @__PURE__ */ jsx4("strong", { children: token.name }),
|
|
1098
|
+
" \xB7 ",
|
|
1099
|
+
token.scopes.join(", "),
|
|
1100
|
+
token.expires_at !== null && /* @__PURE__ */ jsxs4(Fragment, { children: [
|
|
1101
|
+
" \xB7 expires ",
|
|
1102
|
+
new Date(token.expires_at).toLocaleString()
|
|
1103
|
+
] }),
|
|
1104
|
+
" ",
|
|
1105
|
+
/* @__PURE__ */ jsx4(Button, { type: "button", disabled: pending.includes(token.id), onClick: () => void revoke(token), children: "Revoke" })
|
|
1106
|
+
] }, token.id)) })
|
|
1107
|
+
] });
|
|
1108
|
+
}
|
|
1109
|
+
function AppSection({ index, components, csrfToken, request, onIssued, reload, onSuccess, onError, removeLocally }) {
|
|
1110
|
+
const { Button, Input, Label, Textarea, RadioGroup } = resolveAuthComponents(components);
|
|
1111
|
+
const [name, setName] = React5.useState("");
|
|
1112
|
+
const [redirects, setRedirects] = React5.useState("");
|
|
1113
|
+
const [confidential, setConfidential] = React5.useState(true);
|
|
1114
|
+
const [scopes, setScopes] = React5.useState([]);
|
|
1115
|
+
const [busy, setBusy] = React5.useState(false);
|
|
1116
|
+
const [error, setError] = React5.useState(null);
|
|
1117
|
+
const redirectUris = redirects.split("\n").map((line) => line.trim()).filter((line) => line !== "");
|
|
1118
|
+
async function register(event) {
|
|
1119
|
+
event.preventDefault();
|
|
1120
|
+
if (index.register_app_href === null) {
|
|
1121
|
+
return;
|
|
1122
|
+
}
|
|
1123
|
+
setBusy(true);
|
|
1124
|
+
const result = await credentialRequest(
|
|
1125
|
+
"POST",
|
|
1126
|
+
index.register_app_href,
|
|
1127
|
+
{ name, redirect_uris: redirectUris, confidential, scopes },
|
|
1128
|
+
csrfToken,
|
|
1129
|
+
request
|
|
1130
|
+
);
|
|
1131
|
+
setBusy(false);
|
|
1132
|
+
if (!result.ok) {
|
|
1133
|
+
setError(result.message);
|
|
1134
|
+
onError?.("oauth-apps", result.message);
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
setError(null);
|
|
1138
|
+
setName("");
|
|
1139
|
+
setRedirects("");
|
|
1140
|
+
setScopes([]);
|
|
1141
|
+
onIssued(result.data);
|
|
1142
|
+
onSuccess?.("OAuth app registered.");
|
|
1143
|
+
await reload();
|
|
1144
|
+
}
|
|
1145
|
+
const [pending, setPending] = React5.useState([]);
|
|
1146
|
+
async function remove(app) {
|
|
1147
|
+
if (pending.includes(app.id)) {
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
setPending((current) => [...current, app.id]);
|
|
1151
|
+
const result = await credentialRequest("DELETE", app.delete_href, void 0, csrfToken, request);
|
|
1152
|
+
if (!result.ok) {
|
|
1153
|
+
setPending((current) => current.filter((id) => id !== app.id));
|
|
1154
|
+
setError(result.message);
|
|
1155
|
+
onError?.("oauth-apps", result.message);
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
setError(null);
|
|
1159
|
+
removeLocally("apps", app.id);
|
|
1160
|
+
onSuccess?.("OAuth app deleted and its tokens revoked.");
|
|
1161
|
+
await reload();
|
|
1162
|
+
setPending((current) => current.filter((id) => id !== app.id));
|
|
1163
|
+
}
|
|
1164
|
+
return /* @__PURE__ */ jsxs4("section", { style: { display: "grid", gap: "0.75rem", minWidth: 0 }, children: [
|
|
1165
|
+
/* @__PURE__ */ jsx4("h3", { children: "OAuth apps" }),
|
|
1166
|
+
index.register_app_href !== null && /* @__PURE__ */ jsxs4("form", { onSubmit: register, style: { display: "grid", gap: "0.75rem" }, children: [
|
|
1167
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1168
|
+
/* @__PURE__ */ jsx4(Label, { htmlFor: "oauth-app-name", children: "App name" }),
|
|
1169
|
+
/* @__PURE__ */ jsx4(Input, { id: "oauth-app-name", value: name, maxLength: 120, onChange: (event) => setName(event.target.value) })
|
|
1170
|
+
] }),
|
|
1171
|
+
/* @__PURE__ */ jsxs4("div", { style: { display: "grid", gap: "0.25rem" }, children: [
|
|
1172
|
+
/* @__PURE__ */ jsx4(Label, { htmlFor: "oauth-app-redirects", children: "Redirect URIs (one per line)" }),
|
|
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) })
|
|
1174
|
+
] }),
|
|
1175
|
+
/* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0 }, children: [
|
|
1176
|
+
/* @__PURE__ */ jsx4("legend", { children: "Client type" }),
|
|
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
|
+
] })
|
|
1197
|
+
] })
|
|
1198
|
+
] }),
|
|
1199
|
+
/* @__PURE__ */ jsx4(ScopePicker, { idPrefix: "oauth-app-scope", scopes: index.scopes, selected: scopes, onChange: setScopes, components }),
|
|
1200
|
+
/* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(Button, { type: "submit", disabled: busy || name.trim() === "" || redirectUris.length === 0 || scopes.length === 0, children: "Register OAuth app" }) })
|
|
1201
|
+
] }),
|
|
1202
|
+
error !== null && /* @__PURE__ */ jsx4("p", { role: "alert", children: error }),
|
|
1203
|
+
/* @__PURE__ */ jsx4("ul", { style: { display: "grid", gap: "0.5rem", listStyle: "none", padding: 0, margin: 0, minWidth: 0 }, children: index.apps.map((app) => /* @__PURE__ */ jsxs4("li", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1204
|
+
/* @__PURE__ */ jsx4("strong", { children: app.name }),
|
|
1205
|
+
" \xB7 client ID ",
|
|
1206
|
+
/* @__PURE__ */ jsx4("code", { children: app.id }),
|
|
1207
|
+
" \xB7 ",
|
|
1208
|
+
app.confidential ? "confidential" : "public",
|
|
1209
|
+
/* @__PURE__ */ jsx4("br", {}),
|
|
1210
|
+
app.redirect_uris.join(", "),
|
|
1211
|
+
/* @__PURE__ */ jsx4("br", {}),
|
|
1212
|
+
"May request: ",
|
|
1213
|
+
app.scopes.join(", "),
|
|
1214
|
+
" ",
|
|
1215
|
+
/* @__PURE__ */ jsx4(Button, { type: "button", disabled: pending.includes(app.id), onClick: () => void remove(app), children: "Delete" })
|
|
1216
|
+
] }, app.id)) })
|
|
1217
|
+
] });
|
|
1218
|
+
}
|
|
1219
|
+
function ScopePicker({
|
|
1220
|
+
idPrefix,
|
|
1221
|
+
scopes,
|
|
1222
|
+
selected,
|
|
1223
|
+
onChange,
|
|
1224
|
+
components
|
|
1225
|
+
}) {
|
|
1226
|
+
const { Checkbox } = resolveAuthComponents(components);
|
|
1227
|
+
const toggle = (scopeId, checked) => onChange(checked ? [...selected, scopeId] : selected.filter((value) => value !== scopeId));
|
|
1228
|
+
return /* @__PURE__ */ jsxs4("fieldset", { style: { display: "grid", gap: "0.25rem", border: 0, padding: 0, minWidth: 0 }, children: [
|
|
1229
|
+
/* @__PURE__ */ jsx4("legend", { children: "Permissions" }),
|
|
1230
|
+
scopes.map((scope) => {
|
|
1231
|
+
const id = `${idPrefix}-${scope.id}`;
|
|
1232
|
+
return /* @__PURE__ */ jsxs4("label", { htmlFor: id, style: { overflowWrap: "anywhere" }, children: [
|
|
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) }),
|
|
1234
|
+
" ",
|
|
1235
|
+
/* @__PURE__ */ jsx4("code", { children: scope.id }),
|
|
1236
|
+
" ",
|
|
1237
|
+
scope.description
|
|
1238
|
+
] }, scope.id);
|
|
1239
|
+
})
|
|
1240
|
+
] });
|
|
1241
|
+
}
|
|
1242
|
+
function IssuedNotice({
|
|
1243
|
+
issued,
|
|
1244
|
+
components,
|
|
1245
|
+
onDismiss
|
|
1246
|
+
}) {
|
|
1247
|
+
const { Button } = resolveAuthComponents(components);
|
|
1248
|
+
const values = issued.kind === "token" ? [{ label: "API token", url: issued.token }] : [
|
|
1249
|
+
{ label: "Client ID", url: issued.client_id },
|
|
1250
|
+
...issued.client_secret !== null ? [{ label: "Client secret", url: issued.client_secret }] : []
|
|
1251
|
+
];
|
|
1252
|
+
return /* @__PURE__ */ jsxs4("div", { role: "status", style: { display: "grid", gap: "0.5rem", minWidth: 0 }, children: [
|
|
1253
|
+
/* @__PURE__ */ jsxs4("p", { style: { overflowWrap: "anywhere" }, children: [
|
|
1254
|
+
issued.kind === "token" ? `API token \u201C${issued.name}\u201D created.` : `OAuth app \u201C${issued.name}\u201D registered.`,
|
|
1255
|
+
" Copy it now: it will not be shown again."
|
|
1256
|
+
] }),
|
|
1257
|
+
values.map((value) => /* @__PURE__ */ jsxs4("div", { style: { minWidth: 0, overflowWrap: "anywhere" }, children: [
|
|
1258
|
+
value.label,
|
|
1259
|
+
": ",
|
|
1260
|
+
/* @__PURE__ */ jsx4("code", { children: value.url }),
|
|
1261
|
+
" ",
|
|
1262
|
+
/* @__PURE__ */ jsx4(CopyButton, { text: value.url, label: value.label, components })
|
|
1263
|
+
] }, value.label)),
|
|
1264
|
+
/* @__PURE__ */ jsx4("div", { children: /* @__PURE__ */ jsx4(Button, { type: "button", onClick: onDismiss, children: "Done" }) })
|
|
1265
|
+
] });
|
|
1266
|
+
}
|
|
1267
|
+
function CopyButton({ text, label, components }) {
|
|
1268
|
+
const { Button } = resolveAuthComponents(components);
|
|
1269
|
+
const [state, setState] = React5.useState("idle");
|
|
1270
|
+
return /* @__PURE__ */ jsxs4(Fragment, { children: [
|
|
1271
|
+
/* @__PURE__ */ jsx4(
|
|
1272
|
+
Button,
|
|
1273
|
+
{
|
|
1274
|
+
type: "button",
|
|
1275
|
+
"aria-label": `Copy ${label}`,
|
|
1276
|
+
onClick: async () => {
|
|
1277
|
+
if (typeof navigator === "undefined" || typeof navigator.clipboard?.writeText !== "function") {
|
|
1278
|
+
setState("failed");
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
try {
|
|
1282
|
+
await navigator.clipboard.writeText(text);
|
|
1283
|
+
setState("copied");
|
|
1284
|
+
} catch {
|
|
1285
|
+
setState("failed");
|
|
1286
|
+
}
|
|
1287
|
+
},
|
|
1288
|
+
children: state === "copied" ? "Copied" : "Copy"
|
|
1289
|
+
}
|
|
1290
|
+
),
|
|
1291
|
+
state === "failed" && /* @__PURE__ */ jsx4("span", { role: "status", children: " Copying is unavailable here; select the text and copy it manually." })
|
|
1292
|
+
] });
|
|
1293
|
+
}
|
|
1294
|
+
function describeDuration(spec) {
|
|
1295
|
+
const match = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?)?$/.exec(spec);
|
|
1296
|
+
if (!match) {
|
|
1297
|
+
return spec;
|
|
1298
|
+
}
|
|
1299
|
+
const units = ["year", "month", "week", "day", "hour", "minute"];
|
|
1300
|
+
const parts = match.slice(1).map((value, position) => value ? `${Number(value)} ${units[position]}${Number(value) === 1 ? "" : "s"}` : null).filter((part) => part !== null);
|
|
1301
|
+
return parts.length > 0 ? parts.join(" ") : spec;
|
|
1302
|
+
}
|
|
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
|
+
}
|
|
1313
|
+
let response;
|
|
1314
|
+
try {
|
|
1315
|
+
response = await fetch(url, {
|
|
1316
|
+
method,
|
|
1317
|
+
credentials: "same-origin",
|
|
1318
|
+
headers: {
|
|
1319
|
+
Accept: "application/json",
|
|
1320
|
+
...body !== void 0 ? { "Content-Type": "application/json" } : {},
|
|
1321
|
+
...method !== "GET" ? { "X-CSRF-TOKEN": getCsrfToken(csrfToken) } : {}
|
|
1322
|
+
},
|
|
1323
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
1324
|
+
});
|
|
1325
|
+
} catch {
|
|
1326
|
+
return { ok: false, status: 0, message: "The server could not be reached. Check the connection and try again." };
|
|
1327
|
+
}
|
|
1328
|
+
const payload = await response.json().catch(() => null);
|
|
1329
|
+
if (response.ok) {
|
|
1330
|
+
return { ok: true, status: response.status, data: unwrapData(payload) };
|
|
1331
|
+
}
|
|
1332
|
+
return { ok: false, status: response.status, message: refusalMessage(response.status, payload) };
|
|
1333
|
+
}
|
|
1334
|
+
function unwrapData(payload) {
|
|
1335
|
+
return typeof payload === "object" && payload !== null && "data" in payload ? payload.data : payload;
|
|
1336
|
+
}
|
|
1337
|
+
function refusalMessage(status, payload) {
|
|
1338
|
+
if (status === 401 || status === 419) {
|
|
1339
|
+
return "Your session has expired. Reload the page and sign in again.";
|
|
1340
|
+
}
|
|
1341
|
+
if (typeof payload === "object" && payload !== null) {
|
|
1342
|
+
const errors = payload.errors;
|
|
1343
|
+
if (errors && typeof errors === "object") {
|
|
1344
|
+
const first = Object.values(errors)[0];
|
|
1345
|
+
if (Array.isArray(first) && typeof first[0] === "string") {
|
|
1346
|
+
return first[0];
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
const message = payload.message;
|
|
1350
|
+
if (typeof message === "string" && message !== "") {
|
|
1351
|
+
return message;
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
if (status === 403) {
|
|
1355
|
+
return "You do not have permission to do that.";
|
|
1356
|
+
}
|
|
1357
|
+
if (status === 404) {
|
|
1358
|
+
return "That credential no longer exists, or creating credentials is unavailable right now.";
|
|
1359
|
+
}
|
|
1360
|
+
return "That action could not be completed.";
|
|
1361
|
+
}
|
|
945
1362
|
export {
|
|
1363
|
+
ApiCredentialsSection,
|
|
946
1364
|
ChangePasswordForm,
|
|
947
1365
|
LoginForm,
|
|
948
1366
|
PasskeyLoginButton,
|
|
@@ -954,6 +1372,7 @@ export {
|
|
|
954
1372
|
arrayBufferToBase64url,
|
|
955
1373
|
authenticateWithPasskey,
|
|
956
1374
|
base64urlToArrayBuffer,
|
|
1375
|
+
describeDuration,
|
|
957
1376
|
getCsrfToken,
|
|
958
1377
|
getDefaultPasskeyName,
|
|
959
1378
|
isAbortError,
|
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"
|