@agentage/cli 0.50.2 → 0.50.4
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.
|
@@ -2,5 +2,5 @@ import { type AuthState } from '../fs/config.js';
|
|
|
2
2
|
import { type Target } from '../net/discovery.js';
|
|
3
3
|
import { type CliFetchOptions, type CliRequestInit } from '../net/fetch.js';
|
|
4
4
|
import { type Links } from '../net/origins.js';
|
|
5
|
-
export declare const revokePlatformPats: (auth: AuthState,
|
|
5
|
+
export declare const revokePlatformPats: (auth: AuthState, links: Links) => Promise<void>;
|
|
6
6
|
export declare const platformFetch: (auth: AuthState, links: Links, found: Target, path: string, init: CliRequestInit, opts: CliFetchOptions) => Promise<Response>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {} from '../fs/config.js';
|
|
2
|
+
import { isRecord } from '../fs/json-file.js';
|
|
2
3
|
import {} from '../net/discovery.js';
|
|
3
4
|
import { cliFetch } from '../net/fetch.js';
|
|
4
5
|
import {} from '../net/origins.js';
|
|
@@ -12,17 +13,28 @@ const MINT = JSON.stringify({
|
|
|
12
13
|
scopes: SCOPES,
|
|
13
14
|
expiresInDays: 30,
|
|
14
15
|
});
|
|
16
|
+
const isPat = (v) => isRecord(v) &&
|
|
17
|
+
[v['id'], v['token']].every((s) => typeof s === 'string' && s !== '') &&
|
|
18
|
+
typeof v['expiresAt'] === 'string' &&
|
|
19
|
+
!Number.isNaN(Date.parse(v['expiresAt']));
|
|
15
20
|
const withBearer = (init, token) => ({
|
|
16
21
|
...init,
|
|
17
22
|
headers: { ...init.headers, authorization: `Bearer ${token}` },
|
|
18
23
|
});
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
// A malformed entry reads as absent: it is re-minted, never sent.
|
|
25
|
+
const storedPat = (auth, issuer) => {
|
|
26
|
+
const pat = auth.platformPats?.[issuer];
|
|
27
|
+
return isPat(pat) ? pat : undefined;
|
|
28
|
+
};
|
|
29
|
+
const revokePat = (auth, links, id) => {
|
|
30
|
+
const url = `${links.auth}/account/pat/${id}`;
|
|
31
|
+
return authedFetch(auth, links, url, { method: 'DELETE' }, { timeoutMs: 5_000 }).then((res) => res.body?.cancel(), () => undefined);
|
|
32
|
+
};
|
|
33
|
+
// Best-effort; only the issuer of the session's bearer ever receives it.
|
|
34
|
+
export const revokePlatformPats = async (auth, links) => {
|
|
35
|
+
const pat = storedPat(auth, links.auth);
|
|
36
|
+
if (pat)
|
|
37
|
+
await revokePat(auth, links, pat.id);
|
|
26
38
|
};
|
|
27
39
|
// Once refused, a run stops asking: one line, no second mint, no fallback to the OAuth bearer.
|
|
28
40
|
const refused = new Map();
|
|
@@ -36,24 +48,28 @@ const refuse = (issuer, reason) => {
|
|
|
36
48
|
const mint = async (auth, links, opts) => {
|
|
37
49
|
const init = { method: 'POST', headers: { 'content-type': 'application/json' }, body: MINT };
|
|
38
50
|
const res = await authedFetch(auth, links, `${links.auth}/account/pat`, init, opts);
|
|
39
|
-
if (res.status
|
|
40
|
-
|
|
41
|
-
|
|
51
|
+
if (res.status !== 201)
|
|
52
|
+
await res.body?.cancel();
|
|
53
|
+
else {
|
|
54
|
+
const reply = await res.json().catch(() => null);
|
|
55
|
+
if (isRecord(reply) && isPat(reply['data']))
|
|
56
|
+
return reply['data'];
|
|
57
|
+
}
|
|
42
58
|
const reason = `platform token refused (${res.status}): auth may be older than ${VERSION}`;
|
|
43
59
|
throw refuse(links.auth, reason);
|
|
44
60
|
};
|
|
45
61
|
// The stored token while it has over a day left, else a new one; the old one is revoked unawaited.
|
|
46
62
|
const tokenFor = async (auth, links, renew, opts) => {
|
|
47
|
-
const old = auth
|
|
63
|
+
const old = storedPat(auth, links.auth);
|
|
48
64
|
if (!renew && old && Date.parse(old.expiresAt) - Date.now() > DAY_MS)
|
|
49
|
-
return
|
|
65
|
+
return { minted: false, token: old.token };
|
|
50
66
|
const { id, token, expiresAt } = await mint(auth, links, opts);
|
|
51
67
|
const pats = { ...auth.platformPats, [links.auth]: { id, token, expiresAt } };
|
|
52
68
|
auth.platformPats = pats;
|
|
53
69
|
await updateSession(auth.clientId, (current) => void (current.platformPats = pats));
|
|
54
70
|
if (old)
|
|
55
|
-
void revokePat(auth
|
|
56
|
-
return token;
|
|
71
|
+
void revokePat(auth, links, old.id);
|
|
72
|
+
return { minted: true, token };
|
|
57
73
|
};
|
|
58
74
|
// A discovered platform takes only a JWT PAT the session mints; an env PAT goes as is.
|
|
59
75
|
export const platformFetch = async (auth, links, found, path, init, opts) => {
|
|
@@ -66,12 +82,11 @@ export const platformFetch = async (auth, links, found, path, init, opts) => {
|
|
|
66
82
|
if (prior)
|
|
67
83
|
throw prior;
|
|
68
84
|
const call = async (renew) => {
|
|
69
|
-
const minted = await tokenFor(auth, links, renew, opts);
|
|
70
|
-
const token = minted ?? auth.platformPats[links.auth].token;
|
|
85
|
+
const { minted, token } = await tokenFor(auth, links, renew, opts);
|
|
71
86
|
return { minted, res: await cliFetch(url, withBearer(init, token), opts) };
|
|
72
87
|
};
|
|
73
88
|
let { minted, res } = await call(false);
|
|
74
|
-
if (res.status === 401 && minted
|
|
89
|
+
if (res.status === 401 && !minted) {
|
|
75
90
|
await res.body?.cancel();
|
|
76
91
|
({ minted, res } = await call(true));
|
|
77
92
|
}
|
package/dist/lib/auth/sign-in.js
CHANGED
|
@@ -42,7 +42,8 @@ const authorize = async (server, state, opts, deps, authUrl) => {
|
|
|
42
42
|
// A fresh DCR client per run: the redirect URI binds this run's ephemeral callback port.
|
|
43
43
|
export const signIn = async (opts, deps = defaultDeps) => {
|
|
44
44
|
const fqdn = siteFqdn();
|
|
45
|
-
const
|
|
45
|
+
const to = await deps.discover(fqdn);
|
|
46
|
+
const authUrl = to.auth;
|
|
46
47
|
ensureConfigDir();
|
|
47
48
|
const state = randomState();
|
|
48
49
|
const server = await deps.startServer(state);
|
|
@@ -54,7 +55,7 @@ export const signIn = async (opts, deps = defaultDeps) => {
|
|
|
54
55
|
const old = readAuth();
|
|
55
56
|
await mutateAuth(() => ({ siteFqdn: fqdn, issuer: authUrl, clientId, tokens }));
|
|
56
57
|
if (old)
|
|
57
|
-
await revokePlatformPats(old, tokens
|
|
58
|
+
await revokePlatformPats({ ...old, clientId, tokens }, to);
|
|
58
59
|
deps.log(chalk.green('\nSigned in.\n'));
|
|
59
60
|
}
|
|
60
61
|
finally {
|
|
@@ -67,7 +68,7 @@ export const signOut = async (deps = defaultDeps) => {
|
|
|
67
68
|
deps.log('Nothing to disconnect.');
|
|
68
69
|
return;
|
|
69
70
|
}
|
|
70
|
-
await revokePlatformPats(auth);
|
|
71
|
+
await revokePlatformPats(auth, credentialLinks(auth));
|
|
71
72
|
await deps.revoke(credentialLinks(auth).auth, auth.tokens.accessToken);
|
|
72
73
|
await deleteAuth();
|
|
73
74
|
deps.log('Disconnected - local credentials removed.');
|
package/dist/lib/fs/config.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface AuthState {
|
|
|
18
18
|
email: string;
|
|
19
19
|
};
|
|
20
20
|
kind?: 'pat';
|
|
21
|
-
platformPats?: Record<string,
|
|
21
|
+
platformPats?: Record<string, unknown>;
|
|
22
22
|
}
|
|
23
23
|
export declare const getConfigDir: () => string;
|
|
24
24
|
export declare const ensureConfigDir: () => string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentage/cli",
|
|
3
|
-
"version": "0.50.
|
|
3
|
+
"version": "0.50.4",
|
|
4
4
|
"description": "The agentage CLI - connect this machine to agentage from the terminal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"prepublishOnly": "npm run verify"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@agentage/extension": "^0.
|
|
58
|
+
"@agentage/extension": "^0.12.1",
|
|
59
59
|
"chalk": "^6.0.0",
|
|
60
60
|
"commander": "^15.0.0",
|
|
61
61
|
"open": "^11.0.0"
|