@basaltkit/api-keys-ui 1.2.0 → 1.4.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.
@@ -63,6 +78,16 @@ non-browser clients and is not copied to `localStorage` by this UI.
63
78
 
64
79
  Returns the page's HTML as a string — use it directly if you want to serve it your own way (or on another framework).
65
80
 
81
+ ## Guarding the route
82
+
83
+ The route always requires a logged-in user (`auth: true`). Pass `meta` to add
84
+ your own guard — or a rate limit, OpenAPI tags — merged into the page route;
85
+ `auth: true` is applied on top and cannot be switched off:
86
+
87
+ ```ts
88
+ apiKeysUiRoutes({ meta: { can: 'apikeys:manage' } })
89
+ ```
90
+
66
91
  ## Content-Security-Policy
67
92
 
68
93
  The route sets a route-scoped CSP by default: everything locked down and the
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/dist/plugin.d.ts CHANGED
@@ -10,6 +10,12 @@ export interface ApiKeysUiOptions extends ApiKeysPageOptions {
10
10
  * to send no route-scoped CSP at all.
11
11
  */
12
12
  csp?: string | false;
13
+ /**
14
+ * Extra route metadata merged into the page route — a guard such as
15
+ * `{ can: 'apikeys:manage' }`, a rate limit, OpenAPI tags. `auth: true` is
16
+ * always applied on top and cannot be switched off.
17
+ */
18
+ meta?: Record<string, unknown>;
13
19
  }
14
20
  /**
15
21
  * Serves the API-keys management page at `GET /apikeys/ui` (requires a
package/dist/plugin.js CHANGED
@@ -8,11 +8,12 @@ import { apiKeysPageCsp, apiKeysPageHtml } from './html.js';
8
8
  export function apiKeysUiRoutes(options = {}) {
9
9
  const html = apiKeysPageHtml(options);
10
10
  const csp = options.csp === false ? undefined : (options.csp ?? apiKeysPageCsp(options));
11
+ const meta = { ...options.meta, auth: true };
11
12
  return [
12
13
  route({
13
14
  method: 'GET',
14
15
  url: options.path ?? '/apikeys/ui',
15
- meta: { auth: true },
16
+ meta,
16
17
  async handler({ reply }) {
17
18
  if (csp !== undefined)
18
19
  reply.header('content-security-policy', csp);
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.4.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/http": "^2.0.0",
22
- "@basaltkit/core": "^1.3.1"
21
+ "@basaltkit/core": "^1.6.0",
22
+ "@basaltkit/http": "^2.8.0"
23
23
  },
24
24
  "devDependencies": {
25
- "@types/node": "^26.4.0",
25
+ "@types/node": "^26.6.1",
26
26
  "typescript": "^7.0.2",
27
- "vitest": "^4.1.11",
28
- "zod": "^4.5.4",
29
- "@basaltkit/auth": "^2.1.0",
30
- "@basaltkit/fastify": "^2.0.0",
31
- "@basaltkit/tenancy": "^1.10.0",
27
+ "vitest": "^5.0.1",
28
+ "zod": "^4.6.5",
29
+ "@basaltkit/auth": "^4.2.0",
30
+ "@basaltkit/fastify": "^2.6.0",
31
+ "@basaltkit/tenancy": "^3.2.0",
32
32
  "@basaltkit/tsconfig": "^0.24.0"
33
33
  },
34
34
  "publishConfig": {