@shivaedev/identity 0.10.0 → 0.10.1
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 +6 -0
- package/README.md +40 -37
- package/dist/server/better-auth/adapters/database.d.ts +4 -0
- package/dist/server/better-auth/adapters/database.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/database.js +36 -0
- package/dist/server/better-auth/adapters/database.js.map +1 -0
- package/dist/server/better-auth/adapters/transaction.d.ts +4 -0
- package/dist/server/better-auth/adapters/transaction.d.ts.map +1 -0
- package/dist/server/better-auth/adapters/transaction.js +29 -0
- package/dist/server/better-auth/adapters/transaction.js.map +1 -0
- package/dist/server/better-auth/mount.d.ts +1 -3
- package/dist/server/better-auth/mount.d.ts.map +1 -1
- package/dist/server/better-auth/mount.js +0 -25
- package/dist/server/better-auth/mount.js.map +1 -1
- package/dist/server/better-auth/testDatabase.d.ts +11 -11
- package/dist/server/better-auth/testDatabase.d.ts.map +1 -1
- package/dist/server/better-auth/testDatabase.js +9 -20
- package/dist/server/better-auth/testDatabase.js.map +1 -1
- package/dist/server/revalidateStream.d.ts +1 -1
- package/dist/server/revalidateStream.d.ts.map +1 -1
- package/dist/server/revalidateStream.js +2 -2
- package/dist/server/revalidateStream.js.map +1 -1
- package/dist/sessions.d.ts +4 -4
- package/dist/sessions.d.ts.map +1 -1
- package/package.json +7 -13
- package/src/server/better-auth/adapters/database.ts +62 -0
- package/src/server/better-auth/adapters/transaction.ts +37 -0
- package/src/server/better-auth/mount.ts +0 -35
- package/src/server/better-auth/testDatabase.ts +15 -26
- package/src/server/revalidateStream.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.10.1 - 2026-10-31
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- End open authenticated streams with `AuthUnavailable` when a session revalidation cannot reach the auth provider. Revoked sessions still end with `Unauthorized`, and live-change subscriptions use identity's shared stream revalidation.
|
|
8
|
+
|
|
3
9
|
## 0.10.0 - 2026-10-31
|
|
4
10
|
|
|
5
11
|
### Added
|
package/README.md
CHANGED
|
@@ -93,11 +93,12 @@ The application's `auth` owns its storage and session configuration. A null look
|
|
|
93
93
|
`betterAuthMount(options)` returns the Better Auth instance and a native route Layer for `/api/auth/*`. It accepts Better Auth options, including application plugins and session policy. Merge `routes` into the HTTP host and use `auth.api.getSession` with `betterAuthSessions` for the application's RPC session provider.
|
|
94
94
|
|
|
95
95
|
```ts
|
|
96
|
-
import {
|
|
97
|
-
import {
|
|
96
|
+
import { Effect } from "effect";
|
|
97
|
+
import { betterAuthDatabase } from "@shivaedev/identity/server/better-auth/adapters/database.ts";
|
|
98
|
+
import { betterAuthMount } from "@shivaedev/identity/server/better-auth/mount.ts";
|
|
98
99
|
|
|
99
100
|
const authentication = Effect.gen(function* () {
|
|
100
|
-
const database = yield*
|
|
101
|
+
const database = yield* betterAuthDatabase();
|
|
101
102
|
return yield* betterAuthMount({
|
|
102
103
|
database,
|
|
103
104
|
emailAndPassword: { enabled: true },
|
|
@@ -106,9 +107,9 @@ const authentication = Effect.gen(function* () {
|
|
|
106
107
|
});
|
|
107
108
|
```
|
|
108
109
|
|
|
109
|
-
`
|
|
110
|
+
`betterAuthDatabase()` reads the application’s `SqlClient` and reserves connections from its PostgreSQL pool. Build it inside `appHost`’s `application` Effect, or provide sql’s `postgresLayer` for a standalone process. Set `maxConnections` and `applicationName` on the application SQL Layer. The adapter also works with Better Auth’s `getMigrations`; create it inside a SQL transaction when auth writes must commit or roll back with application writes. The application SQL Layer owns shutdown.
|
|
110
111
|
|
|
111
|
-
Run this Effect in the host's scope
|
|
112
|
+
Run this Effect in the host's scope; the application SQL Layer manages its connections. A rejected auth handler returns HTTP 503 with `Authentication is temporarily unavailable`.
|
|
112
113
|
|
|
113
114
|
The mount reads `BETTER_AUTH_SECRET` unless options supply `secret`. `PUBLIC_URL` supplies the default base URL, trusted origin and HTTPS secure-cookie setting. With it unset, `baseURL.allowedHosts` defaults to `["127.0.0.1:*", "localhost:*"]` over HTTP and `trustedOrigins` defaults to `["http://127.0.0.1:*", "http://localhost:*"]`, accepting both loopback names on any port through a development proxy. Explicit `baseURL` (including `baseURL.allowedHosts`) and `trustedOrigins` options each replace their corresponding default; their lists are not merged. RPC Origin policy remains an application choice.
|
|
114
115
|
|
|
@@ -181,13 +182,14 @@ Use the WebView's own fetch with `credentials: "omit"` for incremental NDJSON ch
|
|
|
181
182
|
Give the mount a sign-in policy as its second argument, and resolve RPC sessions through `admittedSessions` so a removal ends access on the next request:
|
|
182
183
|
|
|
183
184
|
```ts
|
|
184
|
-
import {
|
|
185
|
+
import { Effect } from "effect";
|
|
185
186
|
import { allowedAccounts } from "@shivaedev/identity/server/better-auth/allowedAccounts.ts";
|
|
186
|
-
import {
|
|
187
|
+
import { betterAuthDatabase } from "@shivaedev/identity/server/better-auth/adapters/database.ts";
|
|
188
|
+
import { betterAuthMount } from "@shivaedev/identity/server/better-auth/mount.ts";
|
|
187
189
|
import { trustedOrigins } from "@shivaedev/identity/server/origin.ts";
|
|
188
190
|
|
|
189
191
|
const authentication = Effect.gen(function* () {
|
|
190
|
-
const database = yield*
|
|
192
|
+
const database = yield* betterAuthDatabase();
|
|
191
193
|
const mounted = yield* betterAuthMount(
|
|
192
194
|
{ database, session: { cookieCache: { enabled: false } }, socialProviders: { github } },
|
|
193
195
|
{ signIn: { accounts: allowedAccounts, recheckInterval: "5 minutes", rule: inOrganization } },
|
|
@@ -434,7 +436,7 @@ const sessions = {
|
|
|
434
436
|
};
|
|
435
437
|
```
|
|
436
438
|
|
|
437
|
-
An `AuthUnavailable` failure during revalidation
|
|
439
|
+
An `AuthUnavailable` failure during revalidation ends the stream with `AuthUnavailable`. Initial session lookup failures also reject the subscription. Unary calls resolve their session once per call. `MaybeAuthenticated` streams that start anonymous continue without a session monitor; a signed-in optional stream ends with `Unauthorized` when its session is removed. Callers can subscribe again to continue anonymously.
|
|
438
440
|
|
|
439
441
|
### Transport headers and request ids
|
|
440
442
|
|
|
@@ -471,7 +473,7 @@ Run the auth schema migration before the app migration. Use `ON DELETE CASCADE`
|
|
|
471
473
|
|
|
472
474
|
Use `testUtils()` from `better-auth/plugins` in a test-only Better Auth instance with the host's database, secret and cookie policy. Keep the plugin out of production auth options.
|
|
473
475
|
|
|
474
|
-
Use `authTestDatabase
|
|
476
|
+
Use `authTestDatabase` from `@shivaedev/identity/server/better-auth/testDatabase.ts` when a story needs observations or faults in the default Better Auth PostgreSQL schema. It returns Effect readouts `users` (IDs and emails) and `sessions` (IDs, user IDs and emails), plus the test-only faults `expireSessions(userId?)` and `omitImageColumn`. Pass a user id to expire only that user's sessions, or call `expireSessions()` to expire every session. It never returns tokens, password hashes or provider credentials. These fixtures assume the default table and column mappings and read `SqlClient` from the Effect environment; use only an isolated test database. Application stories keep their auth SQL in this provider-owned boundary.
|
|
475
477
|
|
|
476
478
|
```ts
|
|
477
479
|
import { betterAuth } from "better-auth";
|
|
@@ -541,36 +543,37 @@ Register the Effect with a worker schedule, as the starter does at midnight UTC.
|
|
|
541
543
|
|
|
542
544
|
Import the module that defines the name; there is no package-root entry. Paths below follow `@shivaedev/identity/`.
|
|
543
545
|
|
|
544
|
-
| Module | Names and purpose
|
|
545
|
-
| -------------------------------------------- |
|
|
546
|
-
| `identity.ts` | `RequestId`, `Identity`, `OptionalIdentity`, `SessionUser`, `SessionUserWithProviderAccounts`, `SessionUserSchema`: request-local services and the shared user schema.
|
|
547
|
-
| `middleware.ts` | `RequestTracing`, `Authenticated`, `AuthenticatedUser`, `AuthenticatedUserWithProviderAccounts`, `MaybeAuthenticated`, `IdentityFailures`: native RPC middleware declarations and their shared failure schema.
|
|
548
|
-
| `server/providerFailure.ts` | `providerFailure(cause)`: log a redacted provider failure and fail with `AuthUnavailable`.
|
|
549
|
-
| `permission.ts` | `permissionMiddleware`, `hasPermission`: native permission middleware declarations and predicates over resolved grants.
|
|
550
|
-
| `server/permissions.ts` | `permissionLayer`, `permissionRoute`: permission middleware Layers and HTTP route guards.
|
|
551
|
-
| `server/session.ts` | `authenticatedLayer`, `maybeAuthenticatedLayer`, `authenticatedUserLayer`, `authenticatedUserWithProviderAccountsLayer`, `withSessionUser`, `resolveSession`: session middleware implementations and the identity boundary.
|
|
552
|
-
| `server/adapters/betterAuthSessions.ts` | `betterAuthSessions(getSession, getProviderAccounts?)`: a session provider from Better Auth lookups.
|
|
553
|
-
| `server/origin.ts` | `trustedOrigins({ allow, missing })`: an exact Origin allowlist.
|
|
554
|
-
| `server/local.ts` | `localRpcGuard`, `loopbackOnly`, `sameOrigin`, `trustedProxyPeer`: loopback and proxy guards.
|
|
555
|
-
| `server/trustedProxyRanges.ts` | `trustedProxyRanges(trustedProxy?)`: normalized proxy ranges.
|
|
556
|
-
| `server/proxyRanges.ts` | `proxyRanges(value)`: parse configured proxy ranges.
|
|
557
|
-
| `server/transportHeaders.ts` | `transportHeaders(rpcHeaders)`: the headers used for a transport-level decision.
|
|
558
|
-
| `server/tracing.ts` | `requestTracingLayer({ header?, sensitive? })`: the `RequestTracing` implementation.
|
|
559
|
-
| `server/sessionKeys.ts` | `resolveChanges`: session-scoped change subscriptions for custom change handlers.
|
|
560
|
-
| `server/commandChanges.ts` | `commandChanges(effect, rpc, payload)`: scoped command invalidation for custom authentication middleware.
|
|
561
|
-
| `server/better-auth/mount.ts` | `betterAuthMount(options, { signIn?, bearerSessions?, roles? })`, returning `{ auth, routes, sessions, admittedSessions, revokeUnadmitted }
|
|
562
|
-
| `server/better-auth/
|
|
563
|
-
| `server/better-auth/
|
|
564
|
-
| `server/better-auth/
|
|
565
|
-
| `server/better-auth/
|
|
566
|
-
| `server/better-auth/
|
|
567
|
-
| `server/better-auth/
|
|
568
|
-
| `server/better-auth/
|
|
569
|
-
| `server/better-auth/
|
|
546
|
+
| Module | Names and purpose |
|
|
547
|
+
| -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
548
|
+
| `identity.ts` | `RequestId`, `Identity`, `OptionalIdentity`, `SessionUser`, `SessionUserWithProviderAccounts`, `SessionUserSchema`: request-local services and the shared user schema. |
|
|
549
|
+
| `middleware.ts` | `RequestTracing`, `Authenticated`, `AuthenticatedUser`, `AuthenticatedUserWithProviderAccounts`, `MaybeAuthenticated`, `IdentityFailures`: native RPC middleware declarations and their shared failure schema. |
|
|
550
|
+
| `server/providerFailure.ts` | `providerFailure(cause)`: log a redacted provider failure and fail with `AuthUnavailable`. |
|
|
551
|
+
| `permission.ts` | `permissionMiddleware`, `hasPermission`: native permission middleware declarations and predicates over resolved grants. |
|
|
552
|
+
| `server/permissions.ts` | `permissionLayer`, `permissionRoute`: permission middleware Layers and HTTP route guards. |
|
|
553
|
+
| `server/session.ts` | `authenticatedLayer`, `maybeAuthenticatedLayer`, `authenticatedUserLayer`, `authenticatedUserWithProviderAccountsLayer`, `withSessionUser`, `resolveSession`: session middleware implementations and the identity boundary. |
|
|
554
|
+
| `server/adapters/betterAuthSessions.ts` | `betterAuthSessions(getSession, getProviderAccounts?)`: a session provider from Better Auth lookups. |
|
|
555
|
+
| `server/origin.ts` | `trustedOrigins({ allow, missing })`: an exact Origin allowlist. |
|
|
556
|
+
| `server/local.ts` | `localRpcGuard`, `loopbackOnly`, `sameOrigin`, `trustedProxyPeer`: loopback and proxy guards. |
|
|
557
|
+
| `server/trustedProxyRanges.ts` | `trustedProxyRanges(trustedProxy?)`: normalized proxy ranges. |
|
|
558
|
+
| `server/proxyRanges.ts` | `proxyRanges(value)`: parse configured proxy ranges. |
|
|
559
|
+
| `server/transportHeaders.ts` | `transportHeaders(rpcHeaders)`: the headers used for a transport-level decision. |
|
|
560
|
+
| `server/tracing.ts` | `requestTracingLayer({ header?, sensitive? })`: the `RequestTracing` implementation. |
|
|
561
|
+
| `server/sessionKeys.ts` | `resolveChanges`: session-scoped change subscriptions for custom change handlers. |
|
|
562
|
+
| `server/commandChanges.ts` | `commandChanges(effect, rpc, payload)`: scoped command invalidation for custom authentication middleware. |
|
|
563
|
+
| `server/better-auth/mount.ts` | `betterAuthMount(options, { signIn?, bearerSessions?, roles? })`, returning `{ auth, routes, sessions, admittedSessions, revokeUnadmitted }`: native auth routes and the sign-in policy. |
|
|
564
|
+
| `server/better-auth/adapters/database.ts` | `betterAuthDatabase()`: Better Auth storage on the application PostgreSQL client. |
|
|
565
|
+
| `server/better-auth/allowedAccounts.ts` | `AllowedAccounts`, `allowedAccounts`: the provider-account list Schema and its `AUTH_ALLOWED_ACCOUNTS` Config. |
|
|
566
|
+
| `server/better-auth/signInGate.ts` | `SignInPolicy`: the `accounts`, `rule` and `recheckInterval` of a sign-in policy. |
|
|
567
|
+
| `server/better-auth/revokeUnadmitted.ts` | `RevokedSessions`: the `{ users, sessions }` counts of a revocation. |
|
|
568
|
+
| `server/better-auth/userProfiles.ts` | Public profile lookups for other users. |
|
|
569
|
+
| `server/better-auth/sweepExpiredSessions.ts` | `sweepExpiredSessions`: delete expired sessions through the application's SQL client and return the number removed. |
|
|
570
|
+
| `server/better-auth/userReference.ts` | `userReference`: the default auth user table and id column for application foreign keys. |
|
|
571
|
+
| `server/better-auth/testDatabase.ts` | `authTestDatabase`: test-only identity/session readouts and faults for the default PostgreSQL auth schema. |
|
|
572
|
+
| `server/better-auth/testing.ts` | `signedInUser(auth, fields)`, `passwordUser(auth, fields)`, `storedProviderAccounts(auth, userId)`: test-only users, minted sessions and credential storage observations. |
|
|
570
573
|
|
|
571
574
|
### Install, setup and limits
|
|
572
575
|
|
|
573
|
-
Install `@shivaedev/identity` with `effect` as its peer. Everything outside `server/better-auth/` works with only `effect` installed, including the Promise-based `betterAuthSessions` adapter. Better Auth mounts and test fixtures need the optional
|
|
576
|
+
Install `@shivaedev/identity` with `effect` as its peer. Everything outside `server/better-auth/` works with only `effect` installed, including the Promise-based `betterAuthSessions` adapter. Better Auth mounts and test fixtures need the optional peer `better-auth` and the application’s PostgreSQL `SqlClient`; `sweepExpiredSessions` needs only the application's Effect SQL client. Browser code imports only `identity.ts`, `middleware.ts` and `permission.ts`.
|
|
574
577
|
- The package needs Node 24 or newer.
|
|
575
578
|
- Keep the auth provider's configuration, cookie settings and resource authorization in the application. The session adapter takes a session lookup and an optional provider-account lookup and returns a session result; it does not write provider refresh cookies into RPC responses.
|
|
576
579
|
- Without an `HttpServerRequest`, `transportHeaders` falls back to the supplied RPC headers. An application using another transport must decide what headers it can trust.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/database.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAwB,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAiBvC,eAAO,MAAM,kBAAkB,oEA0C7B,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Effect, Exit, Option, Schema, Scope } from "effect";
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
import { betterAuthTransaction } from "./transaction.js";
|
|
4
|
+
const Result = Schema.Struct({ command: Schema.String, rowCount: Schema.Number, rows: Schema.Array(Schema.Unknown) });
|
|
5
|
+
export const betterAuthDatabase = Effect.fn("Identity.betterAuthDatabase")(function* () {
|
|
6
|
+
const sql = yield* SqlClient.SqlClient;
|
|
7
|
+
const transaction = yield* Effect.serviceOption(sql.transactionService);
|
|
8
|
+
const parent = yield* Scope.Scope;
|
|
9
|
+
const context = yield* Effect.context();
|
|
10
|
+
const run = Effect.runPromiseWith(context);
|
|
11
|
+
const pool = {
|
|
12
|
+
async connect() {
|
|
13
|
+
const scope = await run(Scope.fork(parent));
|
|
14
|
+
const connection = await run((Option.isSome(transaction) ? Effect.succeed(transaction.value[0]) : sql.reserve).pipe(Scope.provide(scope), Effect.onError((cause) => Scope.close(scope, Exit.failCause(cause)))));
|
|
15
|
+
const control = Option.isSome(transaction) ? await run(betterAuthTransaction.pipe(Scope.provide(scope))) : undefined;
|
|
16
|
+
function query(text, parameters = []) {
|
|
17
|
+
if (typeof text !== "string") {
|
|
18
|
+
throw new Error("Better Auth database does not support cursors");
|
|
19
|
+
}
|
|
20
|
+
const nested = control?.(text);
|
|
21
|
+
return (nested === undefined ? run(connection.executeRaw(text, parameters)) : nested.then(() => ({ command: text, rowCount: 0, rows: [] }))).then(Schema.decodeUnknownSync(Result));
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
query,
|
|
25
|
+
release: () => {
|
|
26
|
+
void run(Scope.close(scope, Exit.void));
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
// The application SQL Layer owns pool shutdown.
|
|
31
|
+
end: () => Promise.resolve(),
|
|
32
|
+
options: {},
|
|
33
|
+
};
|
|
34
|
+
return pool;
|
|
35
|
+
});
|
|
36
|
+
//# sourceMappingURL=database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/database.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAQzD,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;AAQtH,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC,EAAE,CAAC,6BAA6B,CAAC,CAAC,QAAQ,CAAC;IACnF,MAAM,GAAG,GAAG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C9B,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;gBAClE,CAAC;gBACD,MAAM,MAAM,GAAG,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC;gBAC/B,OAAO,CACN,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC,CACnI,CAAC,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;YAC1C,CAAC;YACD,OAAO;gBACN,KAAK;gBACL,OAAO,EAAE,GAAG,EAAE;oBACb,KAAK,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;gBACzC,CAAC;aACD,CAAC;QACH,CAAC;QACD,gDAAgD;QAChD,GAAG,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;QAC5B,OAAO,EAAE,EAAE;KACX,CAAC;IACF,OAAO,IAAI,CAAC;AACb,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,MAAM,EAAS,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAKvC,eAAO,MAAM,qBAAqB,uBAMnB,MAAM,KAAG,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS,2CAwB/C,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Data, Deferred, Effect, Fiber, Scope } from "effect";
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
class Rollback extends Data.TaggedError("BetterAuthRollback") {
|
|
4
|
+
}
|
|
5
|
+
export const betterAuthTransaction = Effect.gen(function* () {
|
|
6
|
+
const sql = yield* SqlClient.SqlClient;
|
|
7
|
+
const context = yield* Effect.context();
|
|
8
|
+
const run = Effect.runPromiseWith(context);
|
|
9
|
+
const scope = yield* Scope.Scope;
|
|
10
|
+
let active;
|
|
11
|
+
return (text) => {
|
|
12
|
+
if (text === "begin") {
|
|
13
|
+
return run(Effect.gen(function* () {
|
|
14
|
+
const ready = yield* Deferred.make();
|
|
15
|
+
const done = yield* Deferred.make();
|
|
16
|
+
const fiber = yield* sql.withTransaction(Deferred.succeed(ready, undefined).pipe(Effect.andThen(Deferred.await(done)))).pipe(Effect.catchTag("BetterAuthRollback", () => Effect.void), Effect.forkIn(scope));
|
|
17
|
+
active = { done, fiber };
|
|
18
|
+
yield* Effect.raceFirst(Deferred.await(ready), Fiber.join(fiber));
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
if (active !== undefined && (text === "commit" || text === "rollback")) {
|
|
22
|
+
const { done, fiber } = active;
|
|
23
|
+
active = undefined;
|
|
24
|
+
return run((text === "commit" ? Deferred.succeed(done, undefined) : Deferred.fail(done, new Rollback())).pipe(Effect.andThen(Fiber.join(fiber))));
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAGvC,MAAM,QAAS,SAAQ,I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K,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;YACnE,CAAC,CAAC,CACF,CAAC;QACH,CAAC;QACD,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,UAAU,CAAC,EAAE,CAAC;YACxE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;YAC/B,MAAM,GAAG,SAAS,CAAC;YACnB,OAAO,GAAG,CACT,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CACrI,CAAC;QACH,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC,CAAC;AACH,CAAC,CAAC,CAAC"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type BetterAuthOptions } from "better-auth";
|
|
2
|
-
import { Config, Effect
|
|
3
|
-
import { Pool, type PoolConfig } from "pg";
|
|
2
|
+
import { Config, Effect } from "effect";
|
|
4
3
|
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
5
4
|
import { type BearerSessionsOptions } from "#server/better-auth/bearerSessions.ts";
|
|
6
5
|
import { type SignInPolicy } from "#server/better-auth/signInGate.ts";
|
|
@@ -265,5 +264,4 @@ export declare const betterAuthMount: <const TOptions extends BetterAuthOptions,
|
|
|
265
264
|
}>;
|
|
266
265
|
};
|
|
267
266
|
}, Config.ConfigError, R | import("effect/Scope").Scope>;
|
|
268
|
-
export declare const authDatabase: (url: Redacted.Redacted<string>, options?: Pick<PoolConfig, "application_name" | "max"> | undefined) => Effect.Effect<Pool, never, import("effect/Scope").Scope>;
|
|
269
267
|
//# sourceMappingURL=mount.d.ts.map
|
|
@@ -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,
|
|
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,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,KAAK,qBAAqB,EAAsB,MAAM,uCAAuC,CAAC;AAKvG,OAAO,EAAsB,KAAK,YAAY,EAAc,MAAM,mCAAmC,CAAC;AAMtG,MAAM,WAAW,sBAAsB,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,KAAK;IAC3D,QAAQ,CAAC,cAAc,CAAC,EAAE,qBAAqB,GAAG,SAAS,CAAC;IAC5D,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC;CACjD;AAED,eAAO,MAAM,eAAe,SAA0D,QAAQ,SAAS,iBAAiB,EAAE,CAAC,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wDA8DpI,CAAC"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { betterAuth } from "better-auth";
|
|
2
2
|
import { Config, Effect, Option, Redacted } from "effect";
|
|
3
|
-
import { Pool } from "pg";
|
|
4
3
|
import { withBearerSessions } from "#server/better-auth/bearerSessions.ts";
|
|
5
4
|
import { PEER_IP_HEADER } from "#server/better-auth/clientIp.ts";
|
|
6
5
|
import { memoryRateLimit } from "#server/better-auth/memoryRateLimit.ts";
|
|
@@ -68,28 +67,4 @@ export const betterAuthMount = Effect.fn("Platform.betterAuthMount")(function* (
|
|
|
68
67
|
const sessions = mountedSessions(lookupSession, admit, mount.roles);
|
|
69
68
|
return { admittedSessions: admit, auth, revokeUnadmitted: revoke, routes: authRoutes, sessions };
|
|
70
69
|
});
|
|
71
|
-
export const authDatabase = Effect.fn("Platform.authDatabase")(function* (url, options = {}) {
|
|
72
|
-
const clients = new Set();
|
|
73
|
-
const run = Effect.runForkWith(yield* Effect.context());
|
|
74
|
-
return yield* Effect.acquireRelease(Effect.sync(() => {
|
|
75
|
-
const pool = new Pool({
|
|
76
|
-
...options,
|
|
77
|
-
connectionString: Redacted.value(url),
|
|
78
|
-
});
|
|
79
|
-
pool.on("connect", (client) => clients.add(client));
|
|
80
|
-
pool.on("remove", (client) => clients.delete(client));
|
|
81
|
-
pool.on("error", (error) => {
|
|
82
|
-
run(Effect.logDebug({
|
|
83
|
-
event: "auth_database_idle_client_failed",
|
|
84
|
-
message: error.message,
|
|
85
|
-
}));
|
|
86
|
-
});
|
|
87
|
-
return pool;
|
|
88
|
-
}), (pool) => Effect.promise(async () => {
|
|
89
|
-
// pg-pool resolves end before its clients finish disconnecting.
|
|
90
|
-
const disconnected = [...clients].map((client) => new Promise((resolve) => client.once("end", resolve)));
|
|
91
|
-
await pool.end();
|
|
92
|
-
await Promise.all(disconnected);
|
|
93
|
-
}));
|
|
94
|
-
});
|
|
95
70
|
//# sourceMappingURL=mount.js.map
|
|
@@ -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;
|
|
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,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,MAAM,mCAAmC,CAAC;AACtG,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAQvD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,EAAE,CAAC,0BAA0B,CAAC,CAAC,QAAQ,CAAC,EAC7E,OAAiB,EACjB,KAAK,GAAiC,EAAE;IAExC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MA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1
1
|
import { Effect } from "effect";
|
|
2
|
-
import
|
|
3
|
-
export declare
|
|
4
|
-
expireSessions: (userId?: string) => Effect.Effect<void, import("effect/
|
|
5
|
-
omitImageColumn: Effect.Effect<void, import("effect/
|
|
6
|
-
sessionIpAddresses:
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
export declare const authTestDatabase: Effect.Effect<{
|
|
4
|
+
expireSessions: (userId?: string) => Effect.Effect<void, import("effect/sql/SqlError").SqlError, never>;
|
|
5
|
+
omitImageColumn: Effect.Effect<void, import("effect/sql/SqlError").SqlError, never>;
|
|
6
|
+
sessionIpAddresses: import("effect/sql/Statement").Statement<{
|
|
7
7
|
ipAddress: string | null;
|
|
8
|
-
}
|
|
9
|
-
sessions:
|
|
8
|
+
}>;
|
|
9
|
+
sessions: import("effect/sql/Statement").Statement<{
|
|
10
10
|
email: string;
|
|
11
11
|
id: string;
|
|
12
12
|
userId: string;
|
|
13
|
-
}
|
|
14
|
-
users:
|
|
13
|
+
}>;
|
|
14
|
+
users: import("effect/sql/Statement").Statement<{
|
|
15
15
|
email: string;
|
|
16
16
|
id: string;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
17
|
+
}>;
|
|
18
|
+
}, never, SqlClient.SqlClient>;
|
|
19
19
|
//# sourceMappingURL=testDatabase.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testDatabase.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/testDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,
|
|
1
|
+
{"version":3,"file":"testDatabase.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/testDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,eAAO,MAAM,gBAAgB;8BAGD,MAAM;;;mBAKK,MAAM,GAAG,IAAI;;;eAE1C,MAAM;YACT,MAAM;gBACF,MAAM;;;eAEK,MAAM;YAAM,MAAM;;8BAEtC,CAAC"}
|
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
export const authTestDatabase = Effect.gen(function* () {
|
|
4
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
3
5
|
return {
|
|
4
|
-
expireSessions: (userId) =>
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}),
|
|
10
|
-
sessionIpAddresses: Effect.tryPromise(async () => {
|
|
11
|
-
const result = await pool.query('select "ipAddress" from "session" order by "createdAt", id');
|
|
12
|
-
return result.rows;
|
|
13
|
-
}),
|
|
14
|
-
sessions: Effect.tryPromise(async () => {
|
|
15
|
-
const result = await pool.query('select s.id, s."userId", u.email from "session" s join "user" u on u.id = s."userId" order by u.email, s.id');
|
|
16
|
-
return result.rows;
|
|
17
|
-
}),
|
|
18
|
-
users: Effect.tryPromise(async () => {
|
|
19
|
-
const result = await pool.query('select id, email from "user" order by email');
|
|
20
|
-
return result.rows;
|
|
21
|
-
}),
|
|
6
|
+
expireSessions: (userId) => sql `update "session" set "expiresAt" = now() - interval '1 second' ${userId === undefined ? sql.literal("") : sql `where "userId" = ${userId}`}`.pipe(Effect.asVoid),
|
|
7
|
+
omitImageColumn: sql `alter table "user" drop column image`.pipe(Effect.asVoid),
|
|
8
|
+
sessionIpAddresses: sql `select "ipAddress" from "session" order by "createdAt", id`,
|
|
9
|
+
sessions: sql `select s.id, s."userId", u.email from "session" s join "user" u on u.id = s."userId" order by u.email, s.id`,
|
|
10
|
+
users: sql `select id, email from "user" order by email`,
|
|
22
11
|
};
|
|
23
|
-
}
|
|
12
|
+
});
|
|
24
13
|
//# sourceMappingURL=testDatabase.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testDatabase.js","sourceRoot":"","sources":["../../../src/server/better-auth/testDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"testDatabase.js","sourceRoot":"","sources":["../../../src/server/better-auth/testDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IACnD,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,iBAAiB,EAAE,CAAC;IAC7D,OAAO;QACN,cAAc,EAAE,CAAC,MAAe,EAAE,EAAE,CACnC,GAAG,CAAA,kEAAkE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA,oBAAoB,MAAM,EAAE,EAAE,CAAC,IAAI,CACnJ,MAAM,CAAC,MAAM,CACb;QACF,eAAe,EAAE,GAAG,CAAA,sCAAsC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QAC9E,kBAAkB,EAAE,GAAG,CAA8B,4DAA4D;QACjH,QAAQ,EAAE,GAAG,CAIX,6GAA6G;QAC/G,KAAK,EAAE,GAAG,CAA+B,6CAA6C;KACtF,CAAC;AACH,CAAC,CAAC,CAAC"}
|
|
@@ -2,5 +2,5 @@ import { Effect, Option } from "effect";
|
|
|
2
2
|
import { type Rpc } from "effect/rpc";
|
|
3
3
|
import { type AuthUnavailable, type Forbidden, Unauthorized } from "@shivaedev/errors/taxonomy.ts";
|
|
4
4
|
import type { SessionPolicy, SessionShape } from "./session.ts";
|
|
5
|
-
export declare function revalidateStream<TSession extends SessionShape>(policy: SessionPolicy<TSession>, rpc: Rpc.AnyWithProps, id: string, lookup: Effect.Effect<Option.Option<TSession>, AuthUnavailable | Forbidden>): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | Forbidden | Unauthorized, R>;
|
|
5
|
+
export declare function revalidateStream<TSession extends SessionShape>(policy: SessionPolicy<TSession>, rpc: Rpc.AnyWithProps, id: string, lookup: Effect.Effect<Option.Option<TSession>, AuthUnavailable | Forbidden>): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | AuthUnavailable | Forbidden | Unauthorized, R>;
|
|
6
6
|
//# sourceMappingURL=revalidateStream.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"revalidateStream.d.ts","sourceRoot":"","sources":["../../src/server/revalidateStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,KAAK,GAAG,EAAa,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,SAAS,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AACnG,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGhE,wBAAgB,gBAAgB,CAAC,QAAQ,SAAS,YAAY,EAC7D,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,EAC/B,GAAG,EAAE,GAAG,CAAC,YAAY,EACrB,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,eAAe,GAAG,SAAS,CAAC,IAEnE,CAAC,EAAE,CAAC,EAAE,CAAC,UAAU,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"revalidateStream.d.ts","sourceRoot":"","sources":["../../src/server/revalidateStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,KAAK,GAAG,EAAa,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,SAAS,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AACnG,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGhE,wBAAgB,gBAAgB,CAAC,QAAQ,SAAS,YAAY,EAC7D,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,EAC/B,GAAG,EAAE,GAAG,CAAC,YAAY,EACrB,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,eAAe,GAAG,SAAS,CAAC,IAEnE,CAAC,EAAE,CAAC,EAAE,CAAC,UAAU,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,yEAY/C"}
|
|
@@ -8,8 +8,8 @@ export function revalidateStream(policy, rpc, id, lookup) {
|
|
|
8
8
|
return effect;
|
|
9
9
|
}
|
|
10
10
|
const check = Effect.gen(function* () {
|
|
11
|
-
const session = yield* lookup
|
|
12
|
-
if (
|
|
11
|
+
const session = yield* lookup;
|
|
12
|
+
if (Option.isNone(session) || session.value.user.id !== id) {
|
|
13
13
|
return yield* new Unauthorized({ message: "Authentication required" });
|
|
14
14
|
}
|
|
15
15
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"revalidateStream.js","sourceRoot":"","sources":["../../src/server/revalidateStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAY,SAAS,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAwC,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAEnG,OAAO,EAAE,iBAAiB,EAA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YACxE,CAAC;QACF,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,0BAA0B,IAAI,YAAY,CAAC,CAAC,CAAC;IAC9G,CAAC,CAAC;AACH,CAAC"}
|
package/dist/sessions.d.ts
CHANGED
|
@@ -100,7 +100,9 @@ export declare const Sessions: import("@shivaedev/contract/contract.ts").Contrac
|
|
|
100
100
|
readonly queryIndexes: Schema.optional<Schema.$Array<Schema.Number>>;
|
|
101
101
|
}>, Schema.Struct<{
|
|
102
102
|
readonly _tag: Schema.Literal<"Resync">;
|
|
103
|
-
}>]>, ...never[]]>, Schema.Union<readonly [typeof import("@shivaedev/contract/readPolicyErrors.ts").MissingReadPolicy, typeof import("@shivaedev/contract/readPolicyErrors.ts").QueryReadDenied, ...Schema.Union<readonly (typeof Forbidden | typeof AuthUnavailable)[]>[]]>>, Schema.Never, typeof SessionsPermission, never> | import("effect/rpc/Rpc").Rpc<"identity.sessions.
|
|
103
|
+
}>]>, ...never[]]>, Schema.Union<readonly [typeof import("@shivaedev/contract/readPolicyErrors.ts").MissingReadPolicy, typeof import("@shivaedev/contract/readPolicyErrors.ts").QueryReadDenied, ...Schema.Union<readonly (typeof Forbidden | typeof AuthUnavailable)[]>[]]>>, Schema.Never, typeof SessionsPermission, never> | import("effect/rpc/Rpc").Rpc<"identity.sessions.revoke", Schema.Struct<{
|
|
104
|
+
id: Schema.String;
|
|
105
|
+
}>, Schema.Void, Schema.Union<readonly (typeof NotFound | typeof Forbidden | typeof AuthUnavailable)[]>, typeof SessionsPermission, never> | import("effect/rpc/Rpc").Rpc<"identity.sessions.list", Schema.Struct<{
|
|
104
106
|
readonly cursor: Schema.optionalKey<Schema.Struct<{
|
|
105
107
|
readonly id: Schema.String;
|
|
106
108
|
readonly lastActivityAt: Schema.DateTimeUtcFromString;
|
|
@@ -122,7 +124,5 @@ export declare const Sessions: import("@shivaedev/contract/contract.ts").Contrac
|
|
|
122
124
|
readonly name: Schema.String;
|
|
123
125
|
}>;
|
|
124
126
|
}>>;
|
|
125
|
-
}>, Schema.Union<readonly [Schema.Union<readonly (typeof Forbidden | typeof AuthUnavailable)[]>, typeof import("@shivaedev/contract/readPolicyErrors.ts").QueryReadDenied]>, typeof SessionsPermission, never
|
|
126
|
-
id: Schema.String;
|
|
127
|
-
}>, Schema.Void, Schema.Union<readonly (typeof NotFound | typeof Forbidden | typeof AuthUnavailable)[]>, typeof SessionsPermission, never>, readonly [], undefined>;
|
|
127
|
+
}>, Schema.Union<readonly [Schema.Union<readonly (typeof Forbidden | typeof AuthUnavailable)[]>, typeof import("@shivaedev/contract/readPolicyErrors.ts").QueryReadDenied]>, typeof SessionsPermission, never>, readonly [], undefined>;
|
|
128
128
|
//# sourceMappingURL=sessions.d.ts.map
|
package/dist/sessions.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../src/sessions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIxC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAI5D,eAAO,MAAM,eAAe;;;;;;;;;;EAM1B,CAAC;AAEH,eAAO,MAAM,aAAa;;;EAAqF,CAAC;AAChH,eAAO,MAAM,gBAAgB;;;;;;EAG3B,CAAC;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;EAA2G,CAAC;AAEpI,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../src/sessions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIxC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,+BAA+B,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAI5D,eAAO,MAAM,eAAe;;;;;;;;;;EAM1B,CAAC;AAEH,eAAO,MAAM,aAAa;;;EAAqF,CAAC;AAChH,eAAO,MAAM,gBAAgB;;;;;;EAG3B,CAAC;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;EAA2G,CAAC;AAEpI,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uOAiBY,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/identity",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Request identity, sessions, transport trust and permission enforcement for Effect RPC servers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -47,38 +47,32 @@
|
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@shivaedev/access": "0.3.1",
|
|
49
49
|
"@shivaedev/changes": "0.6.0",
|
|
50
|
-
"@shivaedev/contract": "0.
|
|
50
|
+
"@shivaedev/contract": "0.27.0",
|
|
51
51
|
"@shivaedev/errors": "0.1.3",
|
|
52
52
|
"@shivaedev/sql": "0.27.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.12.
|
|
58
|
-
"@shivaedev/quality": "0.48.
|
|
59
|
-
"@shivaedev/server": "0.
|
|
57
|
+
"@shivaedev/admin": "0.12.3",
|
|
58
|
+
"@shivaedev/quality": "0.48.2",
|
|
59
|
+
"@shivaedev/server": "0.10.0",
|
|
60
60
|
"@shivaedev/test": "0.12.0",
|
|
61
61
|
"@shivaedev/test-sql": "0.15.0",
|
|
62
|
-
"@shivaedev/test-story": "0.
|
|
62
|
+
"@shivaedev/test-story": "0.28.0",
|
|
63
63
|
"@types/node": "24.19.2",
|
|
64
|
-
"@types/pg": "8.20.0",
|
|
65
64
|
"better-auth": "1.7.12",
|
|
66
65
|
"effect": "4.0.6",
|
|
67
|
-
"pg": "8.23.0",
|
|
68
66
|
"typescript": "7.0.2",
|
|
69
67
|
"vitest": "5.0.5"
|
|
70
68
|
},
|
|
71
69
|
"peerDependencies": {
|
|
72
70
|
"better-auth": "1.7.12",
|
|
73
|
-
"effect": "4.0.6"
|
|
74
|
-
"pg": "8.23.0"
|
|
71
|
+
"effect": "4.0.6"
|
|
75
72
|
},
|
|
76
73
|
"peerDependenciesMeta": {
|
|
77
74
|
"better-auth": {
|
|
78
75
|
"optional": true
|
|
79
|
-
},
|
|
80
|
-
"pg": {
|
|
81
|
-
"optional": true
|
|
82
76
|
}
|
|
83
77
|
},
|
|
84
78
|
"engines": {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { BetterAuthOptions } from "better-auth";
|
|
2
|
+
import { Effect, Exit, Option, Schema, Scope } from "effect";
|
|
3
|
+
import { SqlClient } from "effect/sql";
|
|
4
|
+
import { betterAuthTransaction } from "./transaction.ts";
|
|
5
|
+
|
|
6
|
+
type PostgresPool = Extract<NonNullable<BetterAuthOptions["database"]>, { connect: unknown }>;
|
|
7
|
+
type PoolClient = Awaited<ReturnType<PostgresPool["connect"]>>;
|
|
8
|
+
interface Cursor<R> {
|
|
9
|
+
close: () => Promise<void>;
|
|
10
|
+
read: (count: number) => Promise<R[]>;
|
|
11
|
+
}
|
|
12
|
+
const Result = Schema.Struct({ command: Schema.String, rowCount: Schema.Number, rows: Schema.Array(Schema.Unknown) });
|
|
13
|
+
|
|
14
|
+
interface QueryResult<R> {
|
|
15
|
+
command: "UPDATE" | "DELETE" | "INSERT" | "SELECT" | "MERGE";
|
|
16
|
+
rowCount: number;
|
|
17
|
+
rows: R[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const betterAuthDatabase = Effect.fn("Identity.betterAuthDatabase")(function* () {
|
|
21
|
+
const sql = yield* SqlClient.SqlClient;
|
|
22
|
+
const transaction = yield* Effect.serviceOption(sql.transactionService);
|
|
23
|
+
const parent = yield* Scope.Scope;
|
|
24
|
+
const context = yield* Effect.context<SqlClient.SqlClient>();
|
|
25
|
+
const run = Effect.runPromiseWith(context);
|
|
26
|
+
const pool: PostgresPool = {
|
|
27
|
+
async connect(): Promise<PoolClient> {
|
|
28
|
+
const scope = await run(Scope.fork(parent));
|
|
29
|
+
const connection = await run(
|
|
30
|
+
(Option.isSome(transaction) ? Effect.succeed(transaction.value[0]) : sql.reserve).pipe(
|
|
31
|
+
Scope.provide(scope),
|
|
32
|
+
Effect.onError((cause) => Scope.close(scope, Exit.failCause(cause))),
|
|
33
|
+
),
|
|
34
|
+
);
|
|
35
|
+
const control = Option.isSome(transaction) ? await run(betterAuthTransaction.pipe(Scope.provide(scope))) : undefined;
|
|
36
|
+
function query<R>(text: string, parameters: readonly unknown[]): Promise<QueryResult<R>>;
|
|
37
|
+
function query<R>(cursor: Cursor<R>): Cursor<R>;
|
|
38
|
+
function query(
|
|
39
|
+
text: string | Cursor<unknown>,
|
|
40
|
+
parameters: readonly unknown[] = [],
|
|
41
|
+
): Promise<Schema.Schema.Type<typeof Result>> | Cursor<unknown> {
|
|
42
|
+
if (typeof text !== "string") {
|
|
43
|
+
throw new Error("Better Auth database does not support cursors");
|
|
44
|
+
}
|
|
45
|
+
const nested = control?.(text);
|
|
46
|
+
return (
|
|
47
|
+
nested === undefined ? run(connection.executeRaw(text, parameters)) : nested.then(() => ({ command: text, rowCount: 0, rows: [] }))
|
|
48
|
+
).then(Schema.decodeUnknownSync(Result));
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
query,
|
|
52
|
+
release: () => {
|
|
53
|
+
void run(Scope.close(scope, Exit.void));
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
// The application SQL Layer owns pool shutdown.
|
|
58
|
+
end: () => Promise.resolve(),
|
|
59
|
+
options: {},
|
|
60
|
+
};
|
|
61
|
+
return pool;
|
|
62
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Data, Deferred, Effect, Fiber, Scope } from "effect";
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
import type { SqlError } from "effect/sql/SqlError";
|
|
4
|
+
|
|
5
|
+
class Rollback extends Data.TaggedError("BetterAuthRollback") {}
|
|
6
|
+
|
|
7
|
+
export const betterAuthTransaction = Effect.gen(function* () {
|
|
8
|
+
const sql = yield* SqlClient.SqlClient;
|
|
9
|
+
const context = yield* Effect.context<never>();
|
|
10
|
+
const run = Effect.runPromiseWith(context);
|
|
11
|
+
const scope = yield* Scope.Scope;
|
|
12
|
+
let active: { readonly done: Deferred.Deferred<void, Rollback>; readonly fiber: Fiber.Fiber<void, SqlError> } | undefined;
|
|
13
|
+
return (text: string): Promise<void> | undefined => {
|
|
14
|
+
if (text === "begin") {
|
|
15
|
+
return run(
|
|
16
|
+
Effect.gen(function* () {
|
|
17
|
+
const ready = yield* Deferred.make<void>();
|
|
18
|
+
const done = yield* Deferred.make<void, Rollback>();
|
|
19
|
+
const fiber = yield* sql.withTransaction(Deferred.succeed(ready, undefined).pipe(Effect.andThen(Deferred.await(done)))).pipe(
|
|
20
|
+
Effect.catchTag("BetterAuthRollback", () => Effect.void),
|
|
21
|
+
Effect.forkIn(scope),
|
|
22
|
+
);
|
|
23
|
+
active = { done, fiber };
|
|
24
|
+
yield* Effect.raceFirst(Deferred.await(ready), Fiber.join(fiber));
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
if (active !== undefined && (text === "commit" || text === "rollback")) {
|
|
29
|
+
const { done, fiber } = active;
|
|
30
|
+
active = undefined;
|
|
31
|
+
return run(
|
|
32
|
+
(text === "commit" ? Deferred.succeed(done, undefined) : Deferred.fail(done, new Rollback())).pipe(Effect.andThen(Fiber.join(fiber))),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
};
|
|
37
|
+
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type BetterAuthOptions, betterAuth } from "better-auth";
|
|
2
2
|
import { Config, Effect, Option, Redacted } from "effect";
|
|
3
|
-
import { Pool, type PoolClient, type PoolConfig } from "pg";
|
|
4
3
|
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
5
4
|
import { type BearerSessionsOptions, withBearerSessions } from "#server/better-auth/bearerSessions.ts";
|
|
6
5
|
import { PEER_IP_HEADER } from "#server/better-auth/clientIp.ts";
|
|
@@ -82,37 +81,3 @@ export const betterAuthMount = Effect.fn("Platform.betterAuthMount")(function* <
|
|
|
82
81
|
const sessions = mountedSessions(lookupSession, admit, mount.roles);
|
|
83
82
|
return { admittedSessions: admit, auth, revokeUnadmitted: revoke, routes: authRoutes, sessions };
|
|
84
83
|
});
|
|
85
|
-
|
|
86
|
-
export const authDatabase = Effect.fn("Platform.authDatabase")(function* (
|
|
87
|
-
url: Redacted.Redacted<string>,
|
|
88
|
-
options: Pick<PoolConfig, "max" | "application_name"> = {},
|
|
89
|
-
) {
|
|
90
|
-
const clients = new Set<PoolClient>();
|
|
91
|
-
const run = Effect.runForkWith(yield* Effect.context<never>());
|
|
92
|
-
return yield* Effect.acquireRelease(
|
|
93
|
-
Effect.sync(() => {
|
|
94
|
-
const pool = new Pool({
|
|
95
|
-
...options,
|
|
96
|
-
connectionString: Redacted.value(url),
|
|
97
|
-
});
|
|
98
|
-
pool.on("connect", (client) => clients.add(client));
|
|
99
|
-
pool.on("remove", (client) => clients.delete(client));
|
|
100
|
-
pool.on("error", (error) => {
|
|
101
|
-
run(
|
|
102
|
-
Effect.logDebug({
|
|
103
|
-
event: "auth_database_idle_client_failed",
|
|
104
|
-
message: error.message,
|
|
105
|
-
}),
|
|
106
|
-
);
|
|
107
|
-
});
|
|
108
|
-
return pool;
|
|
109
|
-
}),
|
|
110
|
-
(pool) =>
|
|
111
|
-
Effect.promise(async () => {
|
|
112
|
-
// pg-pool resolves end before its clients finish disconnecting.
|
|
113
|
-
const disconnected = [...clients].map((client) => new Promise<void>((resolve) => client.once("end", resolve)));
|
|
114
|
-
await pool.end();
|
|
115
|
-
await Promise.all(disconnected);
|
|
116
|
-
}),
|
|
117
|
-
);
|
|
118
|
-
});
|
|
@@ -1,31 +1,20 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export const authTestDatabase = Effect.gen(function* () {
|
|
5
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
5
6
|
return {
|
|
6
7
|
expireSessions: (userId?: string) =>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return result.rows;
|
|
19
|
-
}),
|
|
20
|
-
sessions: Effect.tryPromise(async () => {
|
|
21
|
-
const result = await pool.query<{ email: string; id: string; userId: string }>(
|
|
22
|
-
'select s.id, s."userId", u.email from "session" s join "user" u on u.id = s."userId" order by u.email, s.id',
|
|
23
|
-
);
|
|
24
|
-
return result.rows;
|
|
25
|
-
}),
|
|
26
|
-
users: Effect.tryPromise(async () => {
|
|
27
|
-
const result = await pool.query<{ email: string; id: string }>('select id, email from "user" order by email');
|
|
28
|
-
return result.rows;
|
|
29
|
-
}),
|
|
8
|
+
sql`update "session" set "expiresAt" = now() - interval '1 second' ${userId === undefined ? sql.literal("") : sql`where "userId" = ${userId}`}`.pipe(
|
|
9
|
+
Effect.asVoid,
|
|
10
|
+
),
|
|
11
|
+
omitImageColumn: sql`alter table "user" drop column image`.pipe(Effect.asVoid),
|
|
12
|
+
sessionIpAddresses: sql<{ ipAddress: string | null }>`select "ipAddress" from "session" order by "createdAt", id`,
|
|
13
|
+
sessions: sql<{
|
|
14
|
+
email: string;
|
|
15
|
+
id: string;
|
|
16
|
+
userId: string;
|
|
17
|
+
}>`select s.id, s."userId", u.email from "session" s join "user" u on u.id = s."userId" order by u.email, s.id`,
|
|
18
|
+
users: sql<{ email: string; id: string }>`select id, email from "user" order by email`,
|
|
30
19
|
};
|
|
31
|
-
}
|
|
20
|
+
});
|
|
@@ -15,8 +15,8 @@ export function revalidateStream<TSession extends SessionShape>(
|
|
|
15
15
|
return effect;
|
|
16
16
|
}
|
|
17
17
|
const check = Effect.gen(function* () {
|
|
18
|
-
const session = yield* lookup
|
|
19
|
-
if (
|
|
18
|
+
const session = yield* lookup;
|
|
19
|
+
if (Option.isNone(session) || session.value.user.id !== id) {
|
|
20
20
|
return yield* new Unauthorized({ message: "Authentication required" });
|
|
21
21
|
}
|
|
22
22
|
});
|