Open History to see the last commit
1import { AuthSessionId } from "@t3tools/contracts";
2import { Console, Effect, Layer, Option, References } from "effect";
3import { Argument, Command, Flag, GlobalFlag } from "effect/unstable/cli";
4
5import { AuthControlPlaneRuntimeLive } from "../auth/Layers/AuthControlPlane.ts";
6import { AuthControlPlane } from "../auth/Services/AuthControlPlane.ts";
7import type { AuthControlPlaneShape } from "../auth/Services/AuthControlPlane.ts";
8import {
9 formatIssuedPairingCredential,
10 formatIssuedSession,
11 formatPairingCredentialList,
12 formatSessionList,
13} from "../cliAuthFormat.ts";
14import { ServerConfig } from "../config.ts";
15import {
16 authLocationFlags,
17 type CliAuthLocationFlags,
18 DurationFromString,
19 resolveCliAuthConfig,
20} from "./config.ts";
21
22const runWithAuthControlPlane = <A, E>(
23 flags: CliAuthLocationFlags,
24 run: (authControlPlane: AuthControlPlaneShape) => Effect.Effect<A, E>,
25 options?: {
26 readonly quietLogs?: boolean;
27 },
28) =>
29 Effect.gen(function* () {
30 const logLevel = yield* GlobalFlag.LogLevel;
31 const config = yield* resolveCliAuthConfig(flags, logLevel);
32 const minimumLogLevel = options?.quietLogs ? "Error" : config.logLevel;
33 return yield* Effect.gen(function* () {
34 const authControlPlane = yield* AuthControlPlane;
35 return yield* run(authControlPlane);
36 }).pipe(
37 Effect.provide(
38 Layer.mergeAll(AuthControlPlaneRuntimeLive).pipe(
39 Layer.provide(Layer.succeed(ServerConfig, config)),
40 Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)),
41 ),
42 ),
43 );
44 });
45
46const ttlFlag = Flag.string("ttl").pipe(
47 Flag.withSchema(DurationFromString),
48 Flag.withDescription("TTL, for example `5m`, `1h`, `30d`, or `15 minutes`."),
49 Flag.optional,
50);
51
52const jsonFlag = Flag.boolean("json").pipe(
53 Flag.withDescription("Emit JSON instead of human-readable output."),
54 Flag.withDefault(false),
55);
56
57const sessionRoleFlag = Flag.choice("role", ["owner", "client"]).pipe(
58 Flag.withDescription("Role for the issued bearer session."),
59 Flag.withDefault("owner"),
60);
61
62const labelFlag = Flag.string("label").pipe(
63 Flag.withDescription("Optional human-readable label."),
64 Flag.optional,
65);
66
67const subjectFlag = Flag.string("subject").pipe(
68 Flag.withDescription("Optional session subject."),
69 Flag.optional,
70);
71
72const baseUrlFlag = Flag.string("base-url").pipe(
73 Flag.withDescription("Optional public base URL used to print a ready `/pair#token=...` link."),
74 Flag.optional,
75);
76
77const tokenOnlyFlag = Flag.boolean("token-only").pipe(
78 Flag.withDescription("Print only the issued bearer token."),
79 Flag.withDefault(false),
80);
81
82const pairingCreateCommand = Command.make("create", {
83 ...authLocationFlags,
84 ttl: ttlFlag,
85 label: labelFlag,
86 baseUrl: baseUrlFlag,
87 json: jsonFlag,
88}).pipe(
89 Command.withDescription("Issue a new client pairing token."),
90 Command.withHandler((flags) =>
91 runWithAuthControlPlane(
92 flags,
93 (authControlPlane) =>
94 Effect.gen(function* () {
95 const issued = yield* authControlPlane.createPairingLink({
96 role: "client",
97 subject: "one-time-token",
98 ...(Option.isSome(flags.ttl) ? { ttl: flags.ttl.value } : {}),
99 ...(Option.isSome(flags.label) ? { label: flags.label.value } : {}),
100 });
101 const output = formatIssuedPairingCredential(issued, {
102 json: flags.json,
103 ...(Option.isSome(flags.baseUrl) ? { baseUrl: flags.baseUrl.value } : {}),
104 });
105 yield* Console.log(output);
106 }),
107 {
108 quietLogs: flags.json,
109 },
110 ),
111 ),
112);
113
114const pairingListCommand = Command.make("list", {
115 ...authLocationFlags,
116 json: jsonFlag,
117}).pipe(
118 Command.withDescription("List active client pairing tokens without revealing their secrets."),
119 Command.withHandler((flags) =>
120 runWithAuthControlPlane(
121 flags,
122 (authControlPlane) =>
123 Effect.gen(function* () {
124 const pairingLinks = yield* authControlPlane.listPairingLinks({ role: "client" });
125 yield* Console.log(formatPairingCredentialList(pairingLinks, { json: flags.json }));
126 }),
127 {
128 quietLogs: flags.json,
129 },
130 ),
131 ),
132);
133
134const pairingRevokeCommand = Command.make("revoke", {
135 ...authLocationFlags,
136 id: Argument.string("id").pipe(Argument.withDescription("Pairing credential id to revoke.")),
137}).pipe(
138 Command.withDescription("Revoke an active client pairing token."),
139 Command.withHandler((flags) =>
140 runWithAuthControlPlane(flags, (authControlPlane) =>
141 Effect.gen(function* () {
142 const revoked = yield* authControlPlane.revokePairingLink(flags.id);
143 yield* Console.log(
144 revoked
145 ? `Revoked pairing credential ${flags.id}.\n`
146 : `No active pairing credential found for ${flags.id}.\n`,
147 );
148 }),
149 ),
150 ),
151);
152
153const pairingCommand = Command.make("pairing").pipe(
154 Command.withDescription("Manage one-time client pairing tokens."),
155 Command.withSubcommands([pairingCreateCommand, pairingListCommand, pairingRevokeCommand]),
156);
157
158const sessionIssueCommand = Command.make("issue", {
159 ...authLocationFlags,
160 ttl: ttlFlag,
161 role: sessionRoleFlag,
162 label: labelFlag,
163 subject: subjectFlag,
164 tokenOnly: tokenOnlyFlag,
165 json: jsonFlag,
166}).pipe(
167 Command.withDescription("Issue a bearer session token for headless or remote clients."),
168 Command.withHandler((flags) =>
169 runWithAuthControlPlane(
170 flags,
171 (authControlPlane) =>
172 Effect.gen(function* () {
173 const issued = yield* authControlPlane.issueSession({
174 role: flags.role,
175 ...(Option.isSome(flags.ttl) ? { ttl: flags.ttl.value } : {}),
176 ...(Option.isSome(flags.label) ? { label: flags.label.value } : {}),
177 ...(Option.isSome(flags.subject) ? { subject: flags.subject.value } : {}),
178 });
179 yield* Console.log(
180 formatIssuedSession(issued, {
181 json: flags.json,
182 tokenOnly: flags.tokenOnly,
183 }),
184 );
185 }),
186 {
187 quietLogs: flags.json || flags.tokenOnly,
188 },
189 ),
190 ),
191);
192
193const sessionListCommand = Command.make("list", {
194 ...authLocationFlags,
195 json: jsonFlag,
196}).pipe(
197 Command.withDescription("List active sessions without revealing bearer tokens."),
198 Command.withHandler((flags) =>
199 runWithAuthControlPlane(
200 flags,
201 (authControlPlane) =>
202 Effect.gen(function* () {
203 const sessions = yield* authControlPlane.listSessions();
204 yield* Console.log(formatSessionList(sessions, { json: flags.json }));
205 }),
206 {
207 quietLogs: flags.json,
208 },
209 ),
210 ),
211);
212
213const sessionRevokeCommand = Command.make("revoke", {
214 ...authLocationFlags,
215 sessionId: Argument.string("session-id").pipe(
216 Argument.withDescription("Session id to revoke."),
217 Argument.withSchema(AuthSessionId),
218 ),
219}).pipe(
220 Command.withDescription("Revoke an active session."),
221 Command.withHandler((flags) =>
222 runWithAuthControlPlane(flags, (authControlPlane) =>
223 Effect.gen(function* () {
224 const revoked = yield* authControlPlane.revokeSession(flags.sessionId);
225 yield* Console.log(
226 revoked
227 ? `Revoked session ${flags.sessionId}.\n`
228 : `No active session found for ${flags.sessionId}.\n`,
229 );
230 }),
231 ),
232 ),
233);
234
235const sessionCommand = Command.make("session").pipe(
236 Command.withDescription("Manage bearer sessions."),
237 Command.withSubcommands([sessionIssueCommand, sessionListCommand, sessionRevokeCommand]),
238);
239
240export const authCommand = Command.make("auth").pipe(
241 Command.withDescription("Manage the local auth control plane for headless deployments."),
242 Command.withSubcommands([pairingCommand, sessionCommand]),
243);