Open History to see the last commit
1import type { AuthPairingLink, ServerAuthBootstrapMethod } from "@t3tools/contracts";
2import { Data, DateTime, Duration, Context } from "effect";
3import type { Effect, Stream } from "effect";
4
5export type BootstrapCredentialRole = "owner" | "client";
6
7export interface BootstrapGrant {
8 readonly method: ServerAuthBootstrapMethod;
9 readonly role: BootstrapCredentialRole;
10 readonly subject: string;
11 readonly label?: string;
12 readonly expiresAt: DateTime.DateTime;
13}
14
15export class BootstrapCredentialError extends Data.TaggedError("BootstrapCredentialError")<{
16 readonly message: string;
17 readonly status?: 401 | 500;
18 readonly cause?: unknown;
19}> {}
20
21export interface IssuedBootstrapCredential {
22 readonly id: string;
23 readonly credential: string;
24 readonly label?: string;
25 readonly expiresAt: DateTime.Utc;
26}
27
28export type BootstrapCredentialChange =
29 | {
30 readonly type: "pairingLinkUpserted";
31 readonly pairingLink: AuthPairingLink;
32 }
33 | {
34 readonly type: "pairingLinkRemoved";
35 readonly id: string;
36 };
37
38export interface BootstrapCredentialServiceShape {
39 readonly issueOneTimeToken: (input?: {
40 readonly ttl?: Duration.Duration;
41 readonly role?: BootstrapCredentialRole;
42 readonly subject?: string;
43 readonly label?: string;
44 }) => Effect.Effect<IssuedBootstrapCredential, BootstrapCredentialError>;
45 readonly listActive: () => Effect.Effect<
46 ReadonlyArray<AuthPairingLink>,
47 BootstrapCredentialError
48 >;
49 readonly streamChanges: Stream.Stream<BootstrapCredentialChange>;
50 readonly revoke: (id: string) => Effect.Effect<boolean, BootstrapCredentialError>;
51 readonly consume: (credential: string) => Effect.Effect<BootstrapGrant, BootstrapCredentialError>;
52}
53
54export class BootstrapCredentialService extends Context.Service<
55 BootstrapCredentialService,
56 BootstrapCredentialServiceShape
57>()("t3/auth/Services/BootstrapCredentialService") {}