@soft-boy/agentschema 0.3.2 → 0.3.3
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/package.json +1 -1
- package/src/cred.js +18 -9
- package/src/node.js +2 -0
- package/src/oauth.js +4 -4
package/package.json
CHANGED
package/src/cred.js
CHANGED
|
@@ -51,7 +51,7 @@ export async function credPaste(path, { client, ask, log = () => {} }) {
|
|
|
51
51
|
};
|
|
52
52
|
if (cred.data.auth.type === ENV_VAR) {
|
|
53
53
|
const secretValue = await required(`Value of ${cred.data.auth.secret_name}: `);
|
|
54
|
-
return createCredential({
|
|
54
|
+
return createCredential({ lock, vault, cred, secrets: { secretValue }, client, saveLock: (next) => writeLock(dir, next), log });
|
|
55
55
|
}
|
|
56
56
|
const secrets = { accessToken: await required("Access token: ") };
|
|
57
57
|
const expiresAt = (await ask("Access token expires at (ISO 8601, e.g. 2026-12-31T23:59:59Z; blank if unknown): ", { secret: false })).trim();
|
|
@@ -62,19 +62,20 @@ export async function credPaste(path, { client, ask, log = () => {} }) {
|
|
|
62
62
|
if (needs.refreshToken) secrets.refreshToken = await required("Refresh token: ");
|
|
63
63
|
if (needs.clientSecret) secrets.clientSecret = await required("Client secret: ");
|
|
64
64
|
|
|
65
|
-
return createCredential({
|
|
65
|
+
return createCredential({ lock, vault, cred, secrets, client, saveLock: (next) => writeLock(dir, next), log });
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// Creates a credential with its secrets, creating its vault first if needed, records it in the
|
|
69
|
-
// lock, and asks the API to try it.
|
|
70
|
-
|
|
69
|
+
// lock, and asks the API to try it. `secrets` are what credentialCreateBody takes. Works on a lock
|
|
70
|
+
// object, so a host can keep it wherever it likes, and `saveLock` may be async.
|
|
71
|
+
export async function createCredential({ lock, vault, cred, secrets, client, saveLock = () => {}, log = () => {} }) {
|
|
71
72
|
let vaultId = lock.resources[vaultKey(vault)]?.id;
|
|
72
73
|
if (!vaultId) {
|
|
73
74
|
const body = vaultBody(vault);
|
|
74
75
|
const created = await call({ file: vault.path, kind: "vault" }, () => client.beta.vaults.create(body));
|
|
75
76
|
vaultId = created.id;
|
|
76
77
|
lock.resources[vaultKey(vault)] = { kind: "vault", id: vaultId, hash: hashBody(body) };
|
|
77
|
-
|
|
78
|
+
await saveLock(lock);
|
|
78
79
|
log(`created vault ${vaultId}`);
|
|
79
80
|
}
|
|
80
81
|
|
|
@@ -82,7 +83,7 @@ async function createCredential({ dir, lock, vault, cred }, secrets, { client, l
|
|
|
82
83
|
client.beta.vaults.credentials.create(vaultId, credentialCreateBody(cred, secrets)),
|
|
83
84
|
);
|
|
84
85
|
lock.resources[credentialKey(cred)] = { kind: "credential", id: created.id, vault_id: vaultId, hash: hashBody(credentialSettings(cred)) };
|
|
85
|
-
|
|
86
|
+
await saveLock(lock);
|
|
86
87
|
log(`created credential ${created.id}`);
|
|
87
88
|
|
|
88
89
|
if (cred.data.auth.type === ENV_VAR) return created;
|
|
@@ -172,7 +173,7 @@ export async function credLogin(path, { client, ask, openBrowser, port = 0, fetc
|
|
|
172
173
|
if (existing && existing.vault_id === lock.resources[vaultKey(vault)]?.id) {
|
|
173
174
|
return replaceTokens({ dir, lock, cred, entry: existing }, secrets, { client, log });
|
|
174
175
|
}
|
|
175
|
-
return createCredential({
|
|
176
|
+
return createCredential({ lock, vault, cred, secrets, client, saveLock: (next) => writeLock(dir, next), log });
|
|
176
177
|
} finally {
|
|
177
178
|
await callback.close();
|
|
178
179
|
}
|
|
@@ -230,6 +231,15 @@ export async function credDelete(path, { client, confirm, log = () => {} }) {
|
|
|
230
231
|
log("Cancelled.");
|
|
231
232
|
return;
|
|
232
233
|
}
|
|
234
|
+
await deleteCredential({ lock, cred, client, saveLock: (next) => writeLock(dir, next) });
|
|
235
|
+
log(`deleted credential ${entry.id}`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Permanently deletes a credential, if it has one, and removes it from the lock. One that's already
|
|
239
|
+
// gone counts as deleted. Works on a lock object, like createCredential.
|
|
240
|
+
export async function deleteCredential({ lock, cred, client, saveLock = () => {} }) {
|
|
241
|
+
const entry = lock.resources[credentialKey(cred)];
|
|
242
|
+
if (!entry) return;
|
|
233
243
|
try {
|
|
234
244
|
await client.beta.vaults.credentials.delete(entry.id, { vault_id: entry.vault_id });
|
|
235
245
|
} catch (err) {
|
|
@@ -238,6 +248,5 @@ export async function credDelete(path, { client, confirm, log = () => {} }) {
|
|
|
238
248
|
}
|
|
239
249
|
}
|
|
240
250
|
delete lock.resources[credentialKey(cred)];
|
|
241
|
-
|
|
242
|
-
log(`deleted credential ${entry.id}`);
|
|
251
|
+
await saveLock(lock);
|
|
243
252
|
}
|
package/src/node.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// What agentschema does on a server: anything that needs the API client, hashing or the disk. It
|
|
2
2
|
// works on file maps and lock objects, so a host can keep both in a database.
|
|
3
3
|
export { check } from "./check.js";
|
|
4
|
+
export { createCredential, deleteCredential } from "./cred.js";
|
|
4
5
|
export { DeployError, deploy, deployFiles, prepareLock } from "./deploy.js";
|
|
5
6
|
export { readFolder, updateFrontmatter } from "./folder.js";
|
|
6
7
|
export { LOCK_FILE, hashBody, readLock, skillFingerprint, writeLock } from "./lock.js";
|
|
8
|
+
export { OAuthError, authorizationRequest, discover, exchangeCode, registerClient } from "./oauth.js";
|
|
7
9
|
export { Conversation, SessionError, openSession, startSession } from "./session.js";
|
package/src/oauth.js
CHANGED
|
@@ -117,14 +117,14 @@ export async function startCallbackServer(port = 0) {
|
|
|
117
117
|
|
|
118
118
|
const escape = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
119
119
|
|
|
120
|
-
// Registers
|
|
121
|
-
// Returns { clientId, clientSecret, authMethod }.
|
|
122
|
-
export async function registerClient(registrationEndpoint, redirectUri, { fetch = globalThis.fetch } = {}) {
|
|
120
|
+
// Registers as an OAuth client (RFC 7591), for servers that allow it. `clientName` is what the
|
|
121
|
+
// provider shows on its sign-in page. Returns { clientId, clientSecret, authMethod }.
|
|
122
|
+
export async function registerClient(registrationEndpoint, redirectUri, { fetch = globalThis.fetch, clientName = "agentschema" } = {}) {
|
|
123
123
|
const res = await fetch(registrationEndpoint, {
|
|
124
124
|
method: "POST",
|
|
125
125
|
headers: { "content-type": "application/json", accept: "application/json" },
|
|
126
126
|
body: JSON.stringify({
|
|
127
|
-
client_name:
|
|
127
|
+
client_name: clientName,
|
|
128
128
|
redirect_uris: [redirectUri],
|
|
129
129
|
grant_types: ["authorization_code", "refresh_token"],
|
|
130
130
|
response_types: ["code"],
|