@vllnt/convex-tokens 0.1.0-canary.4bc4600
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/LICENSE +21 -0
- package/README.md +127 -0
- package/dist/client/index.d.ts +116 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +92 -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 +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +40 -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 +71 -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 +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +10 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +13 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/mutations.d.ts +49 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +141 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +51 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +84 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +30 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +23 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +45 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +24 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +35 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +30 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +60 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +69 -0
- package/dist/shared.js.map +1 -0
- package/package.json +112 -0
- package/src/client/index.ts +220 -0
- package/src/client/types.ts +60 -0
- package/src/component/_generated/api.ts +56 -0
- package/src/component/_generated/component.ts +93 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/crons.ts +19 -0
- package/src/component/mutations.ts +158 -0
- package/src/component/queries.ts +90 -0
- package/src/component/schema.ts +23 -0
- package/src/component/validators.ts +28 -0
- package/src/react/index.test.tsx +86 -0
- package/src/react/index.tsx +41 -0
- package/src/shared.ts +98 -0
- package/src/test.ts +12 -0
- package/src/unit.test.ts +48 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandboxed tables — the token registry's own concern. Only the SHA-256 (or
|
|
3
|
+
* SHA-512) hash of a token is stored (`tokenHash`); the raw secret is never
|
|
4
|
+
* persisted. `scope` namespaces tokens and `resourceRef` is an opaque host-owned
|
|
5
|
+
* reference (never assume its shape).
|
|
6
|
+
*/
|
|
7
|
+
declare const _default: import("convex/server").SchemaDefinition<{
|
|
8
|
+
tokens: import("convex/server").TableDefinition<import("convex/values").VObject<{
|
|
9
|
+
resourceRef?: string | undefined;
|
|
10
|
+
tokenHash: string;
|
|
11
|
+
scope: string;
|
|
12
|
+
revoked: boolean;
|
|
13
|
+
expiresAt: number;
|
|
14
|
+
createdAt: number;
|
|
15
|
+
}, {
|
|
16
|
+
tokenHash: import("convex/values").VString<string, "required">;
|
|
17
|
+
scope: import("convex/values").VString<string, "required">;
|
|
18
|
+
resourceRef: import("convex/values").VString<string | undefined, "optional">;
|
|
19
|
+
revoked: import("convex/values").VBoolean<boolean, "required">;
|
|
20
|
+
expiresAt: import("convex/values").VFloat64<number, "required">;
|
|
21
|
+
createdAt: import("convex/values").VFloat64<number, "required">;
|
|
22
|
+
}, "required", "tokenHash" | "scope" | "resourceRef" | "revoked" | "expiresAt" | "createdAt">, {
|
|
23
|
+
by_hash: ["tokenHash", "_creationTime"];
|
|
24
|
+
by_expires: ["expiresAt", "_creationTime"];
|
|
25
|
+
by_scope_resource: ["scope", "resourceRef", "_creationTime"];
|
|
26
|
+
by_revoked_created: ["revoked", "createdAt", "_creationTime"];
|
|
27
|
+
}, {}, {}>;
|
|
28
|
+
}, true>;
|
|
29
|
+
export default _default;
|
|
30
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;AACH,wBAaG"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
/**
|
|
4
|
+
* Sandboxed tables — the token registry's own concern. Only the SHA-256 (or
|
|
5
|
+
* SHA-512) hash of a token is stored (`tokenHash`); the raw secret is never
|
|
6
|
+
* persisted. `scope` namespaces tokens and `resourceRef` is an opaque host-owned
|
|
7
|
+
* reference (never assume its shape).
|
|
8
|
+
*/
|
|
9
|
+
export default defineSchema({
|
|
10
|
+
tokens: defineTable({
|
|
11
|
+
tokenHash: v.string(),
|
|
12
|
+
scope: v.string(),
|
|
13
|
+
resourceRef: v.optional(v.string()),
|
|
14
|
+
revoked: v.boolean(),
|
|
15
|
+
expiresAt: v.number(),
|
|
16
|
+
createdAt: v.number(),
|
|
17
|
+
})
|
|
18
|
+
.index("by_hash", ["tokenHash"])
|
|
19
|
+
.index("by_expires", ["expiresAt"])
|
|
20
|
+
.index("by_scope_resource", ["scope", "resourceRef"])
|
|
21
|
+
.index("by_revoked_created", ["revoked", "createdAt"]),
|
|
22
|
+
});
|
|
23
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;;GAKG;AACH,eAAe,YAAY,CAAC;IAC1B,MAAM,EAAE,WAAW,CAAC;QAClB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;QACrB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,WAAW,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QACnC,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;QACpB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;QACrB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;KACtB,CAAC;SACC,KAAK,CAAC,SAAS,EAAE,CAAC,WAAW,CAAC,CAAC;SAC/B,KAAK,CAAC,YAAY,EAAE,CAAC,WAAW,CAAC,CAAC;SAClC,KAAK,CAAC,mBAAmB,EAAE,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;SACpD,KAAK,CAAC,oBAAoB,EAAE,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;CACzD,CAAC,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result of validating a token hash.
|
|
3
|
+
*
|
|
4
|
+
* Discriminated union on `valid`:
|
|
5
|
+
* - `{ valid: true, resourceRef? }` — the token is live and carries an optional
|
|
6
|
+
* opaque host-owned reference.
|
|
7
|
+
* - `{ valid: false }` — the token is unknown, wrong-scope, revoked, or expired.
|
|
8
|
+
* The failed branch structurally cannot carry `resourceRef`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const validateResult: import("convex/values").VUnion<{
|
|
11
|
+
resourceRef?: string | undefined;
|
|
12
|
+
valid: true;
|
|
13
|
+
} | {
|
|
14
|
+
valid: false;
|
|
15
|
+
}, [import("convex/values").VObject<{
|
|
16
|
+
resourceRef?: string | undefined;
|
|
17
|
+
valid: true;
|
|
18
|
+
}, {
|
|
19
|
+
valid: import("convex/values").VLiteral<true, "required">;
|
|
20
|
+
resourceRef: import("convex/values").VString<string | undefined, "optional">;
|
|
21
|
+
}, "required", "resourceRef" | "valid">, import("convex/values").VObject<{
|
|
22
|
+
valid: false;
|
|
23
|
+
}, {
|
|
24
|
+
valid: import("convex/values").VLiteral<false, "required">;
|
|
25
|
+
}, "required", "valid">], "required", "resourceRef" | "valid">;
|
|
26
|
+
/**
|
|
27
|
+
* Safe, leak-free projection of a token row for a management surface. NEVER
|
|
28
|
+
* includes `tokenHash` — only lifecycle metadata a UI may display.
|
|
29
|
+
*/
|
|
30
|
+
export declare const tokenMetadata: import("convex/values").VObject<{
|
|
31
|
+
resourceRef?: string | undefined;
|
|
32
|
+
scope: string;
|
|
33
|
+
id: import("convex/values").GenericId<"tokens">;
|
|
34
|
+
revoked: boolean;
|
|
35
|
+
expiresAt: number;
|
|
36
|
+
createdAt: number;
|
|
37
|
+
}, {
|
|
38
|
+
id: import("convex/values").VId<import("convex/values").GenericId<"tokens">, "required">;
|
|
39
|
+
scope: import("convex/values").VString<string, "required">;
|
|
40
|
+
resourceRef: import("convex/values").VString<string | undefined, "optional">;
|
|
41
|
+
revoked: import("convex/values").VBoolean<boolean, "required">;
|
|
42
|
+
expiresAt: import("convex/values").VFloat64<number, "required">;
|
|
43
|
+
createdAt: import("convex/values").VFloat64<number, "required">;
|
|
44
|
+
}, "required", "scope" | "resourceRef" | "id" | "revoked" | "expiresAt" | "createdAt">;
|
|
45
|
+
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;8DAG1B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;sFAOxB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
/**
|
|
3
|
+
* Result of validating a token hash.
|
|
4
|
+
*
|
|
5
|
+
* Discriminated union on `valid`:
|
|
6
|
+
* - `{ valid: true, resourceRef? }` — the token is live and carries an optional
|
|
7
|
+
* opaque host-owned reference.
|
|
8
|
+
* - `{ valid: false }` — the token is unknown, wrong-scope, revoked, or expired.
|
|
9
|
+
* The failed branch structurally cannot carry `resourceRef`.
|
|
10
|
+
*/
|
|
11
|
+
export const validateResult = v.union(v.object({ valid: v.literal(true), resourceRef: v.optional(v.string()) }), v.object({ valid: v.literal(false) }));
|
|
12
|
+
/**
|
|
13
|
+
* Safe, leak-free projection of a token row for a management surface. NEVER
|
|
14
|
+
* includes `tokenHash` — only lifecycle metadata a UI may display.
|
|
15
|
+
*/
|
|
16
|
+
export const tokenMetadata = v.object({
|
|
17
|
+
id: v.id("tokens"),
|
|
18
|
+
scope: v.string(),
|
|
19
|
+
resourceRef: v.optional(v.string()),
|
|
20
|
+
revoked: v.boolean(),
|
|
21
|
+
expiresAt: v.number(),
|
|
22
|
+
createdAt: v.number(),
|
|
23
|
+
});
|
|
24
|
+
//# sourceMappingURL=validators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CACnC,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EACzE,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CACtC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC;IAClB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;IACjB,WAAW,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACnC,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;IACpB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;IACrB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional, tree-shakeable React front-tooling for `@vllnt/convex-tokens`.
|
|
3
|
+
*
|
|
4
|
+
* Thin reactive hook over `useQuery` from `convex/react`. It takes the HOST's
|
|
5
|
+
* re-exported `list` query reference plus its args — the component never imports
|
|
6
|
+
* the host `api`. `react` and `convex/react` are optional peer deps: a
|
|
7
|
+
* backend-only consumer pulls none of this code.
|
|
8
|
+
*
|
|
9
|
+
* NO-LEAK CONTRACT: this layer exposes ONLY non-secret token METADATA (id,
|
|
10
|
+
* scope, resourceRef, revoked, expiresAt, createdAt). It NEVER surfaces a raw
|
|
11
|
+
* token or its hash. Minting returns the raw secret exactly once, from the
|
|
12
|
+
* server-side `mint` mutation the host wraps — that mint-once raw value must
|
|
13
|
+
* never be persisted or logged, and it is intentionally NOT exposed by any hook
|
|
14
|
+
* here.
|
|
15
|
+
*/
|
|
16
|
+
import type { FunctionReference } from "convex/server";
|
|
17
|
+
import type { TokenMetadata } from "../client/types.js";
|
|
18
|
+
/**
|
|
19
|
+
* Reactive list of token METADATA for a management surface, scoped by an opaque
|
|
20
|
+
* host `resourceRef` (and an optional namespace). Wraps the `list` query's
|
|
21
|
+
* leak-free projection — never the hash, never a raw token.
|
|
22
|
+
*
|
|
23
|
+
* @param listRef - The host's re-exported `tokens.list` query reference.
|
|
24
|
+
* @param args - `{ scope?, resourceRef }`: optional namespace + the opaque
|
|
25
|
+
* host-owned reference to list tokens for.
|
|
26
|
+
* @returns The token metadata array, or `undefined` while the query loads.
|
|
27
|
+
*/
|
|
28
|
+
export declare function useTokens(listRef: FunctionReference<"query", "public", {
|
|
29
|
+
scope?: string;
|
|
30
|
+
resourceRef: string;
|
|
31
|
+
}, TokenMetadata[]>, args: {
|
|
32
|
+
scope?: string;
|
|
33
|
+
resourceRef: string;
|
|
34
|
+
}): TokenMetadata[] | undefined;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAExD;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CACvB,OAAO,EAAE,iBAAiB,CACxB,OAAO,EACP,QAAQ,EACR;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,EACvC,aAAa,EAAE,CAChB,EACD,IAAI,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GAC5C,aAAa,EAAE,GAAG,SAAS,CAE7B"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional, tree-shakeable React front-tooling for `@vllnt/convex-tokens`.
|
|
3
|
+
*
|
|
4
|
+
* Thin reactive hook over `useQuery` from `convex/react`. It takes the HOST's
|
|
5
|
+
* re-exported `list` query reference plus its args — the component never imports
|
|
6
|
+
* the host `api`. `react` and `convex/react` are optional peer deps: a
|
|
7
|
+
* backend-only consumer pulls none of this code.
|
|
8
|
+
*
|
|
9
|
+
* NO-LEAK CONTRACT: this layer exposes ONLY non-secret token METADATA (id,
|
|
10
|
+
* scope, resourceRef, revoked, expiresAt, createdAt). It NEVER surfaces a raw
|
|
11
|
+
* token or its hash. Minting returns the raw secret exactly once, from the
|
|
12
|
+
* server-side `mint` mutation the host wraps — that mint-once raw value must
|
|
13
|
+
* never be persisted or logged, and it is intentionally NOT exposed by any hook
|
|
14
|
+
* here.
|
|
15
|
+
*/
|
|
16
|
+
import { useQuery } from "convex/react";
|
|
17
|
+
/**
|
|
18
|
+
* Reactive list of token METADATA for a management surface, scoped by an opaque
|
|
19
|
+
* host `resourceRef` (and an optional namespace). Wraps the `list` query's
|
|
20
|
+
* leak-free projection — never the hash, never a raw token.
|
|
21
|
+
*
|
|
22
|
+
* @param listRef - The host's re-exported `tokens.list` query reference.
|
|
23
|
+
* @param args - `{ scope?, resourceRef }`: optional namespace + the opaque
|
|
24
|
+
* host-owned reference to list tokens for.
|
|
25
|
+
* @returns The token metadata array, or `undefined` while the query loads.
|
|
26
|
+
*/
|
|
27
|
+
export function useTokens(listRef, args) {
|
|
28
|
+
return useQuery(listRef, args);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAGxC;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS,CACvB,OAKC,EACD,IAA6C;IAE7C,OAAO,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AACjC,CAAC"}
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** Shared constants + pure utilities used by both `client/` and `component/`. */
|
|
2
|
+
export declare const COMPONENT_NAME = "tokens";
|
|
3
|
+
/** Default namespace when the host does not scope a token. */
|
|
4
|
+
export declare const DEFAULT_SCOPE = "global";
|
|
5
|
+
/** Default token lifetime: 24 hours. */
|
|
6
|
+
export declare const DEFAULT_TTL_MS = 86400000;
|
|
7
|
+
/** Floor for a clamped TTL: 1 minute (a minted token always outlives a `Date.now()` test). */
|
|
8
|
+
export declare const MIN_TTL_MS = 60000;
|
|
9
|
+
/** Default ceiling for a clamped TTL: 30 days. */
|
|
10
|
+
export declare const DEFAULT_MAX_TTL_MS = 2592000000;
|
|
11
|
+
/** Default entropy for a generated raw token, in bytes (48 hex chars). */
|
|
12
|
+
export declare const DEFAULT_TOKEN_BYTES = 24;
|
|
13
|
+
/** Default retention for a revoked token before the prune sweep drops it: 24 hours. */
|
|
14
|
+
export declare const DEFAULT_REVOKED_RETENTION_MS = 86400000;
|
|
15
|
+
/** Max rows a single prune mutation deletes before self-rescheduling. */
|
|
16
|
+
export declare const PRUNE_BATCH = 256;
|
|
17
|
+
/** Hard ceiling on the `limit` argument of `list` queries. */
|
|
18
|
+
export declare const LIST_LIMIT_MAX = 1000;
|
|
19
|
+
/**
|
|
20
|
+
* Supported digest algorithms for {@link hashToken}. Fixed per mount — changing
|
|
21
|
+
* it invalidates every existing hash, so a deployment picks one and keeps it.
|
|
22
|
+
*/
|
|
23
|
+
export type HashAlgo = "SHA-256" | "SHA-512";
|
|
24
|
+
/** Default digest algorithm. */
|
|
25
|
+
export declare const DEFAULT_HASH_ALGO: HashAlgo;
|
|
26
|
+
/** Opaque host-supplied resource reference. Never assume its shape or source. */
|
|
27
|
+
export type ResourceRef = string;
|
|
28
|
+
/**
|
|
29
|
+
* Hex digest of `raw` under `algo` (default SHA-256). The component stores only
|
|
30
|
+
* this hash — the raw token is hashed by the client and never travels to or
|
|
31
|
+
* rests in the component. Uses Web Crypto (`crypto.subtle`), a global in
|
|
32
|
+
* edge-runtime and Node >=18.
|
|
33
|
+
*
|
|
34
|
+
* @param raw - The raw secret token to hash.
|
|
35
|
+
* @param algo - Digest algorithm; fixed per mount. Default `"SHA-256"`.
|
|
36
|
+
* @returns The lowercase hex digest.
|
|
37
|
+
*/
|
|
38
|
+
export declare function hashToken(raw: string, algo?: HashAlgo): Promise<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Generate a cryptographically random raw token as a hex string of `bytes`
|
|
41
|
+
* bytes (`2 * bytes` hex chars). Uses `crypto.getRandomValues` (a global).
|
|
42
|
+
*
|
|
43
|
+
* @param bytes - Entropy in bytes. Default {@link DEFAULT_TOKEN_BYTES}.
|
|
44
|
+
* @returns The hex-encoded random token.
|
|
45
|
+
*/
|
|
46
|
+
export declare function generateToken(bytes?: number): string;
|
|
47
|
+
/**
|
|
48
|
+
* Clamp `ttlMs` into `[minMs, maxMs]`. A non-finite `ttlMs` (NaN / Infinity)
|
|
49
|
+
* falls back to `fallbackMs`; a non-finite `fallbackMs` falls back to `minMs`.
|
|
50
|
+
* The result is therefore always finite and >= `minMs`, so a malformed request
|
|
51
|
+
* can never produce a non-finite `expiresAt` (the expiry invariant holds).
|
|
52
|
+
*
|
|
53
|
+
* @param ttlMs - Requested lifetime in ms.
|
|
54
|
+
* @param minMs - Lower bound (also the ultimate finite fallback).
|
|
55
|
+
* @param maxMs - Upper bound.
|
|
56
|
+
* @param fallbackMs - Used when `ttlMs` is not finite.
|
|
57
|
+
* @returns The clamped, finite TTL.
|
|
58
|
+
*/
|
|
59
|
+
export declare function clampTtl(ttlMs: number, minMs: number, maxMs: number, fallbackMs: number): number;
|
|
60
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAEjF,eAAO,MAAM,cAAc,WAAW,CAAC;AAEvC,8DAA8D;AAC9D,eAAO,MAAM,aAAa,WAAW,CAAC;AAEtC,wCAAwC;AACxC,eAAO,MAAM,cAAc,WAAa,CAAC;AAEzC,8FAA8F;AAC9F,eAAO,MAAM,UAAU,QAAS,CAAC;AAEjC,kDAAkD;AAClD,eAAO,MAAM,kBAAkB,aAAgB,CAAC;AAEhD,0EAA0E;AAC1E,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAEtC,uFAAuF;AACvF,eAAO,MAAM,4BAA4B,WAAa,CAAC;AAEvD,yEAAyE;AACzE,eAAO,MAAM,WAAW,MAAM,CAAC;AAE/B,8DAA8D;AAC9D,eAAO,MAAM,cAAc,OAAO,CAAC;AAEnC;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,SAAS,CAAC;AAE7C,gCAAgC;AAChC,eAAO,MAAM,iBAAiB,EAAE,QAAoB,CAAC;AAErD,iFAAiF;AACjF,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AAEjC;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAC7B,GAAG,EAAE,MAAM,EACX,IAAI,GAAE,QAA4B,GACjC,OAAO,CAAC,MAAM,CAAC,CAMjB;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,SAAsB,GAAG,MAAM,CAMjE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CACtB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,MAAM,GACjB,MAAM,CAIR"}
|
package/dist/shared.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** Shared constants + pure utilities used by both `client/` and `component/`. */
|
|
2
|
+
export const COMPONENT_NAME = "tokens";
|
|
3
|
+
/** Default namespace when the host does not scope a token. */
|
|
4
|
+
export const DEFAULT_SCOPE = "global";
|
|
5
|
+
/** Default token lifetime: 24 hours. */
|
|
6
|
+
export const DEFAULT_TTL_MS = 86_400_000;
|
|
7
|
+
/** Floor for a clamped TTL: 1 minute (a minted token always outlives a `Date.now()` test). */
|
|
8
|
+
export const MIN_TTL_MS = 60_000;
|
|
9
|
+
/** Default ceiling for a clamped TTL: 30 days. */
|
|
10
|
+
export const DEFAULT_MAX_TTL_MS = 2_592_000_000;
|
|
11
|
+
/** Default entropy for a generated raw token, in bytes (48 hex chars). */
|
|
12
|
+
export const DEFAULT_TOKEN_BYTES = 24;
|
|
13
|
+
/** Default retention for a revoked token before the prune sweep drops it: 24 hours. */
|
|
14
|
+
export const DEFAULT_REVOKED_RETENTION_MS = 86_400_000;
|
|
15
|
+
/** Max rows a single prune mutation deletes before self-rescheduling. */
|
|
16
|
+
export const PRUNE_BATCH = 256;
|
|
17
|
+
/** Hard ceiling on the `limit` argument of `list` queries. */
|
|
18
|
+
export const LIST_LIMIT_MAX = 1000;
|
|
19
|
+
/** Default digest algorithm. */
|
|
20
|
+
export const DEFAULT_HASH_ALGO = "SHA-256";
|
|
21
|
+
/**
|
|
22
|
+
* Hex digest of `raw` under `algo` (default SHA-256). The component stores only
|
|
23
|
+
* this hash — the raw token is hashed by the client and never travels to or
|
|
24
|
+
* rests in the component. Uses Web Crypto (`crypto.subtle`), a global in
|
|
25
|
+
* edge-runtime and Node >=18.
|
|
26
|
+
*
|
|
27
|
+
* @param raw - The raw secret token to hash.
|
|
28
|
+
* @param algo - Digest algorithm; fixed per mount. Default `"SHA-256"`.
|
|
29
|
+
* @returns The lowercase hex digest.
|
|
30
|
+
*/
|
|
31
|
+
export async function hashToken(raw, algo = DEFAULT_HASH_ALGO) {
|
|
32
|
+
const bytes = new TextEncoder().encode(raw);
|
|
33
|
+
const digest = await crypto.subtle.digest(algo, bytes);
|
|
34
|
+
return Array.from(new Uint8Array(digest))
|
|
35
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
36
|
+
.join("");
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Generate a cryptographically random raw token as a hex string of `bytes`
|
|
40
|
+
* bytes (`2 * bytes` hex chars). Uses `crypto.getRandomValues` (a global).
|
|
41
|
+
*
|
|
42
|
+
* @param bytes - Entropy in bytes. Default {@link DEFAULT_TOKEN_BYTES}.
|
|
43
|
+
* @returns The hex-encoded random token.
|
|
44
|
+
*/
|
|
45
|
+
export function generateToken(bytes = DEFAULT_TOKEN_BYTES) {
|
|
46
|
+
const buf = new Uint8Array(bytes);
|
|
47
|
+
crypto.getRandomValues(buf);
|
|
48
|
+
return Array.from(buf)
|
|
49
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
50
|
+
.join("");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Clamp `ttlMs` into `[minMs, maxMs]`. A non-finite `ttlMs` (NaN / Infinity)
|
|
54
|
+
* falls back to `fallbackMs`; a non-finite `fallbackMs` falls back to `minMs`.
|
|
55
|
+
* The result is therefore always finite and >= `minMs`, so a malformed request
|
|
56
|
+
* can never produce a non-finite `expiresAt` (the expiry invariant holds).
|
|
57
|
+
*
|
|
58
|
+
* @param ttlMs - Requested lifetime in ms.
|
|
59
|
+
* @param minMs - Lower bound (also the ultimate finite fallback).
|
|
60
|
+
* @param maxMs - Upper bound.
|
|
61
|
+
* @param fallbackMs - Used when `ttlMs` is not finite.
|
|
62
|
+
* @returns The clamped, finite TTL.
|
|
63
|
+
*/
|
|
64
|
+
export function clampTtl(ttlMs, minMs, maxMs, fallbackMs) {
|
|
65
|
+
const fallback = Number.isFinite(fallbackMs) ? fallbackMs : minMs;
|
|
66
|
+
const ttl = Number.isFinite(ttlMs) ? ttlMs : fallback;
|
|
67
|
+
return Math.min(maxMs, Math.max(minMs, ttl));
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAEjF,MAAM,CAAC,MAAM,cAAc,GAAG,QAAQ,CAAC;AAEvC,8DAA8D;AAC9D,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AAEtC,wCAAwC;AACxC,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAC;AAEzC,8FAA8F;AAC9F,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC;AAEjC,kDAAkD;AAClD,MAAM,CAAC,MAAM,kBAAkB,GAAG,aAAa,CAAC;AAEhD,0EAA0E;AAC1E,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAEtC,uFAAuF;AACvF,MAAM,CAAC,MAAM,4BAA4B,GAAG,UAAU,CAAC;AAEvD,yEAAyE;AACzE,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC;AAE/B,8DAA8D;AAC9D,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC;AAQnC,gCAAgC;AAChC,MAAM,CAAC,MAAM,iBAAiB,GAAa,SAAS,CAAC;AAKrD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,GAAW,EACX,OAAiB,iBAAiB;IAElC,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5C,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACvD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;SACtC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;SAC3C,IAAI,CAAC,EAAE,CAAC,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,KAAK,GAAG,mBAAmB;IACvD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;IAC5B,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;SACnB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;SAC3C,IAAI,CAAC,EAAE,CAAC,CAAC;AACd,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,QAAQ,CACtB,KAAa,EACb,KAAa,EACb,KAAa,EACb,UAAkB;IAElB,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC;IAClE,MAAM,GAAG,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC;IACtD,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;AAC/C,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vllnt/convex-tokens",
|
|
3
|
+
"version": "0.1.0-canary.4bc4600",
|
|
4
|
+
"description": "Hashed-secret token primitive with mint-once, TTL, revoke and scope, as a Convex component",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@9.15.4",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./package.json": "./package.json",
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/client/index.d.ts",
|
|
16
|
+
"default": "./dist/client/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./react": {
|
|
19
|
+
"types": "./dist/react/index.d.ts",
|
|
20
|
+
"default": "./dist/react/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./test": "./src/test.ts",
|
|
23
|
+
"./_generated/component.js": {
|
|
24
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./_generated/component": {
|
|
27
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./convex.config": {
|
|
30
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
31
|
+
"default": "./dist/component/convex.config.js"
|
|
32
|
+
},
|
|
33
|
+
"./convex.config.js": {
|
|
34
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
35
|
+
"default": "./dist/component/convex.config.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"types": "./dist/client/index.d.ts",
|
|
39
|
+
"module": "./dist/client/index.js",
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc --project ./tsconfig.build.json",
|
|
42
|
+
"build:codegen": "pnpm convex codegen --component-dir ./src/component && pnpm build",
|
|
43
|
+
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
46
|
+
"lint": "eslint .",
|
|
47
|
+
"test": "vitest run --passWithNoTests",
|
|
48
|
+
"test:watch": "vitest --typecheck --clearScreen false",
|
|
49
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
50
|
+
"generate:llms": "node scripts/generate-llms.mjs",
|
|
51
|
+
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
52
|
+
"prepublishOnly": "npm whoami || npm login",
|
|
53
|
+
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
54
|
+
"release": "npm version patch && npm publish && git push --follow-tags"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"convex": "^1.41.0",
|
|
59
|
+
"react": ">=18"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"react": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@edge-runtime/vm": "^5.0.0",
|
|
68
|
+
"@testing-library/react": "^16.3.2",
|
|
69
|
+
"@types/react": "^19.2.17",
|
|
70
|
+
"@types/react-dom": "^19.2.3",
|
|
71
|
+
"@vitest/coverage-v8": "^4.1.8",
|
|
72
|
+
"@vllnt/eslint-config": "^1.0.0",
|
|
73
|
+
"@vllnt/typescript": "^1.0.0",
|
|
74
|
+
"convex": "^1.41.0",
|
|
75
|
+
"convex-test": "^0.0.53",
|
|
76
|
+
"eslint": "^9.0.0",
|
|
77
|
+
"jsdom": "^29.1.1",
|
|
78
|
+
"prettier": "^3.4.0",
|
|
79
|
+
"react": "^19.2.7",
|
|
80
|
+
"react-dom": "^19.2.7",
|
|
81
|
+
"typescript": "^5.7.0",
|
|
82
|
+
"vitest": "^4.1.8"
|
|
83
|
+
},
|
|
84
|
+
"homepage": "https://github.com/vllnt/convex-tokens#readme",
|
|
85
|
+
"bugs": {
|
|
86
|
+
"url": "https://github.com/vllnt/convex-tokens/issues"
|
|
87
|
+
},
|
|
88
|
+
"author": {
|
|
89
|
+
"name": "bntvllnt",
|
|
90
|
+
"url": "https://bntvllnt.com"
|
|
91
|
+
},
|
|
92
|
+
"funding": {
|
|
93
|
+
"type": "github",
|
|
94
|
+
"url": "https://github.com/sponsors/bntvllnt"
|
|
95
|
+
},
|
|
96
|
+
"repository": {
|
|
97
|
+
"type": "git",
|
|
98
|
+
"url": "https://github.com/vllnt/convex-tokens"
|
|
99
|
+
},
|
|
100
|
+
"engines": {
|
|
101
|
+
"node": ">=18"
|
|
102
|
+
},
|
|
103
|
+
"publishConfig": {
|
|
104
|
+
"access": "public"
|
|
105
|
+
},
|
|
106
|
+
"sideEffects": false,
|
|
107
|
+
"keywords": [
|
|
108
|
+
"convex",
|
|
109
|
+
"convex-component",
|
|
110
|
+
"tokens"
|
|
111
|
+
]
|
|
112
|
+
}
|