@decryption/accounts 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 +21 -0
- package/README.md +163 -2
- package/client.d.ts +77 -0
- package/client.js +381 -0
- package/credentials.d.ts +53 -0
- package/credentials.js +57 -0
- package/endpoint.d.ts +11 -0
- package/endpoint.js +44 -0
- package/esm/client.js +372 -0
- package/esm/credentials.js +53 -0
- package/esm/endpoint.js +39 -0
- package/esm/index.js +5 -0
- package/esm/manager.js +347 -0
- package/esm/types.js +1 -0
- package/index.d.ts +5 -0
- package/index.js +21 -0
- package/manager.d.ts +101 -0
- package/manager.js +352 -0
- package/package.json +45 -4
- package/types.d.ts +104 -0
- package/types.js +2 -0
package/esm/manager.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { AuthError, sdkAuthClient, StepUpRequiredError, } from './client';
|
|
2
|
+
import { normalizeEndpoint } from './endpoint';
|
|
3
|
+
const ACCOUNT_FIELDS = {
|
|
4
|
+
endpoint: 'endpoint',
|
|
5
|
+
email: 'email',
|
|
6
|
+
userId: 'user_id',
|
|
7
|
+
accessToken: 'access_token',
|
|
8
|
+
expiresAt: 'access_token_expires_at',
|
|
9
|
+
totpItem: 'totp_item_id',
|
|
10
|
+
};
|
|
11
|
+
const KEY_FIELDS = {
|
|
12
|
+
endpoint: 'endpoint',
|
|
13
|
+
accountId: 'account_id',
|
|
14
|
+
keyId: 'key_id',
|
|
15
|
+
apiKey: 'api_key',
|
|
16
|
+
expiresAt: 'expires_at',
|
|
17
|
+
databaseId: 'database_id',
|
|
18
|
+
principalId: 'principal_id',
|
|
19
|
+
orgId: 'org_id',
|
|
20
|
+
};
|
|
21
|
+
/** True when `iso` is a timestamp already in the past. */
|
|
22
|
+
export const hasExpired = (iso, now) => iso !== null && Date.parse(iso) <= now.getTime();
|
|
23
|
+
const hostOf = (endpoint) => {
|
|
24
|
+
try {
|
|
25
|
+
return new URL(endpoint).host;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return endpoint;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Constructive accounts and API keys, stored as vault items.
|
|
33
|
+
*
|
|
34
|
+
* Nothing is kept outside the vault: the access token and every API key secret
|
|
35
|
+
* are concealed fields in the same encrypted file as the rest of the vault, so
|
|
36
|
+
* they are covered by the master passphrase, by lock, and by backup/restore.
|
|
37
|
+
*/
|
|
38
|
+
export class AccountManager {
|
|
39
|
+
vault;
|
|
40
|
+
createClient;
|
|
41
|
+
now;
|
|
42
|
+
constructor(vault, options = {}) {
|
|
43
|
+
this.vault = vault;
|
|
44
|
+
this.createClient = options.createClient ?? sdkAuthClient;
|
|
45
|
+
this.now = options.now ?? (() => new Date());
|
|
46
|
+
}
|
|
47
|
+
// ─── accounts ─────────────────────────────────────────────────────────────
|
|
48
|
+
/** Sign in against `endpoint` and store (or refresh) the account. */
|
|
49
|
+
async signIn(options) {
|
|
50
|
+
const endpoint = normalizeEndpoint(options.endpoint);
|
|
51
|
+
const session = await this.createClient({ endpoint }).signIn(options);
|
|
52
|
+
return this.adopt(endpoint, options.email, session);
|
|
53
|
+
}
|
|
54
|
+
/** Create an account on `endpoint`, then store the session it returns. */
|
|
55
|
+
async signUp(options) {
|
|
56
|
+
const endpoint = normalizeEndpoint(options.endpoint);
|
|
57
|
+
const session = await this.createClient({ endpoint }).signUp(options);
|
|
58
|
+
return this.adopt(endpoint, options.email, session);
|
|
59
|
+
}
|
|
60
|
+
async listAccounts() {
|
|
61
|
+
const items = await this.vault.listItems({ kind: 'account' });
|
|
62
|
+
return Promise.all(items.map((item) => this.readAccount(item)));
|
|
63
|
+
}
|
|
64
|
+
async getAccount(itemId) {
|
|
65
|
+
return this.readAccount(await this.requireItem(itemId, 'account'));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The stored bearer token, or null when signed out or expired. This is the
|
|
69
|
+
* value a harness host would ask for; expiry is checked here so a caller
|
|
70
|
+
* cannot accidentally present a dead token.
|
|
71
|
+
*/
|
|
72
|
+
async accessToken(itemId) {
|
|
73
|
+
const fields = await this.readFields(itemId);
|
|
74
|
+
const token = fields[ACCOUNT_FIELDS.accessToken];
|
|
75
|
+
if (!token)
|
|
76
|
+
return null;
|
|
77
|
+
if (hasExpired(fields[ACCOUNT_FIELDS.expiresAt] ?? null, this.now())) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return token;
|
|
81
|
+
}
|
|
82
|
+
/** Sign out server-side and drop the token, keeping the account item. */
|
|
83
|
+
async signOut(itemId) {
|
|
84
|
+
const fields = await this.readFields(itemId);
|
|
85
|
+
const token = fields[ACCOUNT_FIELDS.accessToken];
|
|
86
|
+
const endpoint = fields[ACCOUNT_FIELDS.endpoint];
|
|
87
|
+
if (token) {
|
|
88
|
+
await this.createClient({ endpoint, token }).signOut();
|
|
89
|
+
}
|
|
90
|
+
await this.vault.deleteField(itemId, ACCOUNT_FIELDS.accessToken);
|
|
91
|
+
await this.vault.deleteField(itemId, ACCOUNT_FIELDS.expiresAt);
|
|
92
|
+
}
|
|
93
|
+
/** Remove the account and every API key stored under it, locally. */
|
|
94
|
+
async forget(itemId) {
|
|
95
|
+
for (const key of await this.listApiKeys(itemId)) {
|
|
96
|
+
await this.vault.deleteItemForever(key.itemId);
|
|
97
|
+
}
|
|
98
|
+
await this.vault.deleteItemForever(itemId);
|
|
99
|
+
}
|
|
100
|
+
// ─── one-time codes ───────────────────────────────────────────────────────
|
|
101
|
+
/**
|
|
102
|
+
* Point an account at a code already in the vault, so an MFA step-up can be
|
|
103
|
+
* answered without reaching for a phone. Only the item id is stored; the seed
|
|
104
|
+
* stays where it was, concealed, and is never copied.
|
|
105
|
+
*/
|
|
106
|
+
async linkTotp(accountItemId, totpItemId) {
|
|
107
|
+
await this.requireItem(accountItemId, 'account');
|
|
108
|
+
await this.requireItem(totpItemId, 'totp');
|
|
109
|
+
const fields = await this.vault.listFields(totpItemId);
|
|
110
|
+
if (!fields.some((field) => field.purpose === 'totp_seed')) {
|
|
111
|
+
throw new AuthError('linkTotp', `item ${totpItemId} carries no code seed`);
|
|
112
|
+
}
|
|
113
|
+
await this.vault.setField(accountItemId, ACCOUNT_FIELDS.totpItem, 'text', totpItemId, false);
|
|
114
|
+
}
|
|
115
|
+
async unlinkTotp(accountItemId) {
|
|
116
|
+
await this.vault.deleteField(accountItemId, ACCOUNT_FIELDS.totpItem);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A current code for the account's linked item, or null when nothing is
|
|
120
|
+
* linked — the caller then has to ask a human for one.
|
|
121
|
+
*/
|
|
122
|
+
async stepUpCode(accountItemId) {
|
|
123
|
+
const fields = await this.readFields(accountItemId);
|
|
124
|
+
const totpItemId = fields[ACCOUNT_FIELDS.totpItem];
|
|
125
|
+
if (!totpItemId)
|
|
126
|
+
return null;
|
|
127
|
+
if (!(await this.vault.getItem(totpItemId))) {
|
|
128
|
+
await this.unlinkTotp(accountItemId);
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
return this.vault.totpCode(totpItemId);
|
|
132
|
+
}
|
|
133
|
+
// ─── api keys ─────────────────────────────────────────────────────────────
|
|
134
|
+
/**
|
|
135
|
+
* Mint an API key for an account. The server shows the secret exactly once,
|
|
136
|
+
* so it goes straight into the vault and is never returned to the caller —
|
|
137
|
+
* read it back with `revealApiKey` when it is actually needed.
|
|
138
|
+
*/
|
|
139
|
+
async createApiKey(accountItemId, options, proof) {
|
|
140
|
+
const account = await this.readFields(accountItemId);
|
|
141
|
+
const token = await this.requireToken(accountItemId);
|
|
142
|
+
const endpoint = account[ACCOUNT_FIELDS.endpoint];
|
|
143
|
+
const created = await this.withStepUp(accountItemId, endpoint, token, proof, (client) => client.createApiKey(options));
|
|
144
|
+
const item = await this.vault.createItem('api_key', options.name);
|
|
145
|
+
await this.vault.setField(item.id, KEY_FIELDS.endpoint, 'url', endpoint, false);
|
|
146
|
+
await this.vault.setField(item.id, KEY_FIELDS.accountId, 'text', accountItemId, false);
|
|
147
|
+
await this.vault.setField(item.id, KEY_FIELDS.keyId, 'text', created.keyId, false);
|
|
148
|
+
await this.vault.setField(item.id, KEY_FIELDS.apiKey, 'token', created.apiKey);
|
|
149
|
+
if (options.principalId) {
|
|
150
|
+
await this.vault.setField(item.id, KEY_FIELDS.principalId, 'text', options.principalId, false);
|
|
151
|
+
}
|
|
152
|
+
if (options.orgId) {
|
|
153
|
+
await this.vault.setField(item.id, KEY_FIELDS.orgId, 'text', options.orgId, false);
|
|
154
|
+
}
|
|
155
|
+
if (options.databaseId) {
|
|
156
|
+
await this.vault.setField(item.id, KEY_FIELDS.databaseId, 'text', options.databaseId, false);
|
|
157
|
+
}
|
|
158
|
+
if (created.expiresAt) {
|
|
159
|
+
await this.vault.setField(item.id, KEY_FIELDS.expiresAt, 'text', created.expiresAt, false);
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
itemId: item.id,
|
|
163
|
+
accountItemId,
|
|
164
|
+
endpoint,
|
|
165
|
+
keyId: created.keyId,
|
|
166
|
+
name: options.name,
|
|
167
|
+
expiresAt: created.expiresAt,
|
|
168
|
+
databaseId: options.databaseId ?? null,
|
|
169
|
+
principalId: options.principalId ?? null,
|
|
170
|
+
orgId: options.orgId ?? null,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Say that a key already in the vault is a database's data-plane token, so a
|
|
175
|
+
* harness host asking for that database is handed this key and no other.
|
|
176
|
+
*/
|
|
177
|
+
async assignKeyToDatabase(keyItemId, databaseId) {
|
|
178
|
+
await this.requireItem(keyItemId, 'api_key');
|
|
179
|
+
await this.vault.setField(keyItemId, KEY_FIELDS.databaseId, 'text', databaseId, false);
|
|
180
|
+
}
|
|
181
|
+
/** Every stored key, or only those minted by one account. */
|
|
182
|
+
async listApiKeys(accountItemId) {
|
|
183
|
+
const items = await this.vault.listItems({ kind: 'api_key' });
|
|
184
|
+
const keys = await Promise.all(items.map((item) => this.readApiKey(item)));
|
|
185
|
+
return accountItemId
|
|
186
|
+
? keys.filter((key) => key.accountItemId === accountItemId)
|
|
187
|
+
: keys;
|
|
188
|
+
}
|
|
189
|
+
async revealApiKey(itemId) {
|
|
190
|
+
return this.vault.revealField(itemId, KEY_FIELDS.apiKey);
|
|
191
|
+
}
|
|
192
|
+
/** Revoke server-side, then delete the local copy. */
|
|
193
|
+
async revokeApiKey(itemId, proof) {
|
|
194
|
+
const fields = await this.readFields(itemId);
|
|
195
|
+
const accountItemId = fields[KEY_FIELDS.accountId];
|
|
196
|
+
const token = await this.requireToken(accountItemId);
|
|
197
|
+
await this.withStepUp(accountItemId, fields[KEY_FIELDS.endpoint], token, proof, (client) => client.revokeApiKey(fields[KEY_FIELDS.keyId]));
|
|
198
|
+
await this.vault.deleteItemForever(itemId);
|
|
199
|
+
}
|
|
200
|
+
// ─── principals ───────────────────────────────────────────────────────────
|
|
201
|
+
/**
|
|
202
|
+
* The account's scoped sub-identities, straight from the server — nothing
|
|
203
|
+
* about a principal is secret, and a stale local copy would be worse than
|
|
204
|
+
* none, so this is not cached in the vault.
|
|
205
|
+
*/
|
|
206
|
+
async listPrincipals(accountItemId) {
|
|
207
|
+
const fields = await this.readFields(accountItemId);
|
|
208
|
+
const token = await this.requireToken(accountItemId);
|
|
209
|
+
return this.createClient({
|
|
210
|
+
endpoint: fields[ACCOUNT_FIELDS.endpoint],
|
|
211
|
+
token,
|
|
212
|
+
}).listPrincipals();
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Create a principal an API key can then be minted as. A principal is a
|
|
216
|
+
* narrowing of its owner: it can be read-only, restricted per scope, and
|
|
217
|
+
* allowed to skip step-up, but it can never reach further than the human.
|
|
218
|
+
*/
|
|
219
|
+
async createPrincipal(accountItemId, options, proof) {
|
|
220
|
+
const fields = await this.readFields(accountItemId);
|
|
221
|
+
const token = await this.requireToken(accountItemId);
|
|
222
|
+
return this.withStepUp(accountItemId, fields[ACCOUNT_FIELDS.endpoint], token, proof, (client) => client.createPrincipal(options));
|
|
223
|
+
}
|
|
224
|
+
async deletePrincipal(accountItemId, principalId, proof) {
|
|
225
|
+
const fields = await this.readFields(accountItemId);
|
|
226
|
+
const token = await this.requireToken(accountItemId);
|
|
227
|
+
await this.withStepUp(accountItemId, fields[ACCOUNT_FIELDS.endpoint], token, proof, (client) => client.deletePrincipal(principalId));
|
|
228
|
+
}
|
|
229
|
+
// ─── internals ────────────────────────────────────────────────────────────
|
|
230
|
+
/**
|
|
231
|
+
* Run a sensitive operation, and if the server asks for a freshly proved
|
|
232
|
+
* factor, prove it and run *the same* operation once more. The request is
|
|
233
|
+
* held rather than rebuilt, so nothing about it can change between the two
|
|
234
|
+
* attempts. Without a proof the `StepUpRequiredError` propagates, which is
|
|
235
|
+
* what lets a caller collect one and try again — except for a code demand on
|
|
236
|
+
* an account with a linked code, which the vault answers by itself.
|
|
237
|
+
*/
|
|
238
|
+
async withStepUp(accountItemId, endpoint, token, proof, run) {
|
|
239
|
+
const client = this.createClient({ endpoint, token });
|
|
240
|
+
try {
|
|
241
|
+
return await run(client);
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
if (!(error instanceof StepUpRequiredError))
|
|
245
|
+
throw error;
|
|
246
|
+
if (error.kind === 'mfa') {
|
|
247
|
+
const code = proof?.totpCode ?? (await this.stepUpCode(accountItemId));
|
|
248
|
+
if (!code)
|
|
249
|
+
throw error;
|
|
250
|
+
await client.verifyTotp(code);
|
|
251
|
+
}
|
|
252
|
+
else if (proof?.password) {
|
|
253
|
+
await client.verifyPassword(proof.password);
|
|
254
|
+
}
|
|
255
|
+
else if (proof?.totpCode) {
|
|
256
|
+
await client.verifyTotp(proof.totpCode);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
// A password demand needs the password; only fresh auth takes a code.
|
|
260
|
+
const code = error.kind === 'fresh_auth' ? await this.stepUpCode(accountItemId) : null;
|
|
261
|
+
if (!code)
|
|
262
|
+
throw error;
|
|
263
|
+
await client.verifyTotp(code);
|
|
264
|
+
}
|
|
265
|
+
return run(client);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async adopt(endpoint, email, session) {
|
|
269
|
+
const existing = await this.findAccount(endpoint, email);
|
|
270
|
+
const itemId = existing?.id ??
|
|
271
|
+
(await this.vault.createItem('account', `${email} @ ${hostOf(endpoint)}`)).id;
|
|
272
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.endpoint, 'url', endpoint, false);
|
|
273
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.email, 'username', email, false);
|
|
274
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.userId, 'text', session.userId, false);
|
|
275
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.accessToken, 'token', session.accessToken);
|
|
276
|
+
if (session.accessTokenExpiresAt) {
|
|
277
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.expiresAt, 'text', session.accessTokenExpiresAt, false);
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
itemId,
|
|
281
|
+
endpoint,
|
|
282
|
+
email,
|
|
283
|
+
userId: session.userId,
|
|
284
|
+
totpItemId: (await this.readFields(itemId))[ACCOUNT_FIELDS.totpItem] ?? null,
|
|
285
|
+
accessTokenExpiresAt: session.accessTokenExpiresAt,
|
|
286
|
+
signedIn: !hasExpired(session.accessTokenExpiresAt, this.now()),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
async findAccount(endpoint, email) {
|
|
290
|
+
for (const item of await this.vault.listItems({ kind: 'account' })) {
|
|
291
|
+
const fields = await this.readFields(item.id);
|
|
292
|
+
if (fields[ACCOUNT_FIELDS.endpoint] === endpoint &&
|
|
293
|
+
fields[ACCOUNT_FIELDS.email] === email) {
|
|
294
|
+
return item;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
async requireItem(itemId, kind) {
|
|
300
|
+
const item = await this.vault.getItem(itemId);
|
|
301
|
+
if (!item || item.kind !== kind) {
|
|
302
|
+
throw new Error(`no ${kind} item ${itemId} in this vault`);
|
|
303
|
+
}
|
|
304
|
+
return item;
|
|
305
|
+
}
|
|
306
|
+
async requireToken(accountItemId) {
|
|
307
|
+
const token = await this.accessToken(accountItemId);
|
|
308
|
+
if (!token) {
|
|
309
|
+
throw new AuthError('accessToken', `account ${accountItemId} is signed out — sign in again first`);
|
|
310
|
+
}
|
|
311
|
+
return token;
|
|
312
|
+
}
|
|
313
|
+
async readAccount(item) {
|
|
314
|
+
const fields = await this.readFields(item.id);
|
|
315
|
+
const expiresAt = fields[ACCOUNT_FIELDS.expiresAt] ?? null;
|
|
316
|
+
return {
|
|
317
|
+
itemId: item.id,
|
|
318
|
+
endpoint: fields[ACCOUNT_FIELDS.endpoint] ?? '',
|
|
319
|
+
email: fields[ACCOUNT_FIELDS.email] ?? item.title,
|
|
320
|
+
userId: fields[ACCOUNT_FIELDS.userId] ?? '',
|
|
321
|
+
totpItemId: fields[ACCOUNT_FIELDS.totpItem] ?? null,
|
|
322
|
+
accessTokenExpiresAt: expiresAt,
|
|
323
|
+
signedIn: Boolean(fields[ACCOUNT_FIELDS.accessToken]) &&
|
|
324
|
+
!hasExpired(expiresAt, this.now()),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
async readApiKey(item) {
|
|
328
|
+
const fields = await this.readFields(item.id);
|
|
329
|
+
return {
|
|
330
|
+
itemId: item.id,
|
|
331
|
+
accountItemId: fields[KEY_FIELDS.accountId] ?? '',
|
|
332
|
+
endpoint: fields[KEY_FIELDS.endpoint] ?? '',
|
|
333
|
+
keyId: fields[KEY_FIELDS.keyId] ?? '',
|
|
334
|
+
name: item.title,
|
|
335
|
+
expiresAt: fields[KEY_FIELDS.expiresAt] ?? null,
|
|
336
|
+
databaseId: fields[KEY_FIELDS.databaseId] ?? null,
|
|
337
|
+
principalId: fields[KEY_FIELDS.principalId] ?? null,
|
|
338
|
+
orgId: fields[KEY_FIELDS.orgId] ?? null,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/** Every field of an item, decrypted, keyed by name. */
|
|
342
|
+
async readFields(itemId) {
|
|
343
|
+
const metas = await this.vault.listFields(itemId);
|
|
344
|
+
const entries = await Promise.all(metas.map(async (meta) => [meta.name, await this.vault.revealField(itemId, meta.name)]));
|
|
345
|
+
return Object.fromEntries(entries);
|
|
346
|
+
}
|
|
347
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./client"), exports);
|
|
18
|
+
__exportStar(require("./credentials"), exports);
|
|
19
|
+
__exportStar(require("./endpoint"), exports);
|
|
20
|
+
__exportStar(require("./manager"), exports);
|
|
21
|
+
__exportStar(require("./types"), exports);
|
package/manager.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Vault } from '@decryption/vault';
|
|
2
|
+
import { AuthClientFactory, SignInInput, StepUpProof } from './client';
|
|
3
|
+
import { AccountRecord, ApiKeyRecord, CreateApiKeyOptions, CreatePrincipalOptions, PrincipalRecord } from './types';
|
|
4
|
+
export interface AccountManagerOptions {
|
|
5
|
+
/** Swap the transport — tests pass a fake, hosts pass the SDK client. */
|
|
6
|
+
createClient?: AuthClientFactory;
|
|
7
|
+
/** Clock, injectable so expiry logic is testable. */
|
|
8
|
+
now?: () => Date;
|
|
9
|
+
}
|
|
10
|
+
export interface SignInOptions extends SignInInput {
|
|
11
|
+
endpoint: string;
|
|
12
|
+
}
|
|
13
|
+
/** True when `iso` is a timestamp already in the past. */
|
|
14
|
+
export declare const hasExpired: (iso: string | null, now: Date) => boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Constructive accounts and API keys, stored as vault items.
|
|
17
|
+
*
|
|
18
|
+
* Nothing is kept outside the vault: the access token and every API key secret
|
|
19
|
+
* are concealed fields in the same encrypted file as the rest of the vault, so
|
|
20
|
+
* they are covered by the master passphrase, by lock, and by backup/restore.
|
|
21
|
+
*/
|
|
22
|
+
export declare class AccountManager {
|
|
23
|
+
private readonly vault;
|
|
24
|
+
private readonly createClient;
|
|
25
|
+
private readonly now;
|
|
26
|
+
constructor(vault: Vault, options?: AccountManagerOptions);
|
|
27
|
+
/** Sign in against `endpoint` and store (or refresh) the account. */
|
|
28
|
+
signIn(options: SignInOptions): Promise<AccountRecord>;
|
|
29
|
+
/** Create an account on `endpoint`, then store the session it returns. */
|
|
30
|
+
signUp(options: SignInOptions): Promise<AccountRecord>;
|
|
31
|
+
listAccounts(): Promise<AccountRecord[]>;
|
|
32
|
+
getAccount(itemId: string): Promise<AccountRecord>;
|
|
33
|
+
/**
|
|
34
|
+
* The stored bearer token, or null when signed out or expired. This is the
|
|
35
|
+
* value a harness host would ask for; expiry is checked here so a caller
|
|
36
|
+
* cannot accidentally present a dead token.
|
|
37
|
+
*/
|
|
38
|
+
accessToken(itemId: string): Promise<string | null>;
|
|
39
|
+
/** Sign out server-side and drop the token, keeping the account item. */
|
|
40
|
+
signOut(itemId: string): Promise<void>;
|
|
41
|
+
/** Remove the account and every API key stored under it, locally. */
|
|
42
|
+
forget(itemId: string): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Point an account at a code already in the vault, so an MFA step-up can be
|
|
45
|
+
* answered without reaching for a phone. Only the item id is stored; the seed
|
|
46
|
+
* stays where it was, concealed, and is never copied.
|
|
47
|
+
*/
|
|
48
|
+
linkTotp(accountItemId: string, totpItemId: string): Promise<void>;
|
|
49
|
+
unlinkTotp(accountItemId: string): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* A current code for the account's linked item, or null when nothing is
|
|
52
|
+
* linked — the caller then has to ask a human for one.
|
|
53
|
+
*/
|
|
54
|
+
stepUpCode(accountItemId: string): Promise<string | null>;
|
|
55
|
+
/**
|
|
56
|
+
* Mint an API key for an account. The server shows the secret exactly once,
|
|
57
|
+
* so it goes straight into the vault and is never returned to the caller —
|
|
58
|
+
* read it back with `revealApiKey` when it is actually needed.
|
|
59
|
+
*/
|
|
60
|
+
createApiKey(accountItemId: string, options: CreateApiKeyOptions, proof?: StepUpProof): Promise<ApiKeyRecord>;
|
|
61
|
+
/**
|
|
62
|
+
* Say that a key already in the vault is a database's data-plane token, so a
|
|
63
|
+
* harness host asking for that database is handed this key and no other.
|
|
64
|
+
*/
|
|
65
|
+
assignKeyToDatabase(keyItemId: string, databaseId: string): Promise<void>;
|
|
66
|
+
/** Every stored key, or only those minted by one account. */
|
|
67
|
+
listApiKeys(accountItemId?: string): Promise<ApiKeyRecord[]>;
|
|
68
|
+
revealApiKey(itemId: string): Promise<string>;
|
|
69
|
+
/** Revoke server-side, then delete the local copy. */
|
|
70
|
+
revokeApiKey(itemId: string, proof?: StepUpProof): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* The account's scoped sub-identities, straight from the server — nothing
|
|
73
|
+
* about a principal is secret, and a stale local copy would be worse than
|
|
74
|
+
* none, so this is not cached in the vault.
|
|
75
|
+
*/
|
|
76
|
+
listPrincipals(accountItemId: string): Promise<PrincipalRecord[]>;
|
|
77
|
+
/**
|
|
78
|
+
* Create a principal an API key can then be minted as. A principal is a
|
|
79
|
+
* narrowing of its owner: it can be read-only, restricted per scope, and
|
|
80
|
+
* allowed to skip step-up, but it can never reach further than the human.
|
|
81
|
+
*/
|
|
82
|
+
createPrincipal(accountItemId: string, options: CreatePrincipalOptions, proof?: StepUpProof): Promise<string>;
|
|
83
|
+
deletePrincipal(accountItemId: string, principalId: string, proof?: StepUpProof): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Run a sensitive operation, and if the server asks for a freshly proved
|
|
86
|
+
* factor, prove it and run *the same* operation once more. The request is
|
|
87
|
+
* held rather than rebuilt, so nothing about it can change between the two
|
|
88
|
+
* attempts. Without a proof the `StepUpRequiredError` propagates, which is
|
|
89
|
+
* what lets a caller collect one and try again — except for a code demand on
|
|
90
|
+
* an account with a linked code, which the vault answers by itself.
|
|
91
|
+
*/
|
|
92
|
+
private withStepUp;
|
|
93
|
+
private adopt;
|
|
94
|
+
private findAccount;
|
|
95
|
+
private requireItem;
|
|
96
|
+
private requireToken;
|
|
97
|
+
private readAccount;
|
|
98
|
+
private readApiKey;
|
|
99
|
+
/** Every field of an item, decrypted, keyed by name. */
|
|
100
|
+
private readFields;
|
|
101
|
+
}
|