@decryption/accounts 0.0.0-stage → 0.2.1
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 +176 -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/manager.js
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccountManager = exports.hasExpired = void 0;
|
|
4
|
+
const client_1 = require("./client");
|
|
5
|
+
const endpoint_1 = require("./endpoint");
|
|
6
|
+
const ACCOUNT_FIELDS = {
|
|
7
|
+
endpoint: 'endpoint',
|
|
8
|
+
email: 'email',
|
|
9
|
+
userId: 'user_id',
|
|
10
|
+
accessToken: 'access_token',
|
|
11
|
+
expiresAt: 'access_token_expires_at',
|
|
12
|
+
totpItem: 'totp_item_id',
|
|
13
|
+
};
|
|
14
|
+
const KEY_FIELDS = {
|
|
15
|
+
endpoint: 'endpoint',
|
|
16
|
+
accountId: 'account_id',
|
|
17
|
+
keyId: 'key_id',
|
|
18
|
+
apiKey: 'api_key',
|
|
19
|
+
expiresAt: 'expires_at',
|
|
20
|
+
databaseId: 'database_id',
|
|
21
|
+
principalId: 'principal_id',
|
|
22
|
+
orgId: 'org_id',
|
|
23
|
+
};
|
|
24
|
+
/** True when `iso` is a timestamp already in the past. */
|
|
25
|
+
const hasExpired = (iso, now) => iso !== null && Date.parse(iso) <= now.getTime();
|
|
26
|
+
exports.hasExpired = hasExpired;
|
|
27
|
+
const hostOf = (endpoint) => {
|
|
28
|
+
try {
|
|
29
|
+
return new URL(endpoint).host;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return endpoint;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Constructive accounts and API keys, stored as vault items.
|
|
37
|
+
*
|
|
38
|
+
* Nothing is kept outside the vault: the access token and every API key secret
|
|
39
|
+
* are concealed fields in the same encrypted file as the rest of the vault, so
|
|
40
|
+
* they are covered by the master passphrase, by lock, and by backup/restore.
|
|
41
|
+
*/
|
|
42
|
+
class AccountManager {
|
|
43
|
+
vault;
|
|
44
|
+
createClient;
|
|
45
|
+
now;
|
|
46
|
+
constructor(vault, options = {}) {
|
|
47
|
+
this.vault = vault;
|
|
48
|
+
this.createClient = options.createClient ?? client_1.sdkAuthClient;
|
|
49
|
+
this.now = options.now ?? (() => new Date());
|
|
50
|
+
}
|
|
51
|
+
// ─── accounts ─────────────────────────────────────────────────────────────
|
|
52
|
+
/** Sign in against `endpoint` and store (or refresh) the account. */
|
|
53
|
+
async signIn(options) {
|
|
54
|
+
const endpoint = (0, endpoint_1.normalizeEndpoint)(options.endpoint);
|
|
55
|
+
const session = await this.createClient({ endpoint }).signIn(options);
|
|
56
|
+
return this.adopt(endpoint, options.email, session);
|
|
57
|
+
}
|
|
58
|
+
/** Create an account on `endpoint`, then store the session it returns. */
|
|
59
|
+
async signUp(options) {
|
|
60
|
+
const endpoint = (0, endpoint_1.normalizeEndpoint)(options.endpoint);
|
|
61
|
+
const session = await this.createClient({ endpoint }).signUp(options);
|
|
62
|
+
return this.adopt(endpoint, options.email, session);
|
|
63
|
+
}
|
|
64
|
+
async listAccounts() {
|
|
65
|
+
const items = await this.vault.listItems({ kind: 'account' });
|
|
66
|
+
return Promise.all(items.map((item) => this.readAccount(item)));
|
|
67
|
+
}
|
|
68
|
+
async getAccount(itemId) {
|
|
69
|
+
return this.readAccount(await this.requireItem(itemId, 'account'));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The stored bearer token, or null when signed out or expired. This is the
|
|
73
|
+
* value a harness host would ask for; expiry is checked here so a caller
|
|
74
|
+
* cannot accidentally present a dead token.
|
|
75
|
+
*/
|
|
76
|
+
async accessToken(itemId) {
|
|
77
|
+
const fields = await this.readFields(itemId);
|
|
78
|
+
const token = fields[ACCOUNT_FIELDS.accessToken];
|
|
79
|
+
if (!token)
|
|
80
|
+
return null;
|
|
81
|
+
if ((0, exports.hasExpired)(fields[ACCOUNT_FIELDS.expiresAt] ?? null, this.now())) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
return token;
|
|
85
|
+
}
|
|
86
|
+
/** Sign out server-side and drop the token, keeping the account item. */
|
|
87
|
+
async signOut(itemId) {
|
|
88
|
+
const fields = await this.readFields(itemId);
|
|
89
|
+
const token = fields[ACCOUNT_FIELDS.accessToken];
|
|
90
|
+
const endpoint = fields[ACCOUNT_FIELDS.endpoint];
|
|
91
|
+
if (token) {
|
|
92
|
+
await this.createClient({ endpoint, token }).signOut();
|
|
93
|
+
}
|
|
94
|
+
await this.vault.deleteField(itemId, ACCOUNT_FIELDS.accessToken);
|
|
95
|
+
await this.vault.deleteField(itemId, ACCOUNT_FIELDS.expiresAt);
|
|
96
|
+
}
|
|
97
|
+
/** Remove the account and every API key stored under it, locally. */
|
|
98
|
+
async forget(itemId) {
|
|
99
|
+
for (const key of await this.listApiKeys(itemId)) {
|
|
100
|
+
await this.vault.deleteItemForever(key.itemId);
|
|
101
|
+
}
|
|
102
|
+
await this.vault.deleteItemForever(itemId);
|
|
103
|
+
}
|
|
104
|
+
// ─── one-time codes ───────────────────────────────────────────────────────
|
|
105
|
+
/**
|
|
106
|
+
* Point an account at a code already in the vault, so an MFA step-up can be
|
|
107
|
+
* answered without reaching for a phone. Only the item id is stored; the seed
|
|
108
|
+
* stays where it was, concealed, and is never copied.
|
|
109
|
+
*/
|
|
110
|
+
async linkTotp(accountItemId, totpItemId) {
|
|
111
|
+
await this.requireItem(accountItemId, 'account');
|
|
112
|
+
await this.requireItem(totpItemId, 'totp');
|
|
113
|
+
const fields = await this.vault.listFields(totpItemId);
|
|
114
|
+
if (!fields.some((field) => field.purpose === 'totp_seed')) {
|
|
115
|
+
throw new client_1.AuthError('linkTotp', `item ${totpItemId} carries no code seed`);
|
|
116
|
+
}
|
|
117
|
+
await this.vault.setField(accountItemId, ACCOUNT_FIELDS.totpItem, 'text', totpItemId, false);
|
|
118
|
+
}
|
|
119
|
+
async unlinkTotp(accountItemId) {
|
|
120
|
+
await this.vault.deleteField(accountItemId, ACCOUNT_FIELDS.totpItem);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* A current code for the account's linked item, or null when nothing is
|
|
124
|
+
* linked — the caller then has to ask a human for one.
|
|
125
|
+
*/
|
|
126
|
+
async stepUpCode(accountItemId) {
|
|
127
|
+
const fields = await this.readFields(accountItemId);
|
|
128
|
+
const totpItemId = fields[ACCOUNT_FIELDS.totpItem];
|
|
129
|
+
if (!totpItemId)
|
|
130
|
+
return null;
|
|
131
|
+
if (!(await this.vault.getItem(totpItemId))) {
|
|
132
|
+
await this.unlinkTotp(accountItemId);
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
return this.vault.totpCode(totpItemId);
|
|
136
|
+
}
|
|
137
|
+
// ─── api keys ─────────────────────────────────────────────────────────────
|
|
138
|
+
/**
|
|
139
|
+
* Mint an API key for an account. The server shows the secret exactly once,
|
|
140
|
+
* so it goes straight into the vault and is never returned to the caller —
|
|
141
|
+
* read it back with `revealApiKey` when it is actually needed.
|
|
142
|
+
*/
|
|
143
|
+
async createApiKey(accountItemId, options, proof) {
|
|
144
|
+
const account = await this.readFields(accountItemId);
|
|
145
|
+
const token = await this.requireToken(accountItemId);
|
|
146
|
+
const endpoint = account[ACCOUNT_FIELDS.endpoint];
|
|
147
|
+
const created = await this.withStepUp(accountItemId, endpoint, token, proof, (client) => client.createApiKey(options));
|
|
148
|
+
const item = await this.vault.createItem('api_key', options.name);
|
|
149
|
+
await this.vault.setField(item.id, KEY_FIELDS.endpoint, 'url', endpoint, false);
|
|
150
|
+
await this.vault.setField(item.id, KEY_FIELDS.accountId, 'text', accountItemId, false);
|
|
151
|
+
await this.vault.setField(item.id, KEY_FIELDS.keyId, 'text', created.keyId, false);
|
|
152
|
+
await this.vault.setField(item.id, KEY_FIELDS.apiKey, 'token', created.apiKey);
|
|
153
|
+
if (options.principalId) {
|
|
154
|
+
await this.vault.setField(item.id, KEY_FIELDS.principalId, 'text', options.principalId, false);
|
|
155
|
+
}
|
|
156
|
+
if (options.orgId) {
|
|
157
|
+
await this.vault.setField(item.id, KEY_FIELDS.orgId, 'text', options.orgId, false);
|
|
158
|
+
}
|
|
159
|
+
if (options.databaseId) {
|
|
160
|
+
await this.vault.setField(item.id, KEY_FIELDS.databaseId, 'text', options.databaseId, false);
|
|
161
|
+
}
|
|
162
|
+
if (created.expiresAt) {
|
|
163
|
+
await this.vault.setField(item.id, KEY_FIELDS.expiresAt, 'text', created.expiresAt, false);
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
itemId: item.id,
|
|
167
|
+
accountItemId,
|
|
168
|
+
endpoint,
|
|
169
|
+
keyId: created.keyId,
|
|
170
|
+
name: options.name,
|
|
171
|
+
expiresAt: created.expiresAt,
|
|
172
|
+
databaseId: options.databaseId ?? null,
|
|
173
|
+
principalId: options.principalId ?? null,
|
|
174
|
+
orgId: options.orgId ?? null,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Say that a key already in the vault is a database's data-plane token, so a
|
|
179
|
+
* harness host asking for that database is handed this key and no other.
|
|
180
|
+
*/
|
|
181
|
+
async assignKeyToDatabase(keyItemId, databaseId) {
|
|
182
|
+
await this.requireItem(keyItemId, 'api_key');
|
|
183
|
+
await this.vault.setField(keyItemId, KEY_FIELDS.databaseId, 'text', databaseId, false);
|
|
184
|
+
}
|
|
185
|
+
/** Every stored key, or only those minted by one account. */
|
|
186
|
+
async listApiKeys(accountItemId) {
|
|
187
|
+
const items = await this.vault.listItems({ kind: 'api_key' });
|
|
188
|
+
const keys = await Promise.all(items.map((item) => this.readApiKey(item)));
|
|
189
|
+
return accountItemId
|
|
190
|
+
? keys.filter((key) => key.accountItemId === accountItemId)
|
|
191
|
+
: keys;
|
|
192
|
+
}
|
|
193
|
+
async revealApiKey(itemId) {
|
|
194
|
+
return this.vault.revealField(itemId, KEY_FIELDS.apiKey);
|
|
195
|
+
}
|
|
196
|
+
/** Revoke server-side, then delete the local copy. */
|
|
197
|
+
async revokeApiKey(itemId, proof) {
|
|
198
|
+
const fields = await this.readFields(itemId);
|
|
199
|
+
const accountItemId = fields[KEY_FIELDS.accountId];
|
|
200
|
+
const token = await this.requireToken(accountItemId);
|
|
201
|
+
await this.withStepUp(accountItemId, fields[KEY_FIELDS.endpoint], token, proof, (client) => client.revokeApiKey(fields[KEY_FIELDS.keyId]));
|
|
202
|
+
await this.vault.deleteItemForever(itemId);
|
|
203
|
+
}
|
|
204
|
+
// ─── principals ───────────────────────────────────────────────────────────
|
|
205
|
+
/**
|
|
206
|
+
* The account's scoped sub-identities, straight from the server — nothing
|
|
207
|
+
* about a principal is secret, and a stale local copy would be worse than
|
|
208
|
+
* none, so this is not cached in the vault.
|
|
209
|
+
*/
|
|
210
|
+
async listPrincipals(accountItemId) {
|
|
211
|
+
const fields = await this.readFields(accountItemId);
|
|
212
|
+
const token = await this.requireToken(accountItemId);
|
|
213
|
+
return this.createClient({
|
|
214
|
+
endpoint: fields[ACCOUNT_FIELDS.endpoint],
|
|
215
|
+
token,
|
|
216
|
+
}).listPrincipals();
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Create a principal an API key can then be minted as. A principal is a
|
|
220
|
+
* narrowing of its owner: it can be read-only, restricted per scope, and
|
|
221
|
+
* allowed to skip step-up, but it can never reach further than the human.
|
|
222
|
+
*/
|
|
223
|
+
async createPrincipal(accountItemId, options, proof) {
|
|
224
|
+
const fields = await this.readFields(accountItemId);
|
|
225
|
+
const token = await this.requireToken(accountItemId);
|
|
226
|
+
return this.withStepUp(accountItemId, fields[ACCOUNT_FIELDS.endpoint], token, proof, (client) => client.createPrincipal(options));
|
|
227
|
+
}
|
|
228
|
+
async deletePrincipal(accountItemId, principalId, proof) {
|
|
229
|
+
const fields = await this.readFields(accountItemId);
|
|
230
|
+
const token = await this.requireToken(accountItemId);
|
|
231
|
+
await this.withStepUp(accountItemId, fields[ACCOUNT_FIELDS.endpoint], token, proof, (client) => client.deletePrincipal(principalId));
|
|
232
|
+
}
|
|
233
|
+
// ─── internals ────────────────────────────────────────────────────────────
|
|
234
|
+
/**
|
|
235
|
+
* Run a sensitive operation, and if the server asks for a freshly proved
|
|
236
|
+
* factor, prove it and run *the same* operation once more. The request is
|
|
237
|
+
* held rather than rebuilt, so nothing about it can change between the two
|
|
238
|
+
* attempts. Without a proof the `StepUpRequiredError` propagates, which is
|
|
239
|
+
* what lets a caller collect one and try again — except for a code demand on
|
|
240
|
+
* an account with a linked code, which the vault answers by itself.
|
|
241
|
+
*/
|
|
242
|
+
async withStepUp(accountItemId, endpoint, token, proof, run) {
|
|
243
|
+
const client = this.createClient({ endpoint, token });
|
|
244
|
+
try {
|
|
245
|
+
return await run(client);
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
if (!(error instanceof client_1.StepUpRequiredError))
|
|
249
|
+
throw error;
|
|
250
|
+
if (error.kind === 'mfa') {
|
|
251
|
+
const code = proof?.totpCode ?? (await this.stepUpCode(accountItemId));
|
|
252
|
+
if (!code)
|
|
253
|
+
throw error;
|
|
254
|
+
await client.verifyTotp(code);
|
|
255
|
+
}
|
|
256
|
+
else if (proof?.password) {
|
|
257
|
+
await client.verifyPassword(proof.password);
|
|
258
|
+
}
|
|
259
|
+
else if (proof?.totpCode) {
|
|
260
|
+
await client.verifyTotp(proof.totpCode);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
// A password demand needs the password; only fresh auth takes a code.
|
|
264
|
+
const code = error.kind === 'fresh_auth' ? await this.stepUpCode(accountItemId) : null;
|
|
265
|
+
if (!code)
|
|
266
|
+
throw error;
|
|
267
|
+
await client.verifyTotp(code);
|
|
268
|
+
}
|
|
269
|
+
return run(client);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async adopt(endpoint, email, session) {
|
|
273
|
+
const existing = await this.findAccount(endpoint, email);
|
|
274
|
+
const itemId = existing?.id ??
|
|
275
|
+
(await this.vault.createItem('account', `${email} @ ${hostOf(endpoint)}`)).id;
|
|
276
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.endpoint, 'url', endpoint, false);
|
|
277
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.email, 'username', email, false);
|
|
278
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.userId, 'text', session.userId, false);
|
|
279
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.accessToken, 'token', session.accessToken);
|
|
280
|
+
if (session.accessTokenExpiresAt) {
|
|
281
|
+
await this.vault.setField(itemId, ACCOUNT_FIELDS.expiresAt, 'text', session.accessTokenExpiresAt, false);
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
itemId,
|
|
285
|
+
endpoint,
|
|
286
|
+
email,
|
|
287
|
+
userId: session.userId,
|
|
288
|
+
totpItemId: (await this.readFields(itemId))[ACCOUNT_FIELDS.totpItem] ?? null,
|
|
289
|
+
accessTokenExpiresAt: session.accessTokenExpiresAt,
|
|
290
|
+
signedIn: !(0, exports.hasExpired)(session.accessTokenExpiresAt, this.now()),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
async findAccount(endpoint, email) {
|
|
294
|
+
for (const item of await this.vault.listItems({ kind: 'account' })) {
|
|
295
|
+
const fields = await this.readFields(item.id);
|
|
296
|
+
if (fields[ACCOUNT_FIELDS.endpoint] === endpoint &&
|
|
297
|
+
fields[ACCOUNT_FIELDS.email] === email) {
|
|
298
|
+
return item;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
async requireItem(itemId, kind) {
|
|
304
|
+
const item = await this.vault.getItem(itemId);
|
|
305
|
+
if (!item || item.kind !== kind) {
|
|
306
|
+
throw new Error(`no ${kind} item ${itemId} in this vault`);
|
|
307
|
+
}
|
|
308
|
+
return item;
|
|
309
|
+
}
|
|
310
|
+
async requireToken(accountItemId) {
|
|
311
|
+
const token = await this.accessToken(accountItemId);
|
|
312
|
+
if (!token) {
|
|
313
|
+
throw new client_1.AuthError('accessToken', `account ${accountItemId} is signed out — sign in again first`);
|
|
314
|
+
}
|
|
315
|
+
return token;
|
|
316
|
+
}
|
|
317
|
+
async readAccount(item) {
|
|
318
|
+
const fields = await this.readFields(item.id);
|
|
319
|
+
const expiresAt = fields[ACCOUNT_FIELDS.expiresAt] ?? null;
|
|
320
|
+
return {
|
|
321
|
+
itemId: item.id,
|
|
322
|
+
endpoint: fields[ACCOUNT_FIELDS.endpoint] ?? '',
|
|
323
|
+
email: fields[ACCOUNT_FIELDS.email] ?? item.title,
|
|
324
|
+
userId: fields[ACCOUNT_FIELDS.userId] ?? '',
|
|
325
|
+
totpItemId: fields[ACCOUNT_FIELDS.totpItem] ?? null,
|
|
326
|
+
accessTokenExpiresAt: expiresAt,
|
|
327
|
+
signedIn: Boolean(fields[ACCOUNT_FIELDS.accessToken]) &&
|
|
328
|
+
!(0, exports.hasExpired)(expiresAt, this.now()),
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
async readApiKey(item) {
|
|
332
|
+
const fields = await this.readFields(item.id);
|
|
333
|
+
return {
|
|
334
|
+
itemId: item.id,
|
|
335
|
+
accountItemId: fields[KEY_FIELDS.accountId] ?? '',
|
|
336
|
+
endpoint: fields[KEY_FIELDS.endpoint] ?? '',
|
|
337
|
+
keyId: fields[KEY_FIELDS.keyId] ?? '',
|
|
338
|
+
name: item.title,
|
|
339
|
+
expiresAt: fields[KEY_FIELDS.expiresAt] ?? null,
|
|
340
|
+
databaseId: fields[KEY_FIELDS.databaseId] ?? null,
|
|
341
|
+
principalId: fields[KEY_FIELDS.principalId] ?? null,
|
|
342
|
+
orgId: fields[KEY_FIELDS.orgId] ?? null,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/** Every field of an item, decrypted, keyed by name. */
|
|
346
|
+
async readFields(itemId) {
|
|
347
|
+
const metas = await this.vault.listFields(itemId);
|
|
348
|
+
const entries = await Promise.all(metas.map(async (meta) => [meta.name, await this.vault.revealField(itemId, meta.name)]));
|
|
349
|
+
return Object.fromEntries(entries);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
exports.AccountManager = AccountManager;
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decryption/accounts",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Constructive accounts and API keys, held in the local dcrypt vault",
|
|
5
|
+
"author": "Constructive <developers@constructive.io>",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/decryption",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/decryption"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/decryption/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "makage assets",
|
|
24
|
+
"clean": "makage clean",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"build": "makage build",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
29
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"constructive",
|
|
33
|
+
"accounts",
|
|
34
|
+
"api-keys",
|
|
35
|
+
"auth",
|
|
36
|
+
"vault",
|
|
37
|
+
"local-first"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@constructive-io/sdk": "^1.11.9",
|
|
41
|
+
"@decryption/vault": "0.4.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"makage": "0.3.0"
|
|
45
|
+
},
|
|
46
|
+
"gitHead": "d29d5f2e516edf049a285b30f868360d3b996db0"
|
|
47
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/** A Constructive account held in the vault. */
|
|
2
|
+
export interface AccountRecord {
|
|
3
|
+
/** Vault item id; the handle for every other call. */
|
|
4
|
+
itemId: string;
|
|
5
|
+
/** Auth GraphQL endpoint this account belongs to. */
|
|
6
|
+
endpoint: string;
|
|
7
|
+
email: string;
|
|
8
|
+
userId: string;
|
|
9
|
+
/** Vault item holding a one-time code that can answer an MFA step-up. */
|
|
10
|
+
totpItemId: string | null;
|
|
11
|
+
/** When the stored access token stops working, if the server said. */
|
|
12
|
+
accessTokenExpiresAt: string | null;
|
|
13
|
+
/** False once signed out, or once the token has expired. */
|
|
14
|
+
signedIn: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** An API key minted for an account. The secret itself stays in the vault. */
|
|
17
|
+
export interface ApiKeyRecord {
|
|
18
|
+
itemId: string;
|
|
19
|
+
/** Vault item id of the account that minted it. */
|
|
20
|
+
accountItemId: string;
|
|
21
|
+
endpoint: string;
|
|
22
|
+
/** Server-side id, and the handle used to revoke. */
|
|
23
|
+
keyId: string;
|
|
24
|
+
name: string;
|
|
25
|
+
expiresAt: string | null;
|
|
26
|
+
/** The provisioned database this key is the data-plane token for, if any. */
|
|
27
|
+
databaseId: string | null;
|
|
28
|
+
/** The scoped sub-identity this key acts as, if it was minted for one. */
|
|
29
|
+
principalId: string | null;
|
|
30
|
+
/** The organization it is scoped to, for an org key. */
|
|
31
|
+
orgId: string | null;
|
|
32
|
+
}
|
|
33
|
+
/** What a sign-in or sign-up returns before it is written to the vault. */
|
|
34
|
+
export interface AuthSession {
|
|
35
|
+
userId: string;
|
|
36
|
+
accessToken: string;
|
|
37
|
+
accessTokenExpiresAt: string | null;
|
|
38
|
+
}
|
|
39
|
+
export interface CreatedApiKey {
|
|
40
|
+
apiKey: string;
|
|
41
|
+
keyId: string;
|
|
42
|
+
expiresAt: string | null;
|
|
43
|
+
}
|
|
44
|
+
/** Quantities accepted by the server's interval input. */
|
|
45
|
+
export interface KeyLifetime {
|
|
46
|
+
days?: number;
|
|
47
|
+
hours?: number;
|
|
48
|
+
minutes?: number;
|
|
49
|
+
months?: number;
|
|
50
|
+
years?: number;
|
|
51
|
+
}
|
|
52
|
+
export interface CreateApiKeyOptions {
|
|
53
|
+
name: string;
|
|
54
|
+
expiresIn?: KeyLifetime;
|
|
55
|
+
accessLevel?: string;
|
|
56
|
+
/** Tag the key as this database's data-plane token, for a harness host. */
|
|
57
|
+
databaseId?: string;
|
|
58
|
+
/** Mint the key *as* this principal, so it carries the principal's scope. */
|
|
59
|
+
principalId?: string;
|
|
60
|
+
/** Mint an org key, billed and scoped to this organization. */
|
|
61
|
+
orgId?: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A per-scope narrowing of a principal. No row for a scope means the principal
|
|
65
|
+
* simply inherits its owner there — an override can only take access away.
|
|
66
|
+
*/
|
|
67
|
+
export interface PrincipalScope {
|
|
68
|
+
/** The scope level (membership type) this row restricts. */
|
|
69
|
+
membershipType: number;
|
|
70
|
+
/** Bitmask AND-ed with the owner's permissions during the SPRT cascade. */
|
|
71
|
+
allowedMask: string | null;
|
|
72
|
+
isActive: boolean;
|
|
73
|
+
isReadOnly: boolean;
|
|
74
|
+
useAdminOwner: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A scoped sub-identity — what an API key or an agent actually acts as. It is
|
|
78
|
+
* owned by a human account and can never exceed that human's permissions.
|
|
79
|
+
*/
|
|
80
|
+
export interface PrincipalRecord {
|
|
81
|
+
principalId: string;
|
|
82
|
+
name: string;
|
|
83
|
+
ownerId: string | null;
|
|
84
|
+
isReadOnly: boolean;
|
|
85
|
+
bypassStepUp: boolean;
|
|
86
|
+
useAdminOwner: boolean;
|
|
87
|
+
/** Organizations (or other entities) this principal is scoped to. */
|
|
88
|
+
entityIds: string[];
|
|
89
|
+
scopes: PrincipalScope[];
|
|
90
|
+
}
|
|
91
|
+
export interface CreatePrincipalOptions {
|
|
92
|
+
name: string;
|
|
93
|
+
/**
|
|
94
|
+
* The organization to scope it to. Omitted means a personal principal: one
|
|
95
|
+
* that reaches wherever you do, which is what an unattended job of your own
|
|
96
|
+
* wants — an org id would only narrow it.
|
|
97
|
+
*/
|
|
98
|
+
orgId?: string;
|
|
99
|
+
isReadOnly?: boolean;
|
|
100
|
+
/** Let it skip MFA step-up — the point of a CI identity. */
|
|
101
|
+
bypassStepUp?: boolean;
|
|
102
|
+
/** Inherit the owner's admin rights within the scope. */
|
|
103
|
+
useAdminOwner?: boolean;
|
|
104
|
+
}
|
package/types.js
ADDED