@farm.js/auth 0.1.34 → 0.1.36
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/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +54 -2
- package/package.json +1 -1
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":"AAIA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAMzD,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,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;AAmID;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,aAAa,GAAG,YAAY,EAClC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,MAAM,GAAG,SAAS,CAWpB;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,MAAM,EAAE,CAM5F"}
|
package/dist/runtime.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
3
|
import { createRequire } from "node:module";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
const FARM_AUTH_STATE_KEY = Symbol.for("@farm.js/auth/runtime");
|
|
@@ -130,7 +131,7 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
130
131
|
baseURL: resolveFarmAuthBaseURL(options.mode),
|
|
131
132
|
trustedOrigins: resolveFarmAuthTrustedOrigins(),
|
|
132
133
|
basePath: config.basePath,
|
|
133
|
-
secret: secret ||
|
|
134
|
+
secret: secret || (await resolveFallbackSecret(options)),
|
|
134
135
|
database: await createDatabase(config, options),
|
|
135
136
|
emailAndPassword: {
|
|
136
137
|
enabled: config.emailAndPassword.enabled,
|
|
@@ -144,6 +145,57 @@ async function createBetterAuthOptions(config, options, runtimeOptions = {}) {
|
|
|
144
145
|
},
|
|
145
146
|
};
|
|
146
147
|
}
|
|
148
|
+
/** Random secrets used when a project directory cannot hold one, kept for the process. */
|
|
149
|
+
const ephemeralSecrets = new Map();
|
|
150
|
+
/**
|
|
151
|
+
* The secret used when no secret env is set: development or a migration, which never signs.
|
|
152
|
+
*
|
|
153
|
+
* A fixed fallback would let anyone who reaches a dev server (a `farm preview` URL, `--host`) forge
|
|
154
|
+
* Better Auth's signed tokens, such as email verification links. Development instead generates a
|
|
155
|
+
* random secret once per project in `.farm/auth-secret`, next to the local database, so sessions
|
|
156
|
+
* survive restarts. A production migration gets a throwaway one and writes nothing.
|
|
157
|
+
*/
|
|
158
|
+
async function resolveFallbackSecret(options) {
|
|
159
|
+
const file = path.resolve(options.root, ".farm", "auth-secret");
|
|
160
|
+
if (options.mode !== "development")
|
|
161
|
+
return getEphemeralSecret(file);
|
|
162
|
+
const existing = await readSecretFile(file);
|
|
163
|
+
if (existing)
|
|
164
|
+
return existing;
|
|
165
|
+
const generated = randomBytes(32).toString("hex");
|
|
166
|
+
try {
|
|
167
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
168
|
+
// `wx` keeps a secret another process wrote first; that one is read back below.
|
|
169
|
+
await writeFile(file, `${generated}\n`, { flag: "wx", mode: 0o600 });
|
|
170
|
+
return generated;
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
if (error.code !== "EEXIST")
|
|
174
|
+
return getEphemeralSecret(file);
|
|
175
|
+
}
|
|
176
|
+
// Another process won the race, or the file holds no usable secret.
|
|
177
|
+
const raced = await readSecretFile(file);
|
|
178
|
+
if (raced)
|
|
179
|
+
return raced;
|
|
180
|
+
return writeFile(file, `${generated}\n`, { mode: 0o600 }).then(() => generated, () => getEphemeralSecret(file));
|
|
181
|
+
}
|
|
182
|
+
async function readSecretFile(file) {
|
|
183
|
+
try {
|
|
184
|
+
const value = (await readFile(file, "utf8")).trim();
|
|
185
|
+
return value.length >= 32 ? value : undefined;
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function getEphemeralSecret(key) {
|
|
192
|
+
let secret = ephemeralSecrets.get(key);
|
|
193
|
+
if (!secret) {
|
|
194
|
+
secret = randomBytes(32).toString("hex");
|
|
195
|
+
ephemeralSecrets.set(key, secret);
|
|
196
|
+
}
|
|
197
|
+
return secret;
|
|
198
|
+
}
|
|
147
199
|
/**
|
|
148
200
|
* The origin Better Auth signs cookies and validates requests against.
|
|
149
201
|
*
|