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