@agentic-kit/session 0.0.0-stage → 0.2.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 +23 -0
- package/README.md +207 -2
- package/account-store.d.ts +78 -0
- package/account-store.js +162 -0
- package/agent-store.d.ts +79 -0
- package/agent-store.js +251 -0
- package/agent.d.ts +137 -0
- package/agent.js +514 -0
- package/api-key.d.ts +53 -0
- package/api-key.js +60 -0
- package/app-session.d.ts +48 -0
- package/app-session.js +105 -0
- package/auth-error.d.ts +54 -0
- package/auth-error.js +186 -0
- package/auth.d.ts +169 -0
- package/auth.js +514 -0
- package/backend-store.d.ts +134 -0
- package/backend-store.js +259 -0
- package/env.d.ts +9 -0
- package/env.js +10 -0
- package/esm/account-store.d.ts +78 -0
- package/esm/account-store.js +146 -0
- package/esm/agent-store.d.ts +79 -0
- package/esm/agent-store.js +201 -0
- package/esm/agent.d.ts +137 -0
- package/esm/agent.js +503 -0
- package/esm/api-key.d.ts +53 -0
- package/esm/api-key.js +52 -0
- package/esm/app-session.d.ts +48 -0
- package/esm/app-session.js +66 -0
- package/esm/auth-error.d.ts +54 -0
- package/esm/auth-error.js +171 -0
- package/esm/auth.d.ts +169 -0
- package/esm/auth.js +467 -0
- package/esm/backend-store.d.ts +134 -0
- package/esm/backend-store.js +239 -0
- package/esm/env.d.ts +9 -0
- package/esm/env.js +6 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +11 -0
- package/esm/operation.d.ts +45 -0
- package/esm/operation.js +87 -0
- package/esm/session.d.ts +192 -0
- package/esm/session.js +297 -0
- package/esm/tools-host.d.ts +29 -0
- package/esm/tools-host.js +71 -0
- package/index.d.ts +12 -0
- package/index.js +63 -0
- package/operation.d.ts +45 -0
- package/operation.js +92 -0
- package/package.json +43 -4
- package/session.d.ts +192 -0
- package/session.js +307 -0
- package/tools-host.d.ts +29 -0
- package/tools-host.js +74 -0
package/auth.js
ADDED
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.SecondFactorRequired = void 0;
|
|
37
|
+
exports.proofSignIn = proofSignIn;
|
|
38
|
+
exports.passwordSignIn = passwordSignIn;
|
|
39
|
+
exports.endSignIn = endSignIn;
|
|
40
|
+
exports.signIn = signIn;
|
|
41
|
+
exports.saveSignInChallenge = saveSignInChallenge;
|
|
42
|
+
exports.loadSignInChallenge = loadSignInChallenge;
|
|
43
|
+
exports.clearSignInChallenge = clearSignInChallenge;
|
|
44
|
+
exports.signInTarget = signInTarget;
|
|
45
|
+
exports.signOut = signOut;
|
|
46
|
+
exports.verifySession = verifySession;
|
|
47
|
+
const sdk_1 = require("@constructive-io/sdk");
|
|
48
|
+
const fs = __importStar(require("fs"));
|
|
49
|
+
const path = __importStar(require("path"));
|
|
50
|
+
const account_store_1 = require("./account-store");
|
|
51
|
+
const agent_store_1 = require("./agent-store");
|
|
52
|
+
const api_key_1 = require("./api-key");
|
|
53
|
+
const auth_error_1 = require("./auth-error");
|
|
54
|
+
const backend_store_1 = require("./backend-store");
|
|
55
|
+
const operation_1 = require("./operation");
|
|
56
|
+
const session_1 = require("./session");
|
|
57
|
+
const SELECT = {
|
|
58
|
+
result: {
|
|
59
|
+
select: { userId: true, accessToken: true, accessTokenExpiresAt: true, mfaRequired: true, mfaChallengeToken: true, totpEnabled: true }
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const CREATE_KEY_SELECT = {
|
|
63
|
+
result: { select: { apiKey: true, keyId: true, expiresAt: true } }
|
|
64
|
+
};
|
|
65
|
+
const REVOKE_KEY_SELECT = { result: true };
|
|
66
|
+
const NO_ISSUER = 'The session did not record the auth endpoint that issued it, so its key was not sent anywhere to be revoked; it stays valid until it expires.';
|
|
67
|
+
class UnconfirmedKeyMint extends Error {
|
|
68
|
+
timeout;
|
|
69
|
+
cleanup;
|
|
70
|
+
constructor(timeout, cleanup) {
|
|
71
|
+
super(timeout.message);
|
|
72
|
+
this.timeout = timeout;
|
|
73
|
+
this.cleanup = cleanup;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Talks to one auth endpoint with one bearer. */
|
|
77
|
+
function keyClient(endpoint, bearer) {
|
|
78
|
+
const client = sdk_1.auth.createClient({ endpoint, headers: { Authorization: `Bearer ${bearer}` }, fetch: (0, auth_error_1.trackedFetch)(endpoint).fetch });
|
|
79
|
+
return {
|
|
80
|
+
async mint() {
|
|
81
|
+
const request = client.mutation
|
|
82
|
+
.createApiKey({ input: (0, api_key_1.buildCreateApiKeyInput)({ days: api_key_1.API_KEY_DAYS }) }, { select: CREATE_KEY_SELECT })
|
|
83
|
+
.unwrap();
|
|
84
|
+
let data;
|
|
85
|
+
try {
|
|
86
|
+
data = await (0, auth_error_1.withAuthTimeout)(request, endpoint);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (!(error instanceof auth_error_1.AuthTimeoutError))
|
|
90
|
+
throw error;
|
|
91
|
+
const late = await (0, auth_error_1.withAuthTimeout)(request, endpoint).catch(() => undefined);
|
|
92
|
+
throw new UnconfirmedKeyMint(error, await (0, api_key_1.revokeKey)(late?.createApiKey?.result?.keyId, this.revoke));
|
|
93
|
+
}
|
|
94
|
+
return (0, api_key_1.parseMintedKey)(data?.createApiKey?.result);
|
|
95
|
+
},
|
|
96
|
+
async revoke(keyId) {
|
|
97
|
+
let data;
|
|
98
|
+
try {
|
|
99
|
+
data = (await (0, auth_error_1.withAuthTimeout)(client.mutation.revokeApiKey({ input: { keyId } }, { select: REVOKE_KEY_SELECT }).unwrap(), endpoint));
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
throw new Error((0, auth_error_1.describeAuthError)(err));
|
|
103
|
+
}
|
|
104
|
+
if (data?.revokeApiKey?.result !== true)
|
|
105
|
+
throw new Error('the server did not confirm that the key was revoked');
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Where a stored session's key is revoked: the auth endpoint that issued it, as recorded (`issuedFor`, else Desktop's
|
|
111
|
+
* `authEndpoint`), checked like any endpoint before a bearer goes there. A session that records only that endpoint
|
|
112
|
+
* still names its issuer, so its own token may go back there, though it is never used for anything else.
|
|
113
|
+
*/
|
|
114
|
+
function issuerOf(session) {
|
|
115
|
+
const recorded = session.issuedFor?.authEndpoint ?? session.authEndpoint;
|
|
116
|
+
if (!recorded)
|
|
117
|
+
return { kind: 'unknown', reason: NO_ISSUER };
|
|
118
|
+
try {
|
|
119
|
+
return { kind: 'known', endpoint: (0, backend_store_1.checkedEndpoint)('auth', recorded) };
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
if (!(err instanceof backend_store_1.EndpointError))
|
|
123
|
+
throw err;
|
|
124
|
+
return { kind: 'unknown', reason: `${err.message} So its key was not sent there to be revoked; it stays valid until it expires, unless you revoke it in the app.` };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Revoke a stored session's key at its issuer with the session's own bearer; never throws. */
|
|
128
|
+
async function revokeStoredKey(session) {
|
|
129
|
+
if (!session.keyId)
|
|
130
|
+
return { kind: 'absent' };
|
|
131
|
+
const issuer = issuerOf(session);
|
|
132
|
+
if (issuer.kind === 'unknown')
|
|
133
|
+
return { kind: 'failed', keyId: session.keyId, reason: issuer.reason };
|
|
134
|
+
return (0, api_key_1.revokeKey)(session.keyId, keyClient(issuer.endpoint, session.apiKey ?? session.accessToken).revoke);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The stored bearer is the key: the password sign-in that made it is ended
|
|
138
|
+
* on the server once the key exists, so its token works nowhere.
|
|
139
|
+
*/
|
|
140
|
+
const withKey = (session, key) => ({
|
|
141
|
+
...session,
|
|
142
|
+
accessToken: key.apiKey,
|
|
143
|
+
accessTokenExpiresAt: key.apiKeyExpiresAt,
|
|
144
|
+
apiKey: key.apiKey,
|
|
145
|
+
keyId: key.keyId,
|
|
146
|
+
apiKeyExpiresAt: key.apiKeyExpiresAt
|
|
147
|
+
});
|
|
148
|
+
/** The backend accepted the password, and the account has a second factor: `challenge` waits for its code. */
|
|
149
|
+
class SecondFactorRequired extends auth_error_1.SignInError {
|
|
150
|
+
challenge;
|
|
151
|
+
constructor(challenge) {
|
|
152
|
+
super('second-factor-required', `The account ${challenge.email} needs the code of its second factor to finish the sign-in.`);
|
|
153
|
+
this.challenge = challenge;
|
|
154
|
+
this.name = 'SecondFactorRequired';
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
exports.SecondFactorRequired = SecondFactorRequired;
|
|
158
|
+
// The methods the backend sends a code by, in the order a sign-in tries them.
|
|
159
|
+
const SENT_METHODS = ['email', 'sms'];
|
|
160
|
+
/**
|
|
161
|
+
* Start the second factor of a challenged sign-in: an authenticator app needs nothing sent, else the backend sends a
|
|
162
|
+
* code by the first method the account has on. An account with none of them (a security key only) can't finish here.
|
|
163
|
+
*/
|
|
164
|
+
async function startChallenge(authEndpoint, waiting, totp) {
|
|
165
|
+
if (totp)
|
|
166
|
+
return { ...waiting, method: 'totp' };
|
|
167
|
+
const client = sdk_1.auth.createClient({ endpoint: authEndpoint, fetch: (0, auth_error_1.trackedFetch)(authEndpoint).fetch });
|
|
168
|
+
for (const method of SENT_METHODS) {
|
|
169
|
+
const request = client.mutation
|
|
170
|
+
.sendMfaChallengeCode({ input: { userId: waiting.userId, mfaChallengeToken: waiting.token, mfaMethod: method } }, { select: { result: true } })
|
|
171
|
+
.unwrap();
|
|
172
|
+
let sent;
|
|
173
|
+
try {
|
|
174
|
+
sent = (await (0, auth_error_1.withAuthTimeout)(request, authEndpoint))?.sendMfaChallengeCode?.result;
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
if ((0, auth_error_1.errorCodes)(err).includes('MFA_METHOD_NOT_ENABLED'))
|
|
178
|
+
continue;
|
|
179
|
+
throw (0, auth_error_1.signInFailure)(err, authEndpoint, undefined);
|
|
180
|
+
}
|
|
181
|
+
if (sent !== true) {
|
|
182
|
+
// The backend answers false, not an error, to a resend within its cooldown, a locked account or network, and an unverified address.
|
|
183
|
+
throw new auth_error_1.SignInError('refused', `The server at ${authEndpoint} sent no new code for the second factor: it sends none shortly after the last one, and none while the account or this network is locked out. Wait a little, then sign in with the password again.`, 'MFA_CODE_NOT_SENT');
|
|
184
|
+
}
|
|
185
|
+
return { ...waiting, method };
|
|
186
|
+
}
|
|
187
|
+
throw new auth_error_1.SignInError('second-factor-unsupported', `The account ${waiting.email} needs a second factor that gives no code to type, such as a security key, so it can't sign in here. Sign in through the app, or turn on email codes or an authenticator app for the account.`);
|
|
188
|
+
}
|
|
189
|
+
/** Finish a challenged sign-in with its code, which writes nothing. Throws a `SignInError`. */
|
|
190
|
+
async function completeChallenge(authEndpoint, challenge, code) {
|
|
191
|
+
const http = (0, auth_error_1.trackedFetch)(authEndpoint);
|
|
192
|
+
const request = sdk_1.auth.createClient({ endpoint: authEndpoint, fetch: http.fetch }).mutation
|
|
193
|
+
.completeMfaChallenge(
|
|
194
|
+
// No method for an authenticator app, so a backup code works there too.
|
|
195
|
+
{ input: { userId: challenge.userId, mfaChallengeToken: challenge.token, totpCode: code, ...(challenge.method === 'totp' ? {} : { mfaMethod: challenge.method }) } }, { select: { result: { select: { outUserId: true, accessToken: true, accessTokenExpiresAt: true, deviceApprovalRequired: true } } } })
|
|
196
|
+
.unwrap();
|
|
197
|
+
let record;
|
|
198
|
+
try {
|
|
199
|
+
record = (await (0, auth_error_1.withAuthTimeout)(request, authEndpoint))?.completeMfaChallenge?.result;
|
|
200
|
+
}
|
|
201
|
+
catch (err) {
|
|
202
|
+
const failure = (0, auth_error_1.signInFailure)(err, authEndpoint, http.status());
|
|
203
|
+
if (failure.code === 'INVALID_MFA_CHALLENGE' || failure.code === 'MFA_CHALLENGE_EXPIRED') {
|
|
204
|
+
throw new auth_error_1.SignInError('refused', `The sign-in of ${challenge.email} that waited for a code has ended: it lasts 10 minutes, and a code finishes it once. Sign in with the password again.`, failure.code);
|
|
205
|
+
}
|
|
206
|
+
throw failure;
|
|
207
|
+
}
|
|
208
|
+
if (record?.deviceApprovalRequired) {
|
|
209
|
+
throw new auth_error_1.SignInError('refused', `The server at ${authEndpoint} accepted the code, but this device must be approved before it signs in. Approve it in the app, then sign in again.`);
|
|
210
|
+
}
|
|
211
|
+
if (!record?.accessToken || record.outUserId !== challenge.userId) {
|
|
212
|
+
throw new auth_error_1.SignInError('invalid-credentials', `The server at ${authEndpoint} did not accept the code. Check it and try again within 10 minutes of the sign-in, or sign in with the password again for a new code.`);
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
userId: challenge.userId,
|
|
216
|
+
email: challenge.email,
|
|
217
|
+
accessToken: record.accessToken,
|
|
218
|
+
...(record.accessTokenExpiresAt ? { accessTokenExpiresAt: record.accessTokenExpiresAt } : {})
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* The sign-in `proof` gives at `authEndpoint`, which writes nothing: a password sign-in, or the code that finishes the
|
|
223
|
+
* challenge one started there for `email`. Throws a `SignInError`; `SecondFactorRequired` when the password needs a code.
|
|
224
|
+
*/
|
|
225
|
+
async function proofSignIn(authEndpoint, email, proof) {
|
|
226
|
+
if ('password' in proof)
|
|
227
|
+
return passwordSignIn(authEndpoint, email, proof.password);
|
|
228
|
+
const { challenge } = proof;
|
|
229
|
+
const code = proof.code.trim();
|
|
230
|
+
if (challenge.authEndpoint !== authEndpoint || challenge.email !== email.trim()) {
|
|
231
|
+
throw new auth_error_1.SignInError('invalid-input', `The code answers a sign-in of ${challenge.email} at ${challenge.authEndpoint}, not this one, so it was not sent. Sign in with the password again.`);
|
|
232
|
+
}
|
|
233
|
+
if (!code)
|
|
234
|
+
throw new auth_error_1.SignInError('invalid-input', 'The code of the second factor is required.');
|
|
235
|
+
return completeChallenge(authEndpoint, challenge, code);
|
|
236
|
+
}
|
|
237
|
+
/** One password sign-in at `authEndpoint`, which writes nothing. Throws a `SignInError`; `SecondFactorRequired` when it needs a code. */
|
|
238
|
+
async function passwordSignIn(authEndpoint, email, password) {
|
|
239
|
+
const trimmedEmail = email.trim();
|
|
240
|
+
if (!trimmedEmail || !password)
|
|
241
|
+
throw new auth_error_1.SignInError('invalid-input', 'Email and password are required.');
|
|
242
|
+
const http = (0, auth_error_1.trackedFetch)(authEndpoint);
|
|
243
|
+
const request = sdk_1.auth.createClient({ endpoint: authEndpoint, fetch: http.fetch }).mutation
|
|
244
|
+
.signIn({ input: { email: trimmedEmail, password } }, { select: SELECT })
|
|
245
|
+
.unwrap();
|
|
246
|
+
let record;
|
|
247
|
+
try {
|
|
248
|
+
record = (await (0, auth_error_1.withAuthTimeout)(request, authEndpoint))?.signIn?.result;
|
|
249
|
+
}
|
|
250
|
+
catch (err) {
|
|
251
|
+
throw (0, auth_error_1.signInFailure)(err, authEndpoint, http.status());
|
|
252
|
+
}
|
|
253
|
+
if (record?.mfaRequired) {
|
|
254
|
+
if (!record.userId || !record.mfaChallengeToken) {
|
|
255
|
+
throw new auth_error_1.SignInError('server-error', `The server at ${authEndpoint} asked for a second factor, but sent no challenge to answer. Retry later, or check the backend logs.`);
|
|
256
|
+
}
|
|
257
|
+
throw new SecondFactorRequired(await startChallenge(authEndpoint, { authEndpoint, email: trimmedEmail, userId: record.userId, token: record.mfaChallengeToken }, record.totpEnabled === true));
|
|
258
|
+
}
|
|
259
|
+
if (!record?.accessToken || !record.userId) {
|
|
260
|
+
throw new auth_error_1.SignInError('invalid-credentials', `The server at ${authEndpoint} rejected the email or the password.`);
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
userId: record.userId,
|
|
264
|
+
email: trimmedEmail,
|
|
265
|
+
accessToken: record.accessToken,
|
|
266
|
+
...(record.accessTokenExpiresAt ? { accessTokenExpiresAt: record.accessTokenExpiresAt } : {})
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* End a password sign-in's session on the server, so no bearer session is
|
|
271
|
+
* left behind. Best effort: if the server cannot be reached, the session ends
|
|
272
|
+
* at its own expiry, and the caller's work stands.
|
|
273
|
+
*/
|
|
274
|
+
async function endSignIn(authEndpoint, accessToken) {
|
|
275
|
+
const client = sdk_1.auth.createClient({ endpoint: authEndpoint, headers: { Authorization: `Bearer ${accessToken}` }, fetch: (0, auth_error_1.trackedFetch)(authEndpoint).fetch });
|
|
276
|
+
await (0, auth_error_1.withAuthTimeout)(client.mutation.signOut({ input: {} }, { select: { clientMutationId: true } }).unwrap(), authEndpoint).catch(() => undefined);
|
|
277
|
+
}
|
|
278
|
+
/** The sign-in worked, but no key could be made, so nothing is stored. */
|
|
279
|
+
function noKeyFailure(err, authEndpoint) {
|
|
280
|
+
if (err instanceof UnconfirmedKeyMint) {
|
|
281
|
+
const { cleanup, timeout } = err;
|
|
282
|
+
const guidance = cleanup.kind === 'revoked' ? 'The late key was revoked. Retry the sign-in.'
|
|
283
|
+
: cleanup.kind === 'failed' ? `Revoke key ${cleanup.keyId} in the app before retrying.`
|
|
284
|
+
: 'Check for a new agentic-cli API key in the app and revoke it before retrying.';
|
|
285
|
+
return new auth_error_1.SignInError('backend-unreachable', `Sign-in worked, but key creation timed out (${timeout.message}) and nothing was stored. ${guidance}`);
|
|
286
|
+
}
|
|
287
|
+
const why = `The sign-in to ${authEndpoint} worked, but no API key could be made (${(0, auth_error_1.describeAuthError)(err)}), so nothing was stored and the sign-in was ended.`;
|
|
288
|
+
if ((0, auth_error_1.isNetworkError)(err))
|
|
289
|
+
return new auth_error_1.SignInError('backend-unreachable', `${why} Retry the sign-in.`);
|
|
290
|
+
if (err instanceof auth_error_1.RedirectRefusedError)
|
|
291
|
+
return new auth_error_1.SignInError('server-error', why, `HTTP_${err.status}`);
|
|
292
|
+
const code = (0, auth_error_1.errorCodes)(err)[0];
|
|
293
|
+
if (code === 'API_KEY_LIMIT_REACHED') {
|
|
294
|
+
return new auth_error_1.SignInError('refused', `${why} The account holds as many live API keys as the backend allows: sign out of this backend first, which revokes the key this computer holds, or revoke a key you no longer use in the app, then sign in again.`, code);
|
|
295
|
+
}
|
|
296
|
+
if (code)
|
|
297
|
+
return new auth_error_1.SignInError('refused', `${why} Revoke an API key you no longer use in the app, or check that API keys are on, then sign in again.`, code);
|
|
298
|
+
return new auth_error_1.SignInError('server-error', `${why} Retry later, or check the backend logs.`);
|
|
299
|
+
}
|
|
300
|
+
/** A sign-in that failed before its commit, in the caller's terms: the store is as it was, and `undone` says what was given back. */
|
|
301
|
+
function signInFailed({ step, cause, undone }, authEndpoint, context) {
|
|
302
|
+
const tail = (0, operation_1.undoneText)(undone);
|
|
303
|
+
const withTail = (message) => (tail ? `${message} ${tail}` : message);
|
|
304
|
+
switch (step) {
|
|
305
|
+
case 'sign in':
|
|
306
|
+
return cause instanceof Error ? cause : new Error((0, api_key_1.errorText)(cause));
|
|
307
|
+
case 'make an API key': {
|
|
308
|
+
const failure = noKeyFailure(cause, authEndpoint);
|
|
309
|
+
return new auth_error_1.SignInError(failure.kind, withTail(failure.message), failure.code);
|
|
310
|
+
}
|
|
311
|
+
default:
|
|
312
|
+
return new Error(withTail(`Storing the sign-in under the context "${context}" failed (${(0, api_key_1.errorText)(cause)}), so nothing was stored. Check that the store can be written, then sign in again.`), { cause });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Sign in to `backend` with email and password, and mint the person's 30-day API key inside the fresh step-up
|
|
317
|
+
* window, as one operation (`Operation`):
|
|
318
|
+
*
|
|
319
|
+
* check the store -> sign in [undo: end it] -> make a key [undo: revoke it] -> store the endpoints [undo: put
|
|
320
|
+
* the old ones back] -> choose the context [undo: choose the old one] -> COMMIT: store the session
|
|
321
|
+
* -> revoke the replaced key -> end the password sign-in
|
|
322
|
+
*
|
|
323
|
+
* So it stores a new session with a new key, or changes nothing, and a key is revoked only after its replacement is
|
|
324
|
+
* stored. A context the session cannot be filed under is refused before anything is sent (`ContextInUseError`). A
|
|
325
|
+
* failed sign-in, or one that makes no key, throws a `SignInError`.
|
|
326
|
+
*/
|
|
327
|
+
async function signIn({ store, backend, context = (0, backend_store_1.contextNameFor)(backend), email, ...proof }) {
|
|
328
|
+
const { authEndpoint } = backend;
|
|
329
|
+
const conflict = (0, backend_store_1.backendConflict)(store, backend, context, { replacing: true });
|
|
330
|
+
if (conflict)
|
|
331
|
+
throw conflict;
|
|
332
|
+
const op = new operation_1.Operation((failure) => signInFailed(failure, authEndpoint, context));
|
|
333
|
+
const signedIn = await op.step('sign in', () => proofSignIn(authEndpoint, email, proof), {
|
|
334
|
+
run: (fresh) => endSignIn(authEndpoint, fresh.accessToken),
|
|
335
|
+
undone: '',
|
|
336
|
+
notUndone: () => ''
|
|
337
|
+
});
|
|
338
|
+
const issuer = keyClient(authEndpoint, signedIn.accessToken);
|
|
339
|
+
const minted = await op.step('make an API key', () => issuer.mint(), {
|
|
340
|
+
run: (key) => issuer.revoke(key.keyId),
|
|
341
|
+
undone: 'The new key was revoked again.',
|
|
342
|
+
notUndone: (key, reason) => `The new key ${key.keyId} could not be revoked (${reason}); revoke it in the app.`
|
|
343
|
+
});
|
|
344
|
+
const storedContext = store.loadContext(context);
|
|
345
|
+
await op.step('store the endpoints', () => (0, backend_store_1.writeBackend)(store, backend, context), {
|
|
346
|
+
run: () => (0, backend_store_1.restoreBackend)(store, context, storedContext),
|
|
347
|
+
undone: '',
|
|
348
|
+
notUndone: (_value, reason) => `The old endpoints of "${context}" could not be put back (${reason}); store them again.`
|
|
349
|
+
});
|
|
350
|
+
const settings = store.loadSettings();
|
|
351
|
+
await op.step('choose the context', () => (0, backend_store_1.selectBackend)(store, context), {
|
|
352
|
+
run: () => store.saveSettings(settings),
|
|
353
|
+
undone: '',
|
|
354
|
+
notUndone: (_value, reason) => `The context chosen before could not be chosen again (${reason}); choose it again.`
|
|
355
|
+
});
|
|
356
|
+
const session = withKey({ ...signedIn, signedInAt: Date.now(), issuedFor: backend }, minted);
|
|
357
|
+
const previous = await op.commit('store the session', () => {
|
|
358
|
+
const replaced = (0, account_store_1.loadSession)(store, context);
|
|
359
|
+
(0, account_store_1.saveSession)(store, session, context);
|
|
360
|
+
return replaced;
|
|
361
|
+
});
|
|
362
|
+
// The same account on the same auth plane revokes the replaced key with its new sign-in; anyone else with that key's own bearer.
|
|
363
|
+
let previousKey = { kind: 'absent' };
|
|
364
|
+
if (previous?.keyId) {
|
|
365
|
+
const sameAccount = previous.userId === signedIn.userId && (previous.issuedFor?.authEndpoint ?? previous.authEndpoint) === authEndpoint;
|
|
366
|
+
previousKey = sameAccount ? await (0, api_key_1.revokeKey)(previous.keyId, issuer.revoke) : await revokeStoredKey(previous);
|
|
367
|
+
}
|
|
368
|
+
await op.settle('end the password sign-in', () => endSignIn(authEndpoint, signedIn.accessToken), () => undefined);
|
|
369
|
+
return { session, context, previousKey };
|
|
370
|
+
}
|
|
371
|
+
// A challenge waits in the cache only for the run that gives its code: one file per context, by auth endpoint.
|
|
372
|
+
const challengeFile = (store, context) => path.join((0, account_store_1.storeDirs)(store).cache, 'sign-in', `${(0, account_store_1.checkContextName)(context)}.json`);
|
|
373
|
+
function readChallenges(store, context) {
|
|
374
|
+
try {
|
|
375
|
+
const value = JSON.parse(fs.readFileSync(challengeFile(store, context), 'utf8'));
|
|
376
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
return {};
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function writeChallenges(store, context, challenges) {
|
|
383
|
+
if (Object.keys(challenges).length)
|
|
384
|
+
(0, agent_store_1.writeAtomic)(challengeFile(store, context), JSON.stringify(challenges), 'replace');
|
|
385
|
+
else
|
|
386
|
+
fs.rmSync(challengeFile(store, context), { force: true });
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Keep `challenge` under `context` (mode 0600) for a later run that has its code: a run without a terminal can't ask
|
|
390
|
+
* for it. It replaces the one kept for the same auth endpoint.
|
|
391
|
+
*/
|
|
392
|
+
function saveSignInChallenge(store, context, challenge) {
|
|
393
|
+
writeChallenges(store, context, { ...readChallenges(store, context), [challenge.authEndpoint]: challenge });
|
|
394
|
+
}
|
|
395
|
+
/** The challenge kept under `context` for `authEndpoint`, or null. */
|
|
396
|
+
function loadSignInChallenge(store, context, authEndpoint) {
|
|
397
|
+
const challenge = readChallenges(store, context)[authEndpoint];
|
|
398
|
+
return challenge?.authEndpoint === authEndpoint && typeof challenge.token === 'string' && typeof challenge.userId === 'string' ? challenge : null;
|
|
399
|
+
}
|
|
400
|
+
/** Drop the challenge kept under `context` for `authEndpoint`, once it can't finish a sign-in anymore. */
|
|
401
|
+
function clearSignInChallenge(store, context, authEndpoint) {
|
|
402
|
+
const challenges = readChallenges(store, context);
|
|
403
|
+
delete challenges[authEndpoint];
|
|
404
|
+
writeChallenges(store, context, challenges);
|
|
405
|
+
}
|
|
406
|
+
function signInTarget({ api, context, env, store }) {
|
|
407
|
+
const state = (0, session_1.resolveSession)({ context, env, store });
|
|
408
|
+
switch (state.kind) {
|
|
409
|
+
case 'signed-in':
|
|
410
|
+
case 'signed-out':
|
|
411
|
+
case 'issuer-mismatch':
|
|
412
|
+
if (api === undefined || api.trim() === state.backend.apiEndpoint) {
|
|
413
|
+
return { kind: 'target', backend: state.backend, context: state.context, from: state.source === 'env' ? 'environment' : 'context' };
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
case 'no-backend':
|
|
417
|
+
if (api === undefined)
|
|
418
|
+
return { kind: 'no-backend' };
|
|
419
|
+
break;
|
|
420
|
+
case 'incomplete-backend':
|
|
421
|
+
case 'invalid-backend':
|
|
422
|
+
case 'invalid-override':
|
|
423
|
+
case 'conflict':
|
|
424
|
+
if (api === undefined)
|
|
425
|
+
return { kind: 'unusable', session: state };
|
|
426
|
+
break;
|
|
427
|
+
default: {
|
|
428
|
+
const unknown = state;
|
|
429
|
+
throw new Error(`Unknown session state ${JSON.stringify(unknown)}.`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return { kind: 'target', backend: (0, backend_store_1.backendFromEndpoints)({ api }), context: context ?? null, from: 'api-endpoint' };
|
|
433
|
+
}
|
|
434
|
+
/** The name of the agent whose session this is, usable or not, or null for a person's session. */
|
|
435
|
+
function agentOf(session) {
|
|
436
|
+
if (session.kind === 'agent-unusable')
|
|
437
|
+
return session.agent;
|
|
438
|
+
return session.kind === 'signed-in' && session.source === 'agent' ? session.agent.name : null;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Sign out of exactly the session `resolveSession` gave: clear what is stored under its context, then revoke its API
|
|
442
|
+
* key at the auth endpoint that issued it, with its own bearer. The session is cleared in every state, also when the
|
|
443
|
+
* context cannot be used, the issuer is not recorded or the revoke fails; `key` says what happened to the key, and a
|
|
444
|
+
* key that was not revoked stays valid until it ends. Throws the problem of an `invalid-override` or `conflict`
|
|
445
|
+
* session. Prints nothing.
|
|
446
|
+
*/
|
|
447
|
+
async function signOut({ store, session }) {
|
|
448
|
+
if (session.kind === 'invalid-override' || session.kind === 'conflict')
|
|
449
|
+
throw new Error((0, session_1.sessionProblem)(session));
|
|
450
|
+
// An agent's session, usable or not, carries the person's context, so going on would revoke the person's key and
|
|
451
|
+
// clear their sign-in. The type refuses one; this refuses it from a caller the compiler did not check.
|
|
452
|
+
const agent = agentOf(session);
|
|
453
|
+
if (agent) {
|
|
454
|
+
throw new Error(`signOut ends the person's sign-in, and this is the session of the agent "${agent}", so nothing was signed out. ` +
|
|
455
|
+
"Pass the person's session to sign them out, or remove the agent with removeAgent to end its key.");
|
|
456
|
+
}
|
|
457
|
+
if (session.kind === 'signed-in' && session.source === 'env')
|
|
458
|
+
return { kind: 'from-environment' };
|
|
459
|
+
// Only CONSTRUCTIVE_API_ENDPOINT, naming another backend than the stored one: nothing is signed in there.
|
|
460
|
+
if (session.kind === 'signed-out' && session.source === 'env')
|
|
461
|
+
return { kind: 'not-signed-in', context: null };
|
|
462
|
+
const context = 'context' in session ? session.context : null;
|
|
463
|
+
if (!context || !store.getCredentials(context))
|
|
464
|
+
return { kind: 'not-signed-in', context };
|
|
465
|
+
const stored = (0, account_store_1.loadSession)(store, context);
|
|
466
|
+
if (session.kind === 'signed-in' && (!stored || stored.userId !== session.account.userId ||
|
|
467
|
+
stored.accessToken !== session.account.accessToken ||
|
|
468
|
+
stored.apiKey !== (session.account.key.kind === 'active' ? session.account.key.apiKey : undefined)))
|
|
469
|
+
return { kind: 'not-signed-in', context };
|
|
470
|
+
(0, account_store_1.clearSession)(store, context);
|
|
471
|
+
return { kind: 'cleared', context, key: stored ? await revokeStoredKey(stored) : { kind: 'absent' } };
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Ask the backend once whether the credential of a signed-in session is
|
|
475
|
+
* accepted: `currentUser` at its auth endpoint, with the bearer the tools send
|
|
476
|
+
* (the API key, else the access token), within the sign-in timeout. Classified
|
|
477
|
+
* by facts only: no answer is `unchecked`; HTTP 401 or 403 is `rejected`;
|
|
478
|
+
* another non-2xx status is `unchecked`; `UNAUTHENTICATED` or `FORBIDDEN` is
|
|
479
|
+
* `rejected`, another error code `unchecked`; no user, or another user, is
|
|
480
|
+
* `rejected`. Never throws.
|
|
481
|
+
*/
|
|
482
|
+
async function verifySession(session) {
|
|
483
|
+
const { authEndpoint } = session.backend;
|
|
484
|
+
const bearer = session.account.key.kind === 'active' ? session.account.key.apiKey : session.account.accessToken;
|
|
485
|
+
const http = (0, auth_error_1.trackedFetch)(authEndpoint);
|
|
486
|
+
const client = sdk_1.auth.createClient({ endpoint: authEndpoint, headers: { Authorization: `Bearer ${bearer}` }, fetch: http.fetch });
|
|
487
|
+
let result;
|
|
488
|
+
try {
|
|
489
|
+
result = await (0, auth_error_1.withAuthTimeout)(client.query.currentUser({ select: { id: true } }).execute(), authEndpoint);
|
|
490
|
+
}
|
|
491
|
+
catch (err) {
|
|
492
|
+
if ((0, auth_error_1.isNetworkError)(err) || err instanceof auth_error_1.RedirectRefusedError)
|
|
493
|
+
return { kind: 'unchecked', reason: (0, auth_error_1.describeAuthError)(err) };
|
|
494
|
+
return { kind: 'unchecked', reason: `The server at ${authEndpoint} gave an answer that could not be read (${(0, auth_error_1.describeAuthError)(err)}).` };
|
|
495
|
+
}
|
|
496
|
+
const status = http.status();
|
|
497
|
+
if (status === 401 || status === 403)
|
|
498
|
+
return { kind: 'rejected', reason: `The server at ${authEndpoint} rejected the credential (HTTP ${status}).`, code: `HTTP_${status}` };
|
|
499
|
+
if (status !== undefined && (status < 200 || status >= 300))
|
|
500
|
+
return { kind: 'unchecked', reason: `The server at ${authEndpoint} answered HTTP ${status}.` };
|
|
501
|
+
if (!result.ok) {
|
|
502
|
+
const codes = result.errors.map((error) => error.extensions?.code).filter((code) => typeof code === 'string');
|
|
503
|
+
const rejected = codes.find((code) => code === 'UNAUTHENTICATED' || code === 'FORBIDDEN');
|
|
504
|
+
if (rejected)
|
|
505
|
+
return { kind: 'rejected', reason: `The server at ${authEndpoint} rejected the credential (${rejected}).`, code: rejected };
|
|
506
|
+
return { kind: 'unchecked', reason: `The server at ${authEndpoint} could not check the credential (${codes.join(', ') || 'no error code'}).` };
|
|
507
|
+
}
|
|
508
|
+
const id = result.data?.currentUser?.id;
|
|
509
|
+
if (!id)
|
|
510
|
+
return { kind: 'rejected', reason: `The server at ${authEndpoint} does not know the credential: it sees no signed-in user.` };
|
|
511
|
+
if (id !== session.account.userId)
|
|
512
|
+
return { kind: 'rejected', reason: `The credential belongs to another user than the session (${session.account.userId}).` };
|
|
513
|
+
return { kind: 'accepted' };
|
|
514
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { ConfigStore } from 'appstash';
|
|
2
|
+
/** Every Constructive plane a binary talks to, one GraphQL endpoint each, as text: what a record on disk holds. */
|
|
3
|
+
export interface BackendEndpoints {
|
|
4
|
+
apiEndpoint: string;
|
|
5
|
+
authEndpoint: string;
|
|
6
|
+
modulesEndpoint: string;
|
|
7
|
+
agentEndpoint: string;
|
|
8
|
+
computeEndpoint: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* An endpoint that passed the endpoint check: an absolute https URL, or http on this machine, with no user name or
|
|
12
|
+
* password. The mark is a type only, so a value gets it from the check alone, through every copy of this package.
|
|
13
|
+
*/
|
|
14
|
+
export type Endpoint = string & {
|
|
15
|
+
readonly __checkedEndpoint: true;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A backend whose five endpoints passed the endpoint check. Only `backendFromEndpoints` and the readers of the store
|
|
19
|
+
* make one, so no request can go to an endpoint that was never checked.
|
|
20
|
+
*/
|
|
21
|
+
export type BackendConfig = {
|
|
22
|
+
readonly [K in keyof BackendEndpoints]: Endpoint;
|
|
23
|
+
};
|
|
24
|
+
export declare const PLANES: readonly ["api", "auth", "modules", "agent", "compute"];
|
|
25
|
+
export type Plane = (typeof PLANES)[number];
|
|
26
|
+
export declare function planeEndpoint(config: BackendEndpoints, plane: Plane): string;
|
|
27
|
+
/** The planes whose endpoints differ, compared exactly (no case or slash folding). */
|
|
28
|
+
export declare function differingPlanes(a: BackendEndpoints, b: BackendEndpoints): Plane[];
|
|
29
|
+
/**
|
|
30
|
+
* A stored context: complete, lacking planes (a context another app wrote;
|
|
31
|
+
* planes are never filled in), holding an endpoint that fails the endpoint
|
|
32
|
+
* check (`plane`, `problem`), or stored under a name that is not a valid
|
|
33
|
+
* context name (`reason`). No function sends anything to a context that is not
|
|
34
|
+
* complete.
|
|
35
|
+
*/
|
|
36
|
+
export type BackendContext = {
|
|
37
|
+
name: string;
|
|
38
|
+
current: boolean;
|
|
39
|
+
status: 'complete';
|
|
40
|
+
backend: BackendConfig;
|
|
41
|
+
} | {
|
|
42
|
+
name: string;
|
|
43
|
+
current: boolean;
|
|
44
|
+
status: 'incomplete';
|
|
45
|
+
missing: Plane[];
|
|
46
|
+
apiEndpoint?: string;
|
|
47
|
+
} | {
|
|
48
|
+
name: string;
|
|
49
|
+
current: boolean;
|
|
50
|
+
status: 'invalid';
|
|
51
|
+
plane: Plane;
|
|
52
|
+
problem: string;
|
|
53
|
+
} | {
|
|
54
|
+
name: string;
|
|
55
|
+
current: boolean;
|
|
56
|
+
status: 'invalid-name';
|
|
57
|
+
reason: string;
|
|
58
|
+
};
|
|
59
|
+
export type EndpointProblem = 'not-a-url' | 'scheme' | 'insecure' | 'userinfo' | 'not-derivable';
|
|
60
|
+
/**
|
|
61
|
+
* Whether `url` names this machine, where plain http never crosses a network: a host whose last label is localhost,
|
|
62
|
+
* an IPv4 address in 127/8, or ::1.
|
|
63
|
+
*/
|
|
64
|
+
export declare function isLoopback(url: URL): boolean;
|
|
65
|
+
/** An endpoint that cannot name a backend. The message never shows userinfo. */
|
|
66
|
+
export declare class EndpointError extends Error {
|
|
67
|
+
readonly reason: EndpointProblem;
|
|
68
|
+
readonly plane: Plane;
|
|
69
|
+
constructor(reason: EndpointProblem, plane: Plane, message: string);
|
|
70
|
+
}
|
|
71
|
+
/** `raw` as an endpoint of `plane`, or the `EndpointError` that says why it cannot be one. */
|
|
72
|
+
export declare function checkedEndpoint(plane: Plane, raw: string): Endpoint;
|
|
73
|
+
/** Five endpoints of a backend, each checked as given, none derived. Throws `EndpointError`. */
|
|
74
|
+
export declare function checkedBackend(endpoints: BackendEndpoints): BackendConfig;
|
|
75
|
+
/**
|
|
76
|
+
* The backend that `api` names. A plane given explicitly is used as is; a
|
|
77
|
+
* missing one is derived from an `api.<rest>` host. Throws `EndpointError`.
|
|
78
|
+
*/
|
|
79
|
+
export declare function backendFromEndpoints(endpoints: {
|
|
80
|
+
api: string;
|
|
81
|
+
} & Partial<Record<Exclude<Plane, 'api'>, string>>): BackendConfig;
|
|
82
|
+
/**
|
|
83
|
+
* A backend is a named context in the shared store, named after the api
|
|
84
|
+
* endpoint's host without a leading `api.` (https://api.example.com/graphql ->
|
|
85
|
+
* example.com), a port joined by a dash (example.com-8443). Credentials hang
|
|
86
|
+
* off the context, so each backend keeps an independent session.
|
|
87
|
+
*/
|
|
88
|
+
export declare function contextNameFor(config: BackendEndpoints): string;
|
|
89
|
+
/** A stored context under a valid name: complete or lacking planes. */
|
|
90
|
+
export type NamedBackendContext = Exclude<BackendContext, {
|
|
91
|
+
status: 'invalid-name';
|
|
92
|
+
}>;
|
|
93
|
+
/** One stored context, or null when there is none with that name. */
|
|
94
|
+
export declare function readBackend(store: ConfigStore, name: string): NamedBackendContext | null;
|
|
95
|
+
/** Every stored context. One with a name that does not pass the check is listed as `invalid-name` and stops nothing. */
|
|
96
|
+
export declare function listBackends(store: ConfigStore): BackendContext[];
|
|
97
|
+
/** Storing other endpoints under the context, or removing it, would drop the session filed there while its API key stays valid. */
|
|
98
|
+
export declare class ContextInUseError extends Error {
|
|
99
|
+
readonly context: string;
|
|
100
|
+
readonly action: 'store' | 'remove' | 'agents';
|
|
101
|
+
readonly agents: string[];
|
|
102
|
+
constructor(context: string, action: 'store' | 'remove' | 'agents', agents?: string[]);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Why `config` cannot be stored under `name` now, or null. Credentials filed there must be a session this package
|
|
106
|
+
* wrote, issued for these endpoints; `replacing` takes any session of its own, which a sign-in revokes and replaces.
|
|
107
|
+
* A context whose agents were set up with its endpoints keeps them. The name is checked first, then the agents, then
|
|
108
|
+
* the session: removing an agent needs that sign-in, so the agents must go first.
|
|
109
|
+
*/
|
|
110
|
+
export declare function backendConflict(store: ConfigStore, config: BackendConfig, name: string, { replacing }?: {
|
|
111
|
+
replacing?: boolean;
|
|
112
|
+
}): ContextInUseError | null;
|
|
113
|
+
/**
|
|
114
|
+
* Store the endpoints as a context (default name: `contextNameFor`), keeping
|
|
115
|
+
* its current choice and the session filed under that name, which must have
|
|
116
|
+
* been issued for exactly these five endpoints. Throws `ContextInUseError`
|
|
117
|
+
* when it was not, or cannot be shown to be (sign out of that context first),
|
|
118
|
+
* and when agents of the context were set up with other endpoints.
|
|
119
|
+
*/
|
|
120
|
+
export declare function addBackend(store: ConfigStore, config: BackendConfig, name?: string): {
|
|
121
|
+
name: string;
|
|
122
|
+
};
|
|
123
|
+
/** The context as stored, to put back with `restoreBackend`; null when there is none. */
|
|
124
|
+
export type StoredContext = ReturnType<ConfigStore['loadContext']>;
|
|
125
|
+
/** Write the endpoints under `name` with no check of what is filed there: for a caller that checked with `backendConflict`. */
|
|
126
|
+
export declare function writeBackend(store: ConfigStore, config: BackendConfig, name: string): void;
|
|
127
|
+
/** Put back a context `writeBackend` replaced: its old endpoints, or no context when there was none. */
|
|
128
|
+
export declare function restoreBackend(store: ConfigStore, name: string, previous: StoredContext): void;
|
|
129
|
+
export declare function selectBackend(store: ConfigStore, name: string): void;
|
|
130
|
+
/**
|
|
131
|
+
* Delete a context. Throws `ContextInUseError` while agents are stored under it (`removeAgent` first, which needs the
|
|
132
|
+
* sign-in), then while a session is filed under it (`signOut` first, which revokes its key).
|
|
133
|
+
*/
|
|
134
|
+
export declare function removeBackend(store: ConfigStore, name: string): boolean;
|