@vllnt/convex-api-keys 0.2.1-canary.9b260a8 → 0.2.1-canary.c478e3d

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 (2) hide show
  1. package/README.md +89 -31
  2. package/package.json +25 -10
package/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  <!-- Badges -->
2
+
2
3
  [![Convex Component](https://img.shields.io/badge/convex-component-EE342F.svg)](https://www.convex.dev/components)
3
4
  [![npm version](https://img.shields.io/npm/v/@vllnt/convex-api-keys.svg)](https://www.npmjs.com/package/@vllnt/convex-api-keys)
4
5
  [![CI](https://github.com/vllnt/convex-api-keys/actions/workflows/ci.yml/badge.svg)](https://github.com/vllnt/convex-api-keys/actions/workflows/ci.yml)
@@ -6,33 +7,51 @@
6
7
 
7
8
  # @vllnt/convex-api-keys
8
9
 
9
- Secure API key management as a [Convex component](https://docs.convex.dev/components) — create, validate, revoke, rotate, and track usage with built-in auth boundaries and structured audit logging.
10
+ Secure API key management as a
11
+ [Convex component](https://docs.convex.dev/components) — create, validate,
12
+ revoke, rotate, and track usage with built-in auth boundaries and structured
13
+ audit logging.
10
14
 
11
15
  ```ts
12
16
  const apiKeys = new ApiKeys(components.apiKeys, { prefix: "myapp" });
13
- const { key, keyId } = await apiKeys.create(ctx, { name: "SDK Key", ownerId: orgId });
17
+ const { key, keyId } = await apiKeys.create(ctx, {
18
+ name: "SDK Key",
19
+ ownerId: orgId,
20
+ });
14
21
  const result = await apiKeys.validate(ctx, { key: bearerToken });
15
22
  // result.valid → { keyId, ownerId, scopes, ... } | result.reason
16
23
  ```
17
24
 
18
25
  ## Features
19
26
 
20
- - **Secure by default** — SHA-256 hashed storage, constant-time comparison, prefix-indexed O(1) lookup, server-side secret generation.
21
- - **Auth boundary** — `ownerId` required on all admin mutations; prevents cross-tenant access.
27
+ - **Secure by default** — SHA-256 hashed storage, constant-time comparison,
28
+ prefix-indexed O(1) lookup, server-side secret generation.
29
+ - **Owner checks** — key-management methods match the supplied `ownerId`; the
30
+ host still authenticates and authorizes callers. `configure` is a separate
31
+ host-admin-only surface.
22
32
  - **Key types** — `secret` and `publishable` keys with type-encoded prefixes.
23
- - **Finite-use keys** — `remaining` counter with atomic decrement for one-time-use tokens.
33
+ - **Finite-use keys** — `remaining` counter with atomic decrement for
34
+ one-time-use tokens.
24
35
  - **Disable / enable** — reversible pause without revoking.
25
- - **Rotation** — configurable grace period (60s–30d) where both old and new keys are valid.
26
- - **Bulk revoke by tag, tags & environments, multi-tenant scoping, usage tracking, input validation, structured audit logging.**
36
+ - **Rotation** — configurable grace period (60s–30d) where both old and new keys
37
+ are valid.
38
+ - **Bulk revoke by tag, tags & environments, multi-tenant scoping, usage
39
+ tracking, input validation, structured audit logging.**
27
40
 
28
41
  ## Installation
29
42
 
30
- Peer dependency: `convex@^1.36.1`
43
+ The stable `latest` release is `0.2.0` (`convex@^1.36.1`). This README describes
44
+ current `main` / `canary`, which requires `convex@^1.45.0`.
31
45
 
32
46
  ```bash
33
47
  npm install convex@^1.36.1 @vllnt/convex-api-keys
48
+ # Opt into the current prerelease instead:
49
+ npm install convex@^1.45.0 @vllnt/convex-api-keys@canary
34
50
  ```
35
51
 
52
+ Canary releases are prereleases; pin the resolved version for reproducible
53
+ installs.
54
+
36
55
  Register in your `convex/convex.config.ts`:
37
56
 
38
57
  ```ts
@@ -44,16 +63,43 @@ app.use(apiKeys);
44
63
  export default app;
45
64
  ```
46
65
 
47
- > **Rate limiting** is your responsibility — add `@convex-dev/rate-limiter` at your HTTP action/mutation layer where you have real caller context (IP, auth, plan tier). The component has none.
66
+ > **Rate limiting** is your responsibility — add `@convex-dev/rate-limiter` at
67
+ > your HTTP action/mutation layer where you have real caller context (IP, auth,
68
+ > plan tier). The component has none.
48
69
 
49
70
  ## Usage
50
71
 
72
+ Expose a host mutation that derives ownership from authenticated identity:
73
+
74
+ ```ts
75
+ // convex/keys.ts
76
+ import { ApiKeys } from "@vllnt/convex-api-keys";
77
+ import { components } from "./_generated/api";
78
+ import { mutation } from "./_generated/server";
79
+ import { v } from "convex/values";
80
+
81
+ const apiKeys = new ApiKeys(components.apiKeys);
82
+
83
+ export const create = mutation({
84
+ args: { name: v.string() },
85
+ handler: async (ctx, { name }) => {
86
+ const identity = await ctx.auth.getUserIdentity();
87
+ if (!identity) throw new Error("Unauthenticated");
88
+ return apiKeys.create(ctx, { name, ownerId: identity.tokenIdentifier });
89
+ },
90
+ });
91
+ ```
92
+
93
+ Show the returned raw key only to its owner; do not log or persist it. For
94
+ shared organization ownership, replace this mapping with a host authorization
95
+ check. The following calls illustrate the API inside authorized server handlers:
96
+
51
97
  ```ts
52
98
  import { ApiKeys } from "@vllnt/convex-api-keys";
53
99
  import { components } from "./_generated/api";
54
100
 
55
101
  const apiKeys = new ApiKeys(components.apiKeys, {
56
- prefix: "myapp", // key prefix (default: "vk")
102
+ prefix: "myapp", // key prefix (default: "vk")
57
103
  defaultType: "secret",
58
104
  });
59
105
 
@@ -77,32 +123,41 @@ if (!result.valid) {
77
123
  const { ownerId, scopes, tags, env, type, metadata, remaining } = result;
78
124
  ```
79
125
 
80
- See [docs/API.md](docs/API.md) for the full method reference, key format, and lifecycle.
126
+ See [docs/API.md](docs/API.md) for the full method reference, key format, and
127
+ lifecycle.
81
128
 
82
129
  ## API Reference
83
130
 
84
- | Method | Ctx | Description |
85
- |--------|-----|-------------|
86
- | `create(ctx, options)` | mutation | Create a new API key |
87
- | `validate(ctx, { key })` | mutation | Validate and track usage |
88
- | `revoke(ctx, { keyId, ownerId })` | mutation | Permanently revoke a key |
89
- | `revokeByTag(ctx, { ownerId, tag })` | mutation | Bulk revoke by tag |
90
- | `rotate(ctx, { keyId, ownerId, gracePeriodMs? })` | mutation | Rotate with grace period |
91
- | `update(ctx, { keyId, ownerId, name?, ... })` | mutation | Update metadata in-place |
92
- | `disable(ctx, { keyId, ownerId })` | mutation | Temporarily disable |
93
- | `enable(ctx, { keyId, ownerId })` | mutation | Re-enable a disabled key |
94
- | `configure(ctx, { ... })` | mutation | Runtime config (admin-only) |
95
- | `list(ctx, { ownerId, env?, status?, limit? })` | query | List keys (paginated, default 100) |
96
- | `listByTag(ctx, { ownerId, tag, limit? })` | query | Filter by tag |
97
- | `getUsage(ctx, { keyId, ownerId })` | query | Usage counter (O(1)) |
131
+ | Method | Ctx | Description |
132
+ | ------------------------------------------------- | -------- | -------------------------------------- |
133
+ | `create(ctx, options)` | mutation | Create a new API key |
134
+ | `validate(ctx, { key })` | mutation | Validate and track usage |
135
+ | `revoke(ctx, { keyId, ownerId })` | mutation | Permanently revoke a key |
136
+ | `revokeByTag(ctx, { ownerId, tag })` | mutation | Bulk revoke by tag |
137
+ | `rotate(ctx, { keyId, ownerId, gracePeriodMs? })` | mutation | Rotate with grace period |
138
+ | `update(ctx, { keyId, ownerId, name?, ... })` | mutation | Update metadata in-place |
139
+ | `disable(ctx, { keyId, ownerId })` | mutation | Temporarily disable |
140
+ | `enable(ctx, { keyId, ownerId })` | mutation | Re-enable a disabled key |
141
+ | `configure(ctx, { ... })` | mutation | Runtime config (admin-only) |
142
+ | `list(ctx, { ownerId, env?, status?, limit? })` | query | Bounded array (default 100; no cursor) |
143
+ | `listByTag(ctx, { ownerId, tag, limit? })` | query | Filter by tag |
144
+ | `getUsage(ctx, { keyId, ownerId })` | query | Usage counter (O(1)) |
145
+
146
+ `list` and `listByTag` return bounded arrays, not cursor-based pages; repeated
147
+ calls do not advance through the remaining keys. Authorize reads and mutations
148
+ in the host before supplying an `ownerId`.
98
149
 
99
150
  Full reference: [docs/API.md](docs/API.md).
100
151
 
101
152
  ## Security
102
153
 
103
- - Protects against **accidental cross-tenant bugs in honest host apps** — the `ownerId` check does NOT defend against a compromised host passing a forged `ownerId`.
104
- - Derive `ownerId` from your own auth layer (e.g. `ctx.auth.getUserIdentity()`) before passing it in.
105
- - Raw keys are never stored — only the SHA-256 hash; the raw value is returned once at creation.
154
+ - Protects against **accidental cross-tenant bugs in honest host apps** — the
155
+ `ownerId` check does NOT defend against a compromised host passing a forged
156
+ `ownerId`.
157
+ - Derive `ownerId` from your own auth layer (e.g. `ctx.auth.getUserIdentity()`)
158
+ before passing it in.
159
+ - Raw keys are never stored — only the SHA-256 hash; the raw value is returned
160
+ once at creation.
106
161
 
107
162
  See [docs/API.md](docs/API.md).
108
163
 
@@ -120,13 +175,16 @@ shardedCounterTest.register(t, "apiKeys/shardedCounter");
120
175
 
121
176
  ## Contributing
122
177
 
123
- See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup, testing, and PR guidelines.
178
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup, testing, and PR
179
+ guidelines.
124
180
 
125
181
  ## Author
126
182
 
127
- Built by [bntvllnt](https://github.com/bntvllnt) · [bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
183
+ Built by [bntvllnt](https://github.com/bntvllnt) ·
184
+ [bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
128
185
 
129
- Part of the [@vllnt](https://github.com/vllnt) Convex component fleet — [vllnt.com](https://vllnt.com)
186
+ Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
187
+ [vllnt.com](https://vllnt.com)
130
188
 
131
189
  If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
132
190
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vllnt/convex-api-keys",
3
- "version": "0.2.1-canary.9b260a8",
3
+ "version": "0.2.1-canary.c478e3d",
4
4
  "description": "Convex component for secure API key management — create, validate, revoke, rotate, and track usage",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -43,7 +43,7 @@
43
43
  "test": "pnpm check:agents && vitest run --passWithNoTests",
44
44
  "test:watch": "vitest --typecheck --clearScreen false",
45
45
  "test:coverage": "vitest run --coverage --coverage.reporter=text",
46
- "generate:llms": "node -e \"const fs=require('fs');const files=['README.md','CHANGELOG.md','docs/API.md','src/shared.ts','src/client/types.ts','src/client/index.ts','src/component/schema.ts','src/component/validators.ts','src/component/mutations.ts','src/component/queries.ts','src/test.ts'];let out='# @vllnt/convex-api-keys — Full Source\\n\\nAuto-generated. Do not edit manually.\\n';for(const f of files){out+='\\n---\\n\\n## '+f+'\\n\\n';if(f.endsWith('.md')){out+=fs.readFileSync(f,'utf8')}else{out+='\\`\\`\\`ts\\n'+fs.readFileSync(f,'utf8')+'\\`\\`\\`'}out+='\\n'}fs.writeFileSync('llms-full.txt',out)\"",
46
+ "generate:llms": "node scripts/generate-llms.mjs",
47
47
  "preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test && pnpm typecheck && pnpm generate:llms",
48
48
  "prepublishOnly": "npm whoami || npm login",
49
49
  "alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
@@ -52,7 +52,7 @@
52
52
  "codegen": "pnpm convex codegen --component-dir ./src/component"
53
53
  },
54
54
  "dependencies": {
55
- "@convex-dev/sharded-counter": "^0.2.1"
55
+ "@convex-dev/sharded-counter": "^0.2.3"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "convex": "^1.45.0"
@@ -60,16 +60,16 @@
60
60
  "devDependencies": {
61
61
  "@edge-runtime/vm": "^5.0.0",
62
62
  "@typescript/native": "npm:typescript@7.0.2",
63
- "@vitest/coverage-v8": "^4.1.11",
63
+ "@vitest/coverage-v8": "^5.0.5",
64
64
  "@vllnt/eslint-config": "2.0.0",
65
65
  "@vllnt/typescript": "2.0.0",
66
- "convex": "^1.45.0",
67
- "convex-test": "^0.0.56",
66
+ "convex": "1.48.0",
67
+ "convex-test": "^0.0.65",
68
68
  "eslint": "^9.39.5",
69
- "prettier": "^3.9.6",
69
+ "prettier": "^3.9.10",
70
70
  "typescript": "npm:@typescript/typescript6@^6.0.2",
71
- "vite": "^8.2.2",
72
- "vitest": "^4.1.11"
71
+ "vite": "^8.3.5",
72
+ "vitest": "^5.0.5"
73
73
  },
74
74
  "homepage": "https://github.com/vllnt/convex-api-keys#readme",
75
75
  "bugs": {
@@ -100,5 +100,20 @@
100
100
  "authentication",
101
101
  "multi-tenant",
102
102
  "convex-component"
103
- ]
103
+ ],
104
+ "pnpm": {
105
+ "overrides": {
106
+ "brace-expansion@<1.1.21": "1.1.21",
107
+ "brace-expansion@>=4.0.0 <5.0.12": "5.0.12",
108
+ "deepmerge-ts@<8.0.0": "8.0.0",
109
+ "esbuild@>=0.27.3 <0.28.1": "0.28.1",
110
+ "handlebars@<4.7.10": "4.7.10",
111
+ "magic-string": "1.4.3",
112
+ "postcss": "8.5.31",
113
+ "rolldown": "1.2.15",
114
+ "source-map-js@<1.2.2": "1.2.2",
115
+ "turbo@<2.9.14": "2.9.14",
116
+ "zod@>=4.0.0 <4.6.6": "4.6.6"
117
+ }
118
+ }
104
119
  }