Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BootstrapCredentialService.ts

61 lines2.0 KB

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