@shivaedev/identity 0.14.0 → 0.15.0
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 +12 -0
- package/README.md +38 -1
- package/dist/server/better-auth/adapters/effectHookOptions.d.ts +6 -0
- package/dist/server/better-auth/adapters/effectHookOptions.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/effectHookOptions.js +62 -0
- package/dist/server/better-auth/adapters/effectHookOptions.js.map +1 -0
- package/dist/server/better-auth/adapters/hookRuntime.d.ts +7 -0
- package/dist/server/better-auth/adapters/hookRuntime.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/hookRuntime.js +37 -0
- package/dist/server/better-auth/adapters/hookRuntime.js.map +1 -0
- package/dist/server/better-auth/adapters/response.d.ts +23 -0
- package/dist/server/better-auth/adapters/response.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/response.js +41 -0
- package/dist/server/better-auth/adapters/response.js.map +1 -0
- package/dist/server/better-auth/databaseHooks.d.ts +12 -0
- package/dist/server/better-auth/databaseHooks.d.ts.map +1 -0
- package/dist/server/better-auth/databaseHooks.js +2 -0
- package/dist/server/better-auth/databaseHooks.js.map +1 -0
- package/dist/server/better-auth/mount.d.ts +2 -0
- package/dist/server/better-auth/mount.d.ts.map +1 -1
- package/dist/server/better-auth/mount.js +6 -1
- package/dist/server/better-auth/mount.js.map +1 -1
- package/dist/server/better-auth/mountRoutes.d.ts +1 -20
- package/dist/server/better-auth/mountRoutes.d.ts.map +1 -1
- package/dist/server/better-auth/mountRoutes.js +2 -39
- package/dist/server/better-auth/mountRoutes.js.map +1 -1
- package/dist/server/better-auth/testing.d.ts +18 -0
- package/dist/server/better-auth/testing.d.ts.map +1 -1
- package/dist/server/better-auth/testing.js +6 -2
- package/dist/server/better-auth/testing.js.map +1 -1
- package/package.json +7 -7
- package/src/server/better-auth/adapters/effectHookOptions.ts +87 -0
- package/src/server/better-auth/adapters/hookRuntime.ts +41 -0
- package/src/server/better-auth/adapters/response.ts +56 -0
- package/src/server/better-auth/databaseHooks.ts +15 -0
- package/src/server/better-auth/mount.ts +8 -1
- package/src/server/better-auth/mountRoutes.ts +2 -53
- package/src/server/better-auth/testing.ts +10 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.15.0 - 2026-11-01
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Applications supply Better Auth database hooks as Effects through `betterAuthMount`'s `databaseHooks` option. Mounted requests provide their services, tracing and interruption; typed failures carry an application code and message, and defects remain request defects.
|
|
8
|
+
- Identity exposes `signInAgain(auth, userId)` to mint a fresh test session for an existing user using the caller's Effect Clock and Better Auth's configured lifetime.
|
|
9
|
+
- Generated apps provide session renewal verbs for HTTP visitors and browser cookies after long story clock jumps.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- The starter's app and cookie story kits start TestClock at the present by default so Better Auth's wall-clock endpoints accept minted sessions. Stories can still choose an explicit clock.
|
|
14
|
+
|
|
3
15
|
## 0.14.0 - 2026-10-31
|
|
4
16
|
|
|
5
17
|
### Fixed
|
package/README.md
CHANGED
|
@@ -135,6 +135,41 @@ The auth HTTP server span records the resolved rate-limit address in `auth.rate_
|
|
|
135
135
|
|
|
136
136
|
For email sign-in through `appHost`, the session record's `ipAddress` follows the same rule. With a trusted peer, `AUTH_CLIENT_IP_HEADER=x-real-ip` stores `X-Real-IP` even if `X-Forwarded-For` supplies a different address; the default header selects the rightmost untrusted address in the `X-Forwarded-For` chain, ignoring trusted proxy hops on the right. With an untrusted peer, neither header can supply the stored address. Sign-in attempts from an untrusted peer share one bucket per path; with a trusted peer and `x-real-ip`, changing `X-Forwarded-For` does not change the bucket, while changing `X-Real-IP` does.
|
|
137
137
|
|
|
138
|
+
#### Application database hooks
|
|
139
|
+
|
|
140
|
+
Pass Effect hooks in the mount's second argument under `databaseHooks`. The shape, arguments and successful results match Better Auth's database hooks; each callback returns an Effect instead of a Promise. `BetterAuthDatabaseHooks` and the structural `BetterAuthHookError` type live in `@shivaedev/identity/server/better-auth/databaseHooks.ts`.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import type { BetterAuthOptions } from "better-auth";
|
|
144
|
+
import { Context, Effect } from "effect";
|
|
145
|
+
import type { BetterAuthHookError } from "@shivaedev/identity/server/better-auth/databaseHooks.ts";
|
|
146
|
+
import { betterAuthMount } from "@shivaedev/identity/server/better-auth/mount.ts";
|
|
147
|
+
|
|
148
|
+
class SessionAdmission extends Context.Service<
|
|
149
|
+
SessionAdmission,
|
|
150
|
+
{
|
|
151
|
+
readonly check: (userId: string) => Effect.Effect<void, BetterAuthHookError>;
|
|
152
|
+
}
|
|
153
|
+
>()("app/SessionAdmission") {}
|
|
154
|
+
|
|
155
|
+
const authentication = (options: BetterAuthOptions) =>
|
|
156
|
+
betterAuthMount(options, {
|
|
157
|
+
databaseHooks: {
|
|
158
|
+
session: {
|
|
159
|
+
create: {
|
|
160
|
+
before: (session) => Effect.flatMap(SessionAdmission, ({ check }) => check(session.userId)),
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Provide the hook's services when building the mount. Mounted HTTP requests run hooks with the request's services, spans, log annotations and cancellation signal; request services override the mount's services. A direct call through `auth.api` uses the mount's services and the endpoint request's signal when present.
|
|
168
|
+
|
|
169
|
+
Fail a `before` hook with `{ code: "MEMBERS_ONLY", message: "Membership is required" }`, or an application tagged error with those fields, to refuse the operation with HTTP 403 and that code and message. Defects remain defects in the mounted request, even when Better Auth catches the callback rejection. Hooks follow Better Auth's lifecycle: use `before` to refuse a write; `after` runs after the write and does not undo it.
|
|
170
|
+
|
|
171
|
+
Native callbacks in the first argument run before the corresponding Effect callbacks. A native `before` returning `false` stops the Effect callback too. Data changes compose in order. Identity's sign-in policy still runs after the application's session hook.
|
|
172
|
+
|
|
138
173
|
### Role assignments
|
|
139
174
|
|
|
140
175
|
Pass `{ defaultRole: "member", roles: AppAccess }` as the second argument to `betterAuthMount`. Identity adds `/admin/set-role`, `/admin/update-user` and `/admin/create-user` to the disabled HTTP paths and sets the Better Auth admin plugin's `adminRoles` to `[]`. Existing disabled paths remain in effect. The admin Users page remains the application path for role changes.
|
|
@@ -603,10 +638,12 @@ const { user, headers, cookies } = await Effect.runPromise(
|
|
|
603
638
|
);
|
|
604
639
|
```
|
|
605
640
|
|
|
606
|
-
`options` is the application's auth configuration. `signedInUser` saves the supplied user fields, creates each optional `providerAccounts` entry for that user through Better Auth's internal adapter, and mints one session through Better Auth's `testUtils`, without OAuth or password hashing. The accounts carry no tokens or password. Omitting `providerAccounts` creates only the user and session. It returns an Effect containing Better Auth's login result: the user, session, signed request headers, browser cookies and token. In a story, call `yield* signedInUser(auth, fields)` so minting uses that story's TestClock. If the story also calls Better Auth's auth endpoints, start its TestClock at the present
|
|
641
|
+
`options` is the application's auth configuration. `signedInUser` saves the supplied user fields, creates each optional `providerAccounts` entry for that user through Better Auth's internal adapter, and mints one session through Better Auth's `testUtils`, without OAuth or password hashing. The accounts carry no tokens or password. Omitting `providerAccounts` creates only the user and session. It returns an Effect containing Better Auth's login result: the user, session, signed request headers, browser cookies and token. In a story, call `yield* signedInUser(auth, fields)` so minting uses that story's TestClock. If the story also calls Better Auth's auth endpoints, start its TestClock at the present because those endpoints still check the provider's wall clock. The starter’s app and cookie story kits select that default; stories can still pass an explicit `clock`. To mint users with roles, use the auth instance returned by `betterAuthMount(options, { defaultRole: "member", roles: Access })`. Send `headers` to the real host, or pass `cookies` to test-story's `seedCookies` in a DOM story after setting its host location with `domFetch`. Keep a real sign-in story for each sign-in flow; feature stories start from the minted precondition.
|
|
607
642
|
|
|
608
643
|
`passwordUser(auth, { email, password, ...fields })` from the same testing module saves a user and a credential account using Better Auth's configured password hasher, and returns an Effect containing the user without minting a session. Use it for the one story that signs in through `/api/auth/sign-in/email`; keep feature stories on `signedInUser` so they avoid password hashing.
|
|
609
644
|
|
|
645
|
+
Call `yield* signInAgain(auth, userId)` from `server/better-auth/testing.ts` to mint a fresh session for an existing user after a long story clock jump. It returns the same login shape as `signedInUser` and sets expiry from the caller’s Effect Clock and the configured session lifetime. Replace the visitor’s request headers or seed the returned browser cookies.
|
|
646
|
+
|
|
610
647
|
Use `storedProviderAccounts(auth, userId)` to observe credential storage through Better Auth's internal adapter. It returns account and provider IDs with `hasAccessToken`, `hasRefreshToken`, `hasIdToken` and `hasPassword` booleans. It never returns token values, passwords or password hashes. A DOM story kit that exposes its test auth instance and a session readout can use both helpers:
|
|
611
648
|
|
|
612
649
|
```ts
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BetterAuthOptions } from "better-auth";
|
|
2
|
+
import type { Effect } from "effect";
|
|
3
|
+
import type { BetterAuthDatabaseHooks } from "#server/better-auth/databaseHooks.ts";
|
|
4
|
+
import type { hookRuntime } from "./hookRuntime.ts";
|
|
5
|
+
export declare function effectHookOptions<TOptions extends BetterAuthOptions, R>(options: TOptions, hooks: BetterAuthDatabaseHooks<R> | undefined, runtime: Effect.Success<ReturnType<typeof hookRuntime<R>>>): TOptions;
|
|
6
|
+
//# sourceMappingURL=effectHookOptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effectHookOptions.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/effectHookOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAA0B,MAAM,aAAa,CAAC;AAC7E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,uBAAuB,EAAuB,MAAM,sCAAsC,CAAC;AACzG,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAsBpD,wBAAgB,iBAAiB,CAAC,QAAQ,SAAS,iBAAiB,EAAE,CAAC,EACtE,OAAO,EAAE,QAAQ,EACjB,KAAK,EAAE,uBAAuB,CAAC,CAAC,CAAC,GAAG,SAAS,EAC7C,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,GACxD,QAAQ,CAyDV"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
function mergeResult(previous, next) {
|
|
2
|
+
if (typeof previous === "object" && typeof next === "object") {
|
|
3
|
+
return { ...next, data: { ...previous.data, ...next.data } };
|
|
4
|
+
}
|
|
5
|
+
return next === undefined || next === true ? (previous ?? next) : next;
|
|
6
|
+
}
|
|
7
|
+
export function effectHookOptions(options, hooks, runtime) {
|
|
8
|
+
function composeBefore(hook, own) {
|
|
9
|
+
return async (data, endpoint) => {
|
|
10
|
+
const previous = await own?.(data, endpoint);
|
|
11
|
+
if (previous === false) {
|
|
12
|
+
return previous;
|
|
13
|
+
}
|
|
14
|
+
const input = typeof previous === "object" ? { ...data, ...previous.data } : data;
|
|
15
|
+
const next = await runtime.runHook(() => hook(input, endpoint), endpoint?.request?.signal);
|
|
16
|
+
return mergeResult(previous, next);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function composeAfter(hook, own) {
|
|
20
|
+
return async (data, endpoint) => {
|
|
21
|
+
await own?.(data, endpoint);
|
|
22
|
+
await runtime.runHook(() => hook(data, endpoint), endpoint?.request?.signal);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function operation(own, application) {
|
|
26
|
+
return {
|
|
27
|
+
...own,
|
|
28
|
+
...(application?.before === undefined ? {} : { before: composeBefore(application.before, own?.before) }),
|
|
29
|
+
...(application?.after === undefined ? {} : { after: composeAfter(application.after, own?.after) }),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (hooks === undefined) {
|
|
33
|
+
return options;
|
|
34
|
+
}
|
|
35
|
+
const native = options.databaseHooks;
|
|
36
|
+
return {
|
|
37
|
+
...options,
|
|
38
|
+
databaseHooks: {
|
|
39
|
+
account: {
|
|
40
|
+
create: operation(native?.account?.create, hooks.account?.create),
|
|
41
|
+
delete: operation(native?.account?.delete, hooks.account?.delete),
|
|
42
|
+
update: operation(native?.account?.update, hooks.account?.update),
|
|
43
|
+
},
|
|
44
|
+
session: {
|
|
45
|
+
create: operation(native?.session?.create, hooks.session?.create),
|
|
46
|
+
delete: operation(native?.session?.delete, hooks.session?.delete),
|
|
47
|
+
update: operation(native?.session?.update, hooks.session?.update),
|
|
48
|
+
},
|
|
49
|
+
user: {
|
|
50
|
+
create: operation(native?.user?.create, hooks.user?.create),
|
|
51
|
+
delete: operation(native?.user?.delete, hooks.user?.delete),
|
|
52
|
+
update: operation(native?.user?.update, hooks.user?.update),
|
|
53
|
+
},
|
|
54
|
+
verification: {
|
|
55
|
+
create: operation(native?.verification?.create, hooks.verification?.create),
|
|
56
|
+
delete: operation(native?.verification?.delete, hooks.verification?.delete),
|
|
57
|
+
update: operation(native?.verification?.update, hooks.verification?.update),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=effectHookOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effectHookOptions.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/effectHookOptions.ts"],"names":[],"mappings":"AAkBA,SAAS,WAAW,CAAuB,QAAuB,EAAE,IAAO;IAC1E,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9D,OAAO,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,EAAE,GAAG,QAAQ,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;IAC9D,CAAC;IACD,OAAO,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,IAAI,IAAI,CA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EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC;gBAC3D,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC;gBAC3D,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC;aAC3D;YACD,YAAY,EAAE;gBACb,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC;gBAC3E,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC;gBAC3E,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC;aAC3E;SACD;KACD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Cause, Effect } from "effect";
|
|
2
|
+
import type { BetterAuthHookError } from "#server/better-auth/databaseHooks.ts";
|
|
3
|
+
export declare const hookRuntime: <R>() => Effect.Effect<{
|
|
4
|
+
runHook: <A>(hook: () => Effect.Effect<A, BetterAuthHookError, R>, signal: AbortSignal | undefined) => Promise<A>;
|
|
5
|
+
runRequest: <A>(request: (signal: AbortSignal) => Promise<A>) => Effect.Effect<A, Cause.UnknownError, never>;
|
|
6
|
+
}, never, R>;
|
|
7
|
+
//# sourceMappingURL=hookRuntime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hookRuntime.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/hookRuntime.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,KAAK,EAAW,MAAM,EAAU,MAAM,QAAQ,CAAC;AAC7D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAEhF,eAAO,MAAM,WAAW,GAAgC,CAAC;cAIvC,CAAC,QAAQ,MAAM,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,mBAAmB,EAAE,CAAC,CAAC,UAAU,WAAW,GAAG,SAAS,KAAG,OAAO,CAAC,CAAC,CAAC;iBAiBxG,CAAC,WAAW,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC;YAc5D,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { APIError } from "better-auth/api";
|
|
3
|
+
import { Context, Effect, Result } from "effect";
|
|
4
|
+
export const hookRuntime = Effect.fnUntraced(function* () {
|
|
5
|
+
const context = yield* Effect.context();
|
|
6
|
+
const requests = new AsyncLocalStorage();
|
|
7
|
+
return {
|
|
8
|
+
runHook: async (hook, signal) => {
|
|
9
|
+
const request = requests.getStore();
|
|
10
|
+
function remember(cause) {
|
|
11
|
+
return Effect.sync(() => {
|
|
12
|
+
if (request !== undefined) {
|
|
13
|
+
request.cause = cause;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const result = await Effect.runPromiseWith(request?.context ?? context)(Effect.suspend(hook).pipe(Effect.result, Effect.tapCause(remember)), {
|
|
18
|
+
signal,
|
|
19
|
+
});
|
|
20
|
+
if (Result.isFailure(result)) {
|
|
21
|
+
throw new APIError("FORBIDDEN", { code: result.failure.code, message: result.failure.message });
|
|
22
|
+
}
|
|
23
|
+
return result.success;
|
|
24
|
+
},
|
|
25
|
+
runRequest: (request) => Effect.withFiber((fiber) => Effect.gen(function* () {
|
|
26
|
+
const state = {
|
|
27
|
+
context: Context.merge(context, fiber.context),
|
|
28
|
+
};
|
|
29
|
+
const exit = yield* Effect.exit(Effect.tryPromise((signal) => requests.run(state, () => request(signal))));
|
|
30
|
+
if (state.cause !== undefined) {
|
|
31
|
+
return yield* Effect.failCause(state.cause);
|
|
32
|
+
}
|
|
33
|
+
return yield* exit;
|
|
34
|
+
})),
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=hookRuntime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hookRuntime.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/hookRuntime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAc,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAG7D,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC;IACrD,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAK,CAAC;IAC3C,MAAM,QAAQ,GAAG,IAAI,iBAAiB,EAAwE,CAAC;IAC/G,OAAO;QACN,OAAO,EAAE,KAAK,EAAK,IAAoD,EAAE,MAA+B,EAAc,EAAE;YACvH,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,EAAE,CAAC;YACpC,SAAS,QAAQ,CAAC,KAAyB;gBAC1C,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;oBACvB,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;wBAC3B,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC;oBACvB,CAAC;gBACF,CAAC,CAAC,CAAC;YACJ,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,OAAO,IAAI,OAAO,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,EAAE;gBAC5I,MAAM;aACN,CAAC,CAAC;YACH,IAAI,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC9B,MAAM,IAAI,QAAQ,CAAC,WAAW,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;YACjG,CAAC;YACD,OAAO,MAAM,CAAC,OAAO,CAAC;QACvB,CAAC;QACD,UAAU,EAAE,CAAI,OAA4C,EAAE,EAAE,CAC/D,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE,CAC1B,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;YACnB,MAAM,KAAK,GAAyE;gBACnF,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC;aAC9C,CAAC;YACF,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YAC3G,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;gBAC/B,OAAO,KAAK,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC7C,CAAC;YACD,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC;QACpB,CAAC,CAAC,CACF;KACF,CAAC;AACH,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Cause, Effect } from "effect";
|
|
2
|
+
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
3
|
+
import { type BearerSessionsOptions } from "#server/better-auth/bearerSessions.ts";
|
|
4
|
+
export interface MountedAuth {
|
|
5
|
+
readonly $context: Promise<{
|
|
6
|
+
readonly authCookies: {
|
|
7
|
+
readonly sessionToken: {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
}>;
|
|
12
|
+
readonly handler: (request: Request) => Promise<Response>;
|
|
13
|
+
}
|
|
14
|
+
export interface MountRouteOptions {
|
|
15
|
+
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
16
|
+
readonly clientIpHeader: string;
|
|
17
|
+
readonly ipv6Subnet?: number | undefined;
|
|
18
|
+
readonly roles?: RoleSet | undefined;
|
|
19
|
+
readonly runRequest?: (<A>(request: (signal: AbortSignal) => Promise<A>) => Effect.Effect<A, Cause.UnknownError>) | undefined;
|
|
20
|
+
readonly trustedProxy?: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
export declare const response: (auth: MountedAuth, web: Request, options: MountRouteOptions) => Effect.Effect<Response, import("@shivaedev/errors/taxonomy.ts").Forbidden | import("effect/Schema").SchemaError | Cause.UnknownError, never>;
|
|
23
|
+
//# sourceMappingURL=response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,KAAK,qBAAqB,EAA2D,MAAM,uCAAuC,CAAC;AAG5I,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;QAAE,QAAQ,CAAC,WAAW,EAAE;YAAE,QAAQ,CAAC,YAAY,EAAE;gBAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAA;KAAE,CAAC,CAAC;IAC3G,QAAQ,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC1D;AACD,MAAM,WAAW,iBAAiB;IACjC,QAAQ,CAAC,cAAc,CAAC,EAAE,qBAAqB,GAAG,SAAS,CAAC;IAC5D,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC,GAAG,SAAS,CAAC;IAC9H,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC3C;AA6BD,eAAO,MAAM,QAAQ,+MAUnB,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { cookieSessionResponse, hasSessionCookie, sessionHeaders } from "#server/better-auth/bearerSessions.ts";
|
|
3
|
+
import { sessionPermissions } from "#server/better-auth/sessionPermissions.ts";
|
|
4
|
+
function replacesSession(request, reply, native) {
|
|
5
|
+
return (native
|
|
6
|
+
&& reply.ok
|
|
7
|
+
&& reply.headers.has("set-auth-token")
|
|
8
|
+
&& request.headers.has("authorization")
|
|
9
|
+
&& new URL(request.url).pathname.startsWith("/api/auth/sign-in/"));
|
|
10
|
+
}
|
|
11
|
+
async function providerResponse(auth, web, options) {
|
|
12
|
+
if (options.bearerSessions === undefined) {
|
|
13
|
+
return auth.handler(web);
|
|
14
|
+
}
|
|
15
|
+
const { authCookies } = await auth.$context;
|
|
16
|
+
const cookieName = authCookies.sessionToken.name;
|
|
17
|
+
const headers = sessionHeaders(web.headers, cookieName);
|
|
18
|
+
const native = options.bearerSessions.origins.includes(web.headers.get("origin") ?? "") && !hasSessionCookie(web.headers, cookieName);
|
|
19
|
+
const request = new Request(web, { headers });
|
|
20
|
+
const reply = await auth.handler(request);
|
|
21
|
+
if (replacesSession(request, reply, native)) {
|
|
22
|
+
const revoked = await auth.handler(new Request(new URL("/api/auth/sign-out", web.url), { body: "{}", headers, method: "POST" }));
|
|
23
|
+
await revoked.body?.cancel();
|
|
24
|
+
if (!revoked.ok) {
|
|
25
|
+
throw new Error("Session replacement failed");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return native ? reply : cookieSessionResponse(reply);
|
|
29
|
+
}
|
|
30
|
+
export const response = Effect.fnUntraced(function* (auth, web, options) {
|
|
31
|
+
const sessionRequest = options.roles !== undefined && new URL(web.url).pathname === "/api/auth/get-session";
|
|
32
|
+
const url = new URL(web.url);
|
|
33
|
+
if (sessionRequest) {
|
|
34
|
+
url.searchParams.set("disableCookieCache", "true");
|
|
35
|
+
}
|
|
36
|
+
const request = sessionRequest ? new Request(url, web) : web;
|
|
37
|
+
const runRequest = options.runRequest ?? Effect.tryPromise;
|
|
38
|
+
const reply = yield* runRequest((signal) => providerResponse(auth, new Request(request, { signal }), options));
|
|
39
|
+
return sessionRequest && reply.ok && options.roles !== undefined ? yield* sessionPermissions(reply, options.roles) : reply;
|
|
40
|
+
});
|
|
41
|
+
//# sourceMappingURL=response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE5C,OAAO,EAA8B,qBAAqB,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AAC5I,OAAO,EAAE,kBAAkB,EAAE,MAAM,2CAA2C,CAAC;AAc/E,SAAS,eAAe,CAAC,OAAgB,EAAE,KAAe,EAAE,MAAe;IAC1E,OAAO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AAE,IAAI,OAAO,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,kBAAkB,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAC5H,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BetterAuthOptions } from "better-auth";
|
|
2
|
+
import type { Effect } from "effect";
|
|
3
|
+
export interface BetterAuthHookError {
|
|
4
|
+
readonly code: string;
|
|
5
|
+
readonly message: string;
|
|
6
|
+
}
|
|
7
|
+
type EffectHooks<T, R> = {
|
|
8
|
+
readonly [TKey in keyof T]: NonNullable<T[TKey]> extends (...args: infer TArgs) => infer TResult ? (...args: TArgs) => Effect.Effect<Awaited<TResult>, BetterAuthHookError, R> : EffectHooks<NonNullable<T[TKey]>, R>;
|
|
9
|
+
};
|
|
10
|
+
export type BetterAuthDatabaseHooks<R = never> = EffectHooks<NonNullable<BetterAuthOptions["databaseHooks"]>, R>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=databaseHooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"databaseHooks.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/databaseHooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,MAAM,WAAW,mBAAmB;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CACzB;AAED,KAAK,WAAW,CAAC,CAAC,EAAE,CAAC,IAAI;IACxB,QAAQ,EAAE,IAAI,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,MAAM,KAAK,KAAK,MAAM,OAAO,GAC7F,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,mBAAmB,EAAE,CAAC,CAAC,GAC3E,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,uBAAuB,CAAC,CAAC,GAAG,KAAK,IAAI,WAAW,CAAC,WAAW,CAAC,iBAAiB,CAAC,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"databaseHooks.js","sourceRoot":"","sources":["../../../src/server/better-auth/databaseHooks.ts"],"names":[],"mappings":""}
|
|
@@ -6,8 +6,10 @@ import { type StoredRoleOptions } from "#server/better-auth/adapters/storedRoleO
|
|
|
6
6
|
import { type BearerSessionsOptions } from "#server/better-auth/bearerSessions.ts";
|
|
7
7
|
import { type SignInPolicy } from "#server/better-auth/signInGate.ts";
|
|
8
8
|
import type { AccountRoleSeed } from "./accountRoleSeed.ts";
|
|
9
|
+
import type { BetterAuthDatabaseHooks } from "./databaseHooks.ts";
|
|
9
10
|
export type BetterAuthMountOptions<E = never, R = never> = {
|
|
10
11
|
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
12
|
+
readonly databaseHooks?: BetterAuthDatabaseHooks<R> | undefined;
|
|
11
13
|
readonly hardening?: BetterAuthHardeningOptions;
|
|
12
14
|
readonly signIn?: SignInPolicy<E, R> | undefined;
|
|
13
15
|
} & ({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mount.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/mount.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAc,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAoB,MAAM,QAAQ,CAAC;AAC1D,OA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CAAC;IAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;CAAE,CAAC,CAChH,CAAC;AAEF,eAAO,MAAM,eAAe,SAA0D,QAAQ,SAAS,iBAAiB,EAAE,CAAC,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4DAiEpI,CAAC"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { betterAuth } from "better-auth";
|
|
2
2
|
import { Config, Effect, Option, Redacted } from "effect";
|
|
3
|
+
import { effectHookOptions } from "#server/better-auth/adapters/effectHookOptions.ts";
|
|
3
4
|
import { withHardening } from "#server/better-auth/adapters/hardening.ts";
|
|
5
|
+
import { hookRuntime } from "#server/better-auth/adapters/hookRuntime.ts";
|
|
4
6
|
import { roleSeedOptions } from "#server/better-auth/adapters/roleSeedOptions.ts";
|
|
5
7
|
import { signInPolicyOptions } from "#server/better-auth/adapters/signInPolicyOptions.ts";
|
|
6
8
|
import { storedRoleOptions } from "#server/better-auth/adapters/storedRoleOptions.ts";
|
|
@@ -25,7 +27,9 @@ export const betterAuthMount = Effect.fn("Platform.betterAuthMount")(function* (
|
|
|
25
27
|
yield* policy.accounts;
|
|
26
28
|
}
|
|
27
29
|
const gate = yield* signInGate(policy);
|
|
28
|
-
const
|
|
30
|
+
const runtime = yield* hookRuntime();
|
|
31
|
+
const hooked = effectHookOptions(options, mount.databaseHooks, runtime);
|
|
32
|
+
const configured = mount.roles === undefined ? hooked : storedRoleOptions(closeRoleEndpoints(hooked), mount);
|
|
29
33
|
const gated = yield* signInPolicyOptions(configured, gate, () => auth.$context);
|
|
30
34
|
const seed = mount.roleSeed === undefined ? undefined : yield* mount.roleSeed;
|
|
31
35
|
const seeded = roleSeedOptions(gate.active ? { ...configured, ...gated } : configured, seed);
|
|
@@ -62,6 +66,7 @@ export const betterAuthMount = Effect.fn("Platform.betterAuthMount")(function* (
|
|
|
62
66
|
clientIpHeader,
|
|
63
67
|
ipv6Subnet: options.advanced?.ipAddress?.ipv6Subnet,
|
|
64
68
|
roles: mount.roles,
|
|
69
|
+
runRequest: runtime.runRequest,
|
|
65
70
|
trustedProxy,
|
|
66
71
|
});
|
|
67
72
|
const admit = admittedSessions(gate, () => auth.$context, mount.bearerSessions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mount.js","sourceRoot":"","sources":["../../../src/server/better-auth/mount.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,UAAU,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAE1D,OAAO,EAAmC,aAAa,EAAE,MAAM,2CAA2C,CAAC;AAC3G,OAAO,EAAE,eAAe,EAAE,MAAM,iDAAiD,CAAC;AAClF,OAAO,EAAE,mBAAmB,EAAE,MAAM,qDAAqD,CAAC;AAC1F,OAAO,EAA0B,iBAAiB,EAAE,MAAM,mDAAmD,CAAC;AAC9G,OAAO,EAA8B,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AACvG,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yCAAyC,CAAC;AAC3E,OAAO,EAAyC,UAAU,EAAE,MA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K,CAAC,cAAc,CAAC,CAAC;IAChF,MAAM,MAAM,GAAG,gBAAgB,CAAC,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC3D,MAAM,aAAa,GAAY,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;IACnE,MAAM,QAAQ,GAAG,eAAe,CAAC,aAAa,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IACpE,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,IAAI,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC;AAClG,CAAC,CAAC,CAAC"}
|
|
@@ -1,23 +1,4 @@
|
|
|
1
1
|
import { HttpRouter } from "effect/http";
|
|
2
|
-
import type
|
|
3
|
-
import { type BearerSessionsOptions } from "#server/better-auth/bearerSessions.ts";
|
|
4
|
-
interface MountedAuth {
|
|
5
|
-
readonly $context: Promise<{
|
|
6
|
-
readonly authCookies: {
|
|
7
|
-
readonly sessionToken: {
|
|
8
|
-
readonly name: string;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
}>;
|
|
12
|
-
readonly handler: (request: Request) => Promise<Response>;
|
|
13
|
-
}
|
|
14
|
-
interface MountRouteOptions {
|
|
15
|
-
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
16
|
-
readonly clientIpHeader: string;
|
|
17
|
-
readonly ipv6Subnet?: number | undefined;
|
|
18
|
-
readonly roles?: RoleSet | undefined;
|
|
19
|
-
readonly trustedProxy?: string | undefined;
|
|
20
|
-
}
|
|
2
|
+
import { type MountedAuth, type MountRouteOptions } from "#server/better-auth/adapters/response.ts";
|
|
21
3
|
export declare function mountRoutes(auth: MountedAuth, options: MountRouteOptions): import("effect/Layer").Layer<never, never, HttpRouter.HttpRouter>;
|
|
22
|
-
export {};
|
|
23
4
|
//# sourceMappingURL=mountRoutes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mountRoutes.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/mountRoutes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAyC,MAAM,aAAa,CAAC;AAEhF,OAAO,
|
|
1
|
+
{"version":3,"file":"mountRoutes.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/mountRoutes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAyC,MAAM,aAAa,CAAC;AAEhF,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,iBAAiB,EAAY,MAAM,0CAA0C,CAAC;AAI9G,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,iBAAiB,qEA4BxE"}
|
|
@@ -1,46 +1,9 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
3
3
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
|
-
import {
|
|
4
|
+
import { response } from "#server/better-auth/adapters/response.ts";
|
|
5
5
|
import { providerFailure } from "#server/providerFailure.ts";
|
|
6
6
|
import { clientIpHeaders } from "./clientIp.js";
|
|
7
|
-
import { sessionPermissions } from "./sessionPermissions.js";
|
|
8
|
-
function replacesSession(request, response, native) {
|
|
9
|
-
return (native
|
|
10
|
-
&& response.ok
|
|
11
|
-
&& response.headers.has("set-auth-token")
|
|
12
|
-
&& request.headers.has("authorization")
|
|
13
|
-
&& new URL(request.url).pathname.startsWith("/api/auth/sign-in/"));
|
|
14
|
-
}
|
|
15
|
-
async function providerResponse(auth, web, options) {
|
|
16
|
-
if (options.bearerSessions === undefined) {
|
|
17
|
-
return auth.handler(web);
|
|
18
|
-
}
|
|
19
|
-
const { authCookies } = await auth.$context;
|
|
20
|
-
const cookieName = authCookies.sessionToken.name;
|
|
21
|
-
const headers = sessionHeaders(web.headers, cookieName);
|
|
22
|
-
const native = options.bearerSessions.origins.includes(web.headers.get("origin") ?? "") && !hasSessionCookie(web.headers, cookieName);
|
|
23
|
-
const request = new Request(web, { headers });
|
|
24
|
-
const response = await auth.handler(request);
|
|
25
|
-
if (replacesSession(request, response, native)) {
|
|
26
|
-
const revoked = await auth.handler(new Request(new URL("/api/auth/sign-out", web.url), { body: "{}", headers, method: "POST" }));
|
|
27
|
-
await revoked.body?.cancel();
|
|
28
|
-
if (!revoked.ok) {
|
|
29
|
-
throw new Error("Session replacement failed");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return native ? response : cookieSessionResponse(response);
|
|
33
|
-
}
|
|
34
|
-
const authResponse = Effect.fnUntraced(function* (auth, web, options) {
|
|
35
|
-
const sessionRequest = options.roles !== undefined && new URL(web.url).pathname === "/api/auth/get-session";
|
|
36
|
-
const url = new URL(web.url);
|
|
37
|
-
if (sessionRequest) {
|
|
38
|
-
url.searchParams.set("disableCookieCache", "true");
|
|
39
|
-
}
|
|
40
|
-
const request = sessionRequest ? new Request(url, web) : web;
|
|
41
|
-
const response = yield* Effect.tryPromise((signal) => providerResponse(auth, new Request(request, { signal }), options));
|
|
42
|
-
return sessionRequest && response.ok && options.roles !== undefined ? yield* sessionPermissions(response, options.roles) : response;
|
|
43
|
-
});
|
|
44
7
|
export function mountRoutes(auth, options) {
|
|
45
8
|
const setClientIp = clientIpHeaders(options);
|
|
46
9
|
return HttpRouter.use((router) => router.add("*", "/api/auth/*", (request) => Effect.gen(function* () {
|
|
@@ -50,7 +13,7 @@ export function mountRoutes(auth, options) {
|
|
|
50
13
|
"auth.rate_limit.address": clientIp.address,
|
|
51
14
|
"auth.rate_limit.address_source": clientIp.source,
|
|
52
15
|
});
|
|
53
|
-
return HttpServerResponse.fromWeb(yield*
|
|
16
|
+
return HttpServerResponse.fromWeb(yield* response(auth, web, options).pipe(Effect.catchTag("Forbidden", (error) => Effect.succeed(Response.json({ message: error.message }, { status: HttpStatus.fromLiteral("Forbidden") }))), Effect.catch(providerFailure)));
|
|
54
17
|
}).pipe(Effect.catch(() => Effect.succeed(HttpServerResponse.text("Authentication is temporarily unavailable", { status: HttpStatus.fromLiteral("ServiceUnavailable") }))))));
|
|
55
18
|
}
|
|
56
19
|
//# sourceMappingURL=mountRoutes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mountRoutes.js","sourceRoot":"","sources":["../../../src/server/better-auth/mountRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"mountRoutes.js","sourceRoot":"","sources":["../../../src/server/better-auth/mountRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAA4C,QAAQ,EAAE,MAAM,0CAA0C,CAAC;AAC9G,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,MAAM,UAAU,WAAW,CAAC,IAAiB,EAAE,OAA0B;IACxE,MAAM,WAAW,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IAC7C,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAChC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,aAAa,EAAE,CAAC,OAAO,EAAE,EAAE,CAC1C,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnB,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACpD,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAC3C,KAAK,CAAC,CAAC,MAAM,CAAC,mBAAmB,CAAC;YACjC,yBAAyB,EAAE,QAAQ,CAAC,OAAO;YAC3C,gCAAgC,EAAE,QAAQ,CAAC,MAAM;SACjD,CAAC,CAAC;QACH,OAAO,kBAAkB,CAAC,OAAO,CAChC,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC,IAAI,CACvC,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CACtC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAC1G,EACD,MAAM,CAAC,KAAK,CAAC,eAAe,CAAC,CAC7B,CACD,CAAC;IACH,CAAC,CAAC,CAAC,IAAI,CACN,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CACjB,MAAM,CAAC,OAAO,CACb,kBAAkB,CAAC,IAAI,CAAC,2CAA2C,EAAE,EAAE,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,oBAAoB,CAAC,EAAE,CAAC,CAC9H,CACD,CACD,CACD,CACD,CAAC;AACH,CAAC"}
|
|
@@ -34,6 +34,24 @@ export declare const signedInUser: (auth: {
|
|
|
34
34
|
expiresAt: Date;
|
|
35
35
|
};
|
|
36
36
|
}, never, never>;
|
|
37
|
+
export declare const signInAgain: (auth: {
|
|
38
|
+
readonly $context: Promise<TestContext & AccountContext & Pick<AuthContext, "sessionConfig">>;
|
|
39
|
+
}, userId: string) => Effect.Effect<{
|
|
40
|
+
user: import("better-auth").User;
|
|
41
|
+
headers: Headers;
|
|
42
|
+
cookies: import("better-auth/plugins").TestCookie[];
|
|
43
|
+
token: string;
|
|
44
|
+
session: {
|
|
45
|
+
id: string;
|
|
46
|
+
createdAt: Date;
|
|
47
|
+
updatedAt: Date;
|
|
48
|
+
userId: string;
|
|
49
|
+
token: string;
|
|
50
|
+
ipAddress?: string | null | undefined;
|
|
51
|
+
userAgent?: string | null | undefined;
|
|
52
|
+
expiresAt: Date;
|
|
53
|
+
};
|
|
54
|
+
}, never, never>;
|
|
37
55
|
export declare function storedProviderAccounts(auth: {
|
|
38
56
|
readonly $context: Promise<AccountContext>;
|
|
39
57
|
}, userId: string): Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/testing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAS,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEvC,KAAK,WAAW,GAAG,UAAU,CAAC,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC/E,KAAK,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;AAE3D,eAAO,MAAM,YAAY;uBACG,OAAO,CAAC,WAAW,GAAG,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,eAAe,CAAC,CAAC;;;;;;;;;;gCAKxE,SAAS;QAAE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/testing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAS,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEvC,KAAK,WAAW,GAAG,UAAU,CAAC,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC/E,KAAK,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,iBAAiB,CAAC,CAAC;AAE3D,eAAO,MAAM,YAAY;uBACG,OAAO,CAAC,WAAW,GAAG,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,eAAe,CAAC,CAAC;;;;;;;;;;gCAKxE,SAAS;QAAE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE;;;;;;;;;;;;;;;;gBASlG,CAAC;AAEH,eAAO,MAAM,WAAW;uBACI,OAAO,CAAC,WAAW,GAAG,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,eAAe,CAAC,CAAC;;;;;;;;;;;;;;;;gBASpG,CAAC;AAEH,wBAAsB,sBAAsB,CAAC,IAAI,EAAE;IAAE,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;CAAE,EAAE,MAAM,EAAE,MAAM;;;;;;;KAWhH;AAED,eAAO,MAAM,YAAY;uBACG,OAAO,CAAC,WAAW,GAAG,cAAc,GAAG;QAAE,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,CAAA;KAAE,CAAC;;;;;;;;;;oBACjC,MAAM;uBAAqB,MAAM;;;;;;;;;gBAO9H,CAAC"}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { Clock, Effect } from "effect";
|
|
2
2
|
export const signedInUser = Effect.fnUntraced(function* (auth, { providerAccounts = [], ...fields }) {
|
|
3
|
-
const { test, internalAdapter
|
|
3
|
+
const { test, internalAdapter } = yield* Effect.promise(() => auth.$context);
|
|
4
4
|
const user = yield* Effect.promise(() => test.saveUser(test.createUser(fields)));
|
|
5
5
|
for (const { providerId, accountId } of providerAccounts) {
|
|
6
6
|
yield* Effect.promise(() => internalAdapter.createAccount({ accountId, providerId, userId: user.id }));
|
|
7
7
|
}
|
|
8
|
+
return yield* signInAgain(auth, user.id);
|
|
9
|
+
});
|
|
10
|
+
export const signInAgain = Effect.fnUntraced(function* (auth, userId) {
|
|
11
|
+
const { test, internalAdapter, sessionConfig } = yield* Effect.promise(() => auth.$context);
|
|
8
12
|
const now = yield* Clock.currentTimeMillis;
|
|
9
|
-
const login = yield* Effect.promise(() => test.login({ userId
|
|
13
|
+
const login = yield* Effect.promise(() => test.login({ userId }));
|
|
10
14
|
const expiresAt = new Date(now + sessionConfig.expiresIn * 1000);
|
|
11
15
|
yield* Effect.promise(() => internalAdapter.updateSession(login.token, { expiresAt }));
|
|
12
16
|
return { ...login, session: { ...login.session, expiresAt } };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.js","sourceRoot":"","sources":["../../../src/server/better-auth/testing.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKvC,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EACtD,IAAuG,EACvG,EACC,gBAAgB,GAAG,EAAE,EACrB,GAAG,MAAM,EAGT;IAED,MAAM,EAAE,IAAI,EAAE,eAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"testing.js","sourceRoot":"","sources":["../../../src/server/better-auth/testing.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKvC,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EACtD,IAAuG,EACvG,EACC,gBAAgB,GAAG,EAAE,EACrB,GAAG,MAAM,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AAC,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACjF,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IACpE,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,aAAa,CAAC,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC9I,OAAO,IAAI,CAAC;AACb,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/identity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Request identity, sessions, transport trust and permission enforcement for Effect RPC servers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -49,17 +49,17 @@
|
|
|
49
49
|
"@shivaedev/changes": "0.6.0",
|
|
50
50
|
"@shivaedev/contract": "0.30.0",
|
|
51
51
|
"@shivaedev/errors": "0.4.0",
|
|
52
|
-
"@shivaedev/sql": "0.
|
|
52
|
+
"@shivaedev/sql": "0.30.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@effect/platform-node": "4.0.6",
|
|
56
56
|
"@effect/sql-pg": "4.0.6",
|
|
57
|
-
"@shivaedev/admin": "0.16.
|
|
58
|
-
"@shivaedev/quality": "0.
|
|
59
|
-
"@shivaedev/server": "0.
|
|
57
|
+
"@shivaedev/admin": "0.16.2",
|
|
58
|
+
"@shivaedev/quality": "0.53.0",
|
|
59
|
+
"@shivaedev/server": "0.16.0",
|
|
60
60
|
"@shivaedev/test": "0.12.0",
|
|
61
|
-
"@shivaedev/test-sql": "0.15.
|
|
62
|
-
"@shivaedev/test-story": "0.31.
|
|
61
|
+
"@shivaedev/test-sql": "0.15.3",
|
|
62
|
+
"@shivaedev/test-story": "0.31.3",
|
|
63
63
|
"@types/node": "24.19.2",
|
|
64
64
|
"better-auth": "1.7.12",
|
|
65
65
|
"effect": "4.0.6",
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { BetterAuthOptions, GenericEndpointContext } from "better-auth";
|
|
2
|
+
import type { Effect } from "effect";
|
|
3
|
+
import type { BetterAuthDatabaseHooks, BetterAuthHookError } from "#server/better-auth/databaseHooks.ts";
|
|
4
|
+
import type { hookRuntime } from "./hookRuntime.ts";
|
|
5
|
+
|
|
6
|
+
type SessionCreate = NonNullable<NonNullable<NonNullable<BetterAuthOptions["databaseHooks"]>["session"]>["create"]>;
|
|
7
|
+
type HookResult = Awaited<ReturnType<NonNullable<SessionCreate["before"]>>>;
|
|
8
|
+
type NativeHook<TData, TResult> = (data: TData, endpoint: GenericEndpointContext | null) => Promise<TResult>;
|
|
9
|
+
type EffectHook<TData, TResult, R> = (data: TData, endpoint: GenericEndpointContext | null) => Effect.Effect<TResult, BetterAuthHookError, R>;
|
|
10
|
+
interface Operation<TBefore, TAfter, TResult> {
|
|
11
|
+
after?: NativeHook<TAfter, void>;
|
|
12
|
+
before?: NativeHook<TBefore, TResult>;
|
|
13
|
+
}
|
|
14
|
+
interface EffectOperation<TBefore, TAfter, TResult, R> {
|
|
15
|
+
readonly after?: EffectHook<TAfter, void, R>;
|
|
16
|
+
readonly before?: EffectHook<TBefore, TResult, R>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function mergeResult<T extends HookResult>(previous: T | undefined, next: T): T | undefined {
|
|
20
|
+
if (typeof previous === "object" && typeof next === "object") {
|
|
21
|
+
return { ...next, data: { ...previous.data, ...next.data } };
|
|
22
|
+
}
|
|
23
|
+
return next === undefined || next === true ? (previous ?? next) : next;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function effectHookOptions<TOptions extends BetterAuthOptions, R>(
|
|
27
|
+
options: TOptions,
|
|
28
|
+
hooks: BetterAuthDatabaseHooks<R> | undefined,
|
|
29
|
+
runtime: Effect.Success<ReturnType<typeof hookRuntime<R>>>,
|
|
30
|
+
): TOptions {
|
|
31
|
+
function composeBefore<TData extends object, TResult extends HookResult>(hook: EffectHook<TData, TResult, R>, own?: NativeHook<TData, TResult>) {
|
|
32
|
+
return async (data: TData, endpoint: GenericEndpointContext | null) => {
|
|
33
|
+
const previous = await own?.(data, endpoint);
|
|
34
|
+
if (previous === false) {
|
|
35
|
+
return previous;
|
|
36
|
+
}
|
|
37
|
+
const input = typeof previous === "object" ? { ...data, ...previous.data } : data;
|
|
38
|
+
const next = await runtime.runHook(() => hook(input, endpoint), endpoint?.request?.signal);
|
|
39
|
+
return mergeResult(previous, next);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function composeAfter<TData>(hook: EffectHook<TData, void, R>, own?: NativeHook<TData, void>) {
|
|
43
|
+
return async (data: TData, endpoint: GenericEndpointContext | null) => {
|
|
44
|
+
await own?.(data, endpoint);
|
|
45
|
+
await runtime.runHook(() => hook(data, endpoint), endpoint?.request?.signal);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function operation<TBefore extends object, TAfter, TResult extends HookResult>(
|
|
49
|
+
own: Operation<TBefore, TAfter, TResult> | undefined,
|
|
50
|
+
application: EffectOperation<TBefore, TAfter, TResult, R> | undefined,
|
|
51
|
+
) {
|
|
52
|
+
return {
|
|
53
|
+
...own,
|
|
54
|
+
...(application?.before === undefined ? {} : { before: composeBefore(application.before, own?.before) }),
|
|
55
|
+
...(application?.after === undefined ? {} : { after: composeAfter(application.after, own?.after) }),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (hooks === undefined) {
|
|
59
|
+
return options;
|
|
60
|
+
}
|
|
61
|
+
const native = options.databaseHooks;
|
|
62
|
+
return {
|
|
63
|
+
...options,
|
|
64
|
+
databaseHooks: {
|
|
65
|
+
account: {
|
|
66
|
+
create: operation(native?.account?.create, hooks.account?.create),
|
|
67
|
+
delete: operation(native?.account?.delete, hooks.account?.delete),
|
|
68
|
+
update: operation(native?.account?.update, hooks.account?.update),
|
|
69
|
+
},
|
|
70
|
+
session: {
|
|
71
|
+
create: operation(native?.session?.create, hooks.session?.create),
|
|
72
|
+
delete: operation(native?.session?.delete, hooks.session?.delete),
|
|
73
|
+
update: operation(native?.session?.update, hooks.session?.update),
|
|
74
|
+
},
|
|
75
|
+
user: {
|
|
76
|
+
create: operation(native?.user?.create, hooks.user?.create),
|
|
77
|
+
delete: operation(native?.user?.delete, hooks.user?.delete),
|
|
78
|
+
update: operation(native?.user?.update, hooks.user?.update),
|
|
79
|
+
},
|
|
80
|
+
verification: {
|
|
81
|
+
create: operation(native?.verification?.create, hooks.verification?.create),
|
|
82
|
+
delete: operation(native?.verification?.delete, hooks.verification?.delete),
|
|
83
|
+
update: operation(native?.verification?.update, hooks.verification?.update),
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { APIError } from "better-auth/api";
|
|
3
|
+
import { type Cause, Context, Effect, Result } from "effect";
|
|
4
|
+
import type { BetterAuthHookError } from "#server/better-auth/databaseHooks.ts";
|
|
5
|
+
|
|
6
|
+
export const hookRuntime = Effect.fnUntraced(function* <R>() {
|
|
7
|
+
const context = yield* Effect.context<R>();
|
|
8
|
+
const requests = new AsyncLocalStorage<{ readonly context: Context.Context<R>; cause?: Cause.Cause<never> }>();
|
|
9
|
+
return {
|
|
10
|
+
runHook: async <A>(hook: () => Effect.Effect<A, BetterAuthHookError, R>, signal: AbortSignal | undefined): Promise<A> => {
|
|
11
|
+
const request = requests.getStore();
|
|
12
|
+
function remember(cause: Cause.Cause<never>) {
|
|
13
|
+
return Effect.sync(() => {
|
|
14
|
+
if (request !== undefined) {
|
|
15
|
+
request.cause = cause;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const result = await Effect.runPromiseWith(request?.context ?? context)(Effect.suspend(hook).pipe(Effect.result, Effect.tapCause(remember)), {
|
|
20
|
+
signal,
|
|
21
|
+
});
|
|
22
|
+
if (Result.isFailure(result)) {
|
|
23
|
+
throw new APIError("FORBIDDEN", { code: result.failure.code, message: result.failure.message });
|
|
24
|
+
}
|
|
25
|
+
return result.success;
|
|
26
|
+
},
|
|
27
|
+
runRequest: <A>(request: (signal: AbortSignal) => Promise<A>) =>
|
|
28
|
+
Effect.withFiber((fiber) =>
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
const state: { readonly context: Context.Context<R>; cause?: Cause.Cause<never> } = {
|
|
31
|
+
context: Context.merge(context, fiber.context),
|
|
32
|
+
};
|
|
33
|
+
const exit = yield* Effect.exit(Effect.tryPromise((signal) => requests.run(state, () => request(signal))));
|
|
34
|
+
if (state.cause !== undefined) {
|
|
35
|
+
return yield* Effect.failCause(state.cause);
|
|
36
|
+
}
|
|
37
|
+
return yield* exit;
|
|
38
|
+
}),
|
|
39
|
+
),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type Cause, Effect } from "effect";
|
|
2
|
+
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
3
|
+
import { type BearerSessionsOptions, cookieSessionResponse, hasSessionCookie, sessionHeaders } from "#server/better-auth/bearerSessions.ts";
|
|
4
|
+
import { sessionPermissions } from "#server/better-auth/sessionPermissions.ts";
|
|
5
|
+
|
|
6
|
+
export interface MountedAuth {
|
|
7
|
+
readonly $context: Promise<{ readonly authCookies: { readonly sessionToken: { readonly name: string } } }>;
|
|
8
|
+
readonly handler: (request: Request) => Promise<Response>;
|
|
9
|
+
}
|
|
10
|
+
export interface MountRouteOptions {
|
|
11
|
+
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
12
|
+
readonly clientIpHeader: string;
|
|
13
|
+
readonly ipv6Subnet?: number | undefined;
|
|
14
|
+
readonly roles?: RoleSet | undefined;
|
|
15
|
+
readonly runRequest?: (<A>(request: (signal: AbortSignal) => Promise<A>) => Effect.Effect<A, Cause.UnknownError>) | undefined;
|
|
16
|
+
readonly trustedProxy?: string | undefined;
|
|
17
|
+
}
|
|
18
|
+
function replacesSession(request: Request, reply: Response, native: boolean) {
|
|
19
|
+
return (
|
|
20
|
+
native
|
|
21
|
+
&& reply.ok
|
|
22
|
+
&& reply.headers.has("set-auth-token")
|
|
23
|
+
&& request.headers.has("authorization")
|
|
24
|
+
&& new URL(request.url).pathname.startsWith("/api/auth/sign-in/")
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
async function providerResponse(auth: MountedAuth, web: Request, options: MountRouteOptions) {
|
|
28
|
+
if (options.bearerSessions === undefined) {
|
|
29
|
+
return auth.handler(web);
|
|
30
|
+
}
|
|
31
|
+
const { authCookies } = await auth.$context;
|
|
32
|
+
const cookieName = authCookies.sessionToken.name;
|
|
33
|
+
const headers = sessionHeaders(web.headers, cookieName);
|
|
34
|
+
const native = options.bearerSessions.origins.includes(web.headers.get("origin") ?? "") && !hasSessionCookie(web.headers, cookieName);
|
|
35
|
+
const request = new Request(web, { headers });
|
|
36
|
+
const reply = await auth.handler(request);
|
|
37
|
+
if (replacesSession(request, reply, native)) {
|
|
38
|
+
const revoked = await auth.handler(new Request(new URL("/api/auth/sign-out", web.url), { body: "{}", headers, method: "POST" }));
|
|
39
|
+
await revoked.body?.cancel();
|
|
40
|
+
if (!revoked.ok) {
|
|
41
|
+
throw new Error("Session replacement failed");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return native ? reply : cookieSessionResponse(reply);
|
|
45
|
+
}
|
|
46
|
+
export const response = Effect.fnUntraced(function* (auth: MountedAuth, web: Request, options: MountRouteOptions) {
|
|
47
|
+
const sessionRequest = options.roles !== undefined && new URL(web.url).pathname === "/api/auth/get-session";
|
|
48
|
+
const url = new URL(web.url);
|
|
49
|
+
if (sessionRequest) {
|
|
50
|
+
url.searchParams.set("disableCookieCache", "true");
|
|
51
|
+
}
|
|
52
|
+
const request = sessionRequest ? new Request(url, web) : web;
|
|
53
|
+
const runRequest = options.runRequest ?? Effect.tryPromise;
|
|
54
|
+
const reply = yield* runRequest((signal) => providerResponse(auth, new Request(request, { signal }), options));
|
|
55
|
+
return sessionRequest && reply.ok && options.roles !== undefined ? yield* sessionPermissions(reply, options.roles) : reply;
|
|
56
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BetterAuthOptions } from "better-auth";
|
|
2
|
+
import type { Effect } from "effect";
|
|
3
|
+
|
|
4
|
+
export interface BetterAuthHookError {
|
|
5
|
+
readonly code: string;
|
|
6
|
+
readonly message: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type EffectHooks<T, R> = {
|
|
10
|
+
readonly [TKey in keyof T]: NonNullable<T[TKey]> extends (...args: infer TArgs) => infer TResult
|
|
11
|
+
? (...args: TArgs) => Effect.Effect<Awaited<TResult>, BetterAuthHookError, R>
|
|
12
|
+
: EffectHooks<NonNullable<T[TKey]>, R>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type BetterAuthDatabaseHooks<R = never> = EffectHooks<NonNullable<BetterAuthOptions["databaseHooks"]>, R>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { type BetterAuthOptions, betterAuth } from "better-auth";
|
|
2
2
|
import { Config, Effect, Option, Redacted } from "effect";
|
|
3
3
|
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
4
|
+
import { effectHookOptions } from "#server/better-auth/adapters/effectHookOptions.ts";
|
|
4
5
|
import { type BetterAuthHardeningOptions, withHardening } from "#server/better-auth/adapters/hardening.ts";
|
|
6
|
+
import { hookRuntime } from "#server/better-auth/adapters/hookRuntime.ts";
|
|
5
7
|
import { roleSeedOptions } from "#server/better-auth/adapters/roleSeedOptions.ts";
|
|
6
8
|
import { signInPolicyOptions } from "#server/better-auth/adapters/signInPolicyOptions.ts";
|
|
7
9
|
import { type StoredRoleOptions, storedRoleOptions } from "#server/better-auth/adapters/storedRoleOptions.ts";
|
|
@@ -14,10 +16,12 @@ import { type PolicyStorage, type SignInPolicy, signInGate } from "#server/bette
|
|
|
14
16
|
import type { AccountRoleSeed } from "./accountRoleSeed.ts";
|
|
15
17
|
import { admittedSessions } from "./admittedSessions.ts";
|
|
16
18
|
import { closeRoleEndpoints } from "./closeRoleEndpoints.ts";
|
|
19
|
+
import type { BetterAuthDatabaseHooks } from "./databaseHooks.ts";
|
|
17
20
|
import { mountedSessions } from "./mountedSessions.ts";
|
|
18
21
|
|
|
19
22
|
export type BetterAuthMountOptions<E = never, R = never> = {
|
|
20
23
|
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
24
|
+
readonly databaseHooks?: BetterAuthDatabaseHooks<R> | undefined;
|
|
21
25
|
readonly hardening?: BetterAuthHardeningOptions;
|
|
22
26
|
readonly signIn?: SignInPolicy<E, R> | undefined;
|
|
23
27
|
} & (
|
|
@@ -40,7 +44,9 @@ export const betterAuthMount = Effect.fn("Platform.betterAuthMount")(function* <
|
|
|
40
44
|
yield* policy.accounts;
|
|
41
45
|
}
|
|
42
46
|
const gate = yield* signInGate(policy);
|
|
43
|
-
const
|
|
47
|
+
const runtime = yield* hookRuntime<R>();
|
|
48
|
+
const hooked = effectHookOptions(options, mount.databaseHooks, runtime);
|
|
49
|
+
const configured = mount.roles === undefined ? hooked : storedRoleOptions(closeRoleEndpoints(hooked), mount);
|
|
44
50
|
const gated = yield* signInPolicyOptions(configured, gate, (): Promise<PolicyStorage> => auth.$context);
|
|
45
51
|
const seed = mount.roleSeed === undefined ? undefined : yield* mount.roleSeed;
|
|
46
52
|
const seeded = roleSeedOptions(gate.active ? { ...configured, ...gated } : configured, seed);
|
|
@@ -80,6 +86,7 @@ export const betterAuthMount = Effect.fn("Platform.betterAuthMount")(function* <
|
|
|
80
86
|
clientIpHeader,
|
|
81
87
|
ipv6Subnet: options.advanced?.ipAddress?.ipv6Subnet,
|
|
82
88
|
roles: mount.roles,
|
|
89
|
+
runRequest: runtime.runRequest,
|
|
83
90
|
trustedProxy,
|
|
84
91
|
});
|
|
85
92
|
const admit = admittedSessions(gate, () => auth.$context, mount.bearerSessions);
|
|
@@ -1,61 +1,10 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
3
3
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
|
-
import type
|
|
5
|
-
import { type BearerSessionsOptions, cookieSessionResponse, hasSessionCookie, sessionHeaders } from "#server/better-auth/bearerSessions.ts";
|
|
4
|
+
import { type MountedAuth, type MountRouteOptions, response } from "#server/better-auth/adapters/response.ts";
|
|
6
5
|
import { providerFailure } from "#server/providerFailure.ts";
|
|
7
6
|
import { clientIpHeaders } from "./clientIp.ts";
|
|
8
|
-
import { sessionPermissions } from "./sessionPermissions.ts";
|
|
9
7
|
|
|
10
|
-
interface MountedAuth {
|
|
11
|
-
readonly $context: Promise<{ readonly authCookies: { readonly sessionToken: { readonly name: string } } }>;
|
|
12
|
-
readonly handler: (request: Request) => Promise<Response>;
|
|
13
|
-
}
|
|
14
|
-
interface MountRouteOptions {
|
|
15
|
-
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
16
|
-
readonly clientIpHeader: string;
|
|
17
|
-
readonly ipv6Subnet?: number | undefined;
|
|
18
|
-
readonly roles?: RoleSet | undefined;
|
|
19
|
-
readonly trustedProxy?: string | undefined;
|
|
20
|
-
}
|
|
21
|
-
function replacesSession(request: Request, response: Response, native: boolean) {
|
|
22
|
-
return (
|
|
23
|
-
native
|
|
24
|
-
&& response.ok
|
|
25
|
-
&& response.headers.has("set-auth-token")
|
|
26
|
-
&& request.headers.has("authorization")
|
|
27
|
-
&& new URL(request.url).pathname.startsWith("/api/auth/sign-in/")
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
async function providerResponse(auth: MountedAuth, web: Request, options: MountRouteOptions) {
|
|
31
|
-
if (options.bearerSessions === undefined) {
|
|
32
|
-
return auth.handler(web);
|
|
33
|
-
}
|
|
34
|
-
const { authCookies } = await auth.$context;
|
|
35
|
-
const cookieName = authCookies.sessionToken.name;
|
|
36
|
-
const headers = sessionHeaders(web.headers, cookieName);
|
|
37
|
-
const native = options.bearerSessions.origins.includes(web.headers.get("origin") ?? "") && !hasSessionCookie(web.headers, cookieName);
|
|
38
|
-
const request = new Request(web, { headers });
|
|
39
|
-
const response = await auth.handler(request);
|
|
40
|
-
if (replacesSession(request, response, native)) {
|
|
41
|
-
const revoked = await auth.handler(new Request(new URL("/api/auth/sign-out", web.url), { body: "{}", headers, method: "POST" }));
|
|
42
|
-
await revoked.body?.cancel();
|
|
43
|
-
if (!revoked.ok) {
|
|
44
|
-
throw new Error("Session replacement failed");
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return native ? response : cookieSessionResponse(response);
|
|
48
|
-
}
|
|
49
|
-
const authResponse = Effect.fnUntraced(function* (auth: MountedAuth, web: Request, options: MountRouteOptions) {
|
|
50
|
-
const sessionRequest = options.roles !== undefined && new URL(web.url).pathname === "/api/auth/get-session";
|
|
51
|
-
const url = new URL(web.url);
|
|
52
|
-
if (sessionRequest) {
|
|
53
|
-
url.searchParams.set("disableCookieCache", "true");
|
|
54
|
-
}
|
|
55
|
-
const request = sessionRequest ? new Request(url, web) : web;
|
|
56
|
-
const response = yield* Effect.tryPromise((signal) => providerResponse(auth, new Request(request, { signal }), options));
|
|
57
|
-
return sessionRequest && response.ok && options.roles !== undefined ? yield* sessionPermissions(response, options.roles) : response;
|
|
58
|
-
});
|
|
59
8
|
export function mountRoutes(auth: MountedAuth, options: MountRouteOptions) {
|
|
60
9
|
const setClientIp = clientIpHeaders(options);
|
|
61
10
|
return HttpRouter.use((router) =>
|
|
@@ -68,7 +17,7 @@ export function mountRoutes(auth: MountedAuth, options: MountRouteOptions) {
|
|
|
68
17
|
"auth.rate_limit.address_source": clientIp.source,
|
|
69
18
|
});
|
|
70
19
|
return HttpServerResponse.fromWeb(
|
|
71
|
-
yield*
|
|
20
|
+
yield* response(auth, web, options).pipe(
|
|
72
21
|
Effect.catchTag("Forbidden", (error) =>
|
|
73
22
|
Effect.succeed(Response.json({ message: error.message }, { status: HttpStatus.fromLiteral("Forbidden") })),
|
|
74
23
|
),
|
|
@@ -14,13 +14,21 @@ export const signedInUser = Effect.fnUntraced(function* (
|
|
|
14
14
|
readonly providerAccounts?: readonly { readonly providerId: string; readonly accountId: string }[];
|
|
15
15
|
},
|
|
16
16
|
) {
|
|
17
|
-
const { test, internalAdapter
|
|
17
|
+
const { test, internalAdapter } = yield* Effect.promise(() => auth.$context);
|
|
18
18
|
const user = yield* Effect.promise(() => test.saveUser(test.createUser(fields)));
|
|
19
19
|
for (const { providerId, accountId } of providerAccounts) {
|
|
20
20
|
yield* Effect.promise(() => internalAdapter.createAccount({ accountId, providerId, userId: user.id }));
|
|
21
21
|
}
|
|
22
|
+
return yield* signInAgain(auth, user.id);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const signInAgain = Effect.fnUntraced(function* (
|
|
26
|
+
auth: { readonly $context: Promise<TestContext & AccountContext & Pick<AuthContext, "sessionConfig">> },
|
|
27
|
+
userId: string,
|
|
28
|
+
) {
|
|
29
|
+
const { test, internalAdapter, sessionConfig } = yield* Effect.promise(() => auth.$context);
|
|
22
30
|
const now = yield* Clock.currentTimeMillis;
|
|
23
|
-
const login = yield* Effect.promise(() => test.login({ userId
|
|
31
|
+
const login = yield* Effect.promise(() => test.login({ userId }));
|
|
24
32
|
const expiresAt = new Date(now + sessionConfig.expiresIn * 1000);
|
|
25
33
|
yield* Effect.promise(() => internalAdapter.updateSession(login.token, { expiresAt }));
|
|
26
34
|
return { ...login, session: { ...login.session, expiresAt } };
|