@nocobase/app-plugin-api-keys 0.0.0-stage → 1.0.0-beta.14

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.
Files changed (79) hide show
  1. package/CHANGELOG.md +468 -0
  2. package/README.md +162 -2
  3. package/dist/client/api-keys-client.d.ts +19 -0
  4. package/dist/client/api-keys-client.d.ts.map +1 -0
  5. package/dist/client/api-keys-client.js +7 -0
  6. package/dist/client/api-keys-client.js.map +1 -0
  7. package/dist/client/expiry.d.ts +23 -0
  8. package/dist/client/expiry.d.ts.map +1 -0
  9. package/dist/client/expiry.js +40 -0
  10. package/dist/client/expiry.js.map +1 -0
  11. package/dist/client/index.d.ts +4 -0
  12. package/dist/client/index.d.ts.map +1 -0
  13. package/dist/client/index.js +6 -0
  14. package/dist/client/index.js.map +1 -0
  15. package/dist/client/plugin.d.ts +8 -0
  16. package/dist/client/plugin.d.ts.map +1 -0
  17. package/dist/client/plugin.js +10 -0
  18. package/dist/client/plugin.js.map +1 -0
  19. package/dist/database/migrations/.manifest.json +9 -0
  20. package/dist/database/migrations/202609150001_create_api_key_table.js +58 -0
  21. package/dist/database/migrations/202609150001_create_api_key_table.js.map +1 -0
  22. package/dist/server/api-docs.d.ts +24 -0
  23. package/dist/server/api-docs.d.ts.map +1 -0
  24. package/dist/server/api-docs.js +73 -0
  25. package/dist/server/api-docs.js.map +1 -0
  26. package/dist/server/api-keys.d.ts +52 -0
  27. package/dist/server/api-keys.d.ts.map +1 -0
  28. package/dist/server/api-keys.js +199 -0
  29. package/dist/server/api-keys.js.map +1 -0
  30. package/dist/server/index.d.ts +23 -0
  31. package/dist/server/index.d.ts.map +1 -0
  32. package/dist/server/index.js +22 -0
  33. package/dist/server/index.js.map +1 -0
  34. package/dist/server/key-sessions.d.ts +35 -0
  35. package/dist/server/key-sessions.d.ts.map +1 -0
  36. package/dist/server/key-sessions.js +50 -0
  37. package/dist/server/key-sessions.js.map +1 -0
  38. package/dist/server/plugin.d.ts +4 -0
  39. package/dist/server/plugin.d.ts.map +1 -0
  40. package/dist/server/plugin.js +15 -0
  41. package/dist/server/plugin.js.map +1 -0
  42. package/dist/server/providers/index.d.ts +19 -0
  43. package/dist/server/providers/index.d.ts.map +1 -0
  44. package/dist/server/providers/index.js +72 -0
  45. package/dist/server/providers/index.js.map +1 -0
  46. package/dist/server/routes/errors.d.ts +8 -0
  47. package/dist/server/routes/errors.d.ts.map +1 -0
  48. package/dist/server/routes/errors.js +48 -0
  49. package/dist/server/routes/errors.js.map +1 -0
  50. package/dist/server/routes/index.d.ts +10 -0
  51. package/dist/server/routes/index.d.ts.map +1 -0
  52. package/dist/server/routes/index.js +177 -0
  53. package/dist/server/routes/index.js.map +1 -0
  54. package/dist/server/routes/schemas.d.ts +25 -0
  55. package/dist/server/routes/schemas.d.ts.map +1 -0
  56. package/dist/server/routes/schemas.js +127 -0
  57. package/dist/server/routes/schemas.js.map +1 -0
  58. package/dist/server/scoped-keys.d.ts +116 -0
  59. package/dist/server/scoped-keys.d.ts.map +1 -0
  60. package/dist/server/scoped-keys.js +451 -0
  61. package/dist/server/scoped-keys.js.map +1 -0
  62. package/dist/server/scopes.d.ts +70 -0
  63. package/dist/server/scopes.d.ts.map +1 -0
  64. package/dist/server/scopes.js +165 -0
  65. package/dist/server/scopes.js.map +1 -0
  66. package/dist/server/service.d.ts +28 -0
  67. package/dist/server/service.d.ts.map +1 -0
  68. package/dist/server/service.js +69 -0
  69. package/dist/server/service.js.map +1 -0
  70. package/dist/server/tokens.d.ts +6 -0
  71. package/dist/server/tokens.d.ts.map +1 -0
  72. package/dist/server/tokens.js +5 -0
  73. package/dist/server/tokens.js.map +1 -0
  74. package/dist/shared/scopes.d.ts +160 -0
  75. package/dist/shared/scopes.d.ts.map +1 -0
  76. package/dist/shared/scopes.js +83 -0
  77. package/dist/shared/scopes.js.map +1 -0
  78. package/package.json +76 -3
  79. package/skills/nocobase-app-plugin-api-keys/SKILL.md +161 -0
package/package.json CHANGED
@@ -1,6 +1,79 @@
1
1
  {
2
2
  "name": "@nocobase/app-plugin-api-keys",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "displayName": "API Keys App Plugin",
4
+ "description": "API key authentication for NocoBase applications, built on Better Auth.",
5
+ "version": "1.0.0-beta.14",
6
+ "type": "module",
7
+ "prettier": "@nocobase/dev-config/prettier",
8
+ "engines": {
9
+ "node": ">=24.0.0"
10
+ },
11
+ "sideEffects": false,
12
+ "exports": {
13
+ "./server": {
14
+ "types": "./dist/server/index.d.ts",
15
+ "import": "./dist/server/index.js"
16
+ },
17
+ "./client": {
18
+ "types": "./dist/client/index.d.ts",
19
+ "import": "./dist/client/index.js"
20
+ },
21
+ "./client/plugin": {
22
+ "types": "./dist/client/plugin.d.ts",
23
+ "import": "./dist/client/plugin.js"
24
+ },
25
+ "./shared/scopes": {
26
+ "types": "./dist/shared/scopes.d.ts",
27
+ "import": "./dist/shared/scopes.js"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "files": [
32
+ "dist",
33
+ "README.md",
34
+ "CHANGELOG.md",
35
+ "skills"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "dependencies": {
41
+ "@better-auth/api-key": "1.7.5",
42
+ "@better-auth/core": "1.7.5",
43
+ "@better-auth/utils": "0.4.2",
44
+ "better-auth": "1.7.5",
45
+ "better-call": "1.4.0",
46
+ "hono": "^4.13.2",
47
+ "zod": "^4.6.5"
48
+ },
49
+ "peerDependencies": {
50
+ "@nocobase/app-client": "^3.0.0-beta.0",
51
+ "@nocobase/app-plugin-authentication": "^2.0.0-beta.3",
52
+ "@nocobase/app-plugin-authorization": "^1.0.0-beta.26",
53
+ "@nocobase/app-server": "^2.0.0-beta.3",
54
+ "@nocobase/authorization": "^1.0.0-beta.12",
55
+ "@nocobase/db": "^1.0.0-beta.18",
56
+ "@nocobase/service-provider": "^0.0.2-beta.1"
57
+ },
58
+ "devDependencies": {
59
+ "@types/node": "^24.0.0",
60
+ "eslint": "^10.0.0",
61
+ "prettier": "^3.9.6",
62
+ "typescript": "^6.0.3",
63
+ "vitest": "^4.1.10",
64
+ "@nocobase/app-testing": "0.1.0-beta.2",
65
+ "@nocobase/dev-config": "0.1.0-beta.21"
66
+ },
67
+ "scripts": {
68
+ "clean": "node ./scripts/clean-dist.mjs",
69
+ "build": "pnpm clean && tsc -p tsconfig.json && tsc -p tsconfig.migrations.json && nocobase-db-manifests",
70
+ "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.migrations.json --noEmit",
71
+ "test": "vitest run --passWithNoTests",
72
+ "lint": "eslint . --max-warnings 0",
73
+ "lint:fix": "eslint . --fix --max-warnings 0",
74
+ "format": "prettier . --write",
75
+ "format:check": "prettier . --check",
76
+ "check": "pnpm lint && pnpm format:check && pnpm typecheck && pnpm test && pnpm build",
77
+ "fix": "pnpm lint:fix && pnpm format"
78
+ }
6
79
  }
@@ -0,0 +1,161 @@
1
+ ---
2
+ name: nocobase-app-plugin-api-keys
3
+ description: Register the API Keys plugin in a NocoBase App, let scripts and integrations call the App's API with an x-api-key header, or work out why a key is rejected.
4
+ metadata:
5
+ short-description: Authenticate API requests with user-owned keys
6
+ ---
7
+
8
+ # API Keys App Plugin
9
+
10
+ Use this Skill when an App needs non-interactive callers — a script, a cron job,
11
+ a third-party integration — to reach its API as a real user, or when a key the
12
+ App issued is being rejected. The default configuration resolves a key into its owner’s Session and carries that user’s roles. Applications needing scoped credentials must use a separate configuration with `enableSessionForAPIKeys: false` and enforce their business rules on the server.
13
+
14
+ This package is Better Auth's
15
+ [API Key plugin](https://www.better-auth.com/docs/plugins/api-key) plus the
16
+ `apikey` migration. `apiKey` is wrapped
17
+ only to supply three defaults, all overridable; its name, options and behaviour
18
+ are Better Auth's, and Better Auth's documentation applies unchanged.
19
+
20
+ ## Public surfaces
21
+
22
+ - Server plugin: default export of `@nocobase/app-plugin-api-keys/server`.
23
+ Contributes the `apikey` table migration and nothing else.
24
+ - `apiKey(options?)`, plus `API_KEY_TABLE_NAME`, `API_KEY_ERROR_CODES` and the
25
+ option types, from the same entry. Use these rather than adding
26
+ `@better-auth/api-key` to the App: the migration here matches the schema this
27
+ version declares, and taking both from one package keeps them in step.
28
+ - Client registration factory: `@nocobase/app-plugin-api-keys/client`. It
29
+ contributes no pages.
30
+ - `apiKeyClient`, re-exported from `@better-auth/api-key/client` by the same
31
+ entry.
32
+ - `ApiKeySummary` and the expiry helpers, for the App's own key management
33
+ page. Such a page reads `useAuthenticationClient().apiKey`, typed through the
34
+ authentication plugin's `AuthClientPluginRegistry`, and is declared among the
35
+ App's own routes.
36
+ - HTTP API: `POST /api/auth/api-key/create`, `GET /api/auth/api-key/list`,
37
+ `GET /api/auth/api-key/get`, `POST /api/auth/api-key/update`, and
38
+ `POST /api/auth/api-key/delete`. All five act only on the caller's own keys.
39
+
40
+ The plugin contributes no NocoBase route of its own. The endpoints above are
41
+ Better Auth's, mounted by Authentication's `/api/auth/*` handler. Its server registers one thing: the access check that lets a valid key read the App's API document.
42
+
43
+ ## Register it
44
+
45
+ Both halves are required, and each fails differently on its own.
46
+
47
+ 1. Add the default export to the App's `server/plugins.ts` and
48
+ `client/plugins.ts`.
49
+ 2. Add `apiKey()` to `plugins` in `server/config/auth.ts`, and `apiKeyClient()`
50
+ to `plugins` in `client/config/auth.ts`.
51
+ 3. Run `pnpm nocobase db apply`.
52
+
53
+ Only step 1 leaves the `apikey` table created and no endpoints mounted. Only
54
+ step 2 mounts endpoints against a table that does not exist, and every call
55
+ fails at the database.
56
+
57
+ ### The three defaults
58
+
59
+ `apiKey()` supplies `enableSessionForAPIKeys: true`, `rateLimit: { enabled:
60
+ false }` and `requireName: true`, so an App normally passes nothing.
61
+
62
+ `enableSessionForAPIKeys` is the one that has to be set. Better Auth defaults
63
+ it off, and with it off a key authenticates nothing: keys are still
64
+ issued, and every request carrying one answers 401. Nothing points at the
65
+ configuration, so do not override it without meaning to.
66
+
67
+ `rateLimit` is off because Better Auth's own default is 10 requests per key per
68
+ day, which is a quota for issuing keys rather than for using them. Pass
69
+ `{ enabled: true, maxRequests: 1000, timeWindow: 60_000 }` for a quota the App
70
+ actually wants.
71
+
72
+ All three are overridable, and passing all three back reproduces Better Auth's
73
+ own behaviour exactly.
74
+
75
+ ## Call the API with a key
76
+
77
+ ```bash
78
+ curl -H 'x-api-key: <key>' https://example.com/api/orders
79
+ ```
80
+
81
+ An App route needs no change. `auth.required()` and `auth.optional()` resolve
82
+ the key into the owner's Session, and Authorization then evaluates that user's
83
+ roles. Read `context.get('auth')` exactly as for a cookie Session.
84
+
85
+ Pass `apiKeyHeaders: ['x-api-key', 'authorization-key']` to accept a different
86
+ header.
87
+
88
+ To find out what the App's API offers, read its OpenAPI document with the same key: `GET /api/swagger` returns the JSON document and `GET /api/swagger/docs` the Swagger UI page. A signed-in session or a valid key may read it; anything else gets `401` with `error.reason` `API_DOCS_UNAUTHENTICATED`. The API key endpoints are listed there under `/api/auth/api-key/...`, tagged `Authentication`.
89
+
90
+ ## What a key can reach, and what it cannot
91
+
92
+ A key is its owner, so it reaches the Better Auth endpoints a Session reaches.
93
+ Verified against `@better-auth/api-key` 1.7.1 with only an `x-api-key` header:
94
+
95
+ | Endpoint | Result |
96
+ | -------------------------------------------------------- | -------------------------- |
97
+ | `POST /api-key/create`, `GET /api-key/list` | reachable |
98
+ | `POST /update-user` | reachable |
99
+ | `GET /list-sessions` | reachable |
100
+ | `POST /sign-out` | reachable |
101
+ | `POST /delete-user`, `/change-email`, `/revoke-sessions` | 401 |
102
+ | `POST /change-password` | requires `currentPassword` |
103
+
104
+ Two consequences to plan for:
105
+
106
+ - **A key mints keys.** The successor holds its own expiry and revocation, and
107
+ nothing links it to the key that made it. Revoking a leaked key means
108
+ reviewing the owner's whole list, not deleting the one key you know about.
109
+ - **`/list-sessions` returns the unsigned session token.** The cookie is
110
+ `token.signature`, so replaying the token alone does not authenticate; it
111
+ becomes a takeover only if the auth secret leaks too or cookie signing is
112
+ off.
113
+
114
+ An App that wants either closed adds its own Better Auth `before` hook
115
+ rejecting those paths when the API key header is present. Match on the header
116
+ rather than on the resolved Session, and place the hook before the plugin's
117
+ own, or the Session is minted and returned first.
118
+
119
+ ## Other constraints
120
+
121
+ - **A rejected key is answered with Better Auth's own status, and its code as `error.reason`** (domain `authentication`) in the standard error body, so a guarded route tells the caller why: `401 KEY_EXPIRED`, `401 KEY_NOT_FOUND`, `429 USAGE_EXCEEDED`. `getSession()` throws Better Auth's `APIError` for a
122
+ refused key, as Better Auth itself does; a caller that only asks who is
123
+ signed in catches it.
124
+ - **Disabling a user disables that user's keys immediately**, because
125
+ Authentication re-reads `user.disabledAt` on every request.
126
+ - **The key is shown once.** Only a hash is stored, alongside the first few
127
+ characters used to identify it in a list. A lost key is replaced, not
128
+ recovered.
129
+ - **A client may set only `name`, `expiresIn`, `prefix`, and `metadata`.**
130
+ Better Auth refuses `remaining`, `permissions`, and the rate-limit fields on
131
+ a request that carries headers, so an App that needs those sets them
132
+ server-side.
133
+ - `expiresIn` is seconds, validated in whole days between 1 and 365.
134
+
135
+ ## Ownership
136
+
137
+ The plugin owns the `apikey` table, the `@better-auth/api-key` version, and
138
+ the three defaults. The App owns its two `config/auth.ts` files, any
139
+ option it overrides there, and any page it builds for managing keys.
140
+
141
+ Never write the `apikey` table directly. Its `key` column holds a hash, and a
142
+ row inserted by hand authenticates nothing.
143
+
144
+ ## Verification
145
+
146
+ 1. Sign in and create a key with `authClient.apiKey.create()` (or the App's own
147
+ page), and copy it.
148
+ 2. `curl -H 'x-api-key: <key>' <app>/api/<a route behind auth.required()>` —
149
+ expect the same response the signed-in user gets.
150
+ 3. Revoke the key, repeat step 2 — expect 401, not 500.
151
+ 4. Disable the owning user, repeat step 2 with a second key — expect 401.
152
+
153
+ ## Trusted server extensions
154
+
155
+ `apiKey()` also accepts a configuration array, each with a unique `configId`. The three NocoBase defaults apply to each entry and remain overridable. A non-Session configuration must never share the default Session configuration’s identity. Verification checks the actual stored configuration, so presenting such a key in `x-api-key` does not grant its owner’s Session.
156
+
157
+ `new ApiKeyService(authentication, configId)` provides trusted database-backed `create({ userId, name, expiresIn? })`, `get(id)`, `verify(secret)`, `disable(id)` and `remove(id)` operations. Create returns `{ key, secret }` once; summaries never include the stored hash. Get, disable and remove are configuration-bound; disable and remove are repeatable. Missing configurations fail closed. The service uses the registered Better Auth configuration, endpoints and formal Authentication plugin API. Secondary storage is not supported by this service.
158
+
159
+ These methods are server-only capabilities, not authorization checks. Callers must authorize the actor, bind their own resources, and enforce scopes before allowing the operation. Keep application-specific rules outside this plugin. Applications that reserve a configuration for their own management API must also reject public Better Auth management requests for that configuration; otherwise its owner can update its enabled state or expiration through the self-service endpoints. Better Auth may prune expired rows during normal API Key operations.
160
+
161
+ Server calls run through `Auth.pluginApi()` and the normal Better Auth hooks; `withConnection(connection)` binds all credential writes to a caller-owned transaction. The added get/delete operations are declared with `createAuthEndpoint.serverOnly` and cannot be reached over HTTP.