-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathServerAuthPolicy.ts
More file actions
41 lines (34 loc) · 1.43 KB
/
ServerAuthPolicy.ts
File metadata and controls
41 lines (34 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { ServerAuthDescriptor } from "@t3tools/contracts";
import { Effect, Layer } from "effect";
import { ServerConfig } from "../../config.ts";
import { ServerAuthPolicy, type ServerAuthPolicyShape } from "../Services/ServerAuthPolicy.ts";
import { SESSION_COOKIE_NAME } from "../utils.ts";
import { isLoopbackHost, isWildcardHost } from "../../startupAccess.ts";
export const makeServerAuthPolicy = Effect.gen(function* () {
const config = yield* ServerConfig;
const isRemoteReachable = isWildcardHost(config.host) || !isLoopbackHost(config.host);
const policy =
config.mode === "desktop"
? isRemoteReachable
? "remote-reachable"
: "desktop-managed-local"
: isRemoteReachable
? "remote-reachable"
: "loopback-browser";
const bootstrapMethods: ServerAuthDescriptor["bootstrapMethods"] =
policy === "desktop-managed-local"
? ["desktop-bootstrap"]
: config.mode === "desktop" && policy === "remote-reachable"
? ["desktop-bootstrap", "one-time-token"]
: ["one-time-token"];
const descriptor: ServerAuthDescriptor = {
policy,
bootstrapMethods,
sessionMethods: ["browser-session-cookie", "bearer-session-token"],
sessionCookieName: SESSION_COOKIE_NAME,
};
return {
getDescriptor: () => Effect.succeed(descriptor),
} satisfies ServerAuthPolicyShape;
});
export const ServerAuthPolicyLive = Layer.effect(ServerAuthPolicy, makeServerAuthPolicy);