@agledger/cli 1.4.1 → 2.0.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/LICENSE +10 -14
- package/README.md +162 -16
- package/SKILL.md +14 -9
- package/dist/api-client.d.ts +24 -5
- package/dist/api-client.js +85 -16
- package/dist/base.d.ts +106 -31
- package/dist/base.js +299 -41
- package/dist/commands/api.d.ts +1 -0
- package/dist/commands/api.js +4 -3
- package/dist/commands/auth.d.ts +6 -2
- package/dist/commands/auth.js +20 -10
- package/dist/commands/config.d.ts +1 -0
- package/dist/commands/config.js +17 -1
- package/dist/commands/discover.d.ts +1 -0
- package/dist/commands/discover.js +5 -1
- package/dist/commands/docs.d.ts +1 -0
- package/dist/commands/docs.js +7 -1
- package/dist/commands/help-json.d.ts +1 -0
- package/dist/commands/list-commands.d.ts +1 -0
- package/dist/commands/login.d.ts +16 -3
- package/dist/commands/login.js +121 -10
- package/dist/commands/logout.d.ts +3 -1
- package/dist/commands/logout.js +35 -11
- package/dist/commands/verify.d.ts +22 -1
- package/dist/commands/verify.js +241 -21
- package/dist/oidc.d.ts +143 -0
- package/dist/oidc.js +384 -0
- package/dist/util/config.d.ts +13 -1
- package/oclif.manifest.json +147 -12
- package/package.json +2 -2
package/dist/oidc.js
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OIDC cert credential: exchanges a customer IdP token for a short-lived,
|
|
3
|
+
* Server-signed cert (`POST /v1/auth/oidc/cert`) and signs request bodies with
|
|
4
|
+
* the key bound to it.
|
|
5
|
+
*
|
|
6
|
+
* Nothing here touches disk except reading a token file the operator named.
|
|
7
|
+
* The Ed25519 key pair lives in memory for the life of one credential (one CLI
|
|
8
|
+
* invocation), and the cert is cached in memory only. The token source is
|
|
9
|
+
* called for EVERY exchange: the Server refuses a token id it has already
|
|
10
|
+
* exchanged (409), so a token is never reused.
|
|
11
|
+
*/
|
|
12
|
+
import { spawn } from 'node:child_process';
|
|
13
|
+
import { createHash, generateKeyPairSync, sign } from 'node:crypto';
|
|
14
|
+
import { readFile } from 'node:fs/promises';
|
|
15
|
+
/** Env vars the CLI reads a token source from. The MCP server reads the same names. */
|
|
16
|
+
export const OIDC_ENV = {
|
|
17
|
+
TOKEN_CMD: 'AGLEDGER_OIDC_TOKEN_CMD',
|
|
18
|
+
TOKEN_FILE: 'AGLEDGER_OIDC_TOKEN_FILE',
|
|
19
|
+
AGENT_ID: 'AGLEDGER_OIDC_AGENT_ID',
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Env vars naming an RFC 8693 delegation token, sent as `AGLedger-On-Behalf-Of`
|
|
23
|
+
* when the work is done for a person or another party. The MCP server reads
|
|
24
|
+
* the same names.
|
|
25
|
+
*/
|
|
26
|
+
export const ON_BEHALF_OF_ENV = {
|
|
27
|
+
CMD: 'AGLEDGER_ON_BEHALF_OF_CMD',
|
|
28
|
+
FILE: 'AGLEDGER_ON_BEHALF_OF_FILE',
|
|
29
|
+
};
|
|
30
|
+
/** The token source could not produce a token. `origin` names the variable or profile. */
|
|
31
|
+
export class OidcTokenSourceError extends Error {
|
|
32
|
+
origin;
|
|
33
|
+
kind;
|
|
34
|
+
constructor(message, origin, kind) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.origin = origin;
|
|
37
|
+
this.kind = kind;
|
|
38
|
+
this.name = 'OidcTokenSourceError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** The Server refused the exchange. `body` is its error response with the token scrubbed out. */
|
|
42
|
+
export class OidcExchangeError extends Error {
|
|
43
|
+
status;
|
|
44
|
+
body;
|
|
45
|
+
origin;
|
|
46
|
+
constructor(message, status, body, origin) {
|
|
47
|
+
super(message);
|
|
48
|
+
this.status = status;
|
|
49
|
+
this.body = body;
|
|
50
|
+
this.origin = origin;
|
|
51
|
+
this.name = 'OidcExchangeError';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Bounds on a token command. Mutable so tests can shorten the timeout. */
|
|
55
|
+
export const TOKEN_COMMAND_LIMITS = {
|
|
56
|
+
timeoutMs: 60_000,
|
|
57
|
+
/** A JWT is a few KiB at most; anything past this is not one token. */
|
|
58
|
+
stdoutBytes: 64 * 1024,
|
|
59
|
+
};
|
|
60
|
+
const STDERR_LIMIT = 2_000;
|
|
61
|
+
/** A compact JWS: three base64url segments, the first a JSON header (`eyJ`). */
|
|
62
|
+
const JWT_PATTERN = /eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*/g;
|
|
63
|
+
/** Replace anything shaped like a JWT with a placeholder. Token command stderr
|
|
64
|
+
* and Server error bodies are surfaced to the user, and either can carry one. */
|
|
65
|
+
export function redactTokens(text) {
|
|
66
|
+
return text.replace(JWT_PATTERN, '<redacted-token>');
|
|
67
|
+
}
|
|
68
|
+
/** Deep-copy a JSON value with every string passed through `redactTokens`,
|
|
69
|
+
* and any string equal to or containing `token` replaced. The Server's
|
|
70
|
+
* validation errors echo the offending input back, which for this route is
|
|
71
|
+
* the OIDC token. */
|
|
72
|
+
export function scrub(value, token) {
|
|
73
|
+
if (typeof value === 'string') {
|
|
74
|
+
const cut = token && token.length > 0 ? value.split(token).join('<redacted-token>') : value;
|
|
75
|
+
return redactTokens(cut);
|
|
76
|
+
}
|
|
77
|
+
if (Array.isArray(value))
|
|
78
|
+
return value.map((v) => scrub(v, token));
|
|
79
|
+
if (value !== null && typeof value === 'object') {
|
|
80
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, scrub(v, token)]));
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
function runTokenCommand(source) {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
// Detached puts the shell in its own process group, so a timeout can kill
|
|
87
|
+
// the whole pipeline (`a | b`) rather than only the shell and orphan the rest.
|
|
88
|
+
const child = spawn(source.command, { shell: true, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
89
|
+
let stdout = '';
|
|
90
|
+
let stderr = '';
|
|
91
|
+
let settled = false;
|
|
92
|
+
const fail = (message) => {
|
|
93
|
+
if (settled)
|
|
94
|
+
return;
|
|
95
|
+
settled = true;
|
|
96
|
+
clearTimeout(timer);
|
|
97
|
+
killGroup(child.pid);
|
|
98
|
+
reject(new OidcTokenSourceError(`${source.origin}: ${message}`, source.origin, source.kind));
|
|
99
|
+
};
|
|
100
|
+
const { timeoutMs, stdoutBytes } = TOKEN_COMMAND_LIMITS;
|
|
101
|
+
const timer = setTimeout(() => fail(`the token command did not finish within ${timeoutMs / 1000}s.`), timeoutMs);
|
|
102
|
+
child.stdout.setEncoding('utf8').on('data', (d) => {
|
|
103
|
+
stdout += d;
|
|
104
|
+
if (stdout.length > stdoutBytes) {
|
|
105
|
+
fail(`the token command printed more than ${stdoutBytes / 1024} KiB. Expected one JWT on stdout.`);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
child.stderr.setEncoding('utf8').on('data', (d) => {
|
|
109
|
+
if (stderr.length < STDERR_LIMIT * 2)
|
|
110
|
+
stderr += d;
|
|
111
|
+
});
|
|
112
|
+
child.on('error', (err) => fail(`could not run the token command: ${err.message}`));
|
|
113
|
+
child.on('close', (code, signal) => {
|
|
114
|
+
if (settled)
|
|
115
|
+
return;
|
|
116
|
+
if (code === 0) {
|
|
117
|
+
settled = true;
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
resolve(stdout);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const trimmed = redactTokens(stderr.trim()).slice(0, STDERR_LIMIT);
|
|
123
|
+
const how = signal ? `was killed by ${signal}` : `exited ${code}`;
|
|
124
|
+
fail(`the token command ${how}${trimmed ? `. stderr: ${trimmed}` : ' with no stderr output.'}`);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/** SIGKILL a detached child's whole process group. Already gone is fine. */
|
|
129
|
+
function killGroup(pid) {
|
|
130
|
+
if (pid === undefined)
|
|
131
|
+
return;
|
|
132
|
+
try {
|
|
133
|
+
process.kill(-pid, 'SIGKILL');
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
// The group has exited.
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async function readTokenFile(source) {
|
|
140
|
+
try {
|
|
141
|
+
return await readFile(source.path, 'utf8');
|
|
142
|
+
}
|
|
143
|
+
catch (err) {
|
|
144
|
+
const code = err.code;
|
|
145
|
+
throw new OidcTokenSourceError(`${source.origin}: cannot read the token file ${source.path}${typeof code === 'string' ? ` (${code})` : ''}.`, source.origin, source.kind);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const SUB_ROLE = {
|
|
149
|
+
'cert-exchange': 'which the proof of possession for the cert exchange signs',
|
|
150
|
+
delegation: 'which names the party the work is done on behalf of',
|
|
151
|
+
};
|
|
152
|
+
/** Call the token source once and return a validated compact JWT plus its `sub` and payload. */
|
|
153
|
+
export async function fetchOidcToken(source, purpose = 'cert-exchange') {
|
|
154
|
+
const raw = source.kind === 'command' ? await runTokenCommand(source) : await readTokenFile(source);
|
|
155
|
+
const token = raw.trim();
|
|
156
|
+
const what = source.kind === 'command' ? 'the token command printed' : 'the token file holds';
|
|
157
|
+
if (token.length === 0) {
|
|
158
|
+
throw new OidcTokenSourceError(`${source.origin}: ${what} nothing. Expected one OIDC JWT.`, source.origin, source.kind);
|
|
159
|
+
}
|
|
160
|
+
const parts = token.split('.');
|
|
161
|
+
let payload = {};
|
|
162
|
+
if (parts.length === 3 && parts[1]) {
|
|
163
|
+
try {
|
|
164
|
+
const parsed = JSON.parse(Buffer.from(parts[1], 'base64url').toString('utf8'));
|
|
165
|
+
if (parsed !== null && typeof parsed === 'object')
|
|
166
|
+
payload = parsed;
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
payload = {};
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
// Do not echo the output: if it is a token in some other shape, it would
|
|
174
|
+
// land in the error message.
|
|
175
|
+
throw new OidcTokenSourceError(`${source.origin}: ${what} ${token.length} characters that are not a compact JWT (header.payload.signature).`, source.origin, source.kind);
|
|
176
|
+
}
|
|
177
|
+
const sub = payload.sub;
|
|
178
|
+
if (typeof sub !== 'string' || sub.length === 0) {
|
|
179
|
+
throw new OidcTokenSourceError(`${source.origin}: the JWT has no readable \`sub\` claim, ${SUB_ROLE[purpose]}.`, source.origin, source.kind);
|
|
180
|
+
}
|
|
181
|
+
return { token, sub, payload };
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* One cert credential. Generates its key pair on construction, exchanges on
|
|
185
|
+
* first use, re-exchanges past the refresh point or when forced (a 401), and
|
|
186
|
+
* shares one in-flight exchange between concurrent callers.
|
|
187
|
+
*/
|
|
188
|
+
export class OidcCertCredential {
|
|
189
|
+
source;
|
|
190
|
+
agentId;
|
|
191
|
+
refreshFraction;
|
|
192
|
+
log;
|
|
193
|
+
now;
|
|
194
|
+
fetchImpl;
|
|
195
|
+
userAgent;
|
|
196
|
+
timeoutMs;
|
|
197
|
+
privateKey;
|
|
198
|
+
publicKeyX;
|
|
199
|
+
certJws = null;
|
|
200
|
+
certInfo = null;
|
|
201
|
+
/** Local clock times, from when the cert was received: the Server's clock may differ. */
|
|
202
|
+
refreshAt = 0;
|
|
203
|
+
expiresAt = 0;
|
|
204
|
+
inflight = null;
|
|
205
|
+
exchanges = 0;
|
|
206
|
+
constructor(options) {
|
|
207
|
+
this.source = options.source;
|
|
208
|
+
this.agentId = options.agentId || undefined;
|
|
209
|
+
this.refreshFraction = options.refreshFraction ?? 0.5;
|
|
210
|
+
this.log = options.log ?? (() => { });
|
|
211
|
+
this.now = options.now ?? Date.now;
|
|
212
|
+
this.fetchImpl = options.fetch ?? ((...args) => fetch(...args));
|
|
213
|
+
this.userAgent = options.userAgent ?? 'agledger-cli';
|
|
214
|
+
this.timeoutMs = options.timeoutMs ?? 30_000;
|
|
215
|
+
const { publicKey, privateKey } = generateKeyPairSync('ed25519');
|
|
216
|
+
this.privateKey = privateKey;
|
|
217
|
+
const jwk = publicKey.export({ format: 'jwk' });
|
|
218
|
+
this.publicKeyX = jwk.x;
|
|
219
|
+
}
|
|
220
|
+
/** The cert from the most recent exchange, as the Server returned it. */
|
|
221
|
+
get cert() {
|
|
222
|
+
return this.certInfo;
|
|
223
|
+
}
|
|
224
|
+
/** How many exchanges this credential has performed. */
|
|
225
|
+
get exchangeCount() {
|
|
226
|
+
return this.exchanges;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* A bearer for the next request: the cached cert, or a fresh one when none
|
|
230
|
+
* is held or the refresh point has passed. Pass `rejected` (the bearer the
|
|
231
|
+
* Server just refused with 401) to force a re-exchange; when a concurrent
|
|
232
|
+
* request has already replaced that cert, the replacement is returned
|
|
233
|
+
* instead of exchanging again.
|
|
234
|
+
*/
|
|
235
|
+
async getToken(baseUrl, rejected) {
|
|
236
|
+
const forced = rejected !== undefined && rejected === this.certJws;
|
|
237
|
+
if (!forced && this.certJws !== null && this.now() < this.refreshAt)
|
|
238
|
+
return this.certJws;
|
|
239
|
+
if (!this.inflight) {
|
|
240
|
+
const reason = forced ? 'rejected-401' : this.certJws === null ? 'initial' : 'refresh-point';
|
|
241
|
+
this.inflight = this.exchange(baseUrl, reason).finally(() => {
|
|
242
|
+
this.inflight = null;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
if (forced || this.certJws === null)
|
|
246
|
+
return this.inflight;
|
|
247
|
+
// A refresh-point exchange that fails (IdP down, a token file that has not
|
|
248
|
+
// rotated yet, 429, 5xx) is not fatal while the current cert is still
|
|
249
|
+
// valid: keep using it and try again on the next request.
|
|
250
|
+
const current = this.certJws;
|
|
251
|
+
try {
|
|
252
|
+
return await this.inflight;
|
|
253
|
+
}
|
|
254
|
+
catch (err) {
|
|
255
|
+
if (this.now() < this.expiresAt) {
|
|
256
|
+
// Back off before the next attempt so a stuck source is not re-run on
|
|
257
|
+
// every request (the exchange route is rate-limited per IP).
|
|
258
|
+
this.refreshAt = this.now() + Math.min(30_000, (this.expiresAt - this.now()) / 2);
|
|
259
|
+
this.log({
|
|
260
|
+
event: 'oidc-refresh-deferred',
|
|
261
|
+
source: this.source.origin,
|
|
262
|
+
reason: err instanceof Error ? err.message : String(err),
|
|
263
|
+
certExpiresInSeconds: Math.floor((this.expiresAt - this.now()) / 1000),
|
|
264
|
+
});
|
|
265
|
+
return current;
|
|
266
|
+
}
|
|
267
|
+
throw err;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/** The agent signature headers for a request body, over the exact bytes sent. */
|
|
271
|
+
signBody(body) {
|
|
272
|
+
const hex = createHash('sha256').update(body, 'utf8').digest('hex');
|
|
273
|
+
const signature = sign(null, Buffer.from(`agledger.agent.sig.v1\n${hex}`, 'utf8'), this.privateKey);
|
|
274
|
+
return {
|
|
275
|
+
'X-Agent-Signature-Content-Hash': `sha256:${hex}`,
|
|
276
|
+
'X-Agent-Signature': signature.toString('base64'),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
async exchange(baseUrl, reason) {
|
|
280
|
+
const { token, sub } = await fetchOidcToken(this.source);
|
|
281
|
+
const proofOfPossession = sign(null, Buffer.from(`agledger.oidc.cert.v1\n${sub}`, 'utf8'), this.privateKey).toString('base64');
|
|
282
|
+
const body = {
|
|
283
|
+
oidcToken: token,
|
|
284
|
+
publicKeyJwk: { kty: 'OKP', crv: 'Ed25519', x: this.publicKeyX },
|
|
285
|
+
proofOfPossession,
|
|
286
|
+
...(this.agentId ? { agentId: this.agentId } : {}),
|
|
287
|
+
};
|
|
288
|
+
const controller = new AbortController();
|
|
289
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
290
|
+
let res;
|
|
291
|
+
let parsed;
|
|
292
|
+
try {
|
|
293
|
+
res = await this.fetchImpl(`${baseUrl}/v1/auth/oidc/cert`, {
|
|
294
|
+
method: 'POST',
|
|
295
|
+
headers: {
|
|
296
|
+
Accept: 'application/json',
|
|
297
|
+
'Content-Type': 'application/json',
|
|
298
|
+
'User-Agent': this.userAgent,
|
|
299
|
+
},
|
|
300
|
+
body: JSON.stringify(body),
|
|
301
|
+
signal: controller.signal,
|
|
302
|
+
});
|
|
303
|
+
const text = await res.text();
|
|
304
|
+
try {
|
|
305
|
+
parsed = JSON.parse(text);
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
parsed = { _raw: text };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
finally {
|
|
312
|
+
clearTimeout(timer);
|
|
313
|
+
}
|
|
314
|
+
if (!res.ok) {
|
|
315
|
+
const clean = scrub(parsed, token);
|
|
316
|
+
const detail = typeof clean.detail === 'string' ? clean.detail : '';
|
|
317
|
+
// The Server exchanges a token id once. A token file returns the same
|
|
318
|
+
// token until whatever writes it rotates it, so every CLI run after the
|
|
319
|
+
// first lands here until then.
|
|
320
|
+
const reuseHint = res.status === 409 && this.source.kind === 'file'
|
|
321
|
+
? ` The file at ${this.source.path} still holds a token this Server has already exchanged, and each token can be exchanged once. ` +
|
|
322
|
+
'Run again after the file rotates, or set AGLEDGER_OIDC_TOKEN_CMD to a command that mints a new token each run (for example `kubectl create token <service-account>`).'
|
|
323
|
+
: '';
|
|
324
|
+
throw new OidcExchangeError(`OIDC cert exchange failed (token from ${this.source.origin}): POST /v1/auth/oidc/cert returned ${res.status}${detail ? `: ${detail}` : ''}${reuseHint ? `.${reuseHint}` : ''}`, res.status, clean, this.source.origin);
|
|
325
|
+
}
|
|
326
|
+
const out = parsed;
|
|
327
|
+
if (typeof out.certJws !== 'string' || !out.cert) {
|
|
328
|
+
throw new OidcExchangeError(`OIDC cert exchange failed (token from ${this.source.origin}): the ${res.status} response carried no certJws.`, res.status, scrub(parsed, token), this.source.origin);
|
|
329
|
+
}
|
|
330
|
+
const issued = Date.parse(String(out.cert.issuedAt));
|
|
331
|
+
const expires = Date.parse(String(out.cert.expiresAt));
|
|
332
|
+
const lifetime = Number.isFinite(issued) && Number.isFinite(expires) ? expires - issued : 0;
|
|
333
|
+
// The lifetime is the Server's (expiresAt - issuedAt), timed from local
|
|
334
|
+
// receipt, so a local clock that disagrees with the Server's neither
|
|
335
|
+
// re-exchanges on every request nor holds a cert past its end. A cert
|
|
336
|
+
// whose times do not parse is used for this request and re-exchanged next time.
|
|
337
|
+
const received = this.now();
|
|
338
|
+
this.refreshAt = lifetime > 0 ? received + this.refreshFraction * lifetime : 0;
|
|
339
|
+
this.expiresAt = lifetime > 0 ? received + lifetime : 0;
|
|
340
|
+
this.certJws = out.certJws;
|
|
341
|
+
this.certInfo = out.cert;
|
|
342
|
+
this.exchanges += 1;
|
|
343
|
+
this.log({
|
|
344
|
+
event: 'oidc-exchange',
|
|
345
|
+
reason,
|
|
346
|
+
source: this.source.origin,
|
|
347
|
+
certId: out.cert.id,
|
|
348
|
+
agentId: out.cert.agentId,
|
|
349
|
+
oidcSub: out.cert.oidcSub,
|
|
350
|
+
issuedAt: out.cert.issuedAt,
|
|
351
|
+
expiresAt: out.cert.expiresAt,
|
|
352
|
+
});
|
|
353
|
+
return out.certJws;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* The delegation token for one invocation, read from its source on first use.
|
|
358
|
+
* Unlike the cert exchange, the Server never deduplicates a delegation token,
|
|
359
|
+
* so one is reused until shortly before its `exp` and then read again. A token
|
|
360
|
+
* with no `exp` is read again for every request, since nothing says how long
|
|
361
|
+
* it stays good.
|
|
362
|
+
*/
|
|
363
|
+
export class DelegationToken {
|
|
364
|
+
source;
|
|
365
|
+
now;
|
|
366
|
+
cached = null;
|
|
367
|
+
constructor(source, now = Date.now) {
|
|
368
|
+
this.source = source;
|
|
369
|
+
this.now = now;
|
|
370
|
+
}
|
|
371
|
+
/** The token to send. `reread` skips the cache: the Server just refused the last one. */
|
|
372
|
+
async get(reread = false) {
|
|
373
|
+
if (!reread && this.cached && this.cached.expMs - this.now() > 30_000)
|
|
374
|
+
return this.cached.token;
|
|
375
|
+
const { token, payload } = await fetchOidcToken(this.source, 'delegation');
|
|
376
|
+
const exp = typeof payload.exp === 'number' ? payload.exp * 1000 : null;
|
|
377
|
+
if (exp !== null && exp <= this.now()) {
|
|
378
|
+
this.cached = null;
|
|
379
|
+
throw new OidcTokenSourceError(`${this.source.origin}: the delegation token expired at ${new Date(exp).toISOString()}. Supply a current one.`, this.source.origin, this.source.kind);
|
|
380
|
+
}
|
|
381
|
+
this.cached = exp === null ? null : { token, expMs: exp };
|
|
382
|
+
return token;
|
|
383
|
+
}
|
|
384
|
+
}
|
package/dist/util/config.d.ts
CHANGED
|
@@ -2,9 +2,21 @@
|
|
|
2
2
|
* Profile storage at ~/.agledger/config.json. Credential-sensitive: directory 0700,
|
|
3
3
|
* file 0600, chmodSync belt-and-braces for pre-existing files.
|
|
4
4
|
*/
|
|
5
|
+
/**
|
|
6
|
+
* Where `agledger login --oidc` finds an OIDC token. The source is stored,
|
|
7
|
+
* never a token, cert or key: each exchange calls the source again, because
|
|
8
|
+
* the Server accepts a token id only once.
|
|
9
|
+
*/
|
|
10
|
+
export interface OidcProfileSource {
|
|
11
|
+
tokenCommand?: string;
|
|
12
|
+
tokenFile?: string;
|
|
13
|
+
agentId?: string;
|
|
14
|
+
}
|
|
15
|
+
/** A profile holds an API key or an OIDC token source. */
|
|
5
16
|
export interface Profile {
|
|
6
|
-
apiKey
|
|
17
|
+
apiKey?: string;
|
|
7
18
|
apiUrl?: string;
|
|
19
|
+
oidc?: OidcProfileSource;
|
|
8
20
|
}
|
|
9
21
|
export interface Config {
|
|
10
22
|
profiles: Record<string, Profile>;
|