@vvedantb/server 0.8.0 → 0.8.1
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/CHANGELOG.md +21 -0
- package/README.md +1 -1
- package/dist/convex/index.d.ts +1 -1
- package/dist/convex/index.js +1 -1
- package/dist/convex/verifyClerkOAuthJwt.d.ts +32 -14
- package/dist/convex/verifyClerkOAuthJwt.js +20 -6
- package/docs/convex.md +4 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.8.1
|
|
4
|
+
|
|
5
|
+
Fix: 0.8.0 sent every JWT-shaped bearer to `verifyClerkOAuthJwt`, which rejects any header `typ` other than `at+jwt` or `application/at+jwt`. Clerk session tokens (RS256, `typ` `JWT` or none), which apps accept on `/mcp` with `acceptsToken: ["oauth_token", "session_token"]`, were rejected with no retry. No new dependency.
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- `authenticateClerkBearer` verifies locally only a JWT whose header `typ` is `at+jwt` or `application/at+jwt` (case-insensitive). A failure there is still final: `verifyFallback` is not called. Every other token goes to `verifyFallback`: opaque OAuth tokens, session JWTs, JWTs with another or no `typ`, and JWTs with an undecodable header. `verifyClerkOAuthJwt` is unchanged and still rejects session JWTs when called directly.
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- `verifyFallback` option on `authenticateClerkBearer`. It replaces `verifyOpaque` and receives non-access-token JWTs as well as opaque tokens. `verifyFallback` wins when both are set.
|
|
14
|
+
- `isOAuthAccessTokenJwt(token)`.
|
|
15
|
+
|
|
16
|
+
### Deprecated
|
|
17
|
+
|
|
18
|
+
- `verifyOpaque` option: alias of `verifyFallback`. Callers that pass only `verifyOpaque` still compile and work. Rename it when convenient.
|
|
19
|
+
|
|
20
|
+
### Tests
|
|
21
|
+
|
|
22
|
+
- `tests/convex/verifyClerkOAuthJwt.test.ts`: session JWT, missing `typ`, undecodable header and opaque token go to the fallback; a valid `at+jwt` verifies locally; a bad `at+jwt` returns `null` without the fallback; the `verifyOpaque` alias works.
|
|
23
|
+
|
|
3
24
|
## 0.8.0
|
|
4
25
|
|
|
5
26
|
Additive: V8-safe verification of Clerk OAuth access tokens that are JWTs. Nothing in 0.7.1 is removed or changed. No new dependency (WebCrypto and `fetch` only).
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ There are no subpath exports (0.2.0 removed `/convex` and `/mcp`; see [CHANGELOG
|
|
|
14
14
|
|
|
15
15
|
| Area | What it holds | Docs |
|
|
16
16
|
| --- | --- | --- |
|
|
17
|
-
| Convex helpers | Query-limit errors, Clerk auth config and client, Clerk token verification (Node `verifyClerkAccessToken`, V8 `authenticateClerkBearer` for OAuth JWTs; the MCP token cache is deprecated), Clerk-user lookups and `ensureUser` (`createClerkUserAuth`), bearer tokens, env, MCP CORS, origin-allowlist CORS, `jsonResponse`, server info, test helpers. | [docs/convex.md](docs/convex.md) |
|
|
17
|
+
| Convex helpers | Query-limit errors, Clerk auth config and client, Clerk token verification (Node `verifyClerkAccessToken`, V8 `authenticateClerkBearer` for OAuth access token JWTs, with `verifyFallback` for opaque tokens and session JWTs; the MCP token cache is deprecated), Clerk-user lookups and `ensureUser` (`createClerkUserAuth`), bearer tokens, env, MCP CORS, origin-allowlist CORS, `jsonResponse`, server info, test helpers. | [docs/convex.md](docs/convex.md) |
|
|
18
18
|
| MCP scaffolding | For Convex `httpAction`s: bearer-auth handler, OAuth metadata, route registration, a pure JSON-RPC dispatcher, tool helpers and explorer tools. | [docs/mcp.md](docs/mcp.md) |
|
|
19
19
|
| Crypto | WebCrypto-only base64/base64url/hex, SHA-256, random tokens, `timingSafeEqual`, AES-GCM. Convex default runtime and Node 20+. | [docs/crypto.md](docs/crypto.md) |
|
|
20
20
|
| Optimistic updates | `mapLoadedQuery` (also `forEachLoadedQuery`, `updateAllCachedQueries`), `patchLoadedQuery`, `tempId`, `createMemoryOptimisticStore`. Client-side, no React. | [docs/convex.md](docs/convex.md#optimistic-updates-client-side) |
|
package/dist/convex/index.d.ts
CHANGED
|
@@ -14,4 +14,4 @@ export { defineMcpServerInfo, type McpServerInfo } from "./mcpServerInfo.js";
|
|
|
14
14
|
export { assertMcpTools, expectMcpToolCount } from "./mcpTestHelpers.js";
|
|
15
15
|
export { tempId, type TempIdFormat, type TempIdOptions } from "./tempId.js";
|
|
16
16
|
export { consoleClerkVerifyLogger, verifyClerkAccessToken, type ClerkAuthLike, type ClerkLike, type ClerkRequestStateLike, type ClerkTokenType, type ClerkVerifyRejection, type VerifiedClerkToken, type VerifyClerkAccessTokenOptions, } from "./verifyClerkAccessToken.js";
|
|
17
|
-
export { authenticateClerkBearer, CLERK_JWKS_REFETCH_MIN_MS, CLERK_JWKS_TTL_MS, clearClerkJwksCache, looksLikeJwt, verifyClerkOAuthJwt, type AuthenticateClerkBearerOptions, type ClerkOAuthJwtRejection, type VerifiedClerkOAuthJwt, type VerifyClerkOAuthJwtOptions, } from "./verifyClerkOAuthJwt.js";
|
|
17
|
+
export { authenticateClerkBearer, CLERK_JWKS_REFETCH_MIN_MS, CLERK_JWKS_TTL_MS, clearClerkJwksCache, isOAuthAccessTokenJwt, looksLikeJwt, verifyClerkOAuthJwt, type AuthenticateClerkBearerOptions, type ClerkOAuthJwtRejection, type VerifiedClerkOAuthJwt, type VerifyClerkOAuthJwtOptions, } from "./verifyClerkOAuthJwt.js";
|
package/dist/convex/index.js
CHANGED
|
@@ -14,4 +14,4 @@ export { defineMcpServerInfo } from "./mcpServerInfo.js";
|
|
|
14
14
|
export { assertMcpTools, expectMcpToolCount } from "./mcpTestHelpers.js";
|
|
15
15
|
export { tempId } from "./tempId.js";
|
|
16
16
|
export { consoleClerkVerifyLogger, verifyClerkAccessToken, } from "./verifyClerkAccessToken.js";
|
|
17
|
-
export { authenticateClerkBearer, CLERK_JWKS_REFETCH_MIN_MS, CLERK_JWKS_TTL_MS, clearClerkJwksCache, looksLikeJwt, verifyClerkOAuthJwt, } from "./verifyClerkOAuthJwt.js";
|
|
17
|
+
export { authenticateClerkBearer, CLERK_JWKS_REFETCH_MIN_MS, CLERK_JWKS_TTL_MS, clearClerkJwksCache, isOAuthAccessTokenJwt, looksLikeJwt, verifyClerkOAuthJwt, } from "./verifyClerkOAuthJwt.js";
|
|
@@ -61,26 +61,43 @@ export declare function looksLikeJwt(token: string): boolean;
|
|
|
61
61
|
* module scope for `CLERK_JWKS_TTL_MS`.
|
|
62
62
|
*/
|
|
63
63
|
export declare function verifyClerkOAuthJwt(token: string, options: VerifyClerkOAuthJwtOptions): Promise<VerifiedClerkOAuthJwt | null>;
|
|
64
|
-
|
|
64
|
+
type FallbackResult = {
|
|
65
|
+
clerkUserId: string;
|
|
66
|
+
} | {
|
|
67
|
+
userId: string;
|
|
68
|
+
} | null | undefined;
|
|
69
|
+
type VerifyFallback = (token: string) => Promise<FallbackResult>;
|
|
70
|
+
type AuthenticateClerkBearerBaseOptions = Omit<VerifyClerkOAuthJwtOptions, "onReject"> & {
|
|
65
71
|
/** The bearer token, without the `Bearer ` prefix. */
|
|
66
72
|
token: string;
|
|
73
|
+
/** Called for a rejected OAuth access token JWT. Default: silent. */
|
|
74
|
+
onReject?: (rejection: ClerkOAuthJwtRejection) => void;
|
|
75
|
+
};
|
|
76
|
+
export type AuthenticateClerkBearerOptions = AuthenticateClerkBearerBaseOptions & ({
|
|
67
77
|
/**
|
|
68
|
-
* Verifies
|
|
69
|
-
*
|
|
78
|
+
* Verifies every token that is not an OAuth access token JWT (header `typ`
|
|
79
|
+
* `at+jwt` or `application/at+jwt`): opaque OAuth tokens issued before the
|
|
80
|
+
* switch to JWTs, and other JWTs such as Clerk session tokens. Usually
|
|
81
|
+
* `ctx.runAction(internal.mcp.nodeActions.verifyAccessToken, { token })`.
|
|
70
82
|
* Returns `{ clerkUserId }`, or `{ userId }` as `verifyClerkAccessToken` does, or `null`.
|
|
71
83
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
};
|
|
84
|
+
verifyFallback: VerifyFallback;
|
|
85
|
+
/** @deprecated Use `verifyFallback`. Ignored when `verifyFallback` is set. */
|
|
86
|
+
verifyOpaque?: VerifyFallback;
|
|
87
|
+
} | {
|
|
88
|
+
verifyFallback?: VerifyFallback;
|
|
89
|
+
/** @deprecated Use `verifyFallback`. Alias kept so 0.8.0 callers compile. */
|
|
90
|
+
verifyOpaque: VerifyFallback;
|
|
91
|
+
});
|
|
92
|
+
/** True when the token is a JWT whose header `typ` is `at+jwt` or `application/at+jwt`. */
|
|
93
|
+
export declare function isOAuthAccessTokenJwt(token: string): boolean;
|
|
80
94
|
/**
|
|
81
|
-
* One call for an `httpAction`:
|
|
82
|
-
*
|
|
83
|
-
*
|
|
95
|
+
* One call for an `httpAction`: an OAuth access token JWT (header `typ`
|
|
96
|
+
* `at+jwt` or `application/at+jwt`) is verified locally in V8 (no Node action,
|
|
97
|
+
* no token cache). Every other token goes to `verifyFallback`: opaque OAuth
|
|
98
|
+
* tokens, and JWTs with another or no `typ`, such as Clerk session tokens. An
|
|
99
|
+
* access token JWT that fails local verification is not retried with
|
|
100
|
+
* `verifyFallback`. `verifyOpaque` is a deprecated alias of `verifyFallback`.
|
|
84
101
|
* Returns `{ clerkUserId }` or `null`.
|
|
85
102
|
*
|
|
86
103
|
* Clerk OAuth access tokens last 1 day
|
|
@@ -90,3 +107,4 @@ export type AuthenticateClerkBearerOptions = Omit<VerifyClerkOAuthJwtOptions, "o
|
|
|
90
107
|
export declare function authenticateClerkBearer(options: AuthenticateClerkBearerOptions): Promise<{
|
|
91
108
|
clerkUserId: string;
|
|
92
109
|
} | null>;
|
|
110
|
+
export {};
|
|
@@ -125,10 +125,21 @@ export async function verifyClerkOAuthJwt(token, options) {
|
|
|
125
125
|
return null;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
+
/** True when the token is a JWT whose header `typ` is `at+jwt` or `application/at+jwt`. */
|
|
129
|
+
export function isOAuthAccessTokenJwt(token) {
|
|
130
|
+
if (!looksLikeJwt(token))
|
|
131
|
+
return false;
|
|
132
|
+
const header = decodeJson(token.split(".")[0]);
|
|
133
|
+
const typ = typeof header?.typ === "string" ? header.typ.toLowerCase() : undefined;
|
|
134
|
+
return typ === "at+jwt" || typ === "application/at+jwt";
|
|
135
|
+
}
|
|
128
136
|
/**
|
|
129
|
-
* One call for an `httpAction`:
|
|
130
|
-
*
|
|
131
|
-
*
|
|
137
|
+
* One call for an `httpAction`: an OAuth access token JWT (header `typ`
|
|
138
|
+
* `at+jwt` or `application/at+jwt`) is verified locally in V8 (no Node action,
|
|
139
|
+
* no token cache). Every other token goes to `verifyFallback`: opaque OAuth
|
|
140
|
+
* tokens, and JWTs with another or no `typ`, such as Clerk session tokens. An
|
|
141
|
+
* access token JWT that fails local verification is not retried with
|
|
142
|
+
* `verifyFallback`. `verifyOpaque` is a deprecated alias of `verifyFallback`.
|
|
132
143
|
* Returns `{ clerkUserId }` or `null`.
|
|
133
144
|
*
|
|
134
145
|
* Clerk OAuth access tokens last 1 day
|
|
@@ -136,12 +147,15 @@ export async function verifyClerkOAuthJwt(token, options) {
|
|
|
136
147
|
* so opaque tokens stop arriving about 1 day after the instance switched to JWTs.
|
|
137
148
|
*/
|
|
138
149
|
export async function authenticateClerkBearer(options) {
|
|
139
|
-
const { token, verifyOpaque, ...jwtOptions } = options;
|
|
140
|
-
if (
|
|
150
|
+
const { token, verifyFallback, verifyOpaque, ...jwtOptions } = options;
|
|
151
|
+
if (isOAuthAccessTokenJwt(token)) {
|
|
141
152
|
const verified = await verifyClerkOAuthJwt(token, jwtOptions);
|
|
142
153
|
return verified ? { clerkUserId: verified.clerkUserId } : null;
|
|
143
154
|
}
|
|
144
|
-
const
|
|
155
|
+
const fallback = verifyFallback ?? verifyOpaque;
|
|
156
|
+
if (!fallback)
|
|
157
|
+
return null;
|
|
158
|
+
const result = await fallback(token);
|
|
145
159
|
if (!result)
|
|
146
160
|
return null;
|
|
147
161
|
const id = "clerkUserId" in result ? result.clerkUserId : result.userId;
|
package/docs/convex.md
CHANGED
|
@@ -234,7 +234,7 @@ Stays in the apps: legacy email lookups, migration-table lookups, API-key actors
|
|
|
234
234
|
|
|
235
235
|
## Clerk OAuth JWT verification (V8)
|
|
236
236
|
|
|
237
|
-
Clerk OAuth access tokens are JWTs now. `authenticateClerkBearer` verifies them in the Convex default runtime (`httpAction`): WebCrypto and `fetch`, no Node action, no token cache.
|
|
237
|
+
Clerk OAuth access tokens are JWTs now. `authenticateClerkBearer` verifies them in the Convex default runtime (`httpAction`): WebCrypto and `fetch`, no Node action, no token cache. Tokens that are not OAuth access token JWTs go to `verifyFallback`, your Node `verifyClerkAccessToken` action: opaque tokens issued earlier, and JWTs with another or no header `typ`, such as Clerk session tokens (`acceptsToken: ["oauth_token", "session_token"]`). `verifyOpaque` is a deprecated alias of `verifyFallback`. Clerk OAuth access tokens last 1 day ([Clerk docs](https://clerk.com/docs/guides/configure/auth-strategies/oauth/how-clerk-implements-oauth)), so opaque tokens stop arriving about 1 day after the switch.
|
|
238
238
|
|
|
239
239
|
```ts
|
|
240
240
|
import { authenticateClerkBearer } from "@vvedantb/server";
|
|
@@ -242,19 +242,19 @@ import { authenticateClerkBearer } from "@vvedantb/server";
|
|
|
242
242
|
const principal = await authenticateClerkBearer({
|
|
243
243
|
token,
|
|
244
244
|
issuer: "https://clerk.example.com", // Clerk Frontend API URL
|
|
245
|
-
|
|
245
|
+
verifyFallback: (token) => ctx.runAction(internal.mcp.nodeActions.verifyAccessToken, { token }),
|
|
246
246
|
});
|
|
247
247
|
// { clerkUserId } | null
|
|
248
248
|
```
|
|
249
249
|
|
|
250
250
|
| Export | Semantics |
|
|
251
251
|
| --- | --- |
|
|
252
|
-
| `authenticateClerkBearer({ token, issuer,
|
|
252
|
+
| `authenticateClerkBearer({ token, issuer, verifyFallback, jwksUrl?, fetch?, now?, clockSkewMs?, onReject?, onError? })` | Three base64url segments with header `typ` `at+jwt` or `application/at+jwt`: `verifyClerkOAuthJwt`, never retried through `verifyFallback`. Everything else (opaque tokens, session JWTs, other or missing `typ`): `verifyFallback(token)`, mapped from `{ clerkUserId }` or `{ userId }` to `{ clerkUserId }`. `null` on rejection. |
|
|
253
253
|
| `verifyClerkOAuthJwt(token, options)` | Checks RS256 signature (JWKS from `${issuer}/.well-known/jwks.json` unless `jwksUrl`), `iss` (trailing slash ignored), `exp` and `nbf` with `clockSkewMs` (default 5000), header `typ` `at+jwt` or `application/at+jwt`, and non-empty `sub`. Returns `{ clerkUserId, claims }` or `null`. Silent unless `onReject({ kind })` or `onError` is set. It does not check `aud`; check `claims` yourself if you need it. |
|
|
254
254
|
| `clearClerkJwksCache()` | The JWKS and imported keys are cached in module scope per isolate for `CLERK_JWKS_TTL_MS` (1 hour); an unknown `kid` refetches at most once per `CLERK_JWKS_REFETCH_MIN_MS` (1 minute). Revocation is not checked: a JWT is valid until `exp`. |
|
|
255
255
|
| `looksLikeJwt(token)` | Three non-empty base64url segments. |
|
|
256
256
|
|
|
257
|
-
Migration: change `authenticate` in your http/mcp files to call `authenticateClerkBearer`, keep the Node `verifyAccessToken` action for opaque tokens, then (about 1 day later) drop the Node action, the `mcpTokenCache` table and the purge cron.
|
|
257
|
+
Migration: change `authenticate` in your http/mcp files to call `authenticateClerkBearer`, rename `verifyOpaque` to `verifyFallback`, keep the Node `verifyAccessToken` action for opaque tokens and session tokens, then (about 1 day later) drop the Node action, the `mcpTokenCache` table and the purge cron.
|
|
258
258
|
|
|
259
259
|
## MCP token cache
|
|
260
260
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvedantb/server",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Shared Convex backend helpers, MCP server scaffolding and Node-side MCP OAuth client helpers from one root import: query-limit errors, Clerk auth and token verification, an MCP bearer-token cache, Clerk-user lookups and ensure-user for Convex functions, bearer tokens, env, CORS, JSON responses, MCP route registration, a bearer-auth MCP HTTP handler, OAuth metadata, a JSON-RPC dispatcher, explorer tools, WebCrypto primitives, Convex optimistic-update helpers, MCP OAuth client helpers (provider, sign-in, refresh, revocation, loopback redirect server) for desktop apps, a QuickJS code sandbox that bridges script tool calls to host handlers, and a pure artifacts core (Zod contract, state limits and ops, quiz and flashcards pages, a sandboxed document wrapper with CSP and a guest state bridge).",
|
|
6
6
|
"type": "module",
|