@farm.js/auth 0.1.51-beta.1 → 0.1.51-beta.2
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 +4 -0
- package/dist/internal.d.ts +6 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +13 -0
- package/dist/runtime.d.ts +5 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +49 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -141,6 +141,10 @@ do not run in the browser.
|
|
|
141
141
|
| `FARM_AUTH_SECRET` | Signing secret, required in production. `AUTH_SECRET` and `BETTER_AUTH_SECRET` also work. |
|
|
142
142
|
| `FARM_AUTH_URL` | Public origin. `BETTER_AUTH_URL` is also read. Inferred on Vercel when unset. |
|
|
143
143
|
|
|
144
|
+
Without `FARM_AUTH_URL`, `farm dev` trusts the origin it is serving the request on when the host is
|
|
145
|
+
`localhost`, `*.localhost`, or an IP address, so other ports, the Android emulator's `10.0.2.2`, and
|
|
146
|
+
LAN devices under `--host` can sign in. Production trusts only the configured origin.
|
|
147
|
+
|
|
144
148
|
Without a secret, development generates a random one in `.farm/auth-secret` and reuses it across
|
|
145
149
|
restarts. Loading `farm.config.ts` and running `farm build` never connect to the database. Apply the
|
|
146
150
|
production schema before serving traffic:
|
package/dist/internal.d.ts
CHANGED
|
@@ -15,6 +15,12 @@ interface FarmAuthIntegration {
|
|
|
15
15
|
methods: readonly ["GET", "POST"];
|
|
16
16
|
handler(request: Request): Promise<Response>;
|
|
17
17
|
}>;
|
|
18
|
+
session(request: Request): Promise<FarmAuthIntegrationSession | null>;
|
|
19
|
+
}
|
|
20
|
+
interface FarmAuthIntegrationSession {
|
|
21
|
+
id: string;
|
|
22
|
+
userId: string;
|
|
23
|
+
expiresAt: Date;
|
|
18
24
|
}
|
|
19
25
|
export declare function createFarmAuthIntegration(config: ResolvedFarmAuthConfig, options: FarmAuthIntegrationOptions): FarmAuthIntegration;
|
|
20
26
|
export declare function migrateFarmAuth(): Promise<void>;
|
package/dist/internal.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAUzD,UAAU,0BAA0B;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,aAAa,GAAG,YAAY,CAAC;CACpC;AAED,UAAU,mBAAmB;IAC3B,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAC;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,WAAW,CAAC;IAClB,QAAQ,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAA;KAAE,CAAC;IACjD,MAAM,EAAE,KAAK,CAAC;QACZ,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAClC,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;KAC9C,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAUzD,UAAU,0BAA0B;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,aAAa,GAAG,YAAY,CAAC;CACpC;AAED,UAAU,mBAAmB;IAC3B,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAC;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,WAAW,CAAC;IAClB,QAAQ,EAAE;QAAE,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAA;KAAE,CAAC;IACjD,MAAM,EAAE,KAAK,CAAC;QACZ,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAClC,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;KAC9C,CAAC,CAAC;IACH,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,0BAA0B,GAAG,IAAI,CAAC,CAAC;CACvE;AAED,UAAU,0BAA0B;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,0BAA0B,GAClC,mBAAmB,CAiCrB;AAED,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAErD;AAED,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,MAAM,CAAC,CAEhE;AAED,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAErD"}
|
package/dist/internal.js
CHANGED
|
@@ -16,6 +16,19 @@ export function createFarmAuthIntegration(config, options) {
|
|
|
16
16
|
},
|
|
17
17
|
},
|
|
18
18
|
],
|
|
19
|
+
// Farm's session contract: who the request's cookies sign in. Mobile session tokens use it.
|
|
20
|
+
async session(request) {
|
|
21
|
+
const runtime = await getFarmAuthRuntime();
|
|
22
|
+
const result = (await runtime.api.getSession({ headers: request.headers }));
|
|
23
|
+
const session = result?.session;
|
|
24
|
+
if (!session || typeof session.id !== "string")
|
|
25
|
+
return null;
|
|
26
|
+
return {
|
|
27
|
+
id: session.id,
|
|
28
|
+
userId: String(session.userId),
|
|
29
|
+
expiresAt: new Date(session.expiresAt),
|
|
30
|
+
};
|
|
31
|
+
},
|
|
19
32
|
};
|
|
20
33
|
}
|
|
21
34
|
export async function migrateFarmAuth() {
|
package/dist/runtime.d.ts
CHANGED
|
@@ -54,7 +54,11 @@ export declare function resolveFarmAuthBaseURL(mode: "development" | "production
|
|
|
54
54
|
* A production deployment is still reachable at its own deployment hostname,
|
|
55
55
|
* and previews get a fresh one per build, so both are trusted explicitly
|
|
56
56
|
* rather than leaving direct deployment-URL access broken.
|
|
57
|
+
*
|
|
58
|
+
* `farm dev` without a configured URL also trusts the origin the dev server is
|
|
59
|
+
* serving the request on, so another port, an emulator's `10.0.2.2`, or a LAN
|
|
60
|
+
* address under `--host` can sign in. See `resolveDevServerOrigin`.
|
|
57
61
|
*/
|
|
58
|
-
export declare function resolveFarmAuthTrustedOrigins(env?: NodeJS.ProcessEnv): string[];
|
|
62
|
+
export declare function resolveFarmAuthTrustedOrigins(mode: "development" | "production", env?: NodeJS.ProcessEnv): string[] | ((request?: Request) => string[]);
|
|
59
63
|
export {};
|
|
60
64
|
//# sourceMappingURL=runtime.d.ts.map
|
package/dist/runtime.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAazD,UAAU,sBAAsB;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,aAAa,GAAG,YAAY,CAAC;CACpC;AAED,UAAU,iBAAiB;IACzB,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,GAAG,EAAE;QACH,UAAU,CAAC,KAAK,EAAE;YAAE,OAAO,EAAE,OAAO,CAAA;SAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;KAC3D,CAAC;CACH;AAqBD,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,sBAAsB,GAC9B,IAAI,CAeN;AAED,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,iBAAiB,CAAC,CA6BrE;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CASnE;AAED,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAU7D;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CASrD;AAED,wBAAsB,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAIrD;AAED;;;;GAIG;AACH,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,MAAM,CAAC,CAEhE;AAsID;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,aAAa,GAAG,YAAY,EAClC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,MAAM,GAAG,SAAS,CAIpB;AAaD;;;;;;;;;;GAUG;AACH,wBAAgB,6BAA6B,CAC3C,IAAI,EAAE,aAAa,GAAG,YAAY,EAClC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,MAAM,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,MAAM,EAAE,CAAC,CAa9C"}
|
package/dist/runtime.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
2
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
|
+
import { isIP } from "node:net";
|
|
4
5
|
import path from "node:path";
|
|
5
6
|
const FARM_AUTH_STATE_KEY = Symbol.for("@farm.js/auth/runtime");
|
|
6
7
|
const CURRENT_REQUEST_RESOLVER_KEY = Symbol.for("farm.currentRequestResolver");
|
|
@@ -152,7 +153,7 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
152
153
|
return {
|
|
153
154
|
appName: config.appName || "Farm app",
|
|
154
155
|
baseURL: resolveFarmAuthBaseURL(options.mode),
|
|
155
|
-
trustedOrigins: resolveFarmAuthTrustedOrigins(),
|
|
156
|
+
trustedOrigins: resolveFarmAuthTrustedOrigins(options.mode),
|
|
156
157
|
basePath: config.basePath,
|
|
157
158
|
secret: secret || (await resolveFallbackSecret(options)),
|
|
158
159
|
database: await createDatabase(config, options),
|
|
@@ -234,15 +235,19 @@ function getEphemeralSecret(key) {
|
|
|
234
235
|
* so production prefers the canonical one and previews keep their own URL.
|
|
235
236
|
*/
|
|
236
237
|
export function resolveFarmAuthBaseURL(mode, env = process.env) {
|
|
238
|
+
const configured = resolveConfiguredBaseURL(env);
|
|
239
|
+
if (configured)
|
|
240
|
+
return configured;
|
|
241
|
+
return mode === "development" ? "http://localhost:3000" : undefined;
|
|
242
|
+
}
|
|
243
|
+
function resolveConfiguredBaseURL(env) {
|
|
237
244
|
const configured = env.FARM_AUTH_URL || env.BETTER_AUTH_URL;
|
|
238
245
|
if (configured)
|
|
239
246
|
return configured;
|
|
240
247
|
const vercelHost = env.VERCEL_ENV === "production"
|
|
241
248
|
? env.VERCEL_PROJECT_PRODUCTION_URL || env.VERCEL_URL
|
|
242
249
|
: env.VERCEL_URL;
|
|
243
|
-
|
|
244
|
-
return toAbsoluteOrigin(vercelHost);
|
|
245
|
-
return mode === "development" ? "http://localhost:3000" : undefined;
|
|
250
|
+
return vercelHost ? toAbsoluteOrigin(vercelHost) : undefined;
|
|
246
251
|
}
|
|
247
252
|
/**
|
|
248
253
|
* Hosts that stay valid alongside the base URL.
|
|
@@ -250,14 +255,52 @@ export function resolveFarmAuthBaseURL(mode, env = process.env) {
|
|
|
250
255
|
* A production deployment is still reachable at its own deployment hostname,
|
|
251
256
|
* and previews get a fresh one per build, so both are trusted explicitly
|
|
252
257
|
* rather than leaving direct deployment-URL access broken.
|
|
258
|
+
*
|
|
259
|
+
* `farm dev` without a configured URL also trusts the origin the dev server is
|
|
260
|
+
* serving the request on, so another port, an emulator's `10.0.2.2`, or a LAN
|
|
261
|
+
* address under `--host` can sign in. See `resolveDevServerOrigin`.
|
|
253
262
|
*/
|
|
254
|
-
export function resolveFarmAuthTrustedOrigins(env = process.env) {
|
|
263
|
+
export function resolveFarmAuthTrustedOrigins(mode, env = process.env) {
|
|
255
264
|
const origins = new Set();
|
|
256
265
|
for (const host of [env.VERCEL_URL, env.VERCEL_PROJECT_PRODUCTION_URL, env.VERCEL_BRANCH_URL]) {
|
|
257
266
|
if (host)
|
|
258
267
|
origins.add(toAbsoluteOrigin(host));
|
|
259
268
|
}
|
|
260
|
-
|
|
269
|
+
const trusted = [...origins];
|
|
270
|
+
// A configured URL stays authoritative, in development too.
|
|
271
|
+
if (mode !== "development" || resolveConfiguredBaseURL(env))
|
|
272
|
+
return trusted;
|
|
273
|
+
return (request) => {
|
|
274
|
+
const own = request ? resolveDevServerOrigin(request) : undefined;
|
|
275
|
+
return own ? [...trusted, own] : trusted;
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* The dev server's own origin for this request, when it is safe to trust.
|
|
280
|
+
*
|
|
281
|
+
* In development the request URL comes from the Host the dev server accepted,
|
|
282
|
+
* so a page served by the dev server sends that same origin. Only hosts that
|
|
283
|
+
* cannot be DNS-rebound are trusted: `localhost`, `*.localhost`, and IP
|
|
284
|
+
* literals, the hosts Vite allows without `server.allowedHosts`. A rebinding
|
|
285
|
+
* page on `evil.example` that resolves to the dev server sends its own Host and
|
|
286
|
+
* Origin, which match each other but are not trusted here. A cross-site page
|
|
287
|
+
* sends an Origin that is not this request's origin, so it stays rejected.
|
|
288
|
+
*/
|
|
289
|
+
function resolveDevServerOrigin(request) {
|
|
290
|
+
let url;
|
|
291
|
+
try {
|
|
292
|
+
url = new URL(request.url);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
298
|
+
return undefined;
|
|
299
|
+
const hostname = url.hostname.toLowerCase();
|
|
300
|
+
const trusted = hostname === "localhost" ||
|
|
301
|
+
hostname.endsWith(".localhost") ||
|
|
302
|
+
isIP(hostname.replace(/^\[|\]$/g, "")) !== 0;
|
|
303
|
+
return trusted ? url.origin : undefined;
|
|
261
304
|
}
|
|
262
305
|
function toAbsoluteOrigin(host) {
|
|
263
306
|
return host.startsWith("http://") || host.startsWith("https://") ? host : `https://${host}`;
|