Open History to see the last commit
1import * as Context from "effect/Context";
2import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Schema from "effect/Schema";
7import * as SqlClient from "effect/sql/SqlClient";
8import * as SqlSchema from "effect/sql/SqlSchema";
9
10import { AuthClientMetadata, AuthEnvironmentScopes } from "@t3tools/contracts";
11
12import {
13 type AuthPairingLinkRepositoryError,
14 PersistenceDecodeError,
15 type PersistenceErrorCorrelation,
16 PersistenceSqlError,
17} from "./Errors.ts";
18
19/**
20 * These subjects are reserved for one kind and level of MCP authorization.
21 * Keep peer-link subjects under the same prefix so older servers refuse them too.
22 */
23export const MCP_APPROVAL_SUBJECT_PREFIX = "mcp-approval:";
24export const PEER_LINK_PAIRING_SUBJECT_PREFIX = `${MCP_APPROVAL_SUBJECT_PREFIX}peer-link:`;
25
26const SqliteFlag = Schema.Literals([0, 1]);
27
28export const AuthPairingLinkRecord = Schema.Struct({
29 id: Schema.String,
30 credential: Schema.String,
31 method: Schema.Literals(["desktop-bootstrap", "one-time-token"]),
32 scopes: Schema.fromJsonString(AuthEnvironmentScopes),
33 subject: Schema.String,
34 label: Schema.NullOr(Schema.String),
35 proofKeyThumbprint: Schema.NullOr(Schema.String),
36 createdAt: Schema.DateTimeUtcFromString,
37 expiresAt: Schema.DateTimeUtcFromString,
38 consumedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
39 revokedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
40 /** Spendable only once `proofKeyThumbprint` is a trusted T3 Connect device. */
41 requiresConnectApproval: SqliteFlag,
42 connectApproved: SqliteFlag,
43 requestedClient: Schema.NullOr(Schema.fromJsonString(AuthClientMetadata)),
44});
45export type AuthPairingLinkRecord = typeof AuthPairingLinkRecord.Type;
46
47export const CreateAuthPairingLinkInput = Schema.Struct({
48 id: Schema.String,
49 credential: Schema.String,
50 method: Schema.Literals(["desktop-bootstrap", "one-time-token"]),
51 scopes: AuthEnvironmentScopes,
52 subject: Schema.String,
53 label: Schema.NullOr(Schema.String),
54 proofKeyThumbprint: Schema.NullOr(Schema.String),
55 requiresConnectApproval: Schema.optionalKey(Schema.Boolean),
56 createdAt: Schema.DateTimeUtcFromString,
57 expiresAt: Schema.DateTimeUtcFromString,
58});
59export type CreateAuthPairingLinkInput = typeof CreateAuthPairingLinkInput.Type;
60
61export const ConsumeAuthPairingLinkInput = Schema.Struct({
62 credential: Schema.String,
63 proofKeyThumbprint: Schema.NullOr(Schema.String),
64 requestedScopes: Schema.optionalKey(AuthEnvironmentScopes),
65 requiredScopes: Schema.optionalKey(AuthEnvironmentScopes),
66 /** Allow a purpose-bound code only when its subject matches the request. */
67 allowedSubject: Schema.optionalKey(Schema.String),
68 consumedAt: Schema.DateTimeUtcFromString,
69 now: Schema.DateTimeUtcFromString,
70});
71export type ConsumeAuthPairingLinkInput = typeof ConsumeAuthPairingLinkInput.Type;
72
73export const ListActiveAuthPairingLinksInput = Schema.Struct({
74 now: Schema.DateTimeUtcFromString,
75});
76export type ListActiveAuthPairingLinksInput = typeof ListActiveAuthPairingLinksInput.Type;
77
78export const RevokeAuthPairingLinkInput = Schema.Struct({
79 id: Schema.String,
80 revokedAt: Schema.DateTimeUtcFromString,
81});
82export type RevokeAuthPairingLinkInput = typeof RevokeAuthPairingLinkInput.Type;
83
84export const GetAuthPairingLinkByCredentialInput = Schema.Struct({
85 credential: Schema.String,
86});
87export type GetAuthPairingLinkByCredentialInput = typeof GetAuthPairingLinkByCredentialInput.Type;
88
89export const GetActiveAuthPairingLinkByIdInput = Schema.Struct({
90 id: Schema.String,
91 now: Schema.DateTimeUtcFromString,
92});
93export type GetActiveAuthPairingLinkByIdInput = typeof GetActiveAuthPairingLinkByIdInput.Type;
94
95export const FindPendingConnectPairingLinkInput = Schema.Struct({
96 proofKeyThumbprint: Schema.String,
97 now: Schema.DateTimeUtcFromString,
98});
99export type FindPendingConnectPairingLinkInput = typeof FindPendingConnectPairingLinkInput.Type;
100
101/** Unspent links bound to `proofKeyThumbprint`, or with `keepProofKeyThumbprint` every other bound one. */
102export const RevokeUnspentByProofKeyInput = Schema.Struct({
103 proofKeyThumbprint: Schema.NullOr(Schema.String),
104 keepProofKeyThumbprint: Schema.NullOr(Schema.String),
105 revokedAt: Schema.DateTimeUtcFromString,
106});
107export type RevokeUnspentByProofKeyInput = typeof RevokeUnspentByProofKeyInput.Type;
108
109export const RecordConnectRequestClientInput = Schema.Struct({
110 id: Schema.String,
111 client: Schema.fromJsonString(AuthClientMetadata),
112});
113export type RecordConnectRequestClientInput = typeof RecordConnectRequestClientInput.Type;
114
115const AuthPairingLinkRawDbRow = Schema.Struct({
116 id: Schema.String,
117 credential: Schema.Unknown,
118 method: Schema.Unknown,
119 scopes: Schema.Unknown,
120 subject: Schema.Unknown,
121 label: Schema.Unknown,
122 proofKeyThumbprint: Schema.Unknown,
123 createdAt: Schema.Unknown,
124 expiresAt: Schema.Unknown,
125 consumedAt: Schema.Unknown,
126 revokedAt: Schema.Unknown,
127 requiresConnectApproval: Schema.Unknown,
128 connectApproved: Schema.Unknown,
129 requestedClient: Schema.Unknown,
130});
131
132const decodeAuthPairingLinkDbRow = Schema.decodeUnknownEffect(AuthPairingLinkRecord);
133
134export class AuthPairingLinkRepository extends Context.Service<
135 AuthPairingLinkRepository,
136 {
137 readonly create: (
138 input: CreateAuthPairingLinkInput,
139 ) => Effect.Effect<void, AuthPairingLinkRepositoryError>;
140 readonly consumeAvailable: (
141 input: ConsumeAuthPairingLinkInput,
142 ) => Effect.Effect<Option.Option<AuthPairingLinkRecord>, AuthPairingLinkRepositoryError>;
143 readonly listActive: (
144 input: ListActiveAuthPairingLinksInput,
145 ) => Effect.Effect<ReadonlyArray<AuthPairingLinkRecord>, AuthPairingLinkRepositoryError>;
146 readonly revoke: (
147 input: RevokeAuthPairingLinkInput,
148 ) => Effect.Effect<boolean, AuthPairingLinkRepositoryError>;
149 readonly getByCredential: (
150 input: GetAuthPairingLinkByCredentialInput,
151 ) => Effect.Effect<Option.Option<AuthPairingLinkRecord>, AuthPairingLinkRepositoryError>;
152 readonly getActiveById: (
153 input: GetActiveAuthPairingLinkByIdInput,
154 ) => Effect.Effect<Option.Option<AuthPairingLinkRecord>, AuthPairingLinkRepositoryError>;
155 /** The device's open or denied request, so its retries do not stack up or re-prompt. */
156 readonly findPendingConnect: (
157 input: FindPendingConnectPairingLinkInput,
158 ) => Effect.Effect<Option.Option<AuthPairingLinkRecord>, AuthPairingLinkRepositoryError>;
159 /** Keeps the first description a device gave, so it cannot be swapped while someone decides. */
160 readonly recordConnectRequestClient: (
161 input: RecordConnectRequestClientInput,
162 ) => Effect.Effect<boolean, AuthPairingLinkRepositoryError>;
163 readonly revokeUnspentByProofKey: (
164 input: RevokeUnspentByProofKeyInput,
165 ) => Effect.Effect<ReadonlyArray<string>, AuthPairingLinkRepositoryError>;
166 }
167>()("t3/persistence/AuthPairingLinks/AuthPairingLinkRepository") {}
168
169function toPersistenceSqlOrDecodeError(
170 sqlOperation: string,
171 decodeOperation: string,
172 correlation?: PersistenceErrorCorrelation,
173) {
174 return (cause: unknown): AuthPairingLinkRepositoryError =>
175 Schema.isSchemaError(cause)
176 ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause, correlation)
177 : new PersistenceSqlError({
178 operation: sqlOperation,
179 ...(correlation === undefined ? {} : { correlation }),
180 cause,
181 });
182}
183
184/** @public */
185export const make = Effect.gen(function* () {
186 const sql = yield* SqlClient.SqlClient;
187
188 const createPairingLinkRow = SqlSchema.void({
189 Request: CreateAuthPairingLinkInput,
190 execute: (input) =>
191 sql`
192 INSERT INTO auth_pairing_links (
193 id,
194 credential,
195 method,
196 scopes,
197 subject,
198 label,
199 proof_key_thumbprint,
200 requires_connect_approval,
201 created_at,
202 expires_at,
203 consumed_at,
204 revoked_at
205 )
206 VALUES (
207 ${input.id},
208 ${input.credential},
209 ${input.method},
210 ${JSON.stringify(input.scopes)},
211 ${input.subject},
212 ${input.label},
213 ${input.proofKeyThumbprint},
214 ${input.requiresConnectApproval ? 1 : 0},
215 ${input.createdAt},
216 ${input.expiresAt},
217 NULL,
218 NULL
219 )
220 `,
221 });
222
223 const consumeAvailablePairingLinkRow = SqlSchema.findOneOption({
224 Request: ConsumeAuthPairingLinkInput,
225 Result: AuthPairingLinkRawDbRow,
226 execute: ({
227 credential,
228 proofKeyThumbprint,
229 requestedScopes,
230 requiredScopes,
231 allowedSubject,
232 consumedAt,
233 now,
234 }) =>
235 sql`
236 UPDATE auth_pairing_links
237 SET consumed_at = ${consumedAt}
238 WHERE credential = ${credential}
239 AND revoked_at IS NULL
240 AND consumed_at IS NULL
241 AND expires_at > ${now}
242 AND (
243 subject NOT LIKE ${`${MCP_APPROVAL_SUBJECT_PREFIX}%`}
244 OR subject = ${allowedSubject ?? null}
245 )
246 AND (
247 proof_key_thumbprint IS NULL
248 OR proof_key_thumbprint = ${proofKeyThumbprint}
249 )
250 AND (
251 requires_connect_approval = 0
252 OR proof_key_thumbprint IN (SELECT proof_key_thumbprint FROM auth_connect_devices)
253 )
254 AND (${requestedScopes === undefined ? 1 : 0} OR EXISTS (
255 SELECT 1
256 FROM json_each(${JSON.stringify(requestedScopes ?? [])}) AS requested
257 WHERE requested.value IN (
258 SELECT value FROM json_each(auth_pairing_links.scopes)
259 )
260 ))
261 AND NOT EXISTS (
262 SELECT 1
263 FROM json_each(${JSON.stringify(requiredScopes ?? [])}) AS required
264 WHERE required.value NOT IN (
265 SELECT value FROM json_each(auth_pairing_links.scopes)
266 )
267 )
268 RETURNING
269 id AS "id",
270 credential AS "credential",
271 method AS "method",
272 scopes AS "scopes",
273 subject AS "subject",
274 label AS "label",
275 proof_key_thumbprint AS "proofKeyThumbprint",
276 created_at AS "createdAt",
277 expires_at AS "expiresAt",
278 consumed_at AS "consumedAt",
279 revoked_at AS "revokedAt",
280 requires_connect_approval AS "requiresConnectApproval",
281 EXISTS (
282 SELECT 1 FROM auth_connect_devices
283 WHERE auth_connect_devices.proof_key_thumbprint = auth_pairing_links.proof_key_thumbprint
284 ) AS "connectApproved",
285 requested_client_json AS "requestedClient"
286 `,
287 });
288
289 const listActivePairingLinkRows = SqlSchema.findAll({
290 Request: ListActiveAuthPairingLinksInput,
291 Result: AuthPairingLinkRawDbRow,
292 execute: ({ now }) =>
293 sql`
294 SELECT
295 id AS "id",
296 credential AS "credential",
297 method AS "method",
298 scopes AS "scopes",
299 subject AS "subject",
300 label AS "label",
301 proof_key_thumbprint AS "proofKeyThumbprint",
302 created_at AS "createdAt",
303 expires_at AS "expiresAt",
304 consumed_at AS "consumedAt",
305 revoked_at AS "revokedAt",
306 requires_connect_approval AS "requiresConnectApproval",
307 EXISTS (
308 SELECT 1 FROM auth_connect_devices
309 WHERE auth_connect_devices.proof_key_thumbprint = auth_pairing_links.proof_key_thumbprint
310 ) AS "connectApproved",
311 requested_client_json AS "requestedClient"
312 FROM auth_pairing_links
313 WHERE revoked_at IS NULL
314 AND consumed_at IS NULL
315 AND expires_at > ${now}
316 ORDER BY created_at DESC, id DESC
317 `,
318 });
319
320 const revokePairingLinkRow = SqlSchema.findAll({
321 Request: RevokeAuthPairingLinkInput,
322 Result: Schema.Struct({ id: Schema.String }),
323 execute: ({ id, revokedAt }) =>
324 sql`
325 UPDATE auth_pairing_links
326 SET revoked_at = ${revokedAt},
327 -- Revoking a waiting approval request is a denial; see findPendingConnect.
328 connect_denied = requires_connect_approval
329 WHERE id = ${id}
330 AND revoked_at IS NULL
331 AND consumed_at IS NULL
332 RETURNING id AS "id"
333 `,
334 });
335
336 const getPairingLinkRowByCredential = SqlSchema.findOneOption({
337 Request: GetAuthPairingLinkByCredentialInput,
338 Result: AuthPairingLinkRawDbRow,
339 execute: ({ credential }) =>
340 sql`
341 SELECT
342 id AS "id",
343 credential AS "credential",
344 method AS "method",
345 scopes AS "scopes",
346 subject AS "subject",
347 label AS "label",
348 proof_key_thumbprint AS "proofKeyThumbprint",
349 created_at AS "createdAt",
350 expires_at AS "expiresAt",
351 consumed_at AS "consumedAt",
352 revoked_at AS "revokedAt",
353 requires_connect_approval AS "requiresConnectApproval",
354 EXISTS (
355 SELECT 1 FROM auth_connect_devices
356 WHERE auth_connect_devices.proof_key_thumbprint = auth_pairing_links.proof_key_thumbprint
357 ) AS "connectApproved",
358 requested_client_json AS "requestedClient"
359 FROM auth_pairing_links
360 WHERE credential = ${credential}
361 `,
362 });
363
364 const selectRow = sql`
365 SELECT
366 id AS "id",
367 credential AS "credential",
368 method AS "method",
369 scopes AS "scopes",
370 subject AS "subject",
371 label AS "label",
372 proof_key_thumbprint AS "proofKeyThumbprint",
373 created_at AS "createdAt",
374 expires_at AS "expiresAt",
375 consumed_at AS "consumedAt",
376 revoked_at AS "revokedAt",
377 requires_connect_approval AS "requiresConnectApproval",
378 EXISTS (
379 SELECT 1 FROM auth_connect_devices
380 WHERE auth_connect_devices.proof_key_thumbprint = auth_pairing_links.proof_key_thumbprint
381 ) AS "connectApproved",
382 requested_client_json AS "requestedClient"
383 FROM auth_pairing_links
384 `;
385
386 const getActivePairingLinkRowById = SqlSchema.findOneOption({
387 Request: GetActiveAuthPairingLinkByIdInput,
388 Result: AuthPairingLinkRawDbRow,
389 execute: ({ id, now }) =>
390 sql`
391 ${selectRow}
392 WHERE id = ${id}
393 AND revoked_at IS NULL
394 AND consumed_at IS NULL
395 AND expires_at > ${now}
396 `,
397 });
398
399 const findPendingConnectPairingLinkRow = SqlSchema.findOneOption({
400 Request: FindPendingConnectPairingLinkInput,
401 Result: AuthPairingLinkRawDbRow,
402 execute: ({ proofKeyThumbprint, now }) =>
403 sql`
404 ${selectRow}
405 WHERE proof_key_thumbprint = ${proofKeyThumbprint}
406 AND requires_connect_approval = 1
407 -- A denied request stays the device's answer until it expires, so retries get a
408 -- dead credential back instead of prompting the approver again.
409 AND (revoked_at IS NULL OR connect_denied = 1)
410 AND consumed_at IS NULL
411 AND expires_at > ${now}
412 ORDER BY created_at DESC, id DESC
413 LIMIT 1
414 `,
415 });
416
417 const recordConnectRequestClientRow = SqlSchema.findAll({
418 Request: RecordConnectRequestClientInput,
419 Result: Schema.Struct({ id: Schema.String }),
420 execute: ({ id, client }) =>
421 sql`
422 UPDATE auth_pairing_links
423 SET requested_client_json = ${client}
424 WHERE id = ${id}
425 AND requires_connect_approval = 1
426 AND requested_client_json IS NULL
427 RETURNING id AS "id"
428 `,
429 });
430
431 const decodeOptionalRow =
432 (operation: string) => (rowOption: Option.Option<typeof AuthPairingLinkRawDbRow.Type>) =>
433 Option.match(rowOption, {
434 onNone: () => Effect.succeedNone,
435 onSome: (row) =>
436 decodeAuthPairingLinkDbRow(row).pipe(
437 Effect.mapError((cause) =>
438 PersistenceDecodeError.fromSchemaError(operation, cause, { pairingLinkId: row.id }),
439 ),
440 Effect.asSome,
441 ),
442 });
443
444 const getActiveById: AuthPairingLinkRepository["Service"]["getActiveById"] = (input) =>
445 getActivePairingLinkRowById(input).pipe(
446 NodeSqliteClient.readOnly,
447 Effect.mapError(
448 toPersistenceSqlOrDecodeError(
449 "AuthPairingLinkRepository.getActiveById:query",
450 "AuthPairingLinkRepository.getActiveById:decodeRow",
451 { pairingLinkId: input.id },
452 ),
453 ),
454 Effect.flatMap(decodeOptionalRow("AuthPairingLinkRepository.getActiveById:decodeRow")),
455 );
456
457 const findPendingConnect: AuthPairingLinkRepository["Service"]["findPendingConnect"] = (input) =>
458 findPendingConnectPairingLinkRow(input).pipe(
459 NodeSqliteClient.readOnly,
460 Effect.mapError(
461 toPersistenceSqlOrDecodeError(
462 "AuthPairingLinkRepository.findPendingConnect:query",
463 "AuthPairingLinkRepository.findPendingConnect:decodeRow",
464 ),
465 ),
466 Effect.flatMap(decodeOptionalRow("AuthPairingLinkRepository.findPendingConnect:decodeRow")),
467 );
468
469 const recordConnectRequestClient: AuthPairingLinkRepository["Service"]["recordConnectRequestClient"] =
470 (input) =>
471 recordConnectRequestClientRow(input).pipe(
472 Effect.mapError(
473 toPersistenceSqlOrDecodeError(
474 "AuthPairingLinkRepository.recordConnectRequestClient:query",
475 "AuthPairingLinkRepository.recordConnectRequestClient:encodeRequest",
476 { pairingLinkId: input.id },
477 ),
478 ),
479 Effect.map((rows) => rows.length > 0),
480 );
481
482 const revokeUnspentByProofKeyRows = SqlSchema.findAll({
483 Request: RevokeUnspentByProofKeyInput,
484 Result: Schema.Struct({ id: Schema.String }),
485 execute: ({ proofKeyThumbprint, keepProofKeyThumbprint, revokedAt }) =>
486 sql`
487 -- Only T3 Connect mints bind a proof key, so these are that key's connect credentials.
488 UPDATE auth_pairing_links
489 SET revoked_at = ${revokedAt}
490 WHERE proof_key_thumbprint IS NOT NULL
491 AND revoked_at IS NULL
492 AND consumed_at IS NULL
493 AND (${proofKeyThumbprint} IS NULL OR proof_key_thumbprint = ${proofKeyThumbprint})
494 AND proof_key_thumbprint IS NOT ${keepProofKeyThumbprint}
495 RETURNING id AS "id"
496 `,
497 });
498
499 const revokeUnspentByProofKey: AuthPairingLinkRepository["Service"]["revokeUnspentByProofKey"] = (
500 input,
501 ) =>
502 revokeUnspentByProofKeyRows(input).pipe(
503 Effect.mapError(
504 toPersistenceSqlOrDecodeError(
505 "AuthPairingLinkRepository.revokeUnspentByProofKey:query",
506 "AuthPairingLinkRepository.revokeUnspentByProofKey:decodeRows",
507 ),
508 ),
509 Effect.map((rows) => rows.map((row) => row.id)),
510 );
511
512 const create: AuthPairingLinkRepository["Service"]["create"] = (input) =>
513 createPairingLinkRow(input).pipe(
514 Effect.mapError(
515 toPersistenceSqlOrDecodeError(
516 "AuthPairingLinkRepository.create:query",
517 "AuthPairingLinkRepository.create:encodeRequest",
518 { pairingLinkId: input.id },
519 ),
520 ),
521 );
522
523 const consumeAvailable: AuthPairingLinkRepository["Service"]["consumeAvailable"] = (input) =>
524 consumeAvailablePairingLinkRow(input).pipe(
525 Effect.mapError(
526 toPersistenceSqlOrDecodeError(
527 "AuthPairingLinkRepository.consumeAvailable:query",
528 "AuthPairingLinkRepository.consumeAvailable:decodeRow",
529 ),
530 ),
531 Effect.flatMap((rowOption) =>
532 Option.match(rowOption, {
533 onNone: () => Effect.succeedNone,
534 onSome: (row) =>
535 decodeAuthPairingLinkDbRow(row).pipe(
536 Effect.mapError((cause) =>
537 PersistenceDecodeError.fromSchemaError(
538 "AuthPairingLinkRepository.consumeAvailable:decodeRow",
539 cause,
540 { pairingLinkId: row.id },
541 ),
542 ),
543 Effect.asSome,
544 ),
545 }),
546 ),
547 );
548
549 const listActive: AuthPairingLinkRepository["Service"]["listActive"] = (input) =>
550 listActivePairingLinkRows(input).pipe(
551 NodeSqliteClient.readOnly,
552 Effect.mapError(
553 toPersistenceSqlOrDecodeError(
554 "AuthPairingLinkRepository.listActive:query",
555 "AuthPairingLinkRepository.listActive:decodeRows",
556 ),
557 ),
558 Effect.flatMap((rows) =>
559 Effect.forEach(rows, (row) =>
560 decodeAuthPairingLinkDbRow(row).pipe(
561 Effect.mapError((cause) =>
562 PersistenceDecodeError.fromSchemaError(
563 "AuthPairingLinkRepository.listActive:decodeRows",
564 cause,
565 { pairingLinkId: row.id },
566 ),
567 ),
568 ),
569 ),
570 ),
571 );
572
573 const revoke: AuthPairingLinkRepository["Service"]["revoke"] = (input) =>
574 revokePairingLinkRow(input).pipe(
575 Effect.mapError(
576 toPersistenceSqlOrDecodeError(
577 "AuthPairingLinkRepository.revoke:query",
578 "AuthPairingLinkRepository.revoke:decodeRows",
579 { pairingLinkId: input.id },
580 ),
581 ),
582 Effect.map((rows) => rows.length > 0),
583 );
584
585 const getByCredential: AuthPairingLinkRepository["Service"]["getByCredential"] = (input) =>
586 getPairingLinkRowByCredential(input).pipe(
587 NodeSqliteClient.readOnly,
588 Effect.mapError(
589 toPersistenceSqlOrDecodeError(
590 "AuthPairingLinkRepository.getByCredential:query",
591 "AuthPairingLinkRepository.getByCredential:decodeRow",
592 ),
593 ),
594 Effect.flatMap((rowOption) =>
595 Option.match(rowOption, {
596 onNone: () => Effect.succeedNone,
597 onSome: (row) =>
598 decodeAuthPairingLinkDbRow(row).pipe(
599 Effect.mapError((cause) =>
600 PersistenceDecodeError.fromSchemaError(
601 "AuthPairingLinkRepository.getByCredential:decodeRow",
602 cause,
603 { pairingLinkId: row.id },
604 ),
605 ),
606 Effect.asSome,
607 ),
608 }),
609 ),
610 );
611
612 return {
613 create,
614 consumeAvailable,
615 listActive,
616 revoke,
617 getByCredential,
618 getActiveById,
619 findPendingConnect,
620 recordConnectRequestClient,
621 revokeUnspentByProofKey,
622 } satisfies AuthPairingLinkRepository["Service"];
623});
624
625export const layer = Layer.effect(AuthPairingLinkRepository, make);