|
| 1 | +import type { Command } from "commander"; |
| 2 | +import { withErrorHandler, createClient, resolveOptions, getFormat, outputResponse } from "../helpers.js"; |
| 3 | +import { loadConfig, saveConfig, validateUrl } from "../config.js"; |
| 4 | +import { parseJsonInput } from "../input.js"; |
| 5 | +import { printSuccess, printError, printInfo, printWarning } from "../output.js"; |
| 6 | +import { clientCredentialsGrant } from "../oauth.js"; |
| 7 | +import { addExamples } from "./help.js"; |
| 8 | + |
| 9 | +export function addMeOAuthClientsSubcommand(me: Command): void { |
| 10 | + const oauthClients = me |
| 11 | + .command("oauth-clients") |
| 12 | + .description("Manage your OAuth clients"); |
| 13 | + |
| 14 | + // oauth-clients list |
| 15 | + const list = oauthClients |
| 16 | + .command("list") |
| 17 | + .description("List your OAuth clients") |
| 18 | + .action( |
| 19 | + withErrorHandler(async (_opts: unknown, cmd: Command) => { |
| 20 | + const client = createClient(cmd); |
| 21 | + const format = getFormat(cmd); |
| 22 | + const response = await client.rawRequest("GET", "/me/oauth-clients"); |
| 23 | + outputResponse(response, format); |
| 24 | + }), |
| 25 | + ); |
| 26 | + |
| 27 | + addExamples(list, [ |
| 28 | + { |
| 29 | + description: "List your OAuth clients", |
| 30 | + command: "geonic me oauth-clients list", |
| 31 | + }, |
| 32 | + ]); |
| 33 | + |
| 34 | + // oauth-clients create |
| 35 | + const create = oauthClients |
| 36 | + .command("create [json]") |
| 37 | + .description("Create a new OAuth client") |
| 38 | + .option("--name <name>", "Client name") |
| 39 | + .option("--scopes <scopes>", "Allowed scopes (comma-separated)") |
| 40 | + .option("--save", "Save credentials to config for automatic re-authentication") |
| 41 | + .action( |
| 42 | + withErrorHandler(async (json: unknown, _opts: unknown, cmd: Command) => { |
| 43 | + const opts = cmd.opts() as { |
| 44 | + name?: string; |
| 45 | + scopes?: string; |
| 46 | + save?: boolean; |
| 47 | + }; |
| 48 | + |
| 49 | + let body: unknown; |
| 50 | + if (json) { |
| 51 | + body = await parseJsonInput(json as string | undefined); |
| 52 | + } else if (opts.name || opts.scopes) { |
| 53 | + // Build body from flags |
| 54 | + const payload: Record<string, unknown> = {}; |
| 55 | + if (opts.name) payload.clientName = opts.name; |
| 56 | + if (opts.scopes) payload.allowedScopes = opts.scopes.split(",").map((s) => s.trim()); |
| 57 | + body = payload; |
| 58 | + } else { |
| 59 | + // Read from stdin (pipe or interactive) |
| 60 | + body = await parseJsonInput(); |
| 61 | + } |
| 62 | + |
| 63 | + const client = createClient(cmd); |
| 64 | + const format = getFormat(cmd); |
| 65 | + const response = await client.rawRequest("POST", "/me/oauth-clients", { body }); |
| 66 | + |
| 67 | + const data = response.data as Record<string, unknown>; |
| 68 | + |
| 69 | + if (opts.save) { |
| 70 | + const globalOpts = resolveOptions(cmd); |
| 71 | + const clientId = data.clientId as string | undefined; |
| 72 | + const clientSecret = data.clientSecret as string | undefined; |
| 73 | + |
| 74 | + if (!clientId || !clientSecret) { |
| 75 | + printError("Response missing clientId or clientSecret. Cannot save credentials."); |
| 76 | + outputResponse(response, format); |
| 77 | + printSuccess("OAuth client created."); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Perform client_credentials grant to get a fresh token |
| 82 | + const baseUrl = validateUrl(globalOpts.url!); |
| 83 | + const tokenResult = await clientCredentialsGrant({ |
| 84 | + baseUrl, |
| 85 | + clientId, |
| 86 | + clientSecret, |
| 87 | + scope: (data.allowedScopes as string[] | undefined)?.join(" "), |
| 88 | + }); |
| 89 | + |
| 90 | + const config = loadConfig(globalOpts.profile); |
| 91 | + config.clientId = clientId; |
| 92 | + config.clientSecret = clientSecret; |
| 93 | + config.token = tokenResult.access_token; |
| 94 | + delete config.refreshToken; |
| 95 | + saveConfig(config, globalOpts.profile); |
| 96 | + |
| 97 | + printInfo("Client credentials saved to config. Auto-reauth enabled."); |
| 98 | + } else { |
| 99 | + printWarning( |
| 100 | + "Save the clientSecret now — it will not be shown again.", |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + outputResponse(response, format); |
| 105 | + printSuccess("OAuth client created."); |
| 106 | + }), |
| 107 | + ); |
| 108 | + |
| 109 | + addExamples(create, [ |
| 110 | + { |
| 111 | + description: "Create an OAuth client with flags", |
| 112 | + command: "geonic me oauth-clients create --name my-ci-bot --scopes read:entities,write:entities", |
| 113 | + }, |
| 114 | + { |
| 115 | + description: "Create and save credentials for auto-reauth", |
| 116 | + command: "geonic me oauth-clients create --name my-ci-bot --save", |
| 117 | + }, |
| 118 | + { |
| 119 | + description: "Create an OAuth client from JSON", |
| 120 | + command: 'geonic me oauth-clients create \'{"clientName":"my-bot","allowedScopes":["read:entities"]}\'', |
| 121 | + }, |
| 122 | + ]); |
| 123 | + |
| 124 | + // oauth-clients delete |
| 125 | + const del = oauthClients |
| 126 | + .command("delete <id>") |
| 127 | + .description("Delete an OAuth client") |
| 128 | + .action( |
| 129 | + withErrorHandler(async (id: unknown, _opts: unknown, cmd: Command) => { |
| 130 | + const client = createClient(cmd); |
| 131 | + await client.rawRequest( |
| 132 | + "DELETE", |
| 133 | + `/me/oauth-clients/${encodeURIComponent(String(id))}`, |
| 134 | + ); |
| 135 | + printSuccess("OAuth client deleted."); |
| 136 | + }), |
| 137 | + ); |
| 138 | + |
| 139 | + addExamples(del, [ |
| 140 | + { |
| 141 | + description: "Delete an OAuth client", |
| 142 | + command: "geonic me oauth-clients delete <client-id>", |
| 143 | + }, |
| 144 | + ]); |
| 145 | +} |
0 commit comments