@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 160 lines 5.63 KB
Raw
1 import * as Context from "effect/Context";
2 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Schema from "effect/Schema";
6 import * as SqlClient from "effect/sql/SqlClient";
7 import * as SqlSchema from "effect/sql/SqlSchema";
8
9 import { AuthClientMetadata } from "@t3tools/contracts";
10
11 import { type AuthPairingLinkRepositoryError, PersistenceSqlError } from "./Errors.ts";
12
13 export const AuthConnectDeviceRecord = Schema.Struct({
14 proofKeyThumbprint: Schema.String,
15 client: Schema.NullOr(Schema.fromJsonString(AuthClientMetadata)),
16 approvedAt: Schema.DateTimeUtcFromString,
17 });
18 export type AuthConnectDeviceRecord = typeof AuthConnectDeviceRecord.Type;
19
20 const TrustInput = Schema.Struct({
21 proofKeyThumbprint: Schema.String,
22 client: Schema.NullOr(Schema.fromJsonString(AuthClientMetadata)),
23 approvedAt: Schema.DateTimeUtcFromString,
24 });
25
26 const ThumbprintInput = Schema.Struct({ proofKeyThumbprint: Schema.String });
27
28 const RemoveAllExceptInput = Schema.Struct({
29 keepProofKeyThumbprint: Schema.NullOr(Schema.String),
30 });
31
32 /** T3 Connect device keys this environment lets in without asking. A key leaves when revoked. */
33 export class AuthConnectDeviceRepository extends Context.Service<
34 AuthConnectDeviceRepository,
35 {
36 /** Adds the key, or refreshes how an already trusted key describes itself. */
37 readonly trust: (
38 input: typeof TrustInput.Type,
39 ) => Effect.Effect<void, AuthPairingLinkRepositoryError>;
40 readonly isTrusted: (
41 proofKeyThumbprint: string,
42 ) => Effect.Effect<boolean, AuthPairingLinkRepositoryError>;
43 readonly list: () => Effect.Effect<
44 ReadonlyArray<AuthConnectDeviceRecord>,
45 AuthPairingLinkRepositoryError
46 >;
47 readonly remove: (
48 proofKeyThumbprint: string,
49 ) => Effect.Effect<boolean, AuthPairingLinkRepositoryError>;
50 readonly removeAllExcept: (
51 keepProofKeyThumbprint: string | null,
52 ) => Effect.Effect<number, AuthPairingLinkRepositoryError>;
53 }
54 >()("t3/persistence/AuthConnectDevices/AuthConnectDeviceRepository") {}
55
56 const sqlError = (operation: string) => (cause: unknown) =>
57 new PersistenceSqlError({ operation, cause });
58
59 /** @public */
60 export const make = Effect.gen(function* () {
61 const sql = yield* SqlClient.SqlClient;
62
63 const trustRow = SqlSchema.void({
64 Request: TrustInput,
65 execute: ({ proofKeyThumbprint, client, approvedAt }) =>
66 sql`
67 INSERT INTO auth_connect_devices (proof_key_thumbprint, client_json, approved_at)
68 VALUES (${proofKeyThumbprint}, ${client}, ${approvedAt})
69 ON CONFLICT (proof_key_thumbprint) DO UPDATE
70 SET client_json = COALESCE(excluded.client_json, auth_connect_devices.client_json)
71 `,
72 });
73
74 const isTrustedRow = SqlSchema.findAll({
75 Request: ThumbprintInput,
76 Result: Schema.Struct({ proofKeyThumbprint: Schema.String }),
77 execute: ({ proofKeyThumbprint }) =>
78 sql`
79 SELECT proof_key_thumbprint AS "proofKeyThumbprint"
80 FROM auth_connect_devices
81 WHERE proof_key_thumbprint = ${proofKeyThumbprint}
82 `,
83 });
84
85 const listRows = SqlSchema.findAll({
86 Request: Schema.Void,
87 Result: AuthConnectDeviceRecord,
88 execute: () =>
89 sql`
90 SELECT
91 proof_key_thumbprint AS "proofKeyThumbprint",
92 client_json AS "client",
93 approved_at AS "approvedAt"
94 FROM auth_connect_devices
95 ORDER BY approved_at DESC, proof_key_thumbprint
96 `,
97 });
98
99 const removeRows = SqlSchema.findAll({
100 Request: ThumbprintInput,
101 Result: Schema.Struct({ proofKeyThumbprint: Schema.String }),
102 execute: ({ proofKeyThumbprint }) =>
103 sql`
104 DELETE FROM auth_connect_devices
105 WHERE proof_key_thumbprint = ${proofKeyThumbprint}
106 RETURNING proof_key_thumbprint AS "proofKeyThumbprint"
107 `,
108 });
109
110 const removeAllExceptRows = SqlSchema.findAll({
111 Request: RemoveAllExceptInput,
112 Result: Schema.Struct({ proofKeyThumbprint: Schema.String }),
113 execute: ({ keepProofKeyThumbprint }) =>
114 sql`
115 DELETE FROM auth_connect_devices
116 WHERE proof_key_thumbprint IS NOT ${keepProofKeyThumbprint}
117 RETURNING proof_key_thumbprint AS "proofKeyThumbprint"
118 `,
119 });
120
121 const trust: AuthConnectDeviceRepository["Service"]["trust"] = (input) =>
122 trustRow(input).pipe(Effect.mapError(sqlError("AuthConnectDeviceRepository.trust")));
123
124 const isTrusted: AuthConnectDeviceRepository["Service"]["isTrusted"] = (proofKeyThumbprint) =>
125 isTrustedRow({ proofKeyThumbprint }).pipe(
126 NodeSqliteClient.readOnly,
127 Effect.map((rows) => rows.length > 0),
128 Effect.mapError(sqlError("AuthConnectDeviceRepository.isTrusted")),
129 );
130
131 const list: AuthConnectDeviceRepository["Service"]["list"] = () =>
132 listRows(undefined).pipe(
133 NodeSqliteClient.readOnly,
134 Effect.mapError(sqlError("AuthConnectDeviceRepository.list")),
135 );
136
137 const remove: AuthConnectDeviceRepository["Service"]["remove"] = (proofKeyThumbprint) =>
138 removeRows({ proofKeyThumbprint }).pipe(
139 Effect.map((rows) => rows.length > 0),
140 Effect.mapError(sqlError("AuthConnectDeviceRepository.remove")),
141 );
142
143 const removeAllExcept: AuthConnectDeviceRepository["Service"]["removeAllExcept"] = (
144 keepProofKeyThumbprint,
145 ) =>
146 removeAllExceptRows({ keepProofKeyThumbprint }).pipe(
147 Effect.map((rows) => rows.length),
148 Effect.mapError(sqlError("AuthConnectDeviceRepository.removeAllExcept")),
149 );
150
151 return {
152 trust,
153 isTrusted,
154 list,
155 remove,
156 removeAllExcept,
157 } satisfies AuthConnectDeviceRepository["Service"];
158 });
159
160 export const layer = Layer.effect(AuthConnectDeviceRepository, make);