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