Open History to see the last commit
1import type { AuthEnvironmentScope, AuthSessionId } from "@t3tools/contracts";
2import * as Data from "effect/Data";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5import * as SubscriptionRef from "effect/SubscriptionRef";
6import type { HttpMethod } from "effect/unstable/http";
7
8import { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
9import { EnvironmentSupervisor } from "../connection/supervisor.ts";
10import { environmentEndpointUrl } from "../environment/endpoint.ts";
11import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
12import { makeEnvironmentHttpApiClient } from "../rpc/http.ts";
13import {
14 executeAuthenticatedEnvironmentHttpRequest,
15 type EnvironmentHttpAuthHeaders,
16} from "./environmentHttpAuth.ts";
17
18const AUTH_MUTATION_TIMEOUT_MS = 10_000;
19
20export class EnvironmentNotConnectedError extends Data.TaggedError(
21 "@t3tools/client-runtime/state/authHttp/EnvironmentNotConnectedError",
22)<{ readonly message: string }> {}
23
24const executeAuthRequest = <A, E, R>(input: {
25 readonly method: HttpMethod.HttpMethod;
26 readonly pathname: string;
27 readonly request: (input: {
28 readonly client: Effect.Success<ReturnType<typeof makeEnvironmentHttpApiClient>>;
29 readonly headers: EnvironmentHttpAuthHeaders;
30 }) => Effect.Effect<A, E, R>;
31 readonly isUnauthorizedResponse?: (response: NoInfer<A>) => boolean;
32}) =>
33 Effect.gen(function* () {
34 const supervisor = yield* EnvironmentSupervisor;
35 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
36 if (Option.isNone(prepared)) {
37 return yield* new EnvironmentNotConnectedError({
38 message: "This environment is not connected, so its access settings cannot be changed.",
39 });
40 }
41 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
42 const remoteAuthorization = yield* Effect.serviceOption(RemoteEnvironmentAuthorization);
43 return yield* executeAuthenticatedEnvironmentHttpRequest({
44 prepared: prepared.value,
45 signer,
46 remoteAuthorization,
47 method: input.method,
48 url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, input.pathname),
49 timeoutMs: AUTH_MUTATION_TIMEOUT_MS,
50 request: input.request,
51 ...(input.isUnauthorizedResponse === undefined
52 ? {}
53 : { isUnauthorizedResponse: input.isUnauthorizedResponse }),
54 });
55 });
56
57export const fetchEnvironmentSessionState = Effect.fn(
58 "clientRuntime.state.authHttp.fetchEnvironmentSessionState",
59)(function* () {
60 return yield* executeAuthRequest({
61 method: "GET",
62 pathname: "/api/auth/session",
63 request: ({ client, headers }) => client.auth.session({ headers }),
64 isUnauthorizedResponse: (response) => !response.authenticated,
65 });
66});
67
68export const createEnvironmentPairingCredential = Effect.fn(
69 "clientRuntime.state.authHttp.createEnvironmentPairingCredential",
70)(function* (input: {
71 readonly label?: string;
72 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
73}) {
74 const trimmedLabel = input.label?.trim();
75 return yield* executeAuthRequest({
76 method: "POST",
77 pathname: "/api/auth/pairing-token",
78 request: ({ client, headers }) =>
79 client.auth.pairingCredential({
80 headers,
81 payload: {
82 ...(trimmedLabel ? { label: trimmedLabel } : {}),
83 ...(input.scopes ? { scopes: input.scopes } : {}),
84 },
85 }),
86 });
87});
88
89export const revokeEnvironmentPairingLink = Effect.fn(
90 "clientRuntime.state.authHttp.revokeEnvironmentPairingLink",
91)(function* (input: { readonly id: string }) {
92 return yield* executeAuthRequest({
93 method: "POST",
94 pathname: "/api/auth/pairing-links/revoke",
95 request: ({ client, headers }) =>
96 client.auth.revokePairingLink({ headers, payload: { id: input.id } }),
97 });
98});
99
100export const revokeEnvironmentClientSession = Effect.fn(
101 "clientRuntime.state.authHttp.revokeEnvironmentClientSession",
102)(function* (input: { readonly sessionId: AuthSessionId }) {
103 return yield* executeAuthRequest({
104 method: "POST",
105 pathname: "/api/auth/clients/revoke",
106 request: ({ client, headers }) =>
107 client.auth.revokeClient({ headers, payload: { sessionId: input.sessionId } }),
108 });
109});
110
111export const revokeOtherEnvironmentClientSessions = Effect.fn(
112 "clientRuntime.state.authHttp.revokeOtherEnvironmentClientSessions",
113)(function* () {
114 return yield* executeAuthRequest({
115 method: "POST",
116 pathname: "/api/auth/clients/revoke-others",
117 request: ({ client, headers }) => client.auth.revokeOtherClients({ headers }),
118 });
119});