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

AuthControlPlane.ts

69 lines2.2 KB

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