@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/LICENSE +10 -14
- package/README.md +75 -12
- package/SKILL.md +5 -2
- package/dist/api-client.d.ts +24 -5
- package/dist/api-client.js +85 -16
- package/dist/base.d.ts +83 -28
- package/dist/base.js +194 -34
- package/dist/commands/api.d.ts +1 -0
- 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 +15 -3
- package/dist/commands/login.js +115 -10
- package/dist/commands/logout.d.ts +1 -0
- package/dist/commands/verify.d.ts +7 -0
- package/dist/commands/verify.js +46 -4
- 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 +106 -3
- package/package.json +2 -2
package/dist/commands/login.js
CHANGED
|
@@ -1,36 +1,141 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
1
2
|
import { Flags } from '@oclif/core';
|
|
2
|
-
import {
|
|
3
|
+
import { ApiClient } from '../api-client.js';
|
|
4
|
+
import { BaseCommand, ErrorCode, ExitCode, argvHasFlag } from '../base.js';
|
|
5
|
+
import { OIDC_ENV, fetchOidcToken } from '../oidc.js';
|
|
3
6
|
import { readConfig, writeConfig } from '../util/config.js';
|
|
4
7
|
/**
|
|
5
|
-
* Verify
|
|
6
|
-
* (0600, dir 0700). Supports named profiles so one
|
|
7
|
-
*
|
|
8
|
+
* Verify a credential against the API, then persist it to
|
|
9
|
+
* ~/.agledger/config.json (0600, dir 0700). Supports named profiles so one
|
|
10
|
+
* machine can hold credentials for several Servers.
|
|
11
|
+
*
|
|
12
|
+
* `--oidc` stores an OIDC token SOURCE (a command or a file path), never a
|
|
13
|
+
* token, cert or key. Every later command runs the source again and exchanges
|
|
14
|
+
* the token for a fresh cert held in memory for that one invocation.
|
|
8
15
|
*/
|
|
9
16
|
export default class Login extends BaseCommand {
|
|
10
|
-
static description = 'Verify an API key and store it in ~/.agledger/config.json (0600)';
|
|
17
|
+
static description = 'Verify an API key, or an OIDC token source with --oidc, and store it in ~/.agledger/config.json (0600)';
|
|
18
|
+
static examples = [
|
|
19
|
+
'<%= config.bin %> login --api-url https://agledger.internal --api-key agl_agt_... --profile prod',
|
|
20
|
+
"<%= config.bin %> login --oidc --api-url https://agledger.internal --oidc-token-cmd 'gcloud auth print-identity-token --audiences=agledger'",
|
|
21
|
+
'<%= config.bin %> login --oidc --api-url https://agledger.internal --oidc-token-file /var/run/secrets/tokens/agledger',
|
|
22
|
+
];
|
|
11
23
|
static flags = {
|
|
12
24
|
...BaseCommand.baseFlags,
|
|
13
25
|
profile: Flags.string({ description: 'Profile name', default: 'default' }),
|
|
26
|
+
oidc: Flags.boolean({
|
|
27
|
+
description: 'Store an OIDC token source instead of an API key. A token command is verified by one cert exchange and a GET /v1/auth/me before it is saved. A token file is only checked to hold a JWT: the Server exchanges each token once, so exchanging it here would leave the next command a spent token until the file rotates.',
|
|
28
|
+
default: false,
|
|
29
|
+
}),
|
|
30
|
+
'oidc-token-cmd': Flags.string({
|
|
31
|
+
description: 'With --oidc: a shell command whose stdout is an OIDC JWT, run on every cert exchange.',
|
|
32
|
+
env: OIDC_ENV.TOKEN_CMD,
|
|
33
|
+
}),
|
|
34
|
+
'oidc-token-file': Flags.string({
|
|
35
|
+
description: 'With --oidc: a file holding an OIDC JWT, read on every cert exchange (a projected token in a pod).',
|
|
36
|
+
env: OIDC_ENV.TOKEN_FILE,
|
|
37
|
+
}),
|
|
38
|
+
'oidc-agent-id': Flags.string({
|
|
39
|
+
description: 'With --oidc: the agent the cert binds to, when the token does not map to one itself.',
|
|
40
|
+
env: OIDC_ENV.AGENT_ID,
|
|
41
|
+
}),
|
|
14
42
|
};
|
|
15
43
|
async run() {
|
|
16
44
|
const { flags } = await this.parse(Login);
|
|
45
|
+
if (flags.oidc) {
|
|
46
|
+
await this.loginOidc(flags);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
17
49
|
const apiKey = flags['api-key'];
|
|
18
50
|
if (!apiKey) {
|
|
19
|
-
this.failWith(ErrorCode.AUTH_REQUIRED, 'Provide --api-key or set AGLEDGER_API_KEY.', ExitCode.AUTH_ERROR);
|
|
51
|
+
this.failWith(ErrorCode.AUTH_REQUIRED, 'Provide --api-key or set AGLEDGER_API_KEY.', ExitCode.AUTH_ERROR, 'To store an OIDC token source instead of a key, run `agledger login --oidc`.');
|
|
20
52
|
}
|
|
21
53
|
try {
|
|
22
54
|
const response = await this.callApi(flags, 'GET', '/v1/auth/me');
|
|
23
55
|
if (!response.ok) {
|
|
24
56
|
this.handleApiError(response);
|
|
25
57
|
}
|
|
26
|
-
|
|
27
|
-
config.profiles[flags.profile] = { apiKey: apiKey, apiUrl: flags['api-url'] || undefined };
|
|
28
|
-
config.activeProfile = flags.profile;
|
|
29
|
-
writeConfig(config);
|
|
58
|
+
this.save(flags.profile, { apiKey: apiKey, apiUrl: flags['api-url'] || undefined });
|
|
30
59
|
this.output({ authenticated: true, profile: flags.profile, account: response.body });
|
|
31
60
|
}
|
|
32
61
|
catch (err) {
|
|
33
62
|
this.handleError(err);
|
|
34
63
|
}
|
|
35
64
|
}
|
|
65
|
+
async loginOidc(flags) {
|
|
66
|
+
if (flags['api-key']) {
|
|
67
|
+
this.failWith(ErrorCode.MISSING_INPUT, '--oidc stores an OIDC token source, but an API key was also supplied.', ExitCode.USAGE_ERROR, 'Drop --api-key and unset AGLEDGER_API_KEY, or run `agledger login` without --oidc to store the key.');
|
|
68
|
+
}
|
|
69
|
+
const command = flags['oidc-token-cmd'] || undefined;
|
|
70
|
+
// Stored absolute: later commands run from other directories.
|
|
71
|
+
const file = flags['oidc-token-file'] ? resolve(flags['oidc-token-file']) : undefined;
|
|
72
|
+
if (!command && !file) {
|
|
73
|
+
this.failWith(ErrorCode.AUTH_REQUIRED, '--oidc needs a token source.', ExitCode.AUTH_ERROR, 'Pass --oidc-token-cmd <command> (or set AGLEDGER_OIDC_TOKEN_CMD), or --oidc-token-file <path> (or set AGLEDGER_OIDC_TOKEN_FILE).');
|
|
74
|
+
}
|
|
75
|
+
const apiUrl = flags['api-url'] || undefined;
|
|
76
|
+
if (!apiUrl) {
|
|
77
|
+
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> or set AGLEDGER_API_URL.');
|
|
78
|
+
}
|
|
79
|
+
// The command outranks the file, as it does at call time.
|
|
80
|
+
const source = command
|
|
81
|
+
? { kind: 'command', command, origin: argvHasFlag('--oidc-token-cmd') ? '--oidc-token-cmd' : OIDC_ENV.TOKEN_CMD }
|
|
82
|
+
: { kind: 'file', path: file, origin: argvHasFlag('--oidc-token-file') ? '--oidc-token-file' : OIDC_ENV.TOKEN_FILE };
|
|
83
|
+
const agentId = flags['oidc-agent-id'] || undefined;
|
|
84
|
+
const stored = {
|
|
85
|
+
apiUrl,
|
|
86
|
+
oidc: {
|
|
87
|
+
...(command ? { tokenCommand: command } : { tokenFile: file }),
|
|
88
|
+
...(agentId ? { agentId } : {}),
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
if (source.kind === 'file') {
|
|
92
|
+
// No exchange: the file returns the same token until it rotates, and the
|
|
93
|
+
// Server exchanges a token id once, so a check here would spend the token
|
|
94
|
+
// the next command needs. Confirm it holds a JWT and save.
|
|
95
|
+
try {
|
|
96
|
+
const { sub } = await fetchOidcToken(source);
|
|
97
|
+
this.save(flags.profile, stored);
|
|
98
|
+
this.output({
|
|
99
|
+
saved: true,
|
|
100
|
+
verified: false,
|
|
101
|
+
profile: flags.profile,
|
|
102
|
+
credential: 'oidc-cert',
|
|
103
|
+
tokenSource: 'file',
|
|
104
|
+
oidcSub: sub,
|
|
105
|
+
message: 'The file holds a JWT; it was not exchanged, so the token stays unspent. Run `agledger auth` to exchange it and see the cert.',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
this.handleError(err);
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const credential = this.oidcCredential(flags, { source, ...(agentId ? { agentId } : {}) });
|
|
114
|
+
this.verboseLog(flags, { event: 'auth', credential: 'oidc-cert', source: source.origin, apiUrl });
|
|
115
|
+
try {
|
|
116
|
+
const client = new ApiClient(apiUrl, credential, this.config.version);
|
|
117
|
+
const response = await client.request('GET', '/v1/auth/me');
|
|
118
|
+
if (!response.ok) {
|
|
119
|
+
this.handleApiError(response);
|
|
120
|
+
}
|
|
121
|
+
this.save(flags.profile, stored);
|
|
122
|
+
this.output({
|
|
123
|
+
authenticated: true,
|
|
124
|
+
profile: flags.profile,
|
|
125
|
+
credential: 'oidc-cert',
|
|
126
|
+
tokenSource: 'command',
|
|
127
|
+
cert: credential.cert,
|
|
128
|
+
account: response.body,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
this.handleError(err);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
save(name, profile) {
|
|
136
|
+
const config = readConfig();
|
|
137
|
+
config.profiles[name] = profile;
|
|
138
|
+
config.activeProfile = name;
|
|
139
|
+
writeConfig(config);
|
|
140
|
+
}
|
|
36
141
|
}
|
|
@@ -6,6 +6,7 @@ export default class Logout extends BaseCommand {
|
|
|
6
6
|
quiet: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
7
7
|
'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
8
8
|
'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
9
|
+
verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
9
10
|
profile: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
10
11
|
all: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
11
12
|
};
|
|
@@ -16,6 +16,7 @@ export default class Verify extends BaseCommand {
|
|
|
16
16
|
json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
17
17
|
quiet: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
18
18
|
keys: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
19
|
+
'agent-keys': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
19
20
|
'require-key-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
20
21
|
'require-out-of-band-keys': import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
21
22
|
};
|
|
@@ -29,5 +30,11 @@ export default class Verify extends BaseCommand {
|
|
|
29
30
|
* untouched; verify-core then validates the shape and throws on anything else.
|
|
30
31
|
*/
|
|
31
32
|
private unwrapKeys;
|
|
33
|
+
/**
|
|
34
|
+
* A single JWK, a list of JWKs, or a `{keys: [...]}` JWK Set, as a list. An
|
|
35
|
+
* entry that wraps its key as `{ publicKeyJwk: {...} }` (how an agent records
|
|
36
|
+
* the key it sent at cert exchange) is unwrapped. verify-core validates each.
|
|
37
|
+
*/
|
|
38
|
+
private unwrapAgentKeys;
|
|
32
39
|
private renderHuman;
|
|
33
40
|
}
|
package/dist/commands/verify.js
CHANGED
|
@@ -13,6 +13,7 @@ export default class Verify extends BaseCommand {
|
|
|
13
13
|
static examples = [
|
|
14
14
|
'<%= config.bin %> verify audit-export.json',
|
|
15
15
|
'<%= config.bin %> verify audit-export.json --keys vault-keys.json',
|
|
16
|
+
'<%= config.bin %> verify audit-export.json --agent-keys agent-jwks.json',
|
|
16
17
|
'<%= config.bin %> verify audit-export.json --json',
|
|
17
18
|
'cat audit-export.json | <%= config.bin %> verify -',
|
|
18
19
|
];
|
|
@@ -32,6 +33,13 @@ export default class Verify extends BaseCommand {
|
|
|
32
33
|
'.data array is unwrapped automatically). Merged over any keys embedded ' +
|
|
33
34
|
'in the export.',
|
|
34
35
|
}),
|
|
36
|
+
'agent-keys': Flags.string({
|
|
37
|
+
description: 'Path to a JSON file holding Ed25519 public keys of agent certs: a JWK, a list of JWKs, ' +
|
|
38
|
+
'or a {keys:[...]} JWK Set (an entry may wrap its key as {publicKeyJwk:{...}}). Each is the publicKeyJwk an agent sent at cert exchange ' +
|
|
39
|
+
'(also the cnf.jwk claim in its certJws). An entry whose sealed agent signature names one ' +
|
|
40
|
+
'of them by thumbprint has that signature re-verified offline, and fails ' +
|
|
41
|
+
'CHAIN_AGENT_SIGNATURE_INVALID if it does not verify.',
|
|
42
|
+
}),
|
|
35
43
|
'require-key-id': Flags.string({
|
|
36
44
|
description: 'Require every entry to reference this keyId. Rejects otherwise-valid exports ' +
|
|
37
45
|
'signed by a retired or unexpected key.',
|
|
@@ -53,6 +61,9 @@ export default class Verify extends BaseCommand {
|
|
|
53
61
|
? this.unwrapKeys(this.readJsonSource(flags.keys, 'public keys', 'The --keys file must be a {keyId: SPKI-DER-base64} map or the .data list from ' +
|
|
54
62
|
'`agledger api GET /v1/verification-keys`.'))
|
|
55
63
|
: undefined;
|
|
64
|
+
const agentKeys = flags['agent-keys']
|
|
65
|
+
? this.unwrapAgentKeys(this.readJsonSource(flags['agent-keys'], 'agent keys', 'The --agent-keys file must be an Ed25519 JWK, a list of them, or a {keys:[...]} JWK Set.'))
|
|
66
|
+
: undefined;
|
|
56
67
|
// verify-core throws TypeError at the OOB-key boundary when the file's
|
|
57
68
|
// shape is wrong (e.g. {keyId: 42}, [null], "..."). Catch it so the CLI
|
|
58
69
|
// emits its structured-error envelope rather than oclif's raw exception
|
|
@@ -64,12 +75,14 @@ export default class Verify extends BaseCommand {
|
|
|
64
75
|
publicKeys,
|
|
65
76
|
requireKeyId: flags['require-key-id'],
|
|
66
77
|
requireOutOfBandKeys: flags['require-out-of-band-keys'],
|
|
78
|
+
agentKeys,
|
|
67
79
|
});
|
|
68
80
|
}
|
|
69
81
|
catch (err) {
|
|
70
82
|
if (err instanceof TypeError) {
|
|
71
83
|
this.failWith(ErrorCode.INVALID_JSON_INPUT, err.message, ExitCode.USAGE_ERROR, 'The --keys file must be a {keyId: SPKI-DER-base64} map or a list of ' +
|
|
72
|
-
'{keyId, publicKey, ...} entries (the .data list from /v1/verification-keys)
|
|
84
|
+
'{keyId, publicKey, ...} entries (the .data list from /v1/verification-keys); ' +
|
|
85
|
+
'the --agent-keys file must hold Ed25519 JWKs ({"kty":"OKP","crv":"Ed25519","x":"..."}).');
|
|
73
86
|
}
|
|
74
87
|
throw err;
|
|
75
88
|
}
|
|
@@ -77,7 +90,7 @@ export default class Verify extends BaseCommand {
|
|
|
77
90
|
this.output(result);
|
|
78
91
|
}
|
|
79
92
|
else {
|
|
80
|
-
this.renderHuman(result);
|
|
93
|
+
this.renderHuman(result, agentKeys !== undefined);
|
|
81
94
|
}
|
|
82
95
|
if (!result.valid)
|
|
83
96
|
this.exit(ExitCode.GENERAL_ERROR);
|
|
@@ -99,15 +112,44 @@ export default class Verify extends BaseCommand {
|
|
|
99
112
|
}
|
|
100
113
|
return raw;
|
|
101
114
|
}
|
|
102
|
-
|
|
115
|
+
/**
|
|
116
|
+
* A single JWK, a list of JWKs, or a `{keys: [...]}` JWK Set, as a list. An
|
|
117
|
+
* entry that wraps its key as `{ publicKeyJwk: {...} }` (how an agent records
|
|
118
|
+
* the key it sent at cert exchange) is unwrapped. verify-core validates each.
|
|
119
|
+
*/
|
|
120
|
+
unwrapAgentKeys(raw) {
|
|
121
|
+
const list = Array.isArray(raw)
|
|
122
|
+
? raw
|
|
123
|
+
: raw && typeof raw === 'object' && Array.isArray(raw.keys)
|
|
124
|
+
? raw.keys
|
|
125
|
+
: [raw];
|
|
126
|
+
return list.map((entry) => entry && typeof entry === 'object' && 'publicKeyJwk' in entry
|
|
127
|
+
? entry.publicKeyJwk
|
|
128
|
+
: entry);
|
|
129
|
+
}
|
|
130
|
+
renderHuman(result, agentKeysGiven) {
|
|
103
131
|
if (this.isQuiet)
|
|
104
132
|
return;
|
|
105
133
|
const out = process.stdout;
|
|
106
134
|
const icon = result.valid ? 'PASS' : 'FAIL';
|
|
107
135
|
out.write(`${icon} Record: ${result.recordId}\n`);
|
|
108
136
|
out.write(` Entries: ${result.verifiedEntries}/${result.totalEntries} verified\n`);
|
|
137
|
+
// Agent signatures are checked only against keys the caller supplies, so
|
|
138
|
+
// the lines below say exactly how many were, and the PASS line speaks for
|
|
139
|
+
// the Server's signatures alone.
|
|
140
|
+
const { present, verified } = result.agentSignatures;
|
|
141
|
+
const unchecked = present - verified;
|
|
142
|
+
if (present > 0 && unchecked === 0) {
|
|
143
|
+
out.write(` Agent signatures: ${verified}/${present} re-verified offline against --agent-keys.\n`);
|
|
144
|
+
}
|
|
145
|
+
else if (present > 0 && agentKeysGiven) {
|
|
146
|
+
out.write(` Agent signatures: ${verified}/${present} re-verified offline; ${unchecked} name a key that is not in --agent-keys and were not checked.\n`);
|
|
147
|
+
}
|
|
148
|
+
else if (present > 0) {
|
|
149
|
+
out.write(` Agent signatures: ${present} sealed on the chain, not checked. Pass --agent-keys with the agents' public keys to re-verify them.\n`);
|
|
150
|
+
}
|
|
109
151
|
if (result.valid) {
|
|
110
|
-
out.write(' Hash chain contiguous, every signature verified.\n');
|
|
152
|
+
out.write(' Hash chain contiguous, every Server signature verified.\n');
|
|
111
153
|
return;
|
|
112
154
|
}
|
|
113
155
|
if (result.brokenAt) {
|
package/dist/oidc.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
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
|
+
/** Env vars the CLI reads a token source from. The MCP server reads the same names. */
|
|
13
|
+
export declare const OIDC_ENV: {
|
|
14
|
+
readonly TOKEN_CMD: 'AGLEDGER_OIDC_TOKEN_CMD';
|
|
15
|
+
readonly TOKEN_FILE: 'AGLEDGER_OIDC_TOKEN_FILE';
|
|
16
|
+
readonly AGENT_ID: 'AGLEDGER_OIDC_AGENT_ID';
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Env vars naming an RFC 8693 delegation token, sent as `AGLedger-On-Behalf-Of`
|
|
20
|
+
* when the work is done for a person or another party. The MCP server reads
|
|
21
|
+
* the same names.
|
|
22
|
+
*/
|
|
23
|
+
export declare const ON_BEHALF_OF_ENV: {
|
|
24
|
+
readonly CMD: 'AGLEDGER_ON_BEHALF_OF_CMD';
|
|
25
|
+
readonly FILE: 'AGLEDGER_ON_BEHALF_OF_FILE';
|
|
26
|
+
};
|
|
27
|
+
export type OidcTokenSource = {
|
|
28
|
+
kind: 'command';
|
|
29
|
+
command: string; /** Where the command came from, for errors and --verbose. */
|
|
30
|
+
origin: string;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'file';
|
|
33
|
+
path: string;
|
|
34
|
+
origin: string;
|
|
35
|
+
};
|
|
36
|
+
/** The token source could not produce a token. `origin` names the variable or profile. */
|
|
37
|
+
export declare class OidcTokenSourceError extends Error {
|
|
38
|
+
readonly origin: string;
|
|
39
|
+
readonly kind: OidcTokenSource['kind'];
|
|
40
|
+
constructor(message: string, origin: string, kind: OidcTokenSource['kind']);
|
|
41
|
+
}
|
|
42
|
+
/** The Server refused the exchange. `body` is its error response with the token scrubbed out. */
|
|
43
|
+
export declare class OidcExchangeError extends Error {
|
|
44
|
+
readonly status: number;
|
|
45
|
+
readonly body: unknown;
|
|
46
|
+
readonly origin: string;
|
|
47
|
+
constructor(message: string, status: number, body: unknown, origin: string);
|
|
48
|
+
}
|
|
49
|
+
/** The `cert` object from a 201 exchange response. Carried verbatim; the CLI reads only the times. */
|
|
50
|
+
export type CertInfo = Record<string, unknown> & {
|
|
51
|
+
issuedAt?: string;
|
|
52
|
+
expiresAt?: string;
|
|
53
|
+
};
|
|
54
|
+
export interface OidcCertCredentialOptions {
|
|
55
|
+
source: OidcTokenSource;
|
|
56
|
+
agentId?: string;
|
|
57
|
+
/** Re-exchange once this fraction of the cert's lifetime has passed. Default 0.5. */
|
|
58
|
+
refreshFraction?: number;
|
|
59
|
+
/** Diagnostics sink for --verbose. Never receives a token, cert or key. */
|
|
60
|
+
log?: (event: Record<string, unknown>) => void;
|
|
61
|
+
/** Injectable for tests. */
|
|
62
|
+
now?: () => number;
|
|
63
|
+
fetch?: typeof fetch;
|
|
64
|
+
userAgent?: string;
|
|
65
|
+
timeoutMs?: number;
|
|
66
|
+
}
|
|
67
|
+
/** Bounds on a token command. Mutable so tests can shorten the timeout. */
|
|
68
|
+
export declare const TOKEN_COMMAND_LIMITS: {
|
|
69
|
+
timeoutMs: number;
|
|
70
|
+
/** A JWT is a few KiB at most; anything past this is not one token. */
|
|
71
|
+
stdoutBytes: number;
|
|
72
|
+
};
|
|
73
|
+
/** Replace anything shaped like a JWT with a placeholder. Token command stderr
|
|
74
|
+
* and Server error bodies are surfaced to the user, and either can carry one. */
|
|
75
|
+
export declare function redactTokens(text: string): string;
|
|
76
|
+
/** Deep-copy a JSON value with every string passed through `redactTokens`,
|
|
77
|
+
* and any string equal to or containing `token` replaced. The Server's
|
|
78
|
+
* validation errors echo the offending input back, which for this route is
|
|
79
|
+
* the OIDC token. */
|
|
80
|
+
export declare function scrub(value: unknown, token?: string): unknown;
|
|
81
|
+
/** What a token is for, so a missing `sub` is explained in the right terms. */
|
|
82
|
+
export type TokenPurpose = 'cert-exchange' | 'delegation';
|
|
83
|
+
/** Call the token source once and return a validated compact JWT plus its `sub` and payload. */
|
|
84
|
+
export declare function fetchOidcToken(source: OidcTokenSource, purpose?: TokenPurpose): Promise<{
|
|
85
|
+
token: string;
|
|
86
|
+
sub: string;
|
|
87
|
+
payload: Record<string, unknown>;
|
|
88
|
+
}>;
|
|
89
|
+
/**
|
|
90
|
+
* One cert credential. Generates its key pair on construction, exchanges on
|
|
91
|
+
* first use, re-exchanges past the refresh point or when forced (a 401), and
|
|
92
|
+
* shares one in-flight exchange between concurrent callers.
|
|
93
|
+
*/
|
|
94
|
+
export declare class OidcCertCredential {
|
|
95
|
+
readonly source: OidcTokenSource;
|
|
96
|
+
private readonly agentId;
|
|
97
|
+
private readonly refreshFraction;
|
|
98
|
+
private readonly log;
|
|
99
|
+
private readonly now;
|
|
100
|
+
private readonly fetchImpl;
|
|
101
|
+
private readonly userAgent;
|
|
102
|
+
private readonly timeoutMs;
|
|
103
|
+
private readonly privateKey;
|
|
104
|
+
private readonly publicKeyX;
|
|
105
|
+
private certJws;
|
|
106
|
+
private certInfo;
|
|
107
|
+
/** Local clock times, from when the cert was received: the Server's clock may differ. */
|
|
108
|
+
private refreshAt;
|
|
109
|
+
private expiresAt;
|
|
110
|
+
private inflight;
|
|
111
|
+
private exchanges;
|
|
112
|
+
constructor(options: OidcCertCredentialOptions);
|
|
113
|
+
/** The cert from the most recent exchange, as the Server returned it. */
|
|
114
|
+
get cert(): CertInfo | null;
|
|
115
|
+
/** How many exchanges this credential has performed. */
|
|
116
|
+
get exchangeCount(): number;
|
|
117
|
+
/**
|
|
118
|
+
* A bearer for the next request: the cached cert, or a fresh one when none
|
|
119
|
+
* is held or the refresh point has passed. Pass `rejected` (the bearer the
|
|
120
|
+
* Server just refused with 401) to force a re-exchange; when a concurrent
|
|
121
|
+
* request has already replaced that cert, the replacement is returned
|
|
122
|
+
* instead of exchanging again.
|
|
123
|
+
*/
|
|
124
|
+
getToken(baseUrl: string, rejected?: string): Promise<string>;
|
|
125
|
+
/** The agent signature headers for a request body, over the exact bytes sent. */
|
|
126
|
+
signBody(body: string): Record<string, string>;
|
|
127
|
+
private exchange;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The delegation token for one invocation, read from its source on first use.
|
|
131
|
+
* Unlike the cert exchange, the Server never deduplicates a delegation token,
|
|
132
|
+
* so one is reused until shortly before its `exp` and then read again. A token
|
|
133
|
+
* with no `exp` is read again for every request, since nothing says how long
|
|
134
|
+
* it stays good.
|
|
135
|
+
*/
|
|
136
|
+
export declare class DelegationToken {
|
|
137
|
+
readonly source: OidcTokenSource;
|
|
138
|
+
private readonly now;
|
|
139
|
+
private cached;
|
|
140
|
+
constructor(source: OidcTokenSource, now?: () => number);
|
|
141
|
+
/** The token to send. `reread` skips the cache: the Server just refused the last one. */
|
|
142
|
+
get(reread?: boolean): Promise<string>;
|
|
143
|
+
}
|