@vllnt/convex-api-keys 0.1.0 → 0.1.1-canary.f11443e
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 +44 -55
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/index.d.ts +69 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +82 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +59 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +41 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +141 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +6 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/mutations.d.ts +80 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +422 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +47 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +117 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +52 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +33 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +3 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +4 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/log.d.ts +11 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +15 -0
- package/dist/log.js.map +1 -0
- package/dist/shared.d.ts +38 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +98 -0
- package/dist/shared.js.map +1 -0
- package/package.json +20 -18
- package/src/client/index.ts +23 -38
- package/src/client/types.ts +2 -12
- package/src/component/_generated/component.ts +8 -11
- package/src/component/convex.config.ts +0 -6
- package/src/component/mutations.ts +130 -154
- package/src/component/queries.ts +56 -91
- package/src/component/schema.ts +0 -11
- package/src/shared.ts +53 -25
package/src/component/queries.ts
CHANGED
|
@@ -7,30 +7,52 @@ import { jsonValue } from "./validators.js";
|
|
|
7
7
|
|
|
8
8
|
const counter = new ShardedCounter(components.shardedCounter);
|
|
9
9
|
|
|
10
|
+
const DEFAULT_PAGE_SIZE = 100;
|
|
11
|
+
|
|
12
|
+
const keyItemValidator = v.object({
|
|
13
|
+
keyId: v.id("apiKeys"),
|
|
14
|
+
name: v.string(),
|
|
15
|
+
lookupPrefix: v.string(),
|
|
16
|
+
type: KEY_TYPE,
|
|
17
|
+
env: v.string(),
|
|
18
|
+
scopes: v.array(v.string()),
|
|
19
|
+
tags: v.array(v.string()),
|
|
20
|
+
status: KEY_STATUS,
|
|
21
|
+
metadata: v.optional(jsonValue),
|
|
22
|
+
remaining: v.optional(v.number()),
|
|
23
|
+
expiresAt: v.optional(v.number()),
|
|
24
|
+
createdAt: v.number(),
|
|
25
|
+
lastUsedAt: v.optional(v.number()),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function mapKey(k: any) {
|
|
29
|
+
return {
|
|
30
|
+
keyId: k._id,
|
|
31
|
+
name: k.name,
|
|
32
|
+
lookupPrefix: k.lookupPrefix,
|
|
33
|
+
type: k.type,
|
|
34
|
+
env: k.env,
|
|
35
|
+
scopes: k.scopes,
|
|
36
|
+
tags: k.tags,
|
|
37
|
+
status: k.status,
|
|
38
|
+
metadata: k.metadata,
|
|
39
|
+
remaining: k.remaining,
|
|
40
|
+
expiresAt: k.expiresAt,
|
|
41
|
+
createdAt: k._creationTime,
|
|
42
|
+
lastUsedAt: k.lastUsedAt,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
10
46
|
export const list = query({
|
|
11
47
|
args: {
|
|
12
48
|
ownerId: v.string(),
|
|
13
49
|
env: v.optional(v.string()),
|
|
14
50
|
status: v.optional(KEY_STATUS),
|
|
51
|
+
limit: v.optional(v.number()),
|
|
15
52
|
},
|
|
16
|
-
returns: v.array(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
name: v.string(),
|
|
20
|
-
lookupPrefix: v.string(),
|
|
21
|
-
type: KEY_TYPE,
|
|
22
|
-
env: v.string(),
|
|
23
|
-
scopes: v.array(v.string()),
|
|
24
|
-
tags: v.array(v.string()),
|
|
25
|
-
status: KEY_STATUS,
|
|
26
|
-
metadata: v.optional(jsonValue),
|
|
27
|
-
remaining: v.optional(v.number()),
|
|
28
|
-
expiresAt: v.optional(v.number()),
|
|
29
|
-
createdAt: v.number(),
|
|
30
|
-
lastUsedAt: v.optional(v.number()),
|
|
31
|
-
}),
|
|
32
|
-
),
|
|
33
|
-
handler: async (ctx, { ownerId, env, status }) => {
|
|
53
|
+
returns: v.array(keyItemValidator),
|
|
54
|
+
handler: async (ctx, { ownerId, env, status, limit }) => {
|
|
55
|
+
const pageSize = limit ?? DEFAULT_PAGE_SIZE;
|
|
34
56
|
let keysQuery;
|
|
35
57
|
if (env) {
|
|
36
58
|
keysQuery = ctx.db
|
|
@@ -51,23 +73,8 @@ export const list = query({
|
|
|
51
73
|
.withIndex("by_owner_status", (q) => q.eq("ownerId", ownerId));
|
|
52
74
|
}
|
|
53
75
|
|
|
54
|
-
const keys = await keysQuery.
|
|
55
|
-
|
|
56
|
-
return keys.map((k) => ({
|
|
57
|
-
keyId: k._id,
|
|
58
|
-
name: k.name,
|
|
59
|
-
lookupPrefix: k.lookupPrefix,
|
|
60
|
-
type: k.type,
|
|
61
|
-
env: k.env,
|
|
62
|
-
scopes: k.scopes,
|
|
63
|
-
tags: k.tags,
|
|
64
|
-
status: k.status,
|
|
65
|
-
metadata: k.metadata,
|
|
66
|
-
remaining: k.remaining,
|
|
67
|
-
expiresAt: k.expiresAt,
|
|
68
|
-
createdAt: k._creationTime,
|
|
69
|
-
lastUsedAt: k.lastUsedAt,
|
|
70
|
-
}));
|
|
76
|
+
const keys = await keysQuery.take(pageSize);
|
|
77
|
+
return keys.map(mapKey);
|
|
71
78
|
},
|
|
72
79
|
});
|
|
73
80
|
|
|
@@ -75,88 +82,46 @@ export const listByTag = query({
|
|
|
75
82
|
args: {
|
|
76
83
|
ownerId: v.string(),
|
|
77
84
|
tag: v.string(),
|
|
85
|
+
limit: v.optional(v.number()),
|
|
78
86
|
},
|
|
79
|
-
returns: v.array(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
lookupPrefix: v.string(),
|
|
84
|
-
type: KEY_TYPE,
|
|
85
|
-
env: v.string(),
|
|
86
|
-
scopes: v.array(v.string()),
|
|
87
|
-
tags: v.array(v.string()),
|
|
88
|
-
status: KEY_STATUS,
|
|
89
|
-
metadata: v.optional(jsonValue),
|
|
90
|
-
remaining: v.optional(v.number()),
|
|
91
|
-
expiresAt: v.optional(v.number()),
|
|
92
|
-
createdAt: v.number(),
|
|
93
|
-
lastUsedAt: v.optional(v.number()),
|
|
94
|
-
}),
|
|
95
|
-
),
|
|
96
|
-
handler: async (ctx, { ownerId, tag }) => {
|
|
97
|
-
const keys = await ctx.db
|
|
87
|
+
returns: v.array(keyItemValidator),
|
|
88
|
+
handler: async (ctx, { ownerId, tag, limit }) => {
|
|
89
|
+
const pageSize = limit ?? DEFAULT_PAGE_SIZE;
|
|
90
|
+
const allKeys = await ctx.db
|
|
98
91
|
.query("apiKeys")
|
|
99
92
|
.withIndex("by_owner_status", (q) => q.eq("ownerId", ownerId))
|
|
100
93
|
.collect();
|
|
101
94
|
|
|
102
|
-
return
|
|
95
|
+
return allKeys
|
|
103
96
|
.filter((k) => k.tags.includes(tag))
|
|
104
|
-
.
|
|
105
|
-
|
|
106
|
-
name: k.name,
|
|
107
|
-
lookupPrefix: k.lookupPrefix,
|
|
108
|
-
type: k.type,
|
|
109
|
-
env: k.env,
|
|
110
|
-
scopes: k.scopes,
|
|
111
|
-
tags: k.tags,
|
|
112
|
-
status: k.status,
|
|
113
|
-
metadata: k.metadata,
|
|
114
|
-
remaining: k.remaining,
|
|
115
|
-
expiresAt: k.expiresAt,
|
|
116
|
-
createdAt: k._creationTime,
|
|
117
|
-
lastUsedAt: k.lastUsedAt,
|
|
118
|
-
}));
|
|
97
|
+
.slice(0, pageSize)
|
|
98
|
+
.map(mapKey);
|
|
119
99
|
},
|
|
120
100
|
});
|
|
121
101
|
|
|
122
102
|
export const getUsage = query({
|
|
123
103
|
args: {
|
|
124
104
|
keyId: v.id("apiKeys"),
|
|
125
|
-
|
|
126
|
-
v.object({
|
|
127
|
-
start: v.number(),
|
|
128
|
-
end: v.number(),
|
|
129
|
-
}),
|
|
130
|
-
),
|
|
105
|
+
ownerId: v.string(),
|
|
131
106
|
},
|
|
132
107
|
returns: v.object({
|
|
133
108
|
total: v.number(),
|
|
134
109
|
remaining: v.optional(v.number()),
|
|
135
|
-
lastUsedAt: v.optional(v.number()),
|
|
136
110
|
}),
|
|
137
|
-
handler: async (ctx, { keyId,
|
|
111
|
+
handler: async (ctx, { keyId, ownerId }) => {
|
|
138
112
|
const key = await ctx.db.get(keyId);
|
|
139
113
|
if (!key) {
|
|
140
114
|
throw new Error("key not found");
|
|
141
115
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (period) {
|
|
145
|
-
const events = await ctx.db
|
|
146
|
-
.query("apiKeyEvents")
|
|
147
|
-
.withIndex("by_key", (q) =>
|
|
148
|
-
q.eq("keyId", keyId).gte("timestamp", period.start).lte("timestamp", period.end),
|
|
149
|
-
)
|
|
150
|
-
.collect();
|
|
151
|
-
total = events.filter((e) => e.eventType === "key.validated").length;
|
|
152
|
-
} else {
|
|
153
|
-
total = await counter.count(ctx, keyId);
|
|
116
|
+
if (key.ownerId !== ownerId) {
|
|
117
|
+
throw new Error("unauthorized: key does not belong to owner");
|
|
154
118
|
}
|
|
155
119
|
|
|
120
|
+
const total = await counter.count(ctx, keyId);
|
|
121
|
+
|
|
156
122
|
return {
|
|
157
123
|
total,
|
|
158
124
|
remaining: key.remaining,
|
|
159
|
-
lastUsedAt: key.lastUsedAt,
|
|
160
125
|
};
|
|
161
126
|
},
|
|
162
127
|
});
|
package/src/component/schema.ts
CHANGED
|
@@ -27,17 +27,6 @@ export default defineSchema({
|
|
|
27
27
|
.index("by_owner_status", ["ownerId", "status"])
|
|
28
28
|
.index("by_owner_env", ["ownerId", "env", "status"]),
|
|
29
29
|
|
|
30
|
-
apiKeyEvents: defineTable({
|
|
31
|
-
keyId: v.id("apiKeys"),
|
|
32
|
-
ownerId: v.string(),
|
|
33
|
-
eventType: v.string(),
|
|
34
|
-
reason: v.optional(v.string()),
|
|
35
|
-
metadata: v.optional(jsonValue),
|
|
36
|
-
timestamp: v.number(),
|
|
37
|
-
})
|
|
38
|
-
.index("by_key", ["keyId", "timestamp"])
|
|
39
|
-
.index("by_owner", ["ownerId", "timestamp"]),
|
|
40
|
-
|
|
41
30
|
config: defineTable({
|
|
42
31
|
cleanupIntervalMs: v.optional(v.number()),
|
|
43
32
|
defaultExpiryMs: v.optional(v.number()),
|
package/src/shared.ts
CHANGED
|
@@ -26,32 +26,59 @@ export const TERMINAL_STATUSES: ReadonlySet<KeyStatus> = new Set([
|
|
|
26
26
|
]);
|
|
27
27
|
|
|
28
28
|
export const TAG_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9-]*$/;
|
|
29
|
+
export const KEY_PREFIX_PATTERN = /^[a-zA-Z0-9]+$/;
|
|
30
|
+
export const ENV_PATTERN = /^[a-zA-Z0-9-]+$/;
|
|
31
|
+
|
|
32
|
+
export const MAX_METADATA_SIZE = 4096;
|
|
33
|
+
export const MAX_SCOPES = 50;
|
|
34
|
+
export const MAX_TAGS = 20;
|
|
35
|
+
export const MAX_STRING_LENGTH = 256;
|
|
36
|
+
|
|
37
|
+
export function validateKeyPrefix(prefix: string): void {
|
|
38
|
+
if (!KEY_PREFIX_PATTERN.test(prefix)) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`Invalid keyPrefix "${prefix}": must match ^[a-zA-Z0-9]+$ (alphanumeric only, no underscores)`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
if (prefix.length > MAX_STRING_LENGTH) {
|
|
44
|
+
throw new Error(`keyPrefix must be <= ${MAX_STRING_LENGTH} characters`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function validateEnv(env: string): void {
|
|
49
|
+
if (!ENV_PATTERN.test(env)) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Invalid env "${env}": must match ^[a-zA-Z0-9-]+$ (no underscores — would break key parsing)`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (env.length > MAX_STRING_LENGTH) {
|
|
55
|
+
throw new Error(`env must be <= ${MAX_STRING_LENGTH} characters`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function validateSizeLimits(args: {
|
|
60
|
+
metadata?: unknown;
|
|
61
|
+
scopes?: string[];
|
|
62
|
+
tags?: string[];
|
|
63
|
+
name?: string;
|
|
64
|
+
}): void {
|
|
65
|
+
if (args.metadata !== undefined) {
|
|
66
|
+
const size = JSON.stringify(args.metadata).length;
|
|
67
|
+
if (size > MAX_METADATA_SIZE) {
|
|
68
|
+
throw new Error(`metadata must be <= ${MAX_METADATA_SIZE} bytes (got ${size})`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (args.scopes && args.scopes.length > MAX_SCOPES) {
|
|
72
|
+
throw new Error(`scopes must have <= ${MAX_SCOPES} entries (got ${args.scopes.length})`);
|
|
73
|
+
}
|
|
74
|
+
if (args.tags && args.tags.length > MAX_TAGS) {
|
|
75
|
+
throw new Error(`tags must have <= ${MAX_TAGS} entries (got ${args.tags.length})`);
|
|
76
|
+
}
|
|
77
|
+
if (args.name && args.name.length > MAX_STRING_LENGTH) {
|
|
78
|
+
throw new Error(`name must be <= ${MAX_STRING_LENGTH} characters`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
29
81
|
|
|
30
|
-
export const EVENT_TYPE = v.union(
|
|
31
|
-
v.literal("key.created"),
|
|
32
|
-
v.literal("key.validated"),
|
|
33
|
-
v.literal("key.validate_failed"),
|
|
34
|
-
v.literal("key.revoked"),
|
|
35
|
-
v.literal("key.rotated"),
|
|
36
|
-
v.literal("key.expired"),
|
|
37
|
-
v.literal("key.exhausted"),
|
|
38
|
-
v.literal("key.disabled"),
|
|
39
|
-
v.literal("key.enabled"),
|
|
40
|
-
v.literal("key.updated"),
|
|
41
|
-
v.literal("key.rate_limited"),
|
|
42
|
-
);
|
|
43
|
-
export type EventType =
|
|
44
|
-
| "key.created"
|
|
45
|
-
| "key.validated"
|
|
46
|
-
| "key.validate_failed"
|
|
47
|
-
| "key.revoked"
|
|
48
|
-
| "key.rotated"
|
|
49
|
-
| "key.expired"
|
|
50
|
-
| "key.exhausted"
|
|
51
|
-
| "key.disabled"
|
|
52
|
-
| "key.enabled"
|
|
53
|
-
| "key.updated"
|
|
54
|
-
| "key.rate_limited";
|
|
55
82
|
|
|
56
83
|
export function validateTag(tag: string): void {
|
|
57
84
|
if (!TAG_PATTERN.test(tag)) {
|
|
@@ -111,6 +138,7 @@ export function timingSafeEqual(a: string, b: string): boolean {
|
|
|
111
138
|
return result === 0;
|
|
112
139
|
}
|
|
113
140
|
|
|
141
|
+
/** Compute SHA-256 hash of input, returned as lowercase hex string. */
|
|
114
142
|
export async function sha256Hex(input: string): Promise<string> {
|
|
115
143
|
const encoder = new TextEncoder();
|
|
116
144
|
const data = encoder.encode(input);
|