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

AuthControlPlane.ts

73 lines2.4 KB

1import type {
2 AuthClientMetadata,
3 AuthClientSession,
4 AuthPairingLink,
5 AuthSessionId,
6} from "@t3tools/contracts";
7import * as Data from "effect/Data";
8import * as DateTime from "effect/DateTime";
9import * as Duration from "effect/Duration";
10import * as Effect from "effect/Effect";
11import * as Context from "effect/Context";
12import type { SessionRole } from "./SessionCredentialService.ts";
13​
14export const DEFAULT_SESSION_SUBJECT = "cli-issued-session";
15​
16export interface IssuedPairingLink {
17 readonly id: string;
18 readonly credential: string;
19 readonly role: SessionRole;
20 readonly subject: string;
21 readonly label?: string;
22 readonly createdAt: DateTime.Utc;
23 readonly expiresAt: DateTime.Utc;
25​
26export interface IssuedBearerSession {
27 readonly sessionId: AuthSessionId;
28 readonly token: string;
29 readonly method: "bearer-session-token";
30 readonly role: SessionRole;
31 readonly subject: string;
32 readonly client: AuthClientMetadata;
33 readonly expiresAt: DateTime.Utc;
35​
36export class AuthControlPlaneError extends Data.TaggedError("AuthControlPlaneError")<{
37 readonly message: string;
38 readonly cause?: unknown;
39}> {}
40​
41export interface AuthControlPlaneShape {
42 readonly createPairingLink: (input?: {
43 readonly ttl?: Duration.Duration;
44 readonly label?: string;
45 readonly role?: SessionRole;
46 readonly subject?: string;
47 }) => Effect.Effect<IssuedPairingLink, AuthControlPlaneError>;
48 readonly listPairingLinks: (input?: {
49 readonly role?: SessionRole;
50 readonly excludeSubjects?: ReadonlyArray<string>;
51 }) => Effect.Effect<ReadonlyArray<AuthPairingLink>, AuthControlPlaneError>;
52 readonly revokePairingLink: (id: string) => Effect.Effect<boolean, AuthControlPlaneError>;
53 readonly issueSession: (input?: {
54 readonly ttl?: Duration.Duration;
55 readonly subject?: string;
56 readonly role?: SessionRole;
57 readonly label?: string;
58 }) => Effect.Effect<IssuedBearerSession, AuthControlPlaneError>;
59 readonly listSessions: () => Effect.Effect<
60 ReadonlyArray<AuthClientSession>,
61 AuthControlPlaneError
62 >;
63 readonly revokeSession: (
64 sessionId: AuthSessionId,
65 ) => Effect.Effect<boolean, AuthControlPlaneError>;
66 readonly revokeOtherSessionsExcept: (
67 sessionId: AuthSessionId,
68 ) => Effect.Effect<number, AuthControlPlaneError>;
70​
71export class AuthControlPlane extends Context.Service<AuthControlPlane, AuthControlPlaneShape>()(
72 "t3/AuthControlPlane",
73) {}