@shivaedev/identity 0.38.2 → 0.39.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 +20 -0
- package/README.md +98 -46
- package/dist/client/SessionCredential.d.ts +18 -0
- package/dist/client/SessionCredential.d.ts.map +1 -0
- package/dist/client/SessionCredential.js +11 -0
- package/dist/client/SessionCredential.js.map +1 -0
- package/dist/client/adapters/sessionCredentialPlugin.d.ts +27 -0
- package/dist/client/adapters/sessionCredentialPlugin.d.ts.map +1 -0
- package/dist/client/adapters/sessionCredentialPlugin.js +45 -0
- package/dist/client/adapters/sessionCredentialPlugin.js.map +1 -0
- package/dist/generated/AccountPreferences.d.ts.map +1 -1
- package/dist/generated/AccountPreferences.js +3 -8
- package/dist/generated/AccountPreferences.js.map +1 -1
- package/dist/generated/Sessions.d.ts.map +1 -1
- package/dist/generated/Sessions.js +3 -7
- package/dist/generated/Sessions.js.map +1 -1
- package/dist/server/NativeOrigins.d.ts +5 -0
- package/dist/server/NativeOrigins.d.ts.map +1 -0
- package/dist/server/NativeOrigins.js +16 -0
- package/dist/server/NativeOrigins.js.map +1 -0
- package/dist/server/SessionPolicy.d.ts +2 -0
- package/dist/server/SessionPolicy.d.ts.map +1 -1
- package/dist/server/SessionPolicy.js.map +1 -1
- package/dist/server/adapters/local.d.ts +1 -1
- package/dist/server/adapters/local.d.ts.map +1 -1
- package/dist/server/adapters/local.js +3 -4
- package/dist/server/adapters/local.js.map +1 -1
- package/dist/server/better-auth/adapters/admittedSessions.d.ts +1 -2
- package/dist/server/better-auth/adapters/admittedSessions.d.ts.map +1 -1
- package/dist/server/better-auth/adapters/admittedSessions.js +3 -3
- package/dist/server/better-auth/adapters/admittedSessions.js.map +1 -1
- package/dist/server/better-auth/adapters/bearerSessions.d.ts +1 -3
- package/dist/server/better-auth/adapters/bearerSessions.d.ts.map +1 -1
- package/dist/server/better-auth/adapters/bearerSessions.js +6 -0
- package/dist/server/better-auth/adapters/bearerSessions.js.map +1 -1
- package/dist/server/better-auth/adapters/response.d.ts +1 -2
- package/dist/server/better-auth/adapters/response.d.ts.map +1 -1
- package/dist/server/better-auth/adapters/response.js +2 -2
- package/dist/server/better-auth/adapters/response.js.map +1 -1
- package/dist/server/better-auth/define.d.ts +4 -0
- package/dist/server/better-auth/define.d.ts.map +1 -1
- package/dist/server/better-auth/define.js.map +1 -1
- package/dist/server/better-auth/mount.d.ts +4 -5
- package/dist/server/better-auth/mount.d.ts.map +1 -1
- package/dist/server/better-auth/mount.js +8 -5
- package/dist/server/better-auth/mount.js.map +1 -1
- package/dist/server/localOwner.d.ts +3 -1
- package/dist/server/localOwner.d.ts.map +1 -1
- package/dist/server/localOwner.js +3 -0
- package/dist/server/localOwner.js.map +1 -1
- package/dist/server/lookupSession.d.ts.map +1 -1
- package/dist/server/lookupSession.js +2 -1
- package/dist/server/lookupSession.js.map +1 -1
- package/package.json +8 -8
- package/src/client/SessionCredential.ts +18 -0
- package/src/client/adapters/sessionCredentialPlugin.ts +75 -0
- package/src/generated/AccountPreferences.ts +3 -8
- package/src/generated/Sessions.ts +3 -7
- package/src/server/NativeOrigins.ts +23 -0
- package/src/server/SessionPolicy.ts +2 -0
- package/src/server/adapters/local.ts +2 -4
- package/src/server/better-auth/adapters/admittedSessions.ts +4 -4
- package/src/server/better-auth/adapters/bearerSessions.ts +10 -4
- package/src/server/better-auth/adapters/response.ts +4 -4
- package/src/server/better-auth/define.ts +10 -5
- package/src/server/better-auth/mount.ts +8 -6
- package/src/server/localOwner.ts +4 -0
- package/src/server/lookupSession.ts +2 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Effect, type Layer } from "effect";
|
|
2
|
+
import { SessionCredential } from "#client/SessionCredential.ts";
|
|
3
|
+
|
|
4
|
+
// Structural copies of the Better Auth client plugin shapes: the client core must not reach the optional better-auth peer.
|
|
5
|
+
interface CredentialRequest {
|
|
6
|
+
credentials?: "include" | "omit" | "same-origin" | undefined;
|
|
7
|
+
readonly headers: Headers;
|
|
8
|
+
readonly url: URL | string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface CredentialAnswer {
|
|
12
|
+
readonly data: unknown;
|
|
13
|
+
readonly request: CredentialRequest;
|
|
14
|
+
readonly response: Response;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface CredentialFetchPlugin {
|
|
18
|
+
readonly hooks: {
|
|
19
|
+
readonly onRequest: (context: CredentialRequest) => Promise<void>;
|
|
20
|
+
readonly onSuccess: (context: CredentialAnswer) => Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
readonly id: string;
|
|
23
|
+
readonly name: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface SessionCredentialPlugin {
|
|
27
|
+
readonly fetchPlugins: CredentialFetchPlugin[];
|
|
28
|
+
readonly id: "session-credential";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function bearerOf(credential: string | null) {
|
|
32
|
+
return credential === null ? null : `Bearer ${credential}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const send = Effect.fnUntraced(function* (request: CredentialRequest) {
|
|
36
|
+
const credential = yield* SessionCredential;
|
|
37
|
+
const authorization = bearerOf(yield* credential.read);
|
|
38
|
+
if (authorization !== null) {
|
|
39
|
+
request.headers.set("authorization", authorization);
|
|
40
|
+
}
|
|
41
|
+
request.credentials = "omit";
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// An answer changes the stored credential only when its request carried the one stored now, so a session check sent
|
|
45
|
+
// before sign-in that answers signed out after it cannot wipe the credential sign-in saved.
|
|
46
|
+
const settle = Effect.fnUntraced(function* ({ data, request, response }: CredentialAnswer) {
|
|
47
|
+
const credential = yield* SessionCredential;
|
|
48
|
+
if (bearerOf(yield* credential.read) !== request.headers.get("authorization")) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const path = new URL(request.url, "http://localhost").pathname;
|
|
52
|
+
const issued = response.headers.get("set-auth-token");
|
|
53
|
+
if (path.endsWith("/sign-out") || (path.endsWith("/get-session") && data === null)) {
|
|
54
|
+
return yield* credential.clear;
|
|
55
|
+
}
|
|
56
|
+
if (issued !== null) {
|
|
57
|
+
yield* credential.write(issued);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export function sessionCredentialPlugin(credential: Layer.Layer<SessionCredential>): SessionCredentialPlugin {
|
|
62
|
+
return {
|
|
63
|
+
fetchPlugins: [
|
|
64
|
+
{
|
|
65
|
+
hooks: {
|
|
66
|
+
onRequest: (request) => Effect.runPromise(Effect.provide(send(request), credential)),
|
|
67
|
+
onSuccess: (answer) => Effect.runPromise(Effect.provide(settle(answer), credential)),
|
|
68
|
+
},
|
|
69
|
+
id: "session-credential",
|
|
70
|
+
name: "session credential",
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
id: "session-credential",
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Generated by @shivaedev/build from src/AccountPreferences.ts. Do not edit or commit this file; run `pnpm generate` to write it again.
|
|
2
2
|
import type { AccountPreferences as AccountPreferencesModule } from "#AccountPreferences.ts";
|
|
3
|
-
import { AccountPreferenceValues
|
|
3
|
+
import { AccountPreferenceValues } from "#accountPreferenceValues.ts";
|
|
4
4
|
import { selfSubject } from "#subject.ts";
|
|
5
5
|
import { clientContract } from "@shivaedev/contract/clientContract.ts";
|
|
6
6
|
import type { ClientMatchesModule } from "@shivaedev/contract/clientMatchesModule.ts";
|
|
@@ -14,18 +14,13 @@ export const identityAccountPreferencesWire = {
|
|
|
14
14
|
} as const;
|
|
15
15
|
|
|
16
16
|
export const AccountPreferences = clientContract({
|
|
17
|
-
commands:
|
|
18
|
-
capture: { invalidates: () => [accountPreferenceKeys.list] },
|
|
19
|
-
set: { invalidates: () => [accountPreferenceKeys.list] },
|
|
20
|
-
},
|
|
17
|
+
commands: ["capture", "set"],
|
|
21
18
|
name: "identity.accountPreferences",
|
|
22
19
|
payloads: {
|
|
23
20
|
capture: AccountPreferenceValues.fields,
|
|
24
21
|
set: AccountPreferenceValues.fields,
|
|
25
22
|
},
|
|
26
|
-
queries:
|
|
27
|
-
get: { reads: () => [accountPreferenceKeys.list] },
|
|
28
|
-
},
|
|
23
|
+
queries: ["get"],
|
|
29
24
|
subject: selfSubject,
|
|
30
25
|
wire: identityAccountPreferencesWire,
|
|
31
26
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by @shivaedev/build from src/Sessions.ts. Do not edit or commit this file; run `pnpm generate` to write it again.
|
|
2
2
|
import type { Sessions as SessionsModule } from "#Sessions.ts";
|
|
3
3
|
import { SessionsPermission } from "#sessionPermissions.ts";
|
|
4
|
-
import { SessionListInput
|
|
4
|
+
import { SessionListInput } from "#signedInSessions.ts";
|
|
5
5
|
import { clientContract } from "@shivaedev/contract/clientContract.ts";
|
|
6
6
|
import type { ClientMatchesModule } from "@shivaedev/contract/clientMatchesModule.ts";
|
|
7
7
|
import { AuthUnavailable, Forbidden, NotFound } from "@shivaedev/errors/taxonomy.ts";
|
|
@@ -13,18 +13,14 @@ export const identitySessionsWire = {
|
|
|
13
13
|
} as const;
|
|
14
14
|
|
|
15
15
|
export const Sessions = clientContract({
|
|
16
|
-
commands:
|
|
17
|
-
revoke: { invalidates: () => [sessionKeys.list] },
|
|
18
|
-
},
|
|
16
|
+
commands: ["revoke"],
|
|
19
17
|
middleware: SessionsPermission,
|
|
20
18
|
name: "identity.sessions",
|
|
21
19
|
payloads: {
|
|
22
20
|
list: SessionListInput.fields,
|
|
23
21
|
revoke: { id: Schema.String },
|
|
24
22
|
},
|
|
25
|
-
queries:
|
|
26
|
-
list: { reads: () => [sessionKeys.list] },
|
|
27
|
-
},
|
|
23
|
+
queries: ["list"],
|
|
28
24
|
wire: identitySessionsWire,
|
|
29
25
|
});
|
|
30
26
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Config, Effect, Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export const NativeOrigins = Schema.Array(
|
|
4
|
+
Schema.String.check(
|
|
5
|
+
Schema.makeFilter((origin) => {
|
|
6
|
+
if (!URL.canParse(origin)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const url = new URL(origin);
|
|
10
|
+
const authority = url.origin === "null" ? `${url.protocol}//${url.host}` : url.origin;
|
|
11
|
+
return url.host !== "" && !origin.includes("*") && authority === origin;
|
|
12
|
+
}),
|
|
13
|
+
),
|
|
14
|
+
).pipe(Schema.brand("NativeOrigins"));
|
|
15
|
+
|
|
16
|
+
export type NativeOrigins = typeof NativeOrigins.Type;
|
|
17
|
+
|
|
18
|
+
export const declaredNativeOrigins = Effect.fnUntraced(function* (origins: readonly string[] | undefined) {
|
|
19
|
+
if (origins === undefined) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
return yield* Schema.decodeUnknownEffect(NativeOrigins)(origins).pipe(Effect.mapError((cause) => new Config.ConfigError(cause)));
|
|
23
|
+
});
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { Context, type Duration } from "effect";
|
|
2
2
|
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
3
|
+
import type { NativeOrigins } from "#server/NativeOrigins.ts";
|
|
3
4
|
import type { OriginPolicy } from "#server/origin.ts";
|
|
4
5
|
import type { SessionProvider, SessionShape } from "#server/session.ts";
|
|
5
6
|
|
|
6
7
|
export class SessionPolicy extends Context.Service<
|
|
7
8
|
SessionPolicy,
|
|
8
9
|
{
|
|
10
|
+
readonly nativeOrigins?: NativeOrigins | undefined;
|
|
9
11
|
readonly origin: OriginPolicy;
|
|
10
12
|
readonly provider: SessionProvider<SessionShape>;
|
|
11
13
|
readonly roles?: RoleSet;
|
|
@@ -75,12 +75,10 @@ export function localTransport(trustedProxy?: string, nativeOrigins: readonly st
|
|
|
75
75
|
export const localRpcGuard = Effect.fnUntraced(function* <E, R>(
|
|
76
76
|
effect: Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>,
|
|
77
77
|
trustedProxy?: string,
|
|
78
|
-
nativeOrigins: readonly string[] = [],
|
|
79
78
|
) {
|
|
80
79
|
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
81
|
-
const policy = yield* Effect.serviceOption(SessionPolicy);
|
|
82
|
-
const
|
|
83
|
-
const allowed = localTransport(trustedProxy, [...nativeOrigins, ...origins]);
|
|
80
|
+
const policy = Option.getOrUndefined(yield* Effect.serviceOption(SessionPolicy));
|
|
81
|
+
const allowed = localTransport(trustedProxy, [...(policy?.nativeOrigins ?? []), ...(policy?.origin.trustedOrigins ?? [])]);
|
|
84
82
|
const serialization = yield* RpcSerialization.RpcSerialization;
|
|
85
83
|
const contentType = request.headers["content-type"]?.split(";")[0]?.trim().toLowerCase();
|
|
86
84
|
const legacyNdjson = serialization.contentType === RpcSerialization.ndjson.contentType && contentType === "application/ndjson";
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
type GetSession,
|
|
7
7
|
type SessionWithProviderAccounts,
|
|
8
8
|
} from "#server/adapters/betterAuthSessions.ts";
|
|
9
|
-
import {
|
|
9
|
+
import { sessionHeaders } from "#server/better-auth/adapters/bearerSessions.ts";
|
|
10
10
|
import { fromStorage, type PolicyStorage, type SignInGate } from "#server/better-auth/adapters/signInGate.ts";
|
|
11
11
|
import { providerFailure } from "#server/providerFailure.ts";
|
|
12
12
|
import type { SessionProvider, SessionShape } from "#server/session.ts";
|
|
@@ -15,7 +15,7 @@ interface SessionStorage extends PolicyStorage {
|
|
|
15
15
|
readonly authCookies: { readonly sessionToken: { readonly name: string } };
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export function admittedSessions<E>(gate: SignInGate<E>, storage: () => Promise<SessionStorage>, bearerSessions
|
|
18
|
+
export function admittedSessions<E>(gate: SignInGate<E>, storage: () => Promise<SessionStorage>, bearerSessions = false) {
|
|
19
19
|
function admit<TSession extends SessionShape>(getSession: GetSession<TSession>): SessionProvider<TSession>;
|
|
20
20
|
function admit<TSession extends SessionShape>(
|
|
21
21
|
getSession: GetSession<TSession>,
|
|
@@ -25,9 +25,9 @@ export function admittedSessions<E>(gate: SignInGate<E>, storage: () => Promise<
|
|
|
25
25
|
const provider = getProviderAccounts === undefined ? betterAuthSessions(getSession) : betterAuthSessions(getSession, getProviderAccounts);
|
|
26
26
|
return {
|
|
27
27
|
get: Effect.fnUntraced(function* (headers) {
|
|
28
|
-
const auth = gate.active || bearerSessions
|
|
28
|
+
const auth = gate.active || bearerSessions ? yield* fromStorage(storage) : undefined;
|
|
29
29
|
const selected =
|
|
30
|
-
bearerSessions
|
|
30
|
+
!bearerSessions || auth === undefined
|
|
31
31
|
? headers
|
|
32
32
|
: HttpHeaders.fromInput(sessionHeaders(new Headers(headers), auth.authCookies.sessionToken.name));
|
|
33
33
|
const session = yield* provider.get(selected);
|
|
@@ -2,14 +2,20 @@ import type { BetterAuthOptions } from "better-auth";
|
|
|
2
2
|
import { bearer } from "better-auth/plugins/bearer";
|
|
3
3
|
import { Result } from "effect";
|
|
4
4
|
|
|
5
|
-
export interface BearerSessionsOptions {
|
|
6
|
-
readonly origins: readonly string[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
export function withBearerSessions<TOptions extends BetterAuthOptions>(options: TOptions, enabled: boolean): TOptions {
|
|
10
6
|
return enabled ? { ...options, plugins: [...(options.plugins ?? []), bearer({ requireSignature: true })] } : options;
|
|
11
7
|
}
|
|
12
8
|
|
|
9
|
+
export function nativeTrustedOrigins(
|
|
10
|
+
trusted: NonNullable<BetterAuthOptions["trustedOrigins"]>,
|
|
11
|
+
nativeOrigins: readonly string[] = [],
|
|
12
|
+
): NonNullable<BetterAuthOptions["trustedOrigins"]> {
|
|
13
|
+
if (nativeOrigins.length === 0) {
|
|
14
|
+
return trusted;
|
|
15
|
+
}
|
|
16
|
+
return typeof trusted === "function" ? async (request) => [...(await trusted(request)), ...nativeOrigins] : [...trusted, ...nativeOrigins];
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
export function hasSessionCookie(headers: Headers, cookieName: string): boolean {
|
|
14
20
|
return (
|
|
15
21
|
headers
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Cause, Effect } from "effect";
|
|
2
2
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
3
3
|
import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { cookieSessionResponse, hasSessionCookie, sessionHeaders } from "#server/better-auth/adapters/bearerSessions.ts";
|
|
5
5
|
import type { HookRefusal } from "#server/better-auth/adapters/hookRuntime.ts";
|
|
6
6
|
import { sessionPermissions } from "#server/better-auth/sessionPermissions.ts";
|
|
7
7
|
|
|
@@ -11,9 +11,9 @@ export interface MountedAuth {
|
|
|
11
11
|
}
|
|
12
12
|
export interface MountRouteOptions {
|
|
13
13
|
readonly basePath?: string | undefined;
|
|
14
|
-
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
15
14
|
readonly clientIpHeader: string;
|
|
16
15
|
readonly ipv6Subnet?: number | undefined;
|
|
16
|
+
readonly nativeOrigins?: readonly string[] | undefined;
|
|
17
17
|
readonly roles?: RoleSet | undefined;
|
|
18
18
|
readonly runRequest?: (<A>(request: (signal: AbortSignal) => Promise<A>) => Effect.Effect<A, Cause.UnknownError | HookRefusal>) | undefined;
|
|
19
19
|
readonly trustedProxy?: string | undefined;
|
|
@@ -28,13 +28,13 @@ function replacesSession(request: Request, reply: Response, native: boolean, bas
|
|
|
28
28
|
);
|
|
29
29
|
}
|
|
30
30
|
async function providerResponse(auth: MountedAuth, web: Request, options: MountRouteOptions) {
|
|
31
|
-
if (options.
|
|
31
|
+
if (options.nativeOrigins === undefined) {
|
|
32
32
|
return auth.handler(web);
|
|
33
33
|
}
|
|
34
34
|
const { authCookies } = await auth.$context;
|
|
35
35
|
const cookieName = authCookies.sessionToken.name;
|
|
36
36
|
const headers = sessionHeaders(web.headers, cookieName);
|
|
37
|
-
const native = options.
|
|
37
|
+
const native = options.nativeOrigins.includes(web.headers.get("origin") ?? "") && !hasSessionCookie(web.headers, cookieName);
|
|
38
38
|
const request = new Request(web, { headers });
|
|
39
39
|
const reply = await auth.handler(request);
|
|
40
40
|
const basePath = options.basePath ?? "/api/auth";
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { BetterAuthOptions } from "better-auth";
|
|
2
2
|
import type { AccessPolicy, RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
3
3
|
|
|
4
|
-
export type AuthDeclaration<TOptions extends BetterAuthOptions = BetterAuthOptions> = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
);
|
|
4
|
+
export type AuthDeclaration<TOptions extends BetterAuthOptions = BetterAuthOptions> = {
|
|
5
|
+
readonly nativeOrigins?: readonly string[] | undefined;
|
|
6
|
+
readonly options: TOptions;
|
|
7
|
+
} & ({ readonly defaultRole?: undefined; readonly roles?: undefined } | { readonly defaultRole: string; readonly roles: RoleSet });
|
|
8
8
|
|
|
9
9
|
export interface RoleAuthDeclaration<TRole extends string, TOptions extends BetterAuthOptions = BetterAuthOptions> {
|
|
10
10
|
readonly defaultRole: TRole;
|
|
11
|
+
readonly nativeOrigins?: readonly string[] | undefined;
|
|
11
12
|
readonly options: TOptions;
|
|
12
13
|
readonly roles: AccessPolicy<TRole>;
|
|
13
14
|
}
|
|
@@ -28,10 +29,14 @@ export interface RoleTables<TRole extends string> {
|
|
|
28
29
|
|
|
29
30
|
export function defineAuth<const TOptions extends BetterAuthOptions, TRole extends string>(declaration: {
|
|
30
31
|
readonly defaultRole: NoInfer<TRole>;
|
|
32
|
+
readonly nativeOrigins?: readonly string[] | undefined;
|
|
31
33
|
readonly options: TOptions;
|
|
32
34
|
readonly roles: AccessPolicy<TRole>;
|
|
33
35
|
}): RoleAuthDeclaration<TRole, TOptions>;
|
|
34
|
-
export function defineAuth<const TOptions extends BetterAuthOptions>(declaration: {
|
|
36
|
+
export function defineAuth<const TOptions extends BetterAuthOptions>(declaration: {
|
|
37
|
+
readonly nativeOrigins?: readonly string[] | undefined;
|
|
38
|
+
readonly options: TOptions;
|
|
39
|
+
}): AuthDeclaration<TOptions>;
|
|
35
40
|
export function defineAuth(declaration: AuthDeclaration) {
|
|
36
41
|
return declaration;
|
|
37
42
|
}
|
|
@@ -7,7 +7,7 @@ import { accountPreferenceOptions } from "#server/better-auth/adapters/accountPr
|
|
|
7
7
|
import type { AccountRoleSeed } from "#server/better-auth/adapters/accountRoleSeed.ts";
|
|
8
8
|
import { acquireAuth } from "#server/better-auth/adapters/acquire.ts";
|
|
9
9
|
import { admittedSessions } from "#server/better-auth/adapters/admittedSessions.ts";
|
|
10
|
-
import {
|
|
10
|
+
import { nativeTrustedOrigins, withBearerSessions } from "#server/better-auth/adapters/bearerSessions.ts";
|
|
11
11
|
import { betterAuthDatabase } from "#server/better-auth/adapters/database.ts";
|
|
12
12
|
import { effectHookOptions } from "#server/better-auth/adapters/effectHookOptions.ts";
|
|
13
13
|
import { emailOptions } from "#server/better-auth/adapters/emailOptions.ts";
|
|
@@ -32,11 +32,11 @@ import type { BetterAuthPasswordReset } from "#server/better-auth/PasswordReset.
|
|
|
32
32
|
import { authRouteHandler } from "#server/better-auth/routeHandler.ts";
|
|
33
33
|
import { storedRoleOptions } from "#server/better-auth/storedRoleOptions.ts";
|
|
34
34
|
import { UndeclaredDefaultRole } from "#server/better-auth/UndeclaredDefaultRole.ts";
|
|
35
|
+
import { declaredNativeOrigins } from "#server/NativeOrigins.ts";
|
|
35
36
|
import { providerFailure } from "#server/providerFailure.ts";
|
|
36
37
|
|
|
37
38
|
export interface BetterAuthMountOptions<E = never, R = never> {
|
|
38
39
|
readonly accountPreferences?: boolean;
|
|
39
|
-
readonly bearerSessions?: BearerSessionsOptions | undefined;
|
|
40
40
|
readonly clientIp?: "railway";
|
|
41
41
|
readonly databaseHooks?: BetterAuthDatabaseHooks<R> | undefined;
|
|
42
42
|
readonly emails?: BetterAuthEmails<R> | undefined;
|
|
@@ -58,6 +58,7 @@ const mountAuth = Effect.fn("Identity.mountAuth")(function* <const TOptions exte
|
|
|
58
58
|
const { defaultRole } = declaration;
|
|
59
59
|
yield* declaration.roles.resolve(defaultRole).pipe(Effect.mapError(() => new UndeclaredDefaultRole({ role: defaultRole })));
|
|
60
60
|
}
|
|
61
|
+
const nativeOrigins = yield* declaredNativeOrigins(declaration.nativeOrigins);
|
|
61
62
|
const options =
|
|
62
63
|
declaration.options.database === undefined ? { ...declaration.options, database: yield* betterAuthDatabase() } : declaration.options;
|
|
63
64
|
const { authOptions, clientIpHeader, rateLimitEnabled, trustedProxy } = yield* mountSettings(options, mount.clientIp);
|
|
@@ -83,13 +84,14 @@ const mountAuth = Effect.fn("Identity.mountAuth")(function* <const TOptions exte
|
|
|
83
84
|
const basePath = prefix === "/" ? "" : prefix;
|
|
84
85
|
const customStorage = yield* rateLimitStorage;
|
|
85
86
|
const { auth, profiles } = yield* acquireAuth({
|
|
86
|
-
...withHardening(withBearerSessions(preset,
|
|
87
|
+
...withHardening(withBearerSessions(preset, nativeOrigins !== undefined), mount.hardening),
|
|
87
88
|
...authOptions,
|
|
88
89
|
rateLimit: {
|
|
89
90
|
...rateLimit,
|
|
90
91
|
customStorage,
|
|
91
92
|
enabled: rateLimitEnabled,
|
|
92
93
|
},
|
|
94
|
+
trustedOrigins: nativeTrustedOrigins(authOptions.trustedOrigins, nativeOrigins),
|
|
93
95
|
user: gate.active ? gated.user : configured.user,
|
|
94
96
|
});
|
|
95
97
|
const authRoutes = mountRoutes.pipe(
|
|
@@ -98,9 +100,9 @@ const mountAuth = Effect.fn("Identity.mountAuth")(function* <const TOptions exte
|
|
|
98
100
|
basePath,
|
|
99
101
|
handle: authRouteHandler(auth, {
|
|
100
102
|
basePath,
|
|
101
|
-
bearerSessions: mount.bearerSessions,
|
|
102
103
|
clientIpHeader,
|
|
103
104
|
ipv6Subnet: options.advanced?.ipAddress?.ipv6Subnet,
|
|
105
|
+
nativeOrigins,
|
|
104
106
|
roles: declaration.roles,
|
|
105
107
|
runRequest: runtime.runRequest,
|
|
106
108
|
trustedProxy,
|
|
@@ -108,7 +110,7 @@ const mountAuth = Effect.fn("Identity.mountAuth")(function* <const TOptions exte
|
|
|
108
110
|
}),
|
|
109
111
|
),
|
|
110
112
|
);
|
|
111
|
-
const admit = admittedSessions(gate, () => auth.$context,
|
|
113
|
+
const admit = admittedSessions(gate, () => auth.$context, nativeOrigins !== undefined);
|
|
112
114
|
const admission = yield* signInGate({ ...policy, recheckInterval: 0 });
|
|
113
115
|
const admitsUser = Effect.fn("Identity.admitsUser")(function* (userId: string) {
|
|
114
116
|
if (!admission.active) {
|
|
@@ -119,7 +121,7 @@ const mountAuth = Effect.fn("Identity.mountAuth")(function* <const TOptions exte
|
|
|
119
121
|
});
|
|
120
122
|
const revoke = revokeUnadmitted(gate, () => auth.$context);
|
|
121
123
|
const lookupSession: unknown = Reflect.get(auth.api, "getSession");
|
|
122
|
-
const sessions = mountedSessions(lookupSession, admit, declaration.roles);
|
|
124
|
+
const sessions = { ...mountedSessions(lookupSession, admit, declaration.roles), nativeOrigins };
|
|
123
125
|
return { admitsUser, admittedSessions: admit, auth, profiles, revokeUnadmitted: revoke, routes: authRoutes, sessions };
|
|
124
126
|
});
|
|
125
127
|
|
package/src/server/localOwner.ts
CHANGED
|
@@ -5,20 +5,24 @@ import type { RoleSet } from "@shivaedev/access/roleSet.ts";
|
|
|
5
5
|
import type { SessionUserValue } from "#identity.ts";
|
|
6
6
|
import { localTransport } from "#server/adapters/local.ts";
|
|
7
7
|
import { machineTokens } from "#server/machineTokens.ts";
|
|
8
|
+
import { declaredNativeOrigins } from "#server/NativeOrigins.ts";
|
|
8
9
|
import type { SessionPolicy } from "#server/SessionPolicy.ts";
|
|
9
10
|
|
|
10
11
|
export const localOwner = Effect.fn("Identity.localOwner")(function* (options: {
|
|
12
|
+
readonly nativeOrigins?: readonly string[] | undefined;
|
|
11
13
|
readonly owner: Pick<SessionUserValue, "id" | "name" | "email"> & { readonly role: string };
|
|
12
14
|
readonly roles: RoleSet;
|
|
13
15
|
readonly streamRevalidationInterval?: Duration.Input;
|
|
14
16
|
}) {
|
|
15
17
|
const { owner, roles } = options;
|
|
18
|
+
const nativeOrigins = yield* declaredNativeOrigins(options.nativeOrigins);
|
|
16
19
|
const access = yield* roles.resolve(owner.role);
|
|
17
20
|
const random = yield* SecureRandom.SecureRandom;
|
|
18
21
|
const token = Redacted.make(Hex.encode(yield* random.randomBytes(32)));
|
|
19
22
|
const machines = yield* machineTokens({ owner, roles });
|
|
20
23
|
const allowed = localTransport();
|
|
21
24
|
const sessions: SessionPolicy["Service"] = {
|
|
25
|
+
nativeOrigins,
|
|
22
26
|
origin: (request) => allowed({ headers: request.headers, remoteAddress: request.remoteAddress ?? Option.none() }),
|
|
23
27
|
provider: {
|
|
24
28
|
get: (headers) => {
|
|
@@ -9,7 +9,8 @@ import type { SessionShape } from "#server/session.ts";
|
|
|
9
9
|
export const sessionRequest = Effect.fnUntraced(function* (headers: Headers.Headers, rpc: string) {
|
|
10
10
|
const policy = yield* SessionPolicy;
|
|
11
11
|
const request = yield* transportOriginRequest(headers, rpc);
|
|
12
|
-
|
|
12
|
+
const native = Option.exists(request.origin, (origin) => policy.nativeOrigins?.includes(origin) === true);
|
|
13
|
+
if (!(native || policy.origin(request))) {
|
|
13
14
|
const refused = Option.match(request.origin, { onNone: () => "a request without an Origin header", onSome: (origin) => `origin ${origin}` });
|
|
14
15
|
yield* Effect.logWarning(
|
|
15
16
|
`The session policy refused ${refused} calling ${rpc}. Add the origin to the policy's trusted origins if the application is served from it.`,
|