@basaltkit/api-keys-ui 1.2.0 → 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 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,7 +68,8 @@ 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`.
57
- The page relies on same-origin session credentials. `authRoutes()` issues a
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
58
73
  `HttpOnly` session cookie on login, and the browser sends it automatically to
59
74
  the JSON API routes. The access JWT remains in the login response for
60
75
  non-browser clients and is not copied to `localStorage` by this UI.
package/dist/html.js CHANGED
@@ -25,12 +25,13 @@ async function list() {
25
25
  document.getElementById('error').style.display = 'none';
26
26
  const rows = document.getElementById('rows');
27
27
  if (!Array.isArray(keys) || keys.length === 0) {
28
- rows.innerHTML = '<tr><td colspan="4" class="empty muted">No API keys yet.</td></tr>';
28
+ rows.innerHTML = '<tr><td colspan="5" class="empty muted">No API keys yet.</td></tr>';
29
29
  return;
30
30
  }
31
31
  rows.innerHTML = keys.map((k) =>
32
32
  '<tr><td><code>' + esc(k.prefix) + '…</code></td>' +
33
33
  '<td>' + esc((k.scopes || []).join(', ')) + '</td>' +
34
+ '<td class="muted">' + (k.expiresAt ? new Date(k.expiresAt).toLocaleString() : 'never') + '</td>' +
34
35
  '<td class="muted">' + (k.lastUsedAt ? new Date(k.lastUsedAt).toLocaleString() : 'never') + '</td>' +
35
36
  '<td><button class="link" data-id="' + esc(k.id) + '">Revoke</button></td></tr>').join('');
36
37
  for (const b of rows.querySelectorAll('button[data-id]')) {
@@ -49,7 +50,10 @@ document.getElementById('create').addEventListener('submit', async (e) => {
49
50
  e.preventDefault();
50
51
  const f = new FormData(e.target);
51
52
  const scopes = String(f.get('scopes') || '').split(',').map((s) => s.trim()).filter(Boolean);
52
- const body = JSON.stringify({ name: f.get('name'), ...(scopes.length ? { scopes } : {}) });
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 } : {}) });
53
57
  try {
54
58
  const created = await fetch(API + '/apikeys', { ...opts, method: 'POST', body }).then(json);
55
59
  document.getElementById('error').style.display = 'none';
@@ -125,6 +129,7 @@ export function apiKeysPageHtml(options = {}) {
125
129
  <form id="create">
126
130
  <label>Name<input name="name" placeholder="CI pipeline" required /></label>
127
131
  <label>Scopes (comma-separated)<input name="scopes" placeholder="read, write" /></label>
132
+ <label>Expires (optional)<input name="expiresAt" type="datetime-local" /></label>
128
133
  <button class="primary" type="submit">Create key</button>
129
134
  </form>
130
135
  <div class="reveal" id="reveal">
@@ -137,7 +142,7 @@ export function apiKeysPageHtml(options = {}) {
137
142
  </div>
138
143
 
139
144
  <table>
140
- <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>
141
146
  <tbody id="rows"></tbody>
142
147
  </table>
143
148
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basaltkit/api-keys-ui",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "engines": {
5
5
  "node": ">=22.5.0"
6
6
  },
@@ -26,8 +26,8 @@
26
26
  "typescript": "^7.0.2",
27
27
  "vitest": "^4.1.11",
28
28
  "zod": "^4.5.4",
29
- "@basaltkit/auth": "^2.1.0",
30
29
  "@basaltkit/fastify": "^2.0.0",
30
+ "@basaltkit/auth": "^2.2.0",
31
31
  "@basaltkit/tenancy": "^1.10.0",
32
32
  "@basaltkit/tsconfig": "^0.24.0"
33
33
  },