Open History to see the last commit
1import { AuthSessionId, AuthStandardClientScopes } 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/cli";
8
9import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
10
11import {
12 formatIssuedPairingCredential,
13 formatIssuedSession,
14 formatPairingCredentialList,
15 formatSessionList,
16} from "../cliAuthFormat.ts";
17import * as ServerConfig from "../config.ts";
18import { authScopesFlag } from "./authScopes.ts";
19import {
20 authLocationFlags,
21 type CliAuthLocationFlags,
22 DurationFromString,
23 resolveCliAuthConfig,
24} from "./config.ts";
25
26const runWithEnvironmentAuth = <A, E>(
27 flags: CliAuthLocationFlags,
28 run: (environmentAuth: EnvironmentAuth.EnvironmentAuth["Service"]) => 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 environmentAuth = yield* EnvironmentAuth.EnvironmentAuth;
39 return yield* run(environmentAuth);
40 }).pipe(
41 Effect.provide(
42 Layer.mergeAll(EnvironmentAuth.layerRuntime).pipe(
43 Layer.provide(ServerConfig.layer(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 labelFlag = Flag.String("label").pipe(
62 Flag.withDescription("Optional human-readable label."),
63 Flag.optional,
64);
65
66const subjectFlag = Flag.String("subject").pipe(
67 Flag.withDescription("Optional session subject."),
68 Flag.optional,
69);
70
71const baseUrlFlag = Flag.String("base-url").pipe(
72 Flag.withDescription("Optional public base URL used to print a ready `/pair#token=...` link."),
73 Flag.optional,
74);
75
76const tokenOnlyFlag = Flag.Boolean("token-only").pipe(
77 Flag.withDescription("Print only the issued bearer token."),
78 Flag.withDefault(false),
79);
80
81const pairingCreateCommand = Command.make("create", {
82 ...authLocationFlags,
83 scopes: authScopesFlag(AuthStandardClientScopes),
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 runWithEnvironmentAuth(
92 flags,
93 (environmentAuth) =>
94 Effect.gen(function* () {
95 const issued = yield* environmentAuth.createPairingLink({
96 scopes: flags.scopes,
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 runWithEnvironmentAuth(
121 flags,
122 (environmentAuth) =>
123 Effect.gen(function* () {
124 const pairingLinks = yield* environmentAuth.listPairingLinks({
125 excludeSubjects: [EnvironmentAuth.INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT],
126 });
127 yield* Console.log(formatPairingCredentialList(pairingLinks, { json: flags.json }));
128 }),
129 {
130 quietLogs: flags.json,
131 },
132 ),
133 ),
134);
135
136const pairingRevokeCommand = Command.make("revoke", {
137 ...authLocationFlags,
138 id: Argument.String("id").pipe(Argument.withDescription("Pairing credential id to revoke.")),
139}).pipe(
140 Command.withDescription("Revoke an active client pairing token."),
141 Command.withHandler((flags) =>
142 runWithEnvironmentAuth(flags, (environmentAuth) =>
143 Effect.gen(function* () {
144 const revoked = yield* environmentAuth.revokePairingLink(flags.id);
145 yield* Console.log(
146 revoked
147 ? `Revoked pairing credential ${flags.id}.\n`
148 : `No active pairing credential found for ${flags.id}.\n`,
149 );
150 }),
151 ),
152 ),
153);
154
155const pairingCommand = Command.make("pairing").pipe(
156 Command.withDescription("Manage one-time client pairing tokens."),
157 Command.withSubcommands([pairingCreateCommand, pairingListCommand, pairingRevokeCommand]),
158);
159
160const sessionIssueCommand = Command.make("issue", {
161 ...authLocationFlags,
162 scopes: authScopesFlag(AuthStandardClientScopes),
163 ttl: ttlFlag,
164 label: labelFlag,
165 subject: subjectFlag,
166 tokenOnly: tokenOnlyFlag,
167 json: jsonFlag,
168}).pipe(
169 Command.withDescription("Issue a scoped bearer access token for headless or remote clients."),
170 Command.withHandler((flags) =>
171 runWithEnvironmentAuth(
172 flags,
173 (environmentAuth) =>
174 Effect.gen(function* () {
175 const issued = yield* environmentAuth.issueSession({
176 scopes: flags.scopes,
177 ...(Option.isSome(flags.ttl) ? { ttl: flags.ttl.value } : {}),
178 ...(Option.isSome(flags.label) ? { label: flags.label.value } : {}),
179 ...(Option.isSome(flags.subject) ? { subject: flags.subject.value } : {}),
180 });
181 yield* Console.log(
182 formatIssuedSession(issued, {
183 json: flags.json,
184 tokenOnly: flags.tokenOnly,
185 }),
186 );
187 }),
188 {
189 quietLogs: flags.json || flags.tokenOnly,
190 },
191 ),
192 ),
193);
194
195const sessionListCommand = Command.make("list", {
196 ...authLocationFlags,
197 json: jsonFlag,
198}).pipe(
199 Command.withDescription("List active sessions without revealing bearer tokens."),
200 Command.withHandler((flags) =>
201 runWithEnvironmentAuth(
202 flags,
203 (environmentAuth) =>
204 Effect.gen(function* () {
205 const sessions = yield* environmentAuth.listSessions();
206 yield* Console.log(formatSessionList(sessions, { json: flags.json }));
207 }),
208 {
209 quietLogs: flags.json,
210 },
211 ),
212 ),
213);
214
215const sessionRevokeCommand = Command.make("revoke", {
216 ...authLocationFlags,
217 sessionId: Argument.String("session-id").pipe(
218 Argument.withDescription("Session id to revoke."),
219 Argument.withSchema(AuthSessionId),
220 ),
221}).pipe(
222 Command.withDescription("Revoke an active session."),
223 Command.withHandler((flags) =>
224 runWithEnvironmentAuth(flags, (environmentAuth) =>
225 Effect.gen(function* () {
226 const revoked = yield* environmentAuth.revokeSession(flags.sessionId);
227 yield* Console.log(
228 revoked
229 ? `Revoked session ${flags.sessionId}.\n`
230 : `No active session found for ${flags.sessionId}.\n`,
231 );
232 }),
233 ),
234 ),
235);
236
237const sessionCommand = Command.make("session").pipe(
238 Command.withDescription("Manage bearer sessions."),
239 Command.withSubcommands([sessionIssueCommand, sessionListCommand, sessionRevokeCommand]),
240);
241
242export const authCommand = Command.make("auth").pipe(
243 Command.withDescription("Manage the local auth control plane for headless deployments."),
244 Command.withSubcommands([pairingCommand, sessionCommand]),
245);