MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / makeAuthOptions

Function makeAuthOptions

apps/host-selfhost/src/auth/better-auth.ts:74–307  ·  view source on GitHub ↗
(client: Client, getOrganizationId: () => string, gate?: SignupGate)

Source from the content-addressed store, hash-verified

72// ---------------------------------------------------------------------------
73
74const makeAuthOptions = (client: Client, getOrganizationId: () => string, gate?: SignupGate) => {
75 const config = loadConfig();
76 // A `Secure` session cookie is never sent back over plain HTTP, so an HTTP
77 // alias can sign in and then look signed out on every later request. Drop the
78 // attribute when ANY trusted origin is HTTP. This is not a new relaxation for
79 // the common cases: Better Auth already infers `useSecureCookies` from the
80 // baseURL scheme, so an all-HTTPS instance still gets `true` and the plain
81 // `http://localhost` default still gets `false`. It only changes the mixed
82 // case an operator opts into with EXECUTOR_TRUSTED_ORIGINS.
83 const hasInsecureTrustedOrigin = config.trustedOrigins.some((origin) =>
84 origin.startsWith("http://"),
85 );
86 // Warn only for that mixed case. An HTTP-only instance (local dev, a LAN
87 // deploy) never had Secure cookies to lose, and warning there would fire on
88 // every default boot.
89 const downgradesCanonicalCookies =
90 hasInsecureTrustedOrigin && config.webBaseUrl.startsWith("https://");
91 if (downgradesCanonicalCookies && !warnedInsecureTrustedOrigin) {
92 warnedInsecureTrustedOrigin = true;
93 console.warn(
94 "[executor] EXECUTOR_TRUSTED_ORIGINS contains an http:// origin, so session cookies drop the Secure attribute for every origin — including the https:// canonical URL. Use https:// aliases to keep session cookies transport-secure.",
95 );
96 }
97 // Always resolved (generated + persisted when no env is set); this guards only
98 // an explicitly-set env secret that is too weak.
99 const secret = config.authSecret;
100 if (secret.length < 32) {
101 // oxlint-disable-next-line executor/no-try-catch-or-throw, executor/no-error-constructor -- boundary: a multi-user auth server must not boot with a weak session secret
102 throw new Error("BETTER_AUTH_SECRET (or AUTH_SECRET), if set, must be at least 32 characters");
103 }
104 return {
105 // Hand Better Auth the SAME libSQL client SelfHostDb already opened — NOT a
106 // fresh `{ url }` connection. `{ client }` makes LibsqlDialect adopt the
107 // existing handle (closeClient=false, so SelfHostDb keeps ownership). One
108 // connection means one WAL: see the header comment for why a second
109 // connection is the self-host data-loss bug.
110 //
111 // The cast bridges a dependency skew: @libsql/kysely-libsql pins an older
112 // @libsql/core (0.8) than @libsql/client (0.17), so the two `Client` types
113 // differ — only in `.sync()` (embedded-replica replication, unused here).
114 // The dialect calls execute/batch/transaction/close, which are identical
115 // across both versions, so sharing the 0.17 client is sound at runtime.
116 database: {
117 // oxlint-disable-next-line executor/no-double-cast -- boundary: the two @libsql/core versions' Client types are structurally identical for the calls the dialect makes (see above); no schema/decode applies to a native client handle.
118 dialect: new LibsqlDialect({ client } as unknown as LibsqlDialectConfig),
119 type: "sqlite" as const,
120 },
121 secret,
122 // The canonical browser Origin is config.webBaseUrl; explicitly configured
123 // aliases may also send cookie-authenticated requests. CLI/MCP bearer
124 // requests carry no Origin and are unaffected. We deliberately do NOT derive
125 // either value from the request `Host`: matching the ecosystem (Windmill
126 // `BASE_URL`, n8n `WEBHOOK_URL`), a pinned origin keeps host-header injection
127 // out of OAuth redirects and links. Additional trusted origins affect only
128 // Better Auth's request validation; generated links and OAuth callbacks stay
129 // pinned to config.webBaseUrl.
130 baseURL: config.webBaseUrl,
131 trustedOrigins: [...config.trustedOrigins],

Callers 1

createAuthInstanceFunction · 0.85

Calls 11

loadConfigFunction · 0.90
ssoProviderConfigFunction · 0.90
isOAuthCallbackFunction · 0.90
isAdmittedFunction · 0.90
findRedeemableCodeFunction · 0.90
consumeInviteCodeFunction · 0.90
bearerFunction · 0.85
orgHasNoMembersFunction · 0.85
inviteCodeFromFunction · 0.85
warnMethod · 0.80
mcpFunction · 0.50

Tested by

no test coverage detected