The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | AuthAccessWriteScope, |
| 3 | AuthRevokePairingLinkInput, |
| 4 | AuthSessionId, |
| 5 | AuthStandardClientScopes, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import * as Console from "effect/Console"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Option from "effect/Option"; |
| 11 | import * as References from "effect/References"; |
| 12 | import * as Schema from "effect/Schema"; |
| 13 | import { Argument, Command, Flag, GlobalFlag } from "effect/cli"; |
| 14 | |
| 15 | import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; |
| 16 | |
| 17 | import { |
| 18 | formatConnectDeviceList, |
| 19 | formatIssuedPairingCredential, |
| 20 | formatIssuedSession, |
| 21 | formatPairingCredentialList, |
| 22 | formatSessionList, |
| 23 | } from "../cliAuthFormat.ts"; |
| 24 | import * as ServerConfig from "../config.ts"; |
| 25 | import { authScopesFlag } from "./authScopes.ts"; |
| 26 | import { |
| 27 | authLocationFlags, |
| 28 | type CliAuthLocationFlags, |
| 29 | DurationFromString, |
| 30 | resolveCliAuthConfig, |
| 31 | } from "./config.ts"; |
| 32 | import * as RunningServer from "./runningServer.ts"; |
| 33 | |
| 34 | const runWithEnvironmentAuth = Effect.fn("auth.runWithEnvironmentAuth")(function* <A, E, R>( |
| 35 | flags: CliAuthLocationFlags, |
| 36 | run: (environmentAuth: EnvironmentAuth.EnvironmentAuth["Service"]) => Effect.Effect<A, E, R>, |
| 37 | options?: { |
| 38 | readonly quietLogs?: boolean; |
| 39 | }, |
| 40 | ) { |
| 41 | const logLevel = yield* GlobalFlag.LogLevel; |
| 42 | const config = yield* resolveCliAuthConfig(flags, logLevel, options); |
| 43 | const minimumLogLevel = options?.quietLogs ? "Error" : config.logLevel; |
| 44 | return yield* Effect.gen(function* () { |
| 45 | const environmentAuth = yield* EnvironmentAuth.EnvironmentAuth; |
| 46 | return yield* run(environmentAuth); |
| 47 | }).pipe( |
| 48 | Effect.provide( |
| 49 | EnvironmentAuth.layerRuntime.pipe( |
| 50 | Layer.provideMerge(ServerConfig.layer(config)), |
| 51 | Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)), |
| 52 | ), |
| 53 | ), |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | /** Access changes go through a running server, which applies them to its open connections. */ |
| 58 | const changeAccess = <A, E, R, E2, R2>(change: { |
| 59 | readonly viaServer: (client: RunningServer.ServerClient) => Effect.Effect<A, E, R>; |
| 60 | readonly locally: Effect.Effect<A, E2, R2>; |
| 61 | }) => RunningServer.changeState({ scopes: [AuthAccessWriteScope], ...change }); |
| 62 | |
| 63 | const ttlFlag = Flag.String("ttl").pipe( |
| 64 | Flag.withSchema(DurationFromString), |
| 65 | Flag.withDescription("TTL, for example `5m`, `1h`, `30d`, or `15 minutes`."), |
| 66 | Flag.optional, |
| 67 | ); |
| 68 | |
| 69 | const jsonFlag = Flag.Boolean("json").pipe( |
| 70 | Flag.withDescription("Emit JSON instead of human-readable output."), |
| 71 | Flag.withDefault(false), |
| 72 | ); |
| 73 | |
| 74 | const labelFlag = Flag.String("label").pipe( |
| 75 | Flag.withDescription("Optional human-readable label."), |
| 76 | Flag.optional, |
| 77 | ); |
| 78 | |
| 79 | const subjectFlag = Flag.String("subject").pipe( |
| 80 | Flag.withDescription("Optional session subject."), |
| 81 | Flag.optional, |
| 82 | ); |
| 83 | |
| 84 | const baseUrlFlag = Flag.String("base-url").pipe( |
| 85 | Flag.withSchema( |
| 86 | Schema.URLFromString.pipe( |
| 87 | Schema.check( |
| 88 | Schema.makeFilter((url) => url.protocol === "http:" || url.protocol === "https:", { |
| 89 | expected: "an http:// or https:// URL", |
| 90 | }), |
| 91 | ), |
| 92 | ), |
| 93 | ), |
| 94 | Flag.withDescription("Optional public base URL used to print a ready `/pair#token=...` link."), |
| 95 | Flag.optional, |
| 96 | ); |
| 97 | |
| 98 | const tokenOnlyFlag = Flag.Boolean("token-only").pipe( |
| 99 | Flag.withDescription("Print only the issued bearer token."), |
| 100 | Flag.withDefault(false), |
| 101 | ); |
| 102 | |
| 103 | const pairingCreateCommand = Command.make("create", { |
| 104 | ...authLocationFlags, |
| 105 | scopes: authScopesFlag(AuthStandardClientScopes), |
| 106 | ttl: ttlFlag, |
| 107 | label: labelFlag, |
| 108 | baseUrl: baseUrlFlag, |
| 109 | json: jsonFlag, |
| 110 | }).pipe( |
| 111 | Command.withDescription("Issue a new client pairing token."), |
| 112 | Command.withHandler((flags) => |
| 113 | runWithEnvironmentAuth( |
| 114 | flags, |
| 115 | (environmentAuth) => |
| 116 | Effect.gen(function* () { |
| 117 | const issued = yield* environmentAuth.createPairingLink({ |
| 118 | scopes: flags.scopes, |
| 119 | subject: "one-time-token", |
| 120 | ...(Option.isSome(flags.ttl) ? { ttl: flags.ttl.value } : {}), |
| 121 | ...(Option.isSome(flags.label) ? { label: flags.label.value } : {}), |
| 122 | }); |
| 123 | const output = formatIssuedPairingCredential(issued, { |
| 124 | json: flags.json, |
| 125 | ...(Option.isSome(flags.baseUrl) ? { baseUrl: flags.baseUrl.value.href } : {}), |
| 126 | }); |
| 127 | yield* Console.log(output); |
| 128 | }), |
| 129 | { |
| 130 | quietLogs: flags.json, |
| 131 | }, |
| 132 | ), |
| 133 | ), |
| 134 | ); |
| 135 | |
| 136 | const pairingListCommand = Command.make("list", { |
| 137 | ...authLocationFlags, |
| 138 | json: jsonFlag, |
| 139 | }).pipe( |
| 140 | Command.withDescription("List active client pairing tokens without revealing their secrets."), |
| 141 | Command.withHandler((flags) => |
| 142 | runWithEnvironmentAuth( |
| 143 | flags, |
| 144 | (environmentAuth) => |
| 145 | Effect.gen(function* () { |
| 146 | const pairingLinks = yield* environmentAuth.listPairingLinks({ |
| 147 | excludeSubjects: [EnvironmentAuth.INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT], |
| 148 | }); |
| 149 | yield* Console.log(formatPairingCredentialList(pairingLinks, { json: flags.json })); |
| 150 | }), |
| 151 | { |
| 152 | quietLogs: flags.json, |
| 153 | }, |
| 154 | ), |
| 155 | ), |
| 156 | ); |
| 157 | |
| 158 | const pairingRevokeCommand = Command.make("revoke", { |
| 159 | ...authLocationFlags, |
| 160 | id: Argument.String("id").pipe( |
| 161 | Argument.withDescription("Pairing credential id to revoke."), |
| 162 | // Same schema the server applies, so both paths revoke the same id. |
| 163 | Argument.withSchema(AuthRevokePairingLinkInput.fields.id), |
| 164 | ), |
| 165 | }).pipe( |
| 166 | Command.withDescription("Revoke an active client pairing token."), |
| 167 | Command.withHandler((flags) => |
| 168 | runWithEnvironmentAuth(flags, (environmentAuth) => |
| 169 | Effect.gen(function* () { |
| 170 | const revoked = yield* changeAccess({ |
| 171 | viaServer: (client) => |
| 172 | client.auth |
| 173 | .revokePairingLink({ headers: {}, payload: { id: flags.id } }) |
| 174 | .pipe(Effect.map((result) => result.revoked)), |
| 175 | locally: environmentAuth.revokePairingLink(flags.id), |
| 176 | }); |
| 177 | yield* Console.log( |
| 178 | revoked |
| 179 | ? `Revoked pairing credential ${flags.id}.\n` |
| 180 | : `No active pairing credential found for ${flags.id}.\n`, |
| 181 | ); |
| 182 | }), |
| 183 | ), |
| 184 | ), |
| 185 | ); |
| 186 | |
| 187 | const pairingCommand = Command.make("pairing").pipe( |
| 188 | Command.withDescription("Manage one-time client pairing tokens."), |
| 189 | Command.withSubcommands([pairingCreateCommand, pairingListCommand, pairingRevokeCommand]), |
| 190 | ); |
| 191 | |
| 192 | const connectDeviceListCommand = Command.make("list", { |
| 193 | ...authLocationFlags, |
| 194 | json: jsonFlag, |
| 195 | }).pipe( |
| 196 | Command.withDescription( |
| 197 | "List T3 Connect devices waiting for approval and those already trusted.", |
| 198 | ), |
| 199 | Command.withHandler((flags) => |
| 200 | runWithEnvironmentAuth( |
| 201 | flags, |
| 202 | (environmentAuth) => |
| 203 | Effect.gen(function* () { |
| 204 | const waiting = (yield* environmentAuth.listPairingLinks()).flatMap((link) => |
| 205 | link.connectDeviceRequest ? [link.connectDeviceRequest] : [], |
| 206 | ); |
| 207 | const trusted = yield* environmentAuth.listConnectDevices(); |
| 208 | yield* Console.log(formatConnectDeviceList({ waiting, trusted }, { json: flags.json })); |
| 209 | }), |
| 210 | { quietLogs: flags.json }, |
| 211 | ), |
| 212 | ), |
| 213 | ); |
| 214 | |
| 215 | const verificationCodeArgument = Argument.String("code").pipe( |
| 216 | Argument.withDescription("The code the device shows, such as `K7QM-2XFD`."), |
| 217 | ); |
| 218 | |
| 219 | const connectDeviceApproveCommand = Command.make("approve", { |
| 220 | ...authLocationFlags, |
| 221 | code: verificationCodeArgument, |
| 222 | }).pipe( |
| 223 | Command.withDescription("Approve the waiting T3 Connect device that shows this code."), |
| 224 | Command.withHandler((flags) => |
| 225 | runWithEnvironmentAuth(flags, (environmentAuth) => |
| 226 | Effect.gen(function* () { |
| 227 | const [pairingLinkId, ...others] = yield* environmentAuth.connectRequestsShowingCode( |
| 228 | flags.code, |
| 229 | ); |
| 230 | // Two waiting devices that share a code are ambiguous; approve neither. |
| 231 | const approved = |
| 232 | pairingLinkId !== undefined && |
| 233 | others.length === 0 && |
| 234 | (yield* changeAccess({ |
| 235 | viaServer: (client) => |
| 236 | client.auth |
| 237 | .approveConnectDevice({ headers: {}, payload: { pairingLinkId } }) |
| 238 | .pipe(Effect.map((result) => result.approved)), |
| 239 | locally: environmentAuth.approveConnectDevice(pairingLinkId), |
| 240 | })); |
| 241 | yield* Console.log( |
| 242 | approved |
| 243 | ? `Approved ${flags.code}. The device connects on its next try.\n` |
| 244 | : `No device waiting for approval shows ${flags.code}.\n`, |
| 245 | ); |
| 246 | }), |
| 247 | ), |
| 248 | ), |
| 249 | ); |
| 250 | |
| 251 | const connectDeviceRevokeCommand = Command.make("revoke", { |
| 252 | ...authLocationFlags, |
| 253 | code: verificationCodeArgument, |
| 254 | }).pipe( |
| 255 | Command.withDescription("Stop trusting a T3 Connect device and end its sessions."), |
| 256 | Command.withHandler((flags) => |
| 257 | runWithEnvironmentAuth(flags, (environmentAuth) => |
| 258 | Effect.gen(function* () { |
| 259 | const [device, ...others] = yield* environmentAuth.connectDevicesShowingCode(flags.code); |
| 260 | if (device === undefined) { |
| 261 | return yield* Console.log(`No trusted device shows ${flags.code}.\n`); |
| 262 | } |
| 263 | if (others.length > 0) { |
| 264 | return yield* Console.log( |
| 265 | `More than one trusted device shows ${flags.code}; revoke it from a client.\n`, |
| 266 | ); |
| 267 | } |
| 268 | const { proofKeyThumbprint } = device; |
| 269 | yield* changeAccess({ |
| 270 | viaServer: (client) => |
| 271 | client.auth |
| 272 | .revokeConnectDevice({ headers: {}, payload: { proofKeyThumbprint } }) |
| 273 | .pipe(Effect.map((result) => result.revoked)), |
| 274 | locally: environmentAuth.revokeConnectDevice(proofKeyThumbprint), |
| 275 | }); |
| 276 | yield* Console.log( |
| 277 | `Revoked ${flags.code}. If approval for new devices is off, it can connect again on its own.\n`, |
| 278 | ); |
| 279 | }), |
| 280 | ), |
| 281 | ), |
| 282 | ); |
| 283 | |
| 284 | const connectDeviceCommand = Command.make("connect-device").pipe( |
| 285 | Command.withDescription("Approve and revoke devices that reach this host through T3 Connect."), |
| 286 | Command.withSubcommands([ |
| 287 | connectDeviceListCommand, |
| 288 | connectDeviceApproveCommand, |
| 289 | connectDeviceRevokeCommand, |
| 290 | ]), |
| 291 | ); |
| 292 | |
| 293 | const sessionIssueCommand = Command.make("issue", { |
| 294 | ...authLocationFlags, |
| 295 | scopes: authScopesFlag(AuthStandardClientScopes), |
| 296 | ttl: ttlFlag, |
| 297 | label: labelFlag, |
| 298 | subject: subjectFlag, |
| 299 | tokenOnly: tokenOnlyFlag, |
| 300 | json: jsonFlag, |
| 301 | }).pipe( |
| 302 | Command.withDescription("Issue a scoped bearer access token for headless or remote clients."), |
| 303 | Command.withHandler((flags) => |
| 304 | runWithEnvironmentAuth( |
| 305 | flags, |
| 306 | (environmentAuth) => |
| 307 | Effect.gen(function* () { |
| 308 | const issued = yield* environmentAuth.issueSession({ |
| 309 | scopes: flags.scopes, |
| 310 | ...(Option.isSome(flags.ttl) ? { ttl: flags.ttl.value } : {}), |
| 311 | ...(Option.isSome(flags.label) ? { label: flags.label.value } : {}), |
| 312 | ...(Option.isSome(flags.subject) ? { subject: flags.subject.value } : {}), |
| 313 | }); |
| 314 | yield* Console.log( |
| 315 | formatIssuedSession(issued, { |
| 316 | json: flags.json, |
| 317 | tokenOnly: flags.tokenOnly, |
| 318 | }), |
| 319 | ); |
| 320 | }), |
| 321 | { |
| 322 | quietLogs: flags.json || flags.tokenOnly, |
| 323 | }, |
| 324 | ), |
| 325 | ), |
| 326 | ); |
| 327 | |
| 328 | const sessionListCommand = Command.make("list", { |
| 329 | ...authLocationFlags, |
| 330 | json: jsonFlag, |
| 331 | }).pipe( |
| 332 | Command.withDescription("List active sessions without revealing bearer tokens."), |
| 333 | Command.withHandler((flags) => |
| 334 | runWithEnvironmentAuth( |
| 335 | flags, |
| 336 | (environmentAuth) => |
| 337 | Effect.gen(function* () { |
| 338 | const sessions = yield* environmentAuth.listSessions(); |
| 339 | yield* Console.log(formatSessionList(sessions, { json: flags.json })); |
| 340 | }), |
| 341 | { |
| 342 | quietLogs: flags.json, |
| 343 | }, |
| 344 | ), |
| 345 | ), |
| 346 | ); |
| 347 | |
| 348 | const sessionRevokeCommand = Command.make("revoke", { |
| 349 | ...authLocationFlags, |
| 350 | sessionId: Argument.String("session-id").pipe( |
| 351 | Argument.withDescription("Session id to revoke."), |
| 352 | Argument.withSchema(AuthSessionId), |
| 353 | ), |
| 354 | }).pipe( |
| 355 | Command.withDescription( |
| 356 | "Revoke an active session. A T3 Connect device's session takes the device's trust and other sessions with it; if approval for new devices is off, the device can connect again on its own.", |
| 357 | ), |
| 358 | Command.withHandler((flags) => |
| 359 | runWithEnvironmentAuth(flags, (environmentAuth) => |
| 360 | Effect.gen(function* () { |
| 361 | const revoked = yield* changeAccess({ |
| 362 | viaServer: (client) => |
| 363 | client.auth |
| 364 | .revokeClient({ headers: {}, payload: { sessionId: flags.sessionId } }) |
| 365 | .pipe(Effect.map((result) => result.revoked)), |
| 366 | locally: environmentAuth.revokeClient(flags.sessionId), |
| 367 | }); |
| 368 | yield* Console.log( |
| 369 | revoked |
| 370 | ? `Revoked session ${flags.sessionId}.\n` |
| 371 | : `No active session found for ${flags.sessionId}.\n`, |
| 372 | ); |
| 373 | }), |
| 374 | ), |
| 375 | ), |
| 376 | ); |
| 377 | |
| 378 | const sessionCommand = Command.make("session").pipe( |
| 379 | Command.withDescription("Manage bearer sessions."), |
| 380 | Command.withSubcommands([sessionIssueCommand, sessionListCommand, sessionRevokeCommand]), |
| 381 | ); |
| 382 | |
| 383 | export const authCommand = Command.make("auth").pipe( |
| 384 | Command.withDescription("Manage the local auth control plane for headless deployments."), |
| 385 | Command.withSubcommands([pairingCommand, sessionCommand, connectDeviceCommand]), |
| 386 | ); |