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