@basaltkit/api-keys-ui 1.1.2 → 1.3.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 +19 -0
- package/dist/html.js +38 -13
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -45,6 +45,20 @@ const app = await createApp({
|
|
|
45
45
|
|
|
46
46
|
Open **`/apikeys/ui`** (authenticated) and the user can create, view and revoke their keys.
|
|
47
47
|
|
|
48
|
+
Log in through `/auth/login` before opening the page. The login route sets the
|
|
49
|
+
HttpOnly browser session cookie; the page uses same-origin requests and sends
|
|
50
|
+
that cookie automatically:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
curl -c cookies.txt -X POST http://localhost:3000/auth/login \
|
|
54
|
+
-H 'content-type: application/json' \
|
|
55
|
+
-d '{"email":"ada@example.com","password":"secretpassword1"}'
|
|
56
|
+
curl -b cookies.txt http://localhost:3000/apikeys/ui
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Do not modify the page to read a JWT from `localStorage`. The supported browser
|
|
60
|
+
flow is the HttpOnly cookie.
|
|
61
|
+
|
|
48
62
|
## How it works
|
|
49
63
|
|
|
50
64
|
The page is served by `GET /apikeys/ui` (requires login). In the browser, it calls the JSON routes with `credentials: 'same-origin'`, so it assumes the user's session is already authenticated against `${apiBase}/apikeys`. When creating a key, it reveals the secret **once** (with a warning and a copy button) — after that only the prefix is visible, as required by `@basaltkit/auth`'s security model.
|
|
@@ -54,6 +68,11 @@ The page is served by `GET /apikeys/ui` (requires login). In the browser, it cal
|
|
|
54
68
|
### `apiKeysUiRoutes({ path?, apiBase?, title? })`
|
|
55
69
|
|
|
56
70
|
Returns the route that serves the page. `path` (default `/apikeys/ui`), `apiBase` (where the JSON routes are, default same-origin), `title`.
|
|
71
|
+
The page relies on same-origin session credentials. The create form accepts an
|
|
72
|
+
optional expiration date, which the server validates again. `authRoutes()` issues a
|
|
73
|
+
`HttpOnly` session cookie on login, and the browser sends it automatically to
|
|
74
|
+
the JSON API routes. The access JWT remains in the login response for
|
|
75
|
+
non-browser clients and is not copied to `localStorage` by this UI.
|
|
57
76
|
|
|
58
77
|
### `apiKeysPageHtml({ apiBase?, title? })`
|
|
59
78
|
|
package/dist/html.js
CHANGED
|
@@ -8,40 +8,62 @@ const pageScript = (apiBase) => `
|
|
|
8
8
|
const API = ${scriptJson(apiBase)};
|
|
9
9
|
const esc = (s) => String(s == null ? '' : s).replace(/[&<>"']/g, (c) => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
10
10
|
const opts = { credentials: 'same-origin', headers: { 'content-type': 'application/json' } };
|
|
11
|
+
const showError = (message) => {
|
|
12
|
+
const error = document.getElementById('error');
|
|
13
|
+
error.textContent = message;
|
|
14
|
+
error.style.display = 'block';
|
|
15
|
+
};
|
|
16
|
+
const json = async (response) => {
|
|
17
|
+
const value = await response.json().catch(() => ({}));
|
|
18
|
+
if (!response.ok) throw new Error(value.message || value.error || ('Request failed (' + response.status + ')'));
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
11
21
|
|
|
12
22
|
async function list() {
|
|
13
|
-
|
|
23
|
+
try {
|
|
24
|
+
const keys = await fetch(API + '/apikeys', opts).then(json);
|
|
25
|
+
document.getElementById('error').style.display = 'none';
|
|
14
26
|
const rows = document.getElementById('rows');
|
|
15
27
|
if (!Array.isArray(keys) || keys.length === 0) {
|
|
16
|
-
rows.innerHTML = '<tr><td colspan="
|
|
28
|
+
rows.innerHTML = '<tr><td colspan="5" class="empty muted">No API keys yet.</td></tr>';
|
|
17
29
|
return;
|
|
18
30
|
}
|
|
19
31
|
rows.innerHTML = keys.map((k) =>
|
|
20
32
|
'<tr><td><code>' + esc(k.prefix) + '…</code></td>' +
|
|
21
33
|
'<td>' + esc((k.scopes || []).join(', ')) + '</td>' +
|
|
34
|
+
'<td class="muted">' + (k.expiresAt ? new Date(k.expiresAt).toLocaleString() : 'never') + '</td>' +
|
|
22
35
|
'<td class="muted">' + (k.lastUsedAt ? new Date(k.lastUsedAt).toLocaleString() : 'never') + '</td>' +
|
|
23
36
|
'<td><button class="link" data-id="' + esc(k.id) + '">Revoke</button></td></tr>').join('');
|
|
24
37
|
for (const b of rows.querySelectorAll('button[data-id]')) {
|
|
25
38
|
b.addEventListener('click', async () => {
|
|
26
39
|
if (!confirm('Revoke this key? Any client using it will stop working.')) return;
|
|
27
|
-
|
|
28
|
-
|
|
40
|
+
try {
|
|
41
|
+
await fetch(API + '/apikeys/' + b.dataset.id, { ...opts, method: 'DELETE' }).then(json);
|
|
42
|
+
list();
|
|
43
|
+
} catch (error) { showError(error.message); }
|
|
29
44
|
});
|
|
30
45
|
}
|
|
46
|
+
} catch (error) { showError(error.message); }
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
document.getElementById('create').addEventListener('submit', async (e) => {
|
|
34
50
|
e.preventDefault();
|
|
35
51
|
const f = new FormData(e.target);
|
|
36
52
|
const scopes = String(f.get('scopes') || '').split(',').map((s) => s.trim()).filter(Boolean);
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
53
|
+
const expiration = String(f.get('expiresAt') || '');
|
|
54
|
+
const expiresAt = expiration ? new Date(expiration).getTime() : undefined;
|
|
55
|
+
if (expiration && !Number.isFinite(expiresAt)) { showError('Choose a valid expiration date.'); return; }
|
|
56
|
+
const body = JSON.stringify({ name: f.get('name'), ...(scopes.length ? { scopes } : {}), ...(expiresAt !== undefined ? { expiresAt } : {}) });
|
|
57
|
+
try {
|
|
58
|
+
const created = await fetch(API + '/apikeys', { ...opts, method: 'POST', body }).then(json);
|
|
59
|
+
document.getElementById('error').style.display = 'none';
|
|
60
|
+
if (created && created.key) {
|
|
61
|
+
document.getElementById('newkey').textContent = created.key;
|
|
62
|
+
document.getElementById('reveal').style.display = 'block';
|
|
63
|
+
}
|
|
64
|
+
e.target.reset();
|
|
65
|
+
list();
|
|
66
|
+
} catch (error) { showError(error.message); }
|
|
45
67
|
});
|
|
46
68
|
|
|
47
69
|
document.getElementById('copy').addEventListener('click', () => {
|
|
@@ -97,6 +119,7 @@ export function apiKeysPageHtml(options = {}) {
|
|
|
97
119
|
.reveal { border: 1px solid var(--ok); background: #16a34a18; border-radius: 8px; padding: .75rem 1rem; margin-top: .75rem; display: none; }
|
|
98
120
|
.reveal code { word-break: break-all; }
|
|
99
121
|
.empty { padding: 1rem; text-align: center; }
|
|
122
|
+
.error { color: var(--danger); margin-top: .75rem; display: none; }
|
|
100
123
|
</style>
|
|
101
124
|
</head>
|
|
102
125
|
<body>
|
|
@@ -106,6 +129,7 @@ export function apiKeysPageHtml(options = {}) {
|
|
|
106
129
|
<form id="create">
|
|
107
130
|
<label>Name<input name="name" placeholder="CI pipeline" required /></label>
|
|
108
131
|
<label>Scopes (comma-separated)<input name="scopes" placeholder="read, write" /></label>
|
|
132
|
+
<label>Expires (optional)<input name="expiresAt" type="datetime-local" /></label>
|
|
109
133
|
<button class="primary" type="submit">Create key</button>
|
|
110
134
|
</form>
|
|
111
135
|
<div class="reveal" id="reveal">
|
|
@@ -114,10 +138,11 @@ export function apiKeysPageHtml(options = {}) {
|
|
|
114
138
|
<code id="newkey"></code><button id="copy" type="button">Copy</button>
|
|
115
139
|
</div>
|
|
116
140
|
</div>
|
|
141
|
+
<div class="error" id="error" role="alert"></div>
|
|
117
142
|
</div>
|
|
118
143
|
|
|
119
144
|
<table>
|
|
120
|
-
<thead><tr><th>Prefix</th><th>Scopes</th><th>Last used</th><th></th></tr></thead>
|
|
145
|
+
<thead><tr><th>Prefix</th><th>Scopes</th><th>Expires</th><th>Last used</th><th></th></tr></thead>
|
|
121
146
|
<tbody id="rows"></tbody>
|
|
122
147
|
</table>
|
|
123
148
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/api-keys-ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=22.5.0"
|
|
6
6
|
},
|
|
@@ -18,17 +18,17 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@basaltkit/
|
|
22
|
-
"@basaltkit/
|
|
21
|
+
"@basaltkit/http": "^2.0.0",
|
|
22
|
+
"@basaltkit/core": "^1.3.1"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^26.4.0",
|
|
26
26
|
"typescript": "^7.0.2",
|
|
27
27
|
"vitest": "^4.1.11",
|
|
28
28
|
"zod": "^4.5.4",
|
|
29
|
-
"@basaltkit/auth": "^2.0.0",
|
|
30
29
|
"@basaltkit/fastify": "^2.0.0",
|
|
31
|
-
"@basaltkit/
|
|
30
|
+
"@basaltkit/auth": "^2.2.0",
|
|
31
|
+
"@basaltkit/tenancy": "^1.10.0",
|
|
32
32
|
"@basaltkit/tsconfig": "^0.24.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|