@agledger/cli 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/base.js CHANGED
@@ -11,6 +11,7 @@
11
11
  import { readFileSync } from 'node:fs';
12
12
  import { Command, Flags } from '@oclif/core';
13
13
  import { ApiClient } from './api-client.js';
14
+ import { DelegationToken, OIDC_ENV, ON_BEHALF_OF_ENV, OidcCertCredential, OidcExchangeError, OidcTokenSourceError, } from './oidc.js';
14
15
  import { readConfig } from './util/config.js';
15
16
  /** Semantic exit codes for agent consumption. Stable across releases. */
16
17
  export const ExitCode = {
@@ -33,6 +34,10 @@ export const ExitCode = {
33
34
  */
34
35
  export const ErrorCode = {
35
36
  AUTH_REQUIRED: 'AUTH_REQUIRED',
37
+ /** The OIDC token command or file could not produce a token. */
38
+ OIDC_TOKEN_SOURCE_FAILED: 'OIDC_TOKEN_SOURCE_FAILED',
39
+ /** The Server refused `POST /v1/auth/oidc/cert`; its error body rides along as `apiError`. */
40
+ OIDC_EXCHANGE_FAILED: 'OIDC_EXCHANGE_FAILED',
36
41
  /** Required configuration (currently the API URL) is absent. Exits as a
37
42
  * usage error rather than claiming a new exit code. */
38
43
  CONFIG_ERROR: 'CONFIG_ERROR',
@@ -65,6 +70,44 @@ const PUBLIC_GET_PATHS = new Set([
65
70
  '/docs',
66
71
  '/v1/conformance',
67
72
  ]);
73
+ /** The credential sources, in the order they are tried, for a no-credential error. */
74
+ const CREDENTIAL_SOURCES = 'Set AGLEDGER_API_KEY (or pass --api-key), set AGLEDGER_OIDC_TOKEN_CMD to a command that prints an OIDC token, set AGLEDGER_OIDC_TOKEN_FILE to a file holding one, or run `agledger login`.';
75
+ /** The OIDC token source stored on a profile, if it has one. */
76
+ export function profileOidcSource(name, profile) {
77
+ const oidc = profile?.oidc;
78
+ if (oidc?.tokenCommand) {
79
+ return { kind: 'command', command: oidc.tokenCommand, origin: `profile '${name}' oidc.tokenCommand` };
80
+ }
81
+ if (oidc?.tokenFile) {
82
+ return { kind: 'file', path: oidc.tokenFile, origin: `profile '${name}' oidc.tokenFile` };
83
+ }
84
+ return undefined;
85
+ }
86
+ /** The OIDC token source named by the environment: the command outranks the file. */
87
+ export function envOidcSource() {
88
+ const command = process.env[OIDC_ENV.TOKEN_CMD];
89
+ if (command)
90
+ return { kind: 'command', command, origin: OIDC_ENV.TOKEN_CMD };
91
+ const path = process.env[OIDC_ENV.TOKEN_FILE];
92
+ if (path)
93
+ return { kind: 'file', path, origin: OIDC_ENV.TOKEN_FILE };
94
+ return undefined;
95
+ }
96
+ /** The delegation token source from the environment. The command wins over the file. */
97
+ export function envOnBehalfOfSource() {
98
+ const command = process.env[ON_BEHALF_OF_ENV.CMD];
99
+ if (command)
100
+ return { kind: 'command', command, origin: ON_BEHALF_OF_ENV.CMD };
101
+ const path = process.env[ON_BEHALF_OF_ENV.FILE];
102
+ if (path)
103
+ return { kind: 'file', path, origin: ON_BEHALF_OF_ENV.FILE };
104
+ return undefined;
105
+ }
106
+ /** Whether a flag was passed on the command line (`--flag value` or `--flag=value`),
107
+ * as opposed to reaching oclif through its `env` binding. */
108
+ export function argvHasFlag(flag) {
109
+ return process.argv.some((a) => a === flag || a.startsWith(`${flag}=`));
110
+ }
68
111
  function isPublicPath(method, path) {
69
112
  if (method.toUpperCase() !== 'GET')
70
113
  return false;
@@ -75,6 +118,8 @@ export class BaseCommand extends Command {
75
118
  /** The URL the most recent client was built for, so a network failure can
76
119
  * name the host it actually tried. */
77
120
  lastApiUrl;
121
+ /** The client `callApi` built for this invocation, reused by every later call. */
122
+ client;
78
123
  static baseFlags = {
79
124
  json: Flags.boolean({ description: 'Force JSON output (default when stdout is piped)', default: false }),
80
125
  quiet: Flags.boolean({ description: 'Suppress output (exit code only)', default: false }),
@@ -83,6 +128,10 @@ export class BaseCommand extends Command {
83
128
  profile: Flags.string({
84
129
  description: 'Stored profile to use for credentials (falls back to the active profile)',
85
130
  }),
131
+ verbose: Flags.boolean({
132
+ description: 'Report which credential was used, and each OIDC cert exchange, as JSON lines on stderr. Never prints a key, token or cert.',
133
+ default: false,
134
+ }),
86
135
  };
87
136
  get isJson() {
88
137
  return process.argv.includes('--json') || !process.stdout.isTTY;
@@ -90,39 +139,85 @@ export class BaseCommand extends Command {
90
139
  get isQuiet() {
91
140
  return process.argv.includes('--quiet');
92
141
  }
142
+ /** Write one diagnostic line to stderr under --verbose. Callers pass no secrets. */
143
+ verboseLog(flags, event) {
144
+ if (flags.verbose)
145
+ process.stderr.write(JSON.stringify({ verbose: true, ...event }) + '\n');
146
+ }
93
147
  /**
94
- * Resolve credentials and build the API client.
148
+ * Resolve which credential a command uses, without calling anything.
95
149
  *
96
- * Precedence:
97
- * API key: `--api-key` flag > `AGLEDGER_API_KEY` env > stored profile.
98
- * API URL: `--api-url` flag > `AGLEDGER_API_URL` env > stored profile url.
99
- * There is no default; AGLedger is self-hosted.
150
+ * Precedence, highest first:
151
+ * 1. An API key from `--api-key` or `AGLEDGER_API_KEY` (oclif merges the
152
+ * two into `flags['api-key']`).
153
+ * 2. `AGLEDGER_OIDC_TOKEN_CMD`, then `AGLEDGER_OIDC_TOKEN_FILE`.
154
+ * 3. The selected profile (`--profile <name>`, else the active one): its
155
+ * API key, or the OIDC token source `agledger login --oidc` stored.
100
156
  *
101
- * oclif merges the flag and its `env` source into `flags['api-key']` /
102
- * `flags['api-url']`, so a present value there already represents flag-or-env
103
- * (both of which outrank the profile). When absent, fall back to the selected
104
- * profile (`--profile <name>`, else the active profile) in ~/.agledger/config.json.
157
+ * Explicit per-invocation sources outrank stored ones, which is the rule the
158
+ * API key already followed. `AGLEDGER_OIDC_AGENT_ID` overrides a profile's
159
+ * stored agent id.
105
160
  */
106
- createApiClient(flags, options) {
161
+ resolveCredential(flags) {
107
162
  const config = readConfig();
108
163
  const profileName = flags.profile ?? config.activeProfile;
109
164
  const profile = profileName ? config.profiles[profileName] : undefined;
165
+ const envAgentId = process.env[OIDC_ENV.AGENT_ID] || undefined;
110
166
  // Treat an empty-string flag/env (e.g. AGLEDGER_API_KEY="") as absent so the
111
- // profile fallback still applies.
167
+ // later sources still apply.
112
168
  const flagKey = flags['api-key'] || undefined;
169
+ if (flagKey) {
170
+ const origin = argvHasFlag('--api-key') ? '--api-key' : 'AGLEDGER_API_KEY';
171
+ return { credential: { kind: 'api-key', key: flagKey, from: 'flag-or-env', origin }, profileName, profile };
172
+ }
173
+ const fromEnv = envOidcSource();
174
+ if (fromEnv) {
175
+ return {
176
+ credential: { kind: 'oidc', source: fromEnv, ...(envAgentId ? { agentId: envAgentId } : {}), from: 'env' },
177
+ profileName,
178
+ profile,
179
+ };
180
+ }
181
+ if (profileName && profile?.apiKey) {
182
+ return {
183
+ credential: { kind: 'api-key', key: profile.apiKey, from: 'profile', origin: `profile '${profileName}'` },
184
+ profileName,
185
+ profile,
186
+ };
187
+ }
188
+ const fromProfile = profileName ? profileOidcSource(profileName, profile) : undefined;
189
+ if (fromProfile) {
190
+ const agentId = envAgentId ?? profile?.oidc?.agentId;
191
+ return {
192
+ credential: { kind: 'oidc', source: fromProfile, ...(agentId ? { agentId } : {}), from: 'profile' },
193
+ profileName,
194
+ profile,
195
+ };
196
+ }
197
+ return { credential: { kind: 'none' }, profileName, profile };
198
+ }
199
+ /**
200
+ * Resolve credentials and build the API client.
201
+ *
202
+ * Credential precedence is `resolveCredential`'s. API URL: `--api-url` flag >
203
+ * `AGLEDGER_API_URL` env > stored profile url. There is no default;
204
+ * AGLedger is self-hosted.
205
+ */
206
+ createApiClient(flags, options) {
207
+ const { credential, profile } = this.resolveCredential(flags);
113
208
  const flagUrl = flags['api-url'] || undefined;
114
- // A profile is only consulted when the flag/env didn't supply the key. If the
115
- // caller explicitly named a missing profile AND has no flag/env key to fall
116
- // back on, that's an error worth surfacing (rather than a generic no-key one).
117
- if (flags.profile && !profile && !flagKey) {
209
+ // A profile is only consulted when nothing more explicit supplied a
210
+ // credential. If the caller explicitly named a missing profile AND has
211
+ // nothing else to fall back on, that's an error worth surfacing (rather
212
+ // than a generic no-credential one).
213
+ if (flags.profile && !profile && credential.kind === 'none') {
118
214
  this.failWith(ErrorCode.AUTH_REQUIRED, `Profile '${flags.profile}' not found.`, ExitCode.AUTH_ERROR, 'Run `agledger config list` to see profiles, or `agledger login --profile <name>` to create one.');
119
215
  }
120
- const apiKey = flagKey ?? profile?.apiKey ?? null;
121
- // Discovery surfaces answer without auth, so a keyless invocation proceeds
122
- // anonymously rather than being refused before any request is made. The
123
- // Server, not the CLI, decides what needs a key.
124
- if (!apiKey && !options?.allowAnonymous) {
125
- this.failWith(ErrorCode.AUTH_REQUIRED, 'No API key. Set AGLEDGER_API_KEY, use --api-key, or run `agledger login`.', ExitCode.AUTH_ERROR);
216
+ // Discovery surfaces answer without auth, so a credential-less invocation
217
+ // proceeds anonymously rather than being refused before any request is
218
+ // made. The Server, not the CLI, decides what needs a key.
219
+ if (credential.kind === 'none' && !options?.allowAnonymous) {
220
+ this.failWith(ErrorCode.AUTH_REQUIRED, 'No credential configured.', ExitCode.AUTH_ERROR, CREDENTIAL_SOURCES);
126
221
  }
127
222
  // No placeholder: a default of agledger.example.com resolved nowhere and
128
223
  // turned a missing config into a DNS failure the user could not read.
@@ -132,22 +227,53 @@ export class BaseCommand extends Command {
132
227
  this.failWith(ErrorCode.CONFIG_ERROR, 'No API URL configured. AGLedger is self-hosted, so there is no default server to call.', ExitCode.USAGE_ERROR, 'Pass --api-url <url>, set AGLEDGER_API_URL, or run `agledger login --api-url <url> --api-key <key>`.');
133
228
  }
134
229
  this.lastApiUrl = apiUrl;
135
- return new ApiClient(apiUrl, apiKey, this.config.version);
230
+ const onBehalfOfSource = envOnBehalfOfSource();
231
+ this.verboseLog(flags, {
232
+ event: 'auth',
233
+ ...this.describeCredential(credential),
234
+ apiUrl,
235
+ ...(onBehalfOfSource ? { onBehalfOfSource: onBehalfOfSource.origin } : {}),
236
+ });
237
+ const onBehalfOf = onBehalfOfSource ? new DelegationToken(onBehalfOfSource) : null;
238
+ if (credential.kind === 'oidc') {
239
+ return new ApiClient(apiUrl, this.oidcCredential(flags, credential), this.config.version, undefined, onBehalfOf);
240
+ }
241
+ return new ApiClient(apiUrl, credential.kind === 'api-key' ? credential.key : null, this.config.version, undefined, onBehalfOf);
242
+ }
243
+ /** Build a cert credential for one invocation. Its key pair never leaves memory. */
244
+ oidcCredential(flags, credential) {
245
+ return new OidcCertCredential({
246
+ source: credential.source,
247
+ ...(credential.agentId ? { agentId: credential.agentId } : {}),
248
+ userAgent: `agledger-cli/${this.config.version}`,
249
+ log: (event) => this.verboseLog(flags, event),
250
+ });
251
+ }
252
+ /** A secret-free description of a credential, for --verbose, --dry-run and `auth`. */
253
+ describeCredential(credential) {
254
+ switch (credential.kind) {
255
+ case 'api-key':
256
+ return { credential: 'api-key', source: credential.origin };
257
+ case 'oidc':
258
+ return {
259
+ credential: 'oidc-cert',
260
+ source: credential.source.origin,
261
+ ...(credential.source.kind === 'file' ? { tokenFile: credential.source.path } : {}),
262
+ ...(credential.agentId ? { agentId: credential.agentId } : {}),
263
+ };
264
+ case 'none':
265
+ return { credential: 'none' };
266
+ }
136
267
  }
137
268
  /**
138
269
  * Resolve the credentials that an actual call would use, for --dry-run display.
139
- * Same precedence as `createApiClient` (flag > env > profile), but the key is
140
- * masked so it is safe to print. Returns the resolved api URL and which source
141
- * the key came from. Does not throw on a missing key (dry-run is non-fatal).
270
+ * Same precedence as `createApiClient`, but the key is masked so it is safe
271
+ * to print, and an OIDC token source is named, never run. Does not throw on
272
+ * a missing credential (dry-run is non-fatal).
142
273
  */
143
274
  resolvedAuth(flags) {
144
- const config = readConfig();
145
- const profileName = flags.profile ?? config.activeProfile;
146
- const profile = profileName ? config.profiles[profileName] : undefined;
147
- const flagKey = flags['api-key'] || undefined;
275
+ const { credential, profileName, profile } = this.resolveCredential(flags);
148
276
  const flagUrl = flags['api-url'] || undefined;
149
- const apiKey = flagKey ?? profile?.apiKey ?? null;
150
- const source = flagKey ? 'flag-or-env' : profile?.apiKey ? 'profile' : 'none';
151
277
  // Null, not a placeholder. `agledger.example.com` was removed from
152
278
  // `createApiClient`, which now refuses to build a client without a URL, but
153
279
  // this sibling kept it. The whole job of --dry-run is to report what the
@@ -156,10 +282,13 @@ export class BaseCommand extends Command {
156
282
  // while the same invocation without --dry-run exited 2 with CONFIG_ERROR.
157
283
  const apiUrl = flagUrl ?? profile?.apiUrl ?? null;
158
284
  const mask = (k) => (k.length <= 4 ? '****' : `****${k.slice(-4)}`);
285
+ const source = credential.kind === 'none' ? 'none' : credential.from;
159
286
  return {
160
287
  apiUrl,
161
- apiKey: apiKey ? mask(apiKey) : null,
288
+ apiKey: credential.kind === 'api-key' ? mask(credential.key) : null,
162
289
  source,
290
+ credential: credential.kind === 'oidc' ? 'oidc-cert' : credential.kind,
291
+ ...(credential.kind === 'oidc' ? { oidcTokenSource: credential.source.origin } : {}),
163
292
  // A dry run whose real counterpart would refuse to send says so, rather
164
293
  // than leaving a bare `"apiUrl": null` for the reader to interpret.
165
294
  ...(apiUrl === null
@@ -175,8 +304,19 @@ export class BaseCommand extends Command {
175
304
  * (e.g. `/v1/records`, `/health`, `/federation/v1/peer`). No auto-prefixing.
176
305
  */
177
306
  async callApi(flags, method, path, options) {
178
- const client = this.createApiClient(flags, { allowAnonymous: isPublicPath(method, path) });
179
- return client.request(method, path, options);
307
+ const allowAnonymous = isPublicPath(method, path);
308
+ // One client per invocation. With an OIDC credential a new client means a
309
+ // new key pair and a new exchange, so rebuilding it per call (one per page
310
+ // under --paginate) spent a token per page: a token file 409s on the second
311
+ // exchange, and a command runs into the exchange route's rate limit.
312
+ if (!this.client) {
313
+ this.client = this.createApiClient(flags, { allowAnonymous });
314
+ }
315
+ else if (this.client.isAnonymous && !allowAnonymous) {
316
+ // Same refusal a fresh client would give for a path that needs a credential.
317
+ this.createApiClient(flags, { allowAnonymous });
318
+ }
319
+ return this.client.request(method, path, options);
180
320
  }
181
321
  output(data) {
182
322
  if (this.isQuiet)
@@ -268,6 +408,26 @@ export class BaseCommand extends Command {
268
408
  handleError(err) {
269
409
  if (err instanceof Error && err.message.startsWith('EEXIT:'))
270
410
  throw err;
411
+ if (err instanceof OidcTokenSourceError) {
412
+ this.failWith(ErrorCode.OIDC_TOKEN_SOURCE_FAILED, err.message, ExitCode.AUTH_ERROR, err.kind === 'command'
413
+ ? `Run the command in ${err.origin} by hand and check it prints one JWT on stdout and exits 0.`
414
+ : `Check the file named by ${err.origin} exists, is readable, and holds one JWT.`);
415
+ }
416
+ if (err instanceof OidcExchangeError) {
417
+ // The Server's error body is forwarded as it came (its recoveryHint is
418
+ // the useful part), with the OIDC token scrubbed out of it.
419
+ const error = {
420
+ error: true,
421
+ code: ErrorCode.OIDC_EXCHANGE_FAILED,
422
+ message: err.message,
423
+ status: err.status,
424
+ source: err.origin,
425
+ apiError: err.body,
426
+ };
427
+ process.stderr.write(JSON.stringify(error) + '\n');
428
+ this.exit(this.statusToExitCode(err.status));
429
+ throw new Error('unreachable');
430
+ }
271
431
  if (err instanceof DOMException && err.name === 'AbortError') {
272
432
  this.failWith(ErrorCode.TIMEOUT, 'Request timed out.', ExitCode.TIMEOUT, 'Retry the same command. If it persists, run `agledger discover` to check API connectivity.');
273
433
  }
@@ -22,6 +22,7 @@ export default class Api extends BaseCommand {
22
22
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
23
23
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
24
24
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
25
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
25
26
  data: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
26
27
  input: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
27
28
  field: import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
@@ -1,8 +1,11 @@
1
1
  import { BaseCommand } from '../base.js';
2
2
  /**
3
3
  * Quick "am I logged in?" check. Local-first: returns `authenticated: false`
4
- * with exit 0 when no key is configured (useful for scripts gating on login state).
5
- * When a key is configured, verifies it against the API via GET /v1/auth/me.
4
+ * with exit 0 when no credential is configured (useful for scripts gating on
5
+ * login state). Otherwise verifies the credential via GET /v1/auth/me. With an
6
+ * OIDC token source, that call runs one cert exchange, and the cert the Server
7
+ * issued (id, agent, issuer, subject, scopes, lifetime) is shown beside the
8
+ * identity.
6
9
  */
7
10
  export default class Auth extends BaseCommand {
8
11
  static description: string;
@@ -12,6 +15,7 @@ export default class Auth extends BaseCommand {
12
15
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
16
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
14
17
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
18
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
15
19
  };
16
20
  run(): Promise<void>;
17
21
  }
@@ -1,32 +1,42 @@
1
1
  import { BaseCommand } from '../base.js';
2
2
  /**
3
3
  * Quick "am I logged in?" check. Local-first: returns `authenticated: false`
4
- * with exit 0 when no key is configured (useful for scripts gating on login state).
5
- * When a key is configured, verifies it against the API via GET /v1/auth/me.
4
+ * with exit 0 when no credential is configured (useful for scripts gating on
5
+ * login state). Otherwise verifies the credential via GET /v1/auth/me. With an
6
+ * OIDC token source, that call runs one cert exchange, and the cert the Server
7
+ * issued (id, agent, issuer, subject, scopes, lifetime) is shown beside the
8
+ * identity.
6
9
  */
7
10
  export default class Auth extends BaseCommand {
8
- static description = 'Check current authentication status. Returns `authenticated: false` when no key is configured (exit 0).';
11
+ static description = 'Check current authentication status, including the OIDC cert identity when a token source is configured. Returns `authenticated: false` when no credential is configured (exit 0).';
9
12
  static flags = { ...BaseCommand.baseFlags };
10
13
  async run() {
11
14
  const { flags } = await this.parse(Auth);
12
- // Resolve the key the same way an actual call does: --api-key flag > env >
13
- // stored profile in ~/.agledger/config.json. The previous check looked only
14
- // at the flag/env, so `agledger auth` reported not-authenticated right after
15
- // a successful `login` wrote the key to a profile.
15
+ // Resolve the credential the same way an actual call does. An earlier
16
+ // check looked only at the flag/env, so `agledger auth` reported
17
+ // not-authenticated right after a successful `login` wrote a profile.
16
18
  const auth = this.resolvedAuth(flags);
17
- if (auth.source === 'none') {
18
- this.output({ authenticated: false, message: 'No API key configured. Run `agledger login --api-key <key>`.' });
19
+ if (auth.credential === 'none') {
20
+ this.output({
21
+ authenticated: false,
22
+ message: 'No credential configured. Set AGLEDGER_API_KEY, AGLEDGER_OIDC_TOKEN_CMD or AGLEDGER_OIDC_TOKEN_FILE, or run `agledger login`.',
23
+ });
19
24
  return;
20
25
  }
21
26
  try {
22
- const response = await this.callApi(flags, 'GET', '/v1/auth/me');
27
+ const client = this.createApiClient(flags);
28
+ const response = await client.request('GET', '/v1/auth/me');
23
29
  if (!response.ok) {
24
30
  this.handleApiError(response);
25
31
  }
32
+ const cert = client.credential?.cert;
26
33
  this.output({
27
34
  authenticated: true,
28
35
  source: auth.source,
36
+ credential: auth.credential,
37
+ ...(auth.oidcTokenSource ? { oidcTokenSource: auth.oidcTokenSource } : {}),
29
38
  ...(auth.profile ? { profile: auth.profile } : {}),
39
+ ...(cert ? { cert } : {}),
30
40
  account: response.body,
31
41
  });
32
42
  }
@@ -17,6 +17,7 @@ export default class Config extends BaseCommand {
17
17
  quiet: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
18
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
19
19
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
20
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
20
21
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
21
22
  };
22
23
  run(): Promise<void>;
@@ -1,6 +1,14 @@
1
1
  import { Args, Flags } from '@oclif/core';
2
2
  import { BaseCommand, ErrorCode, ExitCode } from '../base.js';
3
3
  import { configPath, readConfig, writeConfig } from '../util/config.js';
4
+ /** What kind of credential a profile holds. Never echoes the key itself. */
5
+ function credentialOf(p) {
6
+ if (p.apiKey)
7
+ return 'api-key';
8
+ if (p.oidc?.tokenCommand || p.oidc?.tokenFile)
9
+ return 'oidc-cert';
10
+ return 'none';
11
+ }
4
12
  /**
5
13
  * Inspect and switch between stored profiles in ~/.agledger/config.json.
6
14
  *
@@ -39,6 +47,7 @@ export default class Config extends BaseCommand {
39
47
  const profiles = Object.entries(config.profiles).map(([name, p]) => ({
40
48
  name,
41
49
  apiUrl: p.apiUrl,
50
+ credential: credentialOf(p),
42
51
  active: name === config.activeProfile,
43
52
  }));
44
53
  this.output({ activeProfile: config.activeProfile, profiles });
@@ -53,7 +62,14 @@ export default class Config extends BaseCommand {
53
62
  if (!profile) {
54
63
  this.failWith(ErrorCode.MISSING_INPUT, `Profile '${name}' not found.`, ExitCode.USAGE_ERROR, `Run \`agledger config list\` to see profiles.`);
55
64
  }
56
- this.output({ name, apiUrl: profile.apiUrl, active: name === config.activeProfile });
65
+ this.output({
66
+ name,
67
+ apiUrl: profile.apiUrl,
68
+ credential: credentialOf(profile),
69
+ // The stored token source (a command or a path), never a token.
70
+ ...(profile.oidc ? { oidc: profile.oidc } : {}),
71
+ active: name === config.activeProfile,
72
+ });
57
73
  return;
58
74
  }
59
75
  case 'use': {
@@ -15,6 +15,7 @@ export default class Discover extends BaseCommand {
15
15
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
16
16
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
17
17
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
18
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
19
  };
19
20
  run(): Promise<void>;
20
21
  }
@@ -1,3 +1,4 @@
1
+ import { ApiClient } from '../api-client.js';
1
2
  import { BaseCommand } from '../base.js';
2
3
  /**
3
4
  * Cold-start discovery for agents and humans. Mirrors the MCP server's
@@ -15,8 +16,11 @@ export default class Discover extends BaseCommand {
15
16
  // "Call this first" has to be true without a key: /health answers
16
17
  // unauthenticated, and /v1/auth/me simply reports no identity.
17
18
  const client = this.createApiClient(flags, { allowAnonymous: true });
19
+ // /health goes without a credential, so a token source or IdP that is down
20
+ // does not also hide whether the Server is up.
21
+ const anonymous = new ApiClient(client.baseUrl, null, this.config.version);
18
22
  const [health, identity] = await Promise.allSettled([
19
- client.request('GET', '/health'),
23
+ anonymous.request('GET', '/health'),
20
24
  client.request('GET', '/v1/auth/me'),
21
25
  ]);
22
26
  const result = {};
@@ -14,6 +14,7 @@ export default class Docs extends BaseCommand {
14
14
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
15
15
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
16
16
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
17
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
17
18
  full: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
19
  };
19
20
  run(): Promise<void>;
@@ -26,7 +26,13 @@ export default class Docs extends BaseCommand {
26
26
  // able to read the narrative before it has a key.
27
27
  const client = this.createApiClient(flags, { allowAnonymous: true });
28
28
  const path = flags.full ? '/llms-full.txt' : '/llms.txt';
29
- const res = await client.request('GET', path);
29
+ let res;
30
+ try {
31
+ res = await client.request('GET', path);
32
+ }
33
+ catch (err) {
34
+ this.handleError(err);
35
+ }
30
36
  const text = res.body && typeof res.body === 'object' && '_raw' in res.body
31
37
  ? String(res.body._raw)
32
38
  : typeof res.body === 'string'
@@ -11,6 +11,7 @@ export default class HelpJson extends BaseCommand {
11
11
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
12
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
13
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
14
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
14
15
  };
15
16
  run(): Promise<void>;
16
17
  }
@@ -15,6 +15,7 @@ export default class ListCommands extends BaseCommand {
15
15
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
16
16
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
17
17
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
18
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
19
  };
19
20
  run(): Promise<void>;
20
21
  }
@@ -1,17 +1,29 @@
1
1
  import { BaseCommand } from '../base.js';
2
2
  /**
3
- * Verify the provided key against the API, then persist it to ~/.agledger/config.json
4
- * (0600, dir 0700). Supports named profiles so one machine can hold keys for multiple
5
- * instances/environments.
3
+ * Verify a credential against the API, then persist it to
4
+ * ~/.agledger/config.json (0600, dir 0700). Supports named profiles so one
5
+ * machine can hold credentials for several Servers.
6
+ *
7
+ * `--oidc` stores an OIDC token SOURCE (a command or a file path), never a
8
+ * token, cert or key. Every later command runs the source again and exchanges
9
+ * the token for a fresh cert held in memory for that one invocation.
6
10
  */
7
11
  export default class Login extends BaseCommand {
8
12
  static description: string;
13
+ static examples: string[];
9
14
  static flags: {
10
15
  json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
16
  quiet: import("@oclif/core/interfaces").BooleanFlag<boolean>;
12
17
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
18
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
19
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
14
20
  profile: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
21
+ oidc: import("@oclif/core/interfaces").BooleanFlag<boolean>;
22
+ 'oidc-token-cmd': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
23
+ 'oidc-token-file': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
24
+ 'oidc-agent-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
15
25
  };
16
26
  run(): Promise<void>;
27
+ private loginOidc;
28
+ private save;
17
29
  }