@neutralauth/internal-auth 0.10.11
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 +201 -0
- package/README.md +39 -0
- package/dist/auth-config.d.ts +43 -0
- package/dist/auth-config.d.ts.map +1 -0
- package/dist/auth-config.js +43 -0
- package/dist/auth-config.js.map +1 -0
- package/dist/auth-options.d.ts +3 -0
- package/dist/auth-options.d.ts.map +1 -0
- package/dist/auth-options.js +40 -0
- package/dist/auth-options.js.map +1 -0
- package/dist/auth.d.ts +2 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +4 -0
- package/dist/auth.js.map +1 -0
- package/dist/client/adapter-utils.d.ts +66 -0
- package/dist/client/adapter-utils.d.ts.map +1 -0
- package/dist/client/adapter-utils.js +437 -0
- package/dist/client/adapter-utils.js.map +1 -0
- package/dist/client/adapter.d.ts +14 -0
- package/dist/client/adapter.d.ts.map +1 -0
- package/dist/client/adapter.js +274 -0
- package/dist/client/adapter.js.map +1 -0
- package/dist/client/create-api.d.ts +141 -0
- package/dist/client/create-api.d.ts.map +1 -0
- package/dist/client/create-api.js +205 -0
- package/dist/client/create-api.js.map +1 -0
- package/dist/client/create-client.d.ts +183 -0
- package/dist/client/create-client.d.ts.map +1 -0
- package/dist/client/create-client.js +311 -0
- package/dist/client/create-client.js.map +1 -0
- package/dist/client/create-schema.d.ts +19 -0
- package/dist/client/create-schema.d.ts.map +1 -0
- package/dist/client/create-schema.js +114 -0
- package/dist/client/create-schema.js.map +1 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +10 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/plugins/index.d.ts +3 -0
- package/dist/client/plugins/index.d.ts.map +1 -0
- package/dist/client/plugins/index.js +3 -0
- package/dist/client/plugins/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +36 -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 +787 -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/adapter.d.ts +130 -0
- package/dist/component/adapter.d.ts.map +1 -0
- package/dist/component/adapter.js +5 -0
- package/dist/component/adapter.js.map +1 -0
- package/dist/component/adapterTest.d.ts +10 -0
- package/dist/component/adapterTest.d.ts.map +1 -0
- package/dist/component/adapterTest.js +409 -0
- package/dist/component/adapterTest.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 +4 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/schema.d.ts +474 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +139 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/nextjs/client.d.ts +4 -0
- package/dist/nextjs/client.d.ts.map +1 -0
- package/dist/nextjs/client.js +37 -0
- package/dist/nextjs/client.js.map +1 -0
- package/dist/nextjs/index.d.ts +22 -0
- package/dist/nextjs/index.d.ts.map +1 -0
- package/dist/nextjs/index.js +98 -0
- package/dist/nextjs/index.js.map +1 -0
- package/dist/plugins/convex/client.d.ts +6 -0
- package/dist/plugins/convex/client.d.ts.map +1 -0
- package/dist/plugins/convex/client.js +7 -0
- package/dist/plugins/convex/client.js.map +1 -0
- package/dist/plugins/convex/index.d.ts +322 -0
- package/dist/plugins/convex/index.d.ts.map +1 -0
- package/dist/plugins/convex/index.js +422 -0
- package/dist/plugins/convex/index.js.map +1 -0
- package/dist/plugins/cross-domain/client.d.ts +132 -0
- package/dist/plugins/cross-domain/client.d.ts.map +1 -0
- package/dist/plugins/cross-domain/client.js +192 -0
- package/dist/plugins/cross-domain/client.js.map +1 -0
- package/dist/plugins/cross-domain/index.d.ts +51 -0
- package/dist/plugins/cross-domain/index.d.ts.map +1 -0
- package/dist/plugins/cross-domain/index.js +173 -0
- package/dist/plugins/cross-domain/index.js.map +1 -0
- package/dist/plugins/index.d.ts +3 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +3 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/react/index.d.ts +80 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +190 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react-start/index.d.ts +13 -0
- package/dist/react-start/index.d.ts.map +1 -0
- package/dist/react-start/index.js +101 -0
- package/dist/react-start/index.js.map +1 -0
- package/dist/utils/index.d.ts +33 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +91 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +208 -0
- package/src/auth-config.ts +80 -0
- package/src/auth-options.ts +54 -0
- package/src/auth.ts +4 -0
- package/src/client/adapter-utils.ts +639 -0
- package/src/client/adapter.test.ts +83 -0
- package/src/client/adapter.ts +363 -0
- package/src/client/create-api.ts +339 -0
- package/src/client/create-client.ts +452 -0
- package/src/client/create-schema.ts +166 -0
- package/src/client/index.ts +22 -0
- package/src/client/plugins/index.ts +2 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +2008 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +161 -0
- package/src/component/adapter.ts +13 -0
- package/src/component/adapterTest.ts +505 -0
- package/src/component/convex.config.ts +5 -0
- package/src/component/schema.ts +142 -0
- package/src/nextjs/client.tsx +54 -0
- package/src/nextjs/index.ts +152 -0
- package/src/plugins/convex/client.ts +9 -0
- package/src/plugins/convex/index.ts +596 -0
- package/src/plugins/cross-domain/client.test.ts +217 -0
- package/src/plugins/cross-domain/client.ts +234 -0
- package/src/plugins/cross-domain/index.ts +199 -0
- package/src/plugins/index.ts +2 -0
- package/src/react/index.tsx +304 -0
- package/src/react-start/index.ts +153 -0
- package/src/react-start/vite-env.d.ts +2 -0
- package/src/test.ts +18 -0
- package/src/utils/index.ts +171 -0
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { createAuthMiddleware, sessionMiddleware } from "better-auth/api";
|
|
2
|
+
import { createAuthEndpoint, jwt as jwtPlugin, bearer as bearerPlugin, oidcProvider as oidcProviderPlugin, } from "better-auth/plugins";
|
|
3
|
+
import { omit } from "convex-helpers";
|
|
4
|
+
export const JWT_COOKIE_NAME = "convex_jwt";
|
|
5
|
+
const getJwksAlg = (authProvider) => {
|
|
6
|
+
const isCustomJwt = "type" in authProvider && authProvider.type === "customJwt";
|
|
7
|
+
if (isCustomJwt && authProvider.algorithm !== "RS256") {
|
|
8
|
+
throw new Error("Only RS256 is supported for custom JWT with Better Auth");
|
|
9
|
+
}
|
|
10
|
+
return isCustomJwt ? authProvider.algorithm : "EdDSA";
|
|
11
|
+
};
|
|
12
|
+
const parseAuthConfig = (authConfig, opts) => {
|
|
13
|
+
const providerConfigs = authConfig.providers.filter((provider) => provider.applicationID === "convex");
|
|
14
|
+
if (providerConfigs.length > 1) {
|
|
15
|
+
throw new Error("Multiple auth providers with applicationID 'convex' detected. Please use only one.");
|
|
16
|
+
}
|
|
17
|
+
const providerConfig = providerConfigs[0];
|
|
18
|
+
if (!providerConfig) {
|
|
19
|
+
throw new Error("No auth provider with applicationID 'convex' found. Please add one to your auth config.");
|
|
20
|
+
}
|
|
21
|
+
if (!("type" in providerConfig) || providerConfig.type !== "customJwt") {
|
|
22
|
+
return providerConfig;
|
|
23
|
+
}
|
|
24
|
+
const isDataUriJwks = providerConfig.jwks?.startsWith("data:text/");
|
|
25
|
+
if (isDataUriJwks && !opts.jwks) {
|
|
26
|
+
throw new Error("Static JWKS detected in auth config, but missing from Convex plugin");
|
|
27
|
+
}
|
|
28
|
+
if (!isDataUriJwks && opts.jwks) {
|
|
29
|
+
// eslint-disable-next-line no-console
|
|
30
|
+
console.warn("Static JWKS provided to Convex plugin, but not to auth config. This adds an unnecessary network request for token verification.");
|
|
31
|
+
}
|
|
32
|
+
return providerConfig;
|
|
33
|
+
};
|
|
34
|
+
export const convex = (opts) => {
|
|
35
|
+
const jwtExpirationSeconds = opts.jwt?.expirationSeconds ?? opts.jwtExpirationSeconds ?? 60 * 15;
|
|
36
|
+
const oidcProvider = oidcProviderPlugin({
|
|
37
|
+
loginPage: "/not-used",
|
|
38
|
+
metadata: {
|
|
39
|
+
issuer: `${process.env.CONVEX_SITE_URL}`,
|
|
40
|
+
jwks_uri: `${process.env.CONVEX_SITE_URL}${opts.options?.basePath ?? "/api/auth"}/convex/jwks`,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
const providerConfig = parseAuthConfig(opts.authConfig, opts);
|
|
44
|
+
const jwtOptions = {
|
|
45
|
+
jwt: {
|
|
46
|
+
issuer: `${process.env.CONVEX_SITE_URL}`,
|
|
47
|
+
audience: "convex",
|
|
48
|
+
expirationTime: `${jwtExpirationSeconds}s`,
|
|
49
|
+
definePayload: ({ user, session }) => ({
|
|
50
|
+
...(opts.jwt?.definePayload
|
|
51
|
+
? opts.jwt.definePayload({ user, session })
|
|
52
|
+
: omit(user, ["id", "image"])),
|
|
53
|
+
sessionId: session.id,
|
|
54
|
+
iat: Math.floor(new Date().getTime() / 1000),
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
jwks: {
|
|
58
|
+
keyPairConfig: {
|
|
59
|
+
alg: getJwksAlg(providerConfig),
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
const jwks = opts.jwks ? JSON.parse(opts.jwks) : undefined;
|
|
64
|
+
const jwt = jwtPlugin({
|
|
65
|
+
...jwtOptions,
|
|
66
|
+
adapter: {
|
|
67
|
+
createJwk: async (webKey, ctx) => {
|
|
68
|
+
if (opts.jwks) {
|
|
69
|
+
throw new Error("Not implemented");
|
|
70
|
+
}
|
|
71
|
+
// TODO: remove when date parsing for jwks adapter is fixed upstream
|
|
72
|
+
return await ctx.context.adapter.create({
|
|
73
|
+
model: "jwks",
|
|
74
|
+
data: {
|
|
75
|
+
...webKey,
|
|
76
|
+
createdAt: new Date(),
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
getJwks: async (ctx) => {
|
|
81
|
+
if (opts.jwks) {
|
|
82
|
+
return jwks;
|
|
83
|
+
}
|
|
84
|
+
// TODO: remove when date parsing for jwks adapter is fixed upstream
|
|
85
|
+
const keys = await ctx.context.adapter.findMany({
|
|
86
|
+
model: "jwks",
|
|
87
|
+
sortBy: {
|
|
88
|
+
field: "createdAt",
|
|
89
|
+
direction: "desc",
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
return keys.map((key) => ({
|
|
93
|
+
...key,
|
|
94
|
+
createdAt: new Date(key.createdAt),
|
|
95
|
+
...(key.expiresAt ? { expiresAt: new Date(key.expiresAt) } : {}),
|
|
96
|
+
}));
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
// Bearer plugin converts the session token to a cookie
|
|
101
|
+
// for cross domain social login after code verification,
|
|
102
|
+
// and is required for the headers() helper to work.
|
|
103
|
+
const bearer = bearerPlugin();
|
|
104
|
+
const schema = {
|
|
105
|
+
user: {
|
|
106
|
+
fields: { userId: { type: "string", required: false, input: false } },
|
|
107
|
+
},
|
|
108
|
+
...jwt.schema,
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
id: "convex",
|
|
112
|
+
init: (ctx) => {
|
|
113
|
+
const { options, logger: _logger } = ctx;
|
|
114
|
+
if (options.basePath !== "/api/auth" && !opts.options?.basePath) {
|
|
115
|
+
// eslint-disable-next-line no-console
|
|
116
|
+
console.warn(`Better Auth basePath set to ${options.basePath} but no basePath is set in the Convex plugin. This is probably a mistake.`);
|
|
117
|
+
}
|
|
118
|
+
if (opts.options?.basePath &&
|
|
119
|
+
options.basePath !== opts.options?.basePath) {
|
|
120
|
+
// eslint-disable-next-line no-console
|
|
121
|
+
console.warn(`Better Auth basePath ${options.basePath} does not match Convex plugin basePath ${opts.options?.basePath}. This is probably a mistake.`);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
hooks: {
|
|
125
|
+
before: [
|
|
126
|
+
...bearer.hooks.before,
|
|
127
|
+
// Don't attempt to refresh the session with a query ctx
|
|
128
|
+
{
|
|
129
|
+
matcher: (ctx) => {
|
|
130
|
+
return (!ctx.context.adapter.options?.isRunMutationCtx &&
|
|
131
|
+
ctx.path === "/get-session");
|
|
132
|
+
},
|
|
133
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
134
|
+
ctx.query = { ...ctx.query, disableRefresh: true };
|
|
135
|
+
ctx.context.internalAdapter.deleteSession = async (..._args) => {
|
|
136
|
+
//skip
|
|
137
|
+
};
|
|
138
|
+
return { context: ctx };
|
|
139
|
+
}),
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
after: [
|
|
143
|
+
...oidcProvider.hooks.after,
|
|
144
|
+
{
|
|
145
|
+
matcher: (ctx) => {
|
|
146
|
+
return Boolean(ctx.path.startsWith("/sign-in") ||
|
|
147
|
+
ctx.path.startsWith("/sign-up") ||
|
|
148
|
+
ctx.path.startsWith("/callback") ||
|
|
149
|
+
ctx.path.startsWith("/oauth2/callback") ||
|
|
150
|
+
ctx.path.startsWith("/magic-link/verify") ||
|
|
151
|
+
ctx.path.startsWith("/email-otp/verify-email") ||
|
|
152
|
+
ctx.path.startsWith("/phone-number/verify") ||
|
|
153
|
+
ctx.path.startsWith("/siwe/verify") ||
|
|
154
|
+
(ctx.path.startsWith("/get-session") && ctx.context.session));
|
|
155
|
+
},
|
|
156
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
157
|
+
// Set jwt cookie at login for authenticated ssr
|
|
158
|
+
const originalSession = ctx.context.session;
|
|
159
|
+
try {
|
|
160
|
+
ctx.context.session =
|
|
161
|
+
ctx.context.session ?? ctx.context.newSession;
|
|
162
|
+
const { token } = await jwt.endpoints.getToken({
|
|
163
|
+
...ctx,
|
|
164
|
+
headers: {},
|
|
165
|
+
method: "GET",
|
|
166
|
+
returnHeaders: false,
|
|
167
|
+
returnStatus: false,
|
|
168
|
+
});
|
|
169
|
+
const jwtCookie = ctx.context.createAuthCookie(JWT_COOKIE_NAME, {
|
|
170
|
+
maxAge: jwtExpirationSeconds,
|
|
171
|
+
});
|
|
172
|
+
ctx.setCookie(jwtCookie.name, token, jwtCookie.attributes);
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
174
|
+
}
|
|
175
|
+
catch (_err) {
|
|
176
|
+
// no-op, some sign-in calls (eg., when redirecting to 2fa)
|
|
177
|
+
// 401 here
|
|
178
|
+
}
|
|
179
|
+
ctx.context.session = originalSession;
|
|
180
|
+
}),
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
matcher: (ctx) => {
|
|
184
|
+
return (ctx.path?.startsWith("/sign-out") ||
|
|
185
|
+
ctx.path?.startsWith("/delete-user") ||
|
|
186
|
+
(ctx.path?.startsWith("/get-session") && !ctx.context.session));
|
|
187
|
+
},
|
|
188
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
189
|
+
const jwtCookie = ctx.context.createAuthCookie(JWT_COOKIE_NAME, {
|
|
190
|
+
maxAge: 0,
|
|
191
|
+
});
|
|
192
|
+
ctx.setCookie(jwtCookie.name, "", jwtCookie.attributes);
|
|
193
|
+
}),
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
},
|
|
197
|
+
endpoints: {
|
|
198
|
+
getOpenIdConfig: createAuthEndpoint("/convex/.well-known/openid-configuration", {
|
|
199
|
+
method: "GET",
|
|
200
|
+
metadata: {
|
|
201
|
+
isAction: false,
|
|
202
|
+
},
|
|
203
|
+
// TODO: properly type this
|
|
204
|
+
}, async (ctx) => {
|
|
205
|
+
const response = await oidcProvider.endpoints.getOpenIdConfig({
|
|
206
|
+
...ctx,
|
|
207
|
+
returnHeaders: false,
|
|
208
|
+
returnStatus: false,
|
|
209
|
+
});
|
|
210
|
+
return response;
|
|
211
|
+
}),
|
|
212
|
+
getJwks: createAuthEndpoint("/convex/jwks", {
|
|
213
|
+
method: "GET",
|
|
214
|
+
metadata: {
|
|
215
|
+
openapi: {
|
|
216
|
+
description: "Get the JSON Web Key Set",
|
|
217
|
+
responses: {
|
|
218
|
+
"200": {
|
|
219
|
+
description: "JSON Web Key Set retrieved successfully",
|
|
220
|
+
content: {
|
|
221
|
+
"application/json": {
|
|
222
|
+
schema: {
|
|
223
|
+
type: "object",
|
|
224
|
+
properties: {
|
|
225
|
+
keys: {
|
|
226
|
+
type: "array",
|
|
227
|
+
description: "Array of public JSON Web Keys",
|
|
228
|
+
items: {
|
|
229
|
+
type: "object",
|
|
230
|
+
properties: {
|
|
231
|
+
kid: {
|
|
232
|
+
type: "string",
|
|
233
|
+
description: "Key ID uniquely identifying the key, corresponds to the 'id' from the stored Jwk",
|
|
234
|
+
},
|
|
235
|
+
kty: {
|
|
236
|
+
type: "string",
|
|
237
|
+
description: "Key type (e.g., 'RSA', 'EC', 'OKP')",
|
|
238
|
+
},
|
|
239
|
+
alg: {
|
|
240
|
+
type: "string",
|
|
241
|
+
description: "Algorithm intended for use with the key (e.g., 'EdDSA', 'RS256')",
|
|
242
|
+
},
|
|
243
|
+
use: {
|
|
244
|
+
type: "string",
|
|
245
|
+
description: "Intended use of the public key (e.g., 'sig' for signature)",
|
|
246
|
+
enum: ["sig"],
|
|
247
|
+
nullable: true,
|
|
248
|
+
},
|
|
249
|
+
n: {
|
|
250
|
+
type: "string",
|
|
251
|
+
description: "Modulus for RSA keys (base64url-encoded)",
|
|
252
|
+
nullable: true,
|
|
253
|
+
},
|
|
254
|
+
e: {
|
|
255
|
+
type: "string",
|
|
256
|
+
description: "Exponent for RSA keys (base64url-encoded)",
|
|
257
|
+
nullable: true,
|
|
258
|
+
},
|
|
259
|
+
crv: {
|
|
260
|
+
type: "string",
|
|
261
|
+
description: "Curve name for elliptic curve keys (e.g., 'Ed25519', 'P-256')",
|
|
262
|
+
nullable: true,
|
|
263
|
+
},
|
|
264
|
+
x: {
|
|
265
|
+
type: "string",
|
|
266
|
+
description: "X coordinate for elliptic curve keys (base64url-encoded)",
|
|
267
|
+
nullable: true,
|
|
268
|
+
},
|
|
269
|
+
y: {
|
|
270
|
+
type: "string",
|
|
271
|
+
description: "Y coordinate for elliptic curve keys (base64url-encoded)",
|
|
272
|
+
nullable: true,
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
required: ["kid", "kty", "alg"],
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
required: ["keys"],
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
}, async (ctx) => {
|
|
288
|
+
const response = await jwt.endpoints.getJwks({
|
|
289
|
+
...ctx,
|
|
290
|
+
returnHeaders: false,
|
|
291
|
+
returnStatus: false,
|
|
292
|
+
});
|
|
293
|
+
return response;
|
|
294
|
+
}),
|
|
295
|
+
getLatestJwks: createAuthEndpoint(
|
|
296
|
+
// This path is inaccessible due to SERVER_ONLY: true, it's here to
|
|
297
|
+
// avoid errors that occur in all matchers when path is undefined.
|
|
298
|
+
"/convex/latest-jwks", {
|
|
299
|
+
isAction: true,
|
|
300
|
+
method: "POST",
|
|
301
|
+
metadata: {
|
|
302
|
+
SERVER_ONLY: true,
|
|
303
|
+
openapi: {
|
|
304
|
+
description: "Delete and regenerate JWKS, and return the new JWKS for static usage",
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
}, async (ctx) => {
|
|
308
|
+
// Ensure at least one key exists
|
|
309
|
+
await jwtPlugin(jwtOptions).endpoints.getJwks({
|
|
310
|
+
...ctx,
|
|
311
|
+
method: "GET",
|
|
312
|
+
});
|
|
313
|
+
const jwks = await ctx.context.adapter.findMany({
|
|
314
|
+
model: "jwks",
|
|
315
|
+
limit: 1,
|
|
316
|
+
sortBy: {
|
|
317
|
+
field: "createdAt",
|
|
318
|
+
direction: "desc",
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
// Add alg to jwks, otherwise Better Auth will default to EdDSA
|
|
322
|
+
jwks[0].alg = jwtOptions.jwks.keyPairConfig.alg;
|
|
323
|
+
return jwks;
|
|
324
|
+
}),
|
|
325
|
+
rotateKeys: createAuthEndpoint(
|
|
326
|
+
// This path is inaccessible due to SERVER_ONLY: true, it's here to
|
|
327
|
+
// avoid errors that occur in all matchers when path is undefined.
|
|
328
|
+
"/convex/rotate-keys", {
|
|
329
|
+
isAction: true,
|
|
330
|
+
method: "POST",
|
|
331
|
+
metadata: {
|
|
332
|
+
SERVER_ONLY: true,
|
|
333
|
+
openapi: {
|
|
334
|
+
description: "Delete and regenerate JWKS, and return the new JWKS for static usage",
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
}, async (ctx) => {
|
|
338
|
+
await ctx.context.adapter.deleteMany({
|
|
339
|
+
model: "jwks",
|
|
340
|
+
where: [],
|
|
341
|
+
});
|
|
342
|
+
await jwtPlugin(jwtOptions).endpoints.getJwks({
|
|
343
|
+
...ctx,
|
|
344
|
+
method: "GET",
|
|
345
|
+
});
|
|
346
|
+
const jwks = await ctx.context.adapter.findMany({
|
|
347
|
+
model: "jwks",
|
|
348
|
+
limit: 1,
|
|
349
|
+
sortBy: {
|
|
350
|
+
field: "createdAt",
|
|
351
|
+
direction: "desc",
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
jwks[0].alg = jwtOptions.jwks.keyPairConfig.alg;
|
|
355
|
+
return jwks;
|
|
356
|
+
}),
|
|
357
|
+
getToken: createAuthEndpoint("/convex/token", {
|
|
358
|
+
method: "GET",
|
|
359
|
+
requireHeaders: true,
|
|
360
|
+
use: [sessionMiddleware],
|
|
361
|
+
metadata: {
|
|
362
|
+
openapi: {
|
|
363
|
+
description: "Get a JWT token",
|
|
364
|
+
responses: {
|
|
365
|
+
200: {
|
|
366
|
+
description: "Success",
|
|
367
|
+
content: {
|
|
368
|
+
"application/json": {
|
|
369
|
+
schema: {
|
|
370
|
+
type: "object",
|
|
371
|
+
properties: {
|
|
372
|
+
token: {
|
|
373
|
+
type: "string",
|
|
374
|
+
},
|
|
375
|
+
},
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
}, async (ctx) => {
|
|
384
|
+
const runEndpoint = async () => {
|
|
385
|
+
const response = await jwt.endpoints.getToken({
|
|
386
|
+
...ctx,
|
|
387
|
+
returnHeaders: false,
|
|
388
|
+
returnStatus: false,
|
|
389
|
+
});
|
|
390
|
+
const jwtCookie = ctx.context.createAuthCookie(JWT_COOKIE_NAME, {
|
|
391
|
+
maxAge: jwtExpirationSeconds,
|
|
392
|
+
});
|
|
393
|
+
ctx.setCookie(jwtCookie.name, response.token, jwtCookie.attributes);
|
|
394
|
+
return response;
|
|
395
|
+
};
|
|
396
|
+
try {
|
|
397
|
+
return await runEndpoint();
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
// If alg config has changed and no longer matches one or more keys,
|
|
401
|
+
// roll the keys
|
|
402
|
+
if (!opts.jwks && error?.code === "ERR_JOSE_NOT_SUPPORTED") {
|
|
403
|
+
if (opts.jwksRotateOnTokenGenerationError) {
|
|
404
|
+
await ctx.context.adapter.deleteMany({
|
|
405
|
+
model: "jwks",
|
|
406
|
+
where: [],
|
|
407
|
+
});
|
|
408
|
+
return await runEndpoint();
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
// eslint-disable-next-line no-console
|
|
412
|
+
console.error("Try temporarily setting jwksRotateOnTokenGenerationError: true on the Convex Better Auth plugin.");
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
throw error;
|
|
416
|
+
}
|
|
417
|
+
}),
|
|
418
|
+
},
|
|
419
|
+
schema,
|
|
420
|
+
};
|
|
421
|
+
};
|
|
422
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/plugins/convex/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAC1E,OAAO,EACL,kBAAkB,EAClB,GAAG,IAAI,SAAS,EAChB,MAAM,IAAI,YAAY,EACtB,YAAY,IAAI,kBAAkB,GACnC,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAGtC,MAAM,CAAC,MAAM,eAAe,GAAG,YAAY,CAAC;AAE5C,MAAM,UAAU,GAAG,CAAC,YAA0B,EAAE,EAAE;IAChD,MAAM,WAAW,GACf,MAAM,IAAI,YAAY,IAAI,YAAY,CAAC,IAAI,KAAK,WAAW,CAAC;IAC9D,IAAI,WAAW,IAAI,YAAY,CAAC,SAAS,KAAK,OAAO,EAAE,CAAC;QACtD,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;IAC7E,CAAC;IACD,OAAO,WAAW,CAAC,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;AACxD,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,CAAC,UAAsB,EAAE,IAAuB,EAAE,EAAE;IAC1E,MAAM,eAAe,GAAG,UAAU,CAAC,SAAS,CAAC,MAAM,CACjD,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,aAAa,KAAK,QAAQ,CAClD,CAAC;IACF,IAAI,eAAe,C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;6BAAM,CAAC;4BACN,sCAAsC;4BACtC,OAAO,CAAC,KAAK,CACX,kGAAkG,CACnG,CAAC;wBACJ,CAAC;oBACH,CAAC;oBACD,MAAM,KAAK,CAAC;gBACd,CAAC;YACH,CAAC,CACF;SACF;QACD,MAAM;KACoB,CAAC;AAC/B,CAAC,CAAC"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { ClientStore } from "better-auth";
|
|
2
|
+
import type { BetterFetchOption } from "@better-fetch/fetch";
|
|
3
|
+
import type { crossDomain } from "./index.js";
|
|
4
|
+
interface CookieAttributes {
|
|
5
|
+
value: string;
|
|
6
|
+
expires?: Date;
|
|
7
|
+
"max-age"?: number;
|
|
8
|
+
domain?: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
secure?: boolean;
|
|
11
|
+
httpOnly?: boolean;
|
|
12
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
13
|
+
}
|
|
14
|
+
export declare function parseSetCookieHeader(header: string): Map<string, CookieAttributes>;
|
|
15
|
+
export declare function getSetCookie(header: string, prevCookie?: string): string;
|
|
16
|
+
export declare function getCookie(cookie: string): string;
|
|
17
|
+
export declare const crossDomainClient: (opts?: {
|
|
18
|
+
storage?: {
|
|
19
|
+
setItem: (key: string, value: string) => any;
|
|
20
|
+
getItem: (key: string) => string | null;
|
|
21
|
+
};
|
|
22
|
+
storagePrefix?: string;
|
|
23
|
+
disableCache?: boolean;
|
|
24
|
+
}) => {
|
|
25
|
+
id: "cross-domain";
|
|
26
|
+
$InferServerPlugin: ReturnType<typeof crossDomain>;
|
|
27
|
+
getActions(_: import("@better-fetch/fetch").BetterFetch, $store: ClientStore): {
|
|
28
|
+
/**
|
|
29
|
+
* Get the stored cookie.
|
|
30
|
+
*
|
|
31
|
+
* You can use this to get the cookie stored in the device and use it in your fetch
|
|
32
|
+
* requests.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const cookie = client.getCookie();
|
|
37
|
+
* fetch("https://api.example.com", {
|
|
38
|
+
* headers: {
|
|
39
|
+
* cookie,
|
|
40
|
+
* },
|
|
41
|
+
* });
|
|
42
|
+
*/
|
|
43
|
+
getCookie: () => string;
|
|
44
|
+
/**
|
|
45
|
+
* Notify the session signal.
|
|
46
|
+
*
|
|
47
|
+
* This is used to trigger an update in useSession, generally when a new session
|
|
48
|
+
* token is set.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* client.notifySessionSignal();
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
updateSession: () => void;
|
|
56
|
+
/**
|
|
57
|
+
* Get the stored session data.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const sessionData = client.getSessionData();
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
getSessionData: () => Record<string, unknown> | null;
|
|
65
|
+
};
|
|
66
|
+
fetchPlugins: {
|
|
67
|
+
id: string;
|
|
68
|
+
name: string;
|
|
69
|
+
hooks: {
|
|
70
|
+
onSuccess(context: import("@better-fetch/fetch").SuccessContext<any>): Promise<void>;
|
|
71
|
+
};
|
|
72
|
+
init(url: string, options: ({
|
|
73
|
+
method?: string | undefined;
|
|
74
|
+
headers?: (HeadersInit & (HeadersInit | {
|
|
75
|
+
accept: "application/json" | "text/plain" | "application/octet-stream";
|
|
76
|
+
"content-type": "application/json" | "text/plain" | "application/x-www-form-urlencoded" | "multipart/form-data" | "application/octet-stream";
|
|
77
|
+
authorization: "Bearer" | "Basic";
|
|
78
|
+
})) | undefined;
|
|
79
|
+
redirect?: RequestRedirect | undefined;
|
|
80
|
+
cache?: RequestCache | undefined;
|
|
81
|
+
credentials?: RequestCredentials | undefined;
|
|
82
|
+
integrity?: string | undefined;
|
|
83
|
+
keepalive?: boolean | undefined;
|
|
84
|
+
mode?: RequestMode | undefined;
|
|
85
|
+
priority?: RequestPriority | undefined;
|
|
86
|
+
referrer?: string | undefined;
|
|
87
|
+
referrerPolicy?: ReferrerPolicy | undefined;
|
|
88
|
+
signal?: (AbortSignal | null) | undefined;
|
|
89
|
+
window?: null | undefined;
|
|
90
|
+
onRequest?: (<T extends Record<string, any>>(context: import("@better-fetch/fetch").RequestContext<T>) => Promise<import("@better-fetch/fetch").RequestContext | void> | import("@better-fetch/fetch").RequestContext | void) | undefined;
|
|
91
|
+
onResponse?: ((context: import("@better-fetch/fetch").ResponseContext) => Promise<Response | void | import("@better-fetch/fetch").ResponseContext> | Response | import("@better-fetch/fetch").ResponseContext | void) | undefined;
|
|
92
|
+
onSuccess?: ((context: import("@better-fetch/fetch").SuccessContext<any>) => Promise<void> | void) | undefined;
|
|
93
|
+
onError?: ((context: import("@better-fetch/fetch").ErrorContext) => Promise<void> | void) | undefined;
|
|
94
|
+
onRetry?: ((response: import("@better-fetch/fetch").ResponseContext) => Promise<void> | void) | undefined;
|
|
95
|
+
hookOptions?: {
|
|
96
|
+
cloneResponse?: boolean;
|
|
97
|
+
} | undefined;
|
|
98
|
+
timeout?: number | undefined;
|
|
99
|
+
customFetchImpl?: import("@better-fetch/fetch").FetchEsque | undefined;
|
|
100
|
+
plugins?: import("@better-fetch/fetch").BetterFetchPlugin[] | undefined;
|
|
101
|
+
baseURL?: string | undefined;
|
|
102
|
+
throw?: boolean | undefined;
|
|
103
|
+
auth?: ({
|
|
104
|
+
type: "Bearer";
|
|
105
|
+
token: string | Promise<string | undefined> | (() => string | Promise<string | undefined> | undefined) | undefined;
|
|
106
|
+
} | {
|
|
107
|
+
type: "Basic";
|
|
108
|
+
username: string | (() => string | undefined) | undefined;
|
|
109
|
+
password: string | (() => string | undefined) | undefined;
|
|
110
|
+
} | {
|
|
111
|
+
type: "Custom";
|
|
112
|
+
prefix: string | (() => string | undefined) | undefined;
|
|
113
|
+
value: string | (() => string | undefined) | undefined;
|
|
114
|
+
}) | undefined;
|
|
115
|
+
body?: any;
|
|
116
|
+
query?: any;
|
|
117
|
+
params?: any;
|
|
118
|
+
duplex?: "full" | "half" | undefined;
|
|
119
|
+
jsonParser?: ((text: string) => Promise<any> | any) | undefined;
|
|
120
|
+
retry?: import("@better-fetch/fetch").RetryOptions | undefined;
|
|
121
|
+
retryAttempt?: number | undefined;
|
|
122
|
+
output?: (import("@better-fetch/fetch").StandardSchemaV1 | typeof Blob | typeof File) | undefined;
|
|
123
|
+
errorSchema?: import("@better-fetch/fetch").StandardSchemaV1 | undefined;
|
|
124
|
+
disableValidation?: boolean | undefined;
|
|
125
|
+
} & Record<string, any>) | undefined): Promise<{
|
|
126
|
+
url: string;
|
|
127
|
+
options: BetterFetchOption;
|
|
128
|
+
}>;
|
|
129
|
+
}[];
|
|
130
|
+
};
|
|
131
|
+
export {};
|
|
132
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/plugins/cross-domain/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA0B,WAAW,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,UAAU,gBAAgB;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,IAAI,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;CACtC;AAED,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,GACb,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAkB/B;AAOD,wBAAgB,YAAY,CAAC,M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