@le-space/orbitdb-identity-provider-webauthn-did 0.11.0 → 0.12.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/CHANGELOG.md +47 -0
- package/README.md +22 -1
- package/docs/API.md +113 -12
- package/package.json +6 -2
- package/src/errors.js +7 -0
- package/src/index.js +6 -0
- package/src/keystore/vault-keeper.js +555 -0
- package/src/keystore/vault.js +15 -1
- package/src/standalone/index.js +5 -0
- package/src/standalone/webauthn/signers.js +15 -13
- package/src/vault/index.js +28 -0
- package/types/index.d.ts +111 -11
- package/types/standalone.d.ts +113 -2
- package/types/vault.d.ts +29 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,52 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 0.12.0
|
|
4
|
+
|
|
5
|
+
The vault's keeper, once for every application, and the vault on its own entry.
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **`createVaultKeeper({ storage, key, slotInfo })`** (main entry, `./standalone` and `./vault`):
|
|
10
|
+
where an application keeps its vaults — anything with `getItem` and `setItem` — and what every
|
|
11
|
+
unlock does with them. `open` opens the vault this authenticator has a slot in, else one `find`
|
|
12
|
+
returns from elsewhere (a backup, an index), else makes one holding `create()`; `addPasskey`,
|
|
13
|
+
`removePasskey` (by slot id, so a key not at hand can go), `renew` (new vault key and id, every key
|
|
14
|
+
that stays answers and is checked against its slot, so none is left out by mistake),
|
|
15
|
+
`replacePayload`, `status`, `records`. A key removed here is refused (`VAULT_SLOT_REMOVED`)
|
|
16
|
+
instead of being given new, empty books; one key opens one vault per store; an unreadable store
|
|
17
|
+
is refused (`VAULT_STORE_UNREADABLE`) and never overwritten. Le-Space/belege and Le-Space/invoice
|
|
18
|
+
each built this around the vault for themselves; the store's format is Belege's (#406 there), and
|
|
19
|
+
a record Belege's own code wrote opens unchanged.
|
|
20
|
+
- **`rootPayload(root)`** and **`rootOf(payload)`**: one 32-byte root as vault payload,
|
|
21
|
+
`{"version":1,"root":"<hex>"}`, every other key derived from it with `deriveSubkey` — the bytes
|
|
22
|
+
Belege's vaults hold.
|
|
23
|
+
- **The `./vault` entry**: `readPrfOutput`, `deriveSubkey`, `deriveAesKey`, the vault functions,
|
|
24
|
+
the keeper, `rootPayload`, `rootOf`, `PrfUnavailableError` and `VaultError`, with no package in
|
|
25
|
+
its import graph (`tests/vault-entry.test.js`). For applications without OrbitDB (#93, its first
|
|
26
|
+
step).
|
|
27
|
+
- `VaultError` codes `VAULT_STORE_UNREADABLE`, `VAULT_SLOT_REMOVED` and `VAULT_SLOT_IN_USE`.
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
|
|
31
|
+
- **The production audit passes again.** Four `fast-uri` advisories published after 0.11.0
|
|
32
|
+
(GHSA-w74x-v6f9-j3xg and GHSA-wjr9-j795-jxcr moderate, GHSA-xhq7-xmqj-769h and
|
|
33
|
+
GHSA-333x-w4r6-6crp low) affect `fast-uri` below 3.1.9 on `iso-web > iso-kv > conf > ajv`. The
|
|
34
|
+
override `ajv>fast-uri` goes from `^3.1.8` to `^3.1.9`, still on the 3 line ajv 8 asks for. It
|
|
35
|
+
steers this repository's own install; an application resolves `fast-uri` for itself.
|
|
36
|
+
- **Declarations that said what the code does not.** Behaviour is unchanged.
|
|
37
|
+
- `WebAuthnVarsigProvider` had no `static createCredential`, though `docs/API.md` calls it, no
|
|
38
|
+
`isSupported` or `signPayload`, and no options for the constructor or `sign` (#95).
|
|
39
|
+
- `createWebAuthnEd25519Credential` was declared, and documented, to resolve to `null` when the
|
|
40
|
+
registration fails. It has always rejected, and now says so (#95).
|
|
41
|
+
- The varsig identity helpers: `encodeIdentityValue` resolves to `{ hash, bytes }` and was
|
|
42
|
+
declared to return bytes; `createWebAuthnVarsigIdentities` is synchronous and takes
|
|
43
|
+
`(identity, domainLabels?, storage?, fallbackIdentities?)`; `wrapWithVarsigVerification` takes
|
|
44
|
+
`(defaultIdentities, ipfs?, domainLabels?)`; `decodeVarsigIdentityFromBytes` and
|
|
45
|
+
`verifyVarsigIdentity` take `domainLabels`; `createIpfsIdentityStorage` takes the IPFS
|
|
46
|
+
instance, which the declared `Record<string, unknown>` refused for a typed Helia.
|
|
47
|
+
- The README and `docs/API.md` dated the varsig registration's authenticator fix to 0.9.1. It
|
|
48
|
+
shipped in 0.10.0 (#95).
|
|
49
|
+
|
|
3
50
|
## 0.11.0
|
|
4
51
|
|
|
5
52
|
Another session of the same passkey may still write: OrbitDB's verified-identity cache, worked
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ This package provides:
|
|
|
19
19
|
- Authentication/assertion requests omit `allowCredentials` by default, so the browser/authenticator can resolve the credential discoverably.
|
|
20
20
|
- You can switch this centrally with `configureWebAuthn({ discoverableCredentials: true|false })`.
|
|
21
21
|
- Registration is still the point where this package extracts the credential public key from attestation.
|
|
22
|
-
- Registration offers every kind of authenticator — the device's own, a security key such as a YubiKey, a phone — unless `authenticatorType: 'platform' | 'cross-platform'` narrows it
|
|
22
|
+
- Registration offers every kind of authenticator — the device's own, a security key such as a YubiKey, a phone — unless `authenticatorType: 'platform' | 'cross-platform'` narrows it — honoured by `createCredential` since 0.9.1, and by the varsig registration and the standalone signers since 0.10.0. It always requires user verification.
|
|
23
23
|
- Later `navigator.credentials.get()` assertions do not reliably return the public key again, so identity reconstruction still needs metadata from somewhere else.
|
|
24
24
|
|
|
25
25
|
Since **0.6.0** the passkey alone is enough, and stored metadata is a convenience rather than a requirement:
|
|
@@ -252,6 +252,27 @@ const withSecondKey = await addSlot(vault, vaultKey, secondKeysSlot);
|
|
|
252
252
|
|
|
253
253
|
How it works, and what a vault does not do: [docs/API.md](docs/API.md#prf-keys-and-vaults).
|
|
254
254
|
|
|
255
|
+
Since **0.12.0**, `createVaultKeeper({ storage, key, slotInfo })` keeps an application's vaults and does what every unlock needs: open this key's vault or make one, give a second key a slot, take one out, renew the vault key. A key taken out is refused rather than given new, empty books, and an unreadable store is never overwritten. `rootPayload(root)` holds one random root, every other key derived from it with `deriveSubkey`. The `./vault` entry offers all of this with no package in its import graph, for applications without OrbitDB. See [docs/API.md](docs/API.md#keeping-vaults-createvaultkeeper-storage-key-slotinfo-).
|
|
256
|
+
|
|
257
|
+
```javascript
|
|
258
|
+
import {
|
|
259
|
+
createVaultKeeper,
|
|
260
|
+
readPrfOutput,
|
|
261
|
+
rootPayload,
|
|
262
|
+
} from '@le-space/orbitdb-identity-provider-webauthn-did/vault';
|
|
263
|
+
|
|
264
|
+
const keeper = createVaultKeeper({
|
|
265
|
+
storage: localStorage,
|
|
266
|
+
key: 'myapp.vaults.v1',
|
|
267
|
+
slotInfo: 'myapp/vault-slot/v1',
|
|
268
|
+
});
|
|
269
|
+
const opened = await keeper.open({
|
|
270
|
+
prfOutput: await readPrfOutput(credential),
|
|
271
|
+
rawCredentialId: credential.rawCredentialId,
|
|
272
|
+
create: () => rootPayload(crypto.getRandomValues(new Uint8Array(32))),
|
|
273
|
+
});
|
|
274
|
+
```
|
|
275
|
+
|
|
255
276
|
Since **0.10.0**, a secret kept in that vault can be an identity of its own: `createSecretSigner(secret, { info })` turns it into an Ed25519 signer for the provider's `signer` option, so every key that opens the vault writes as the same identity — the root of a database's access controller, which a single passkey's identity would hold for good. See [docs/API.md](docs/API.md#an-identity-that-is-nobodys-passkey-createsecretsignersecret--info-).
|
|
256
277
|
|
|
257
278
|
### Keystore-based DID (WebAuthn + OrbitDB keystore)
|
package/docs/API.md
CHANGED
|
@@ -6,6 +6,7 @@ public API surface:
|
|
|
6
6
|
- root export: `@le-space/orbitdb-identity-provider-webauthn-did`
|
|
7
7
|
- standalone export: `@le-space/orbitdb-identity-provider-webauthn-did/standalone`
|
|
8
8
|
- keystore export: `@le-space/orbitdb-identity-provider-webauthn-did/keystore`
|
|
9
|
+
- vault export: `@le-space/orbitdb-identity-provider-webauthn-did/vault`
|
|
9
10
|
- verification export: `@le-space/orbitdb-identity-provider-webauthn-did/verification`
|
|
10
11
|
|
|
11
12
|
## Root Export
|
|
@@ -386,6 +387,83 @@ What a vault does not do:
|
|
|
386
387
|
`tests/vault-two-authenticators.test.js` runs this in Chromium with two virtual
|
|
387
388
|
authenticators, each with its own PRF secret as two YubiKeys have.
|
|
388
389
|
|
|
390
|
+
#### Keeping vaults: `createVaultKeeper({ storage, key, slotInfo })`
|
|
391
|
+
|
|
392
|
+
Since 0.12.0. Where an application keeps its vaults, and what every unlock
|
|
393
|
+
does with them. Le-Space/belege and Le-Space/invoice each wrote this for
|
|
394
|
+
themselves around the same vault; the keeper is that code once, with the
|
|
395
|
+
rules each of them had to learn:
|
|
396
|
+
|
|
397
|
+
```js
|
|
398
|
+
const keeper = createVaultKeeper({
|
|
399
|
+
storage: localStorage, // anything with getItem and setItem
|
|
400
|
+
key: 'myapp.vaults.v1',
|
|
401
|
+
slotInfo: 'myapp/vault-slot/v1',
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
const prfOutput = await readPrfOutput(credential);
|
|
405
|
+
const opened = await keeper.open({
|
|
406
|
+
prfOutput,
|
|
407
|
+
rawCredentialId: credential.rawCredentialId,
|
|
408
|
+
label: 'YubiKey 5C',
|
|
409
|
+
create: () => rootPayload(crypto.getRandomValues(new Uint8Array(32))),
|
|
410
|
+
});
|
|
411
|
+
const root = rootOf(opened.payload);
|
|
412
|
+
const listKey = await deriveSubkey(root, 'myapp/list-key/v1');
|
|
413
|
+
|
|
414
|
+
// With the vault open, touch the second key and give it a slot of its own.
|
|
415
|
+
await keeper.addPasskey(opened, {
|
|
416
|
+
prfOutput: secondPrfOutput,
|
|
417
|
+
rawCredentialId,
|
|
418
|
+
label,
|
|
419
|
+
});
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
- `open({ prfOutput, rawCredentialId, label?, create?, find? })` opens the
|
|
423
|
+
vault this authenticator has a slot in. Without one it asks `find(kid)` for
|
|
424
|
+
a record kept elsewhere — a backup, an index — and keeps it here once it has
|
|
425
|
+
opened; without that it makes a new vault holding `create()`, with this
|
|
426
|
+
authenticator's slot. It returns `{ record, payload, vaultKey, kid, created,
|
|
427
|
+
found }`; keep that in memory while the vault is open, it holds the vault key.
|
|
428
|
+
- Pass `find` on every unlock where the vault may be kept elsewhere, not only
|
|
429
|
+
on a restore path. A synced passkey on a new device otherwise makes a vault
|
|
430
|
+
of its own, and from then on `open` finds that one here and never asks
|
|
431
|
+
`find` again (Le-Space/belege#524).
|
|
432
|
+
- An authenticator removed here is refused (`VAULT_SLOT_REMOVED`), never given
|
|
433
|
+
a new vault: for an application whose books existed before the vault, that
|
|
434
|
+
would be a stale copy that looks like the real books.
|
|
435
|
+
- `addPasskey(opened, { prfOutput, rawCredentialId, label? })` gives one more
|
|
436
|
+
authenticator a slot. One key opens one vault per store: a key that already
|
|
437
|
+
opens another vault here is refused (`VAULT_SLOT_EXISTS`).
|
|
438
|
+
- `removePasskey(opened, kid)` takes a slot out by its id, so a key that is not
|
|
439
|
+
at hand can go too. It revokes nothing already opened.
|
|
440
|
+
- `renew(opened, { answers, removed?, payload? })` makes a new vault key and
|
|
441
|
+
vault id, with a slot for every authenticator that stays. Every one of them
|
|
442
|
+
answers with its PRF output, checked against its slot, so none is left out
|
|
443
|
+
by mistake; the ones in `removed` are taken out. A new `payload` replaces
|
|
444
|
+
keys a removed authenticator must not know.
|
|
445
|
+
- `replacePayload(opened, payload)`, `status(rawCredentialId)` (is there a
|
|
446
|
+
vault here for this key, without asking it?) and `records()`.
|
|
447
|
+
|
|
448
|
+
The store holds a JSON array under `key`, one record per vault:
|
|
449
|
+
`{ vault, slots: [{ kid, label, credentialId, addedAt }], removed: [kid] }`.
|
|
450
|
+
`slots` names in public which authenticator each slot belongs to; `removed`
|
|
451
|
+
remembers the keys taken out here; fields the keeper does not know are kept.
|
|
452
|
+
It is the format Belege has kept since Le-Space/belege#406, and
|
|
453
|
+
`tests/vault-keeper.test.js` opens a record Belege's own code wrote. An
|
|
454
|
+
unreadable store is refused (`VAULT_STORE_UNREADABLE`) and never overwritten:
|
|
455
|
+
it may hold the only copy of a vault. The keeper holds no secret, and two pages
|
|
456
|
+
writing one store at once means the last write wins.
|
|
457
|
+
|
|
458
|
+
`rootPayload(root)` and `rootOf(payload)` write and read the simplest payload,
|
|
459
|
+
`{"version":1,"root":"<hex>"}`: one 32-byte root, every other key derived from
|
|
460
|
+
it with `deriveSubkey(root, info)`, so the vault never changes when the
|
|
461
|
+
application needs one more key. Belege's vaults hold exactly that.
|
|
462
|
+
|
|
463
|
+
More refusals from the keeper, as `VaultError` codes: `VAULT_STORE_UNREADABLE`,
|
|
464
|
+
`VAULT_SLOT_REMOVED`, and `VAULT_SLOT_IN_USE` — the authenticator that opened
|
|
465
|
+
the vault cannot remove itself, or be left out of a renewal.
|
|
466
|
+
|
|
389
467
|
#### An identity that is nobody's passkey: `createSecretSigner(secret, { info })`
|
|
390
468
|
|
|
391
469
|
Since 0.10.0. A database whose access controller is rooted at one passkey's
|
|
@@ -523,12 +601,13 @@ Key exports:
|
|
|
523
601
|
|
|
524
602
|
- `WebAuthnVarsigProvider`
|
|
525
603
|
- `createWebAuthnVarsigIdentity(options)`
|
|
526
|
-
- `createWebAuthnVarsigIdentities(
|
|
527
|
-
|
|
528
|
-
- `
|
|
529
|
-
- `
|
|
530
|
-
- `
|
|
531
|
-
- `
|
|
604
|
+
- `createWebAuthnVarsigIdentities(identity, domainLabels?, storage?, fallbackIdentities?)`
|
|
605
|
+
— synchronous
|
|
606
|
+
- `encodeIdentityValue(value)` — resolves to `{ hash, bytes }`
|
|
607
|
+
- `decodeVarsigIdentityFromBytes(bytes, domainLabels?)`
|
|
608
|
+
- `verifyVarsigIdentity(identity, domainLabels?)`
|
|
609
|
+
- `createIpfsIdentityStorage(ipfs)`
|
|
610
|
+
- `wrapWithVarsigVerification(defaultIdentities, ipfs?, domainLabels?)`
|
|
532
611
|
- `DEFAULT_DOMAIN_LABELS`
|
|
533
612
|
- `storeWebAuthnVarsigCredential(credential, key?)`
|
|
534
613
|
- `loadWebAuthnVarsigCredential(key?)`
|
|
@@ -549,8 +628,8 @@ const identity = await createWebAuthnVarsigIdentity({ credential });
|
|
|
549
628
|
`WebAuthnVarsigProvider.createCredential(options)` takes `userId`,
|
|
550
629
|
`displayName`, `domain`, `discoverableCredentials`, `forceP256` (offer only
|
|
551
630
|
P-256), and `authenticatorType` / `authenticatorAttachment` as
|
|
552
|
-
`WebAuthnDIDProvider.createCredential` does. Before 0.
|
|
553
|
-
of authenticator the same way and offered every kind.
|
|
631
|
+
`WebAuthnDIDProvider.createCredential` does. Before 0.10.0 it dropped the
|
|
632
|
+
choice of authenticator the same way and offered every kind.
|
|
554
633
|
|
|
555
634
|
It always requires user verification, whatever the options say. A varsig
|
|
556
635
|
signature without the UV flag is refused, when it is made and when it is
|
|
@@ -582,7 +661,9 @@ import {
|
|
|
582
661
|
|
|
583
662
|
The `create…Signer` functions register as
|
|
584
663
|
`WebAuthnVarsigProvider.createCredential` does and take its options;
|
|
585
|
-
`createWebAuthnEd25519Credential` passes on `options.authenticatorType` alone
|
|
664
|
+
`createWebAuthnEd25519Credential` passes on `options.authenticatorType` alone,
|
|
665
|
+
and rejects when the registration fails — it never resolved to `null`, though
|
|
666
|
+
its JSDoc and types said so.
|
|
586
667
|
|
|
587
668
|
```js
|
|
588
669
|
const signer = await createWebAuthnSigner({
|
|
@@ -621,9 +702,9 @@ const ucanSigner = signer.toUcantoSigner({
|
|
|
621
702
|
|
|
622
703
|
The same functions as the root export, without OrbitDB: `readPrfOutput`,
|
|
623
704
|
`deriveSubkey`, `deriveAesKey`, `createVault`, `openVault`, `addSlot`,
|
|
624
|
-
`removeSlot`, `replacePayload`, `slotIdFor`, `
|
|
625
|
-
`
|
|
626
|
-
[PRF Keys and Vaults](#prf-keys-and-vaults).
|
|
705
|
+
`removeSlot`, `replacePayload`, `slotIdFor`, `createVaultKeeper`,
|
|
706
|
+
`rootPayload`, `rootOf`, `createSecretSigner`, `PrfUnavailableError` and
|
|
707
|
+
`VaultError`. See [PRF Keys and Vaults](#prf-keys-and-vaults).
|
|
627
708
|
|
|
628
709
|
### P-256 Wallet Primitives
|
|
629
710
|
|
|
@@ -668,6 +749,25 @@ const { did, publicKey, archive } = await client.generateEd25519Identity();
|
|
|
668
749
|
const signature = await client.sign(new TextEncoder().encode('entry'));
|
|
669
750
|
```
|
|
670
751
|
|
|
752
|
+
## Vault Export
|
|
753
|
+
|
|
754
|
+
Since 0.12.0. The passkey vault on its own, for an application that keeps its
|
|
755
|
+
data outside OrbitDB: `readPrfOutput`, `deriveSubkey`, `deriveAesKey`,
|
|
756
|
+
`createVault`, `openVault`, `addSlot`, `removeSlot`, `replacePayload`,
|
|
757
|
+
`slotIdFor`, `createVaultKeeper`, `rootPayload`, `rootOf`,
|
|
758
|
+
`PrfUnavailableError` and `VaultError`. Its import graph holds no package at
|
|
759
|
+
all — WebCrypto and `navigator.credentials` are all it needs, and
|
|
760
|
+
`tests/vault-entry.test.js` keeps it that way. `createSecretSigner` is not in
|
|
761
|
+
it: it needs `@libp2p/crypto`.
|
|
762
|
+
|
|
763
|
+
```js
|
|
764
|
+
import {
|
|
765
|
+
createVaultKeeper,
|
|
766
|
+
readPrfOutput,
|
|
767
|
+
rootPayload,
|
|
768
|
+
} from '@le-space/orbitdb-identity-provider-webauthn-did/vault';
|
|
769
|
+
```
|
|
770
|
+
|
|
671
771
|
## Verification Export
|
|
672
772
|
|
|
673
773
|
```js
|
|
@@ -699,6 +799,7 @@ The package publishes declaration files:
|
|
|
699
799
|
- `types/index.d.ts`
|
|
700
800
|
- `types/keystore.d.ts`
|
|
701
801
|
- `types/standalone.d.ts`
|
|
802
|
+
- `types/vault.d.ts`
|
|
702
803
|
- `types/verification.d.ts`
|
|
703
804
|
|
|
704
805
|
They are connected through `package.json` `exports.types`, so TypeScript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@le-space/orbitdb-identity-provider-webauthn-did",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "WebAuthn-based DID identity provider for OrbitDB for hardware-secured wallets and biometric Passkey authentication",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -21,6 +21,10 @@
|
|
|
21
21
|
"./keystore": {
|
|
22
22
|
"types": "./types/keystore.d.ts",
|
|
23
23
|
"import": "./src/keystore/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./vault": {
|
|
26
|
+
"types": "./types/vault.d.ts",
|
|
27
|
+
"import": "./src/vault/index.js"
|
|
24
28
|
}
|
|
25
29
|
},
|
|
26
30
|
"scripts": {
|
|
@@ -147,7 +151,7 @@
|
|
|
147
151
|
"iso-web": "^3.1.2",
|
|
148
152
|
"iso-webauthn-varsig": "npm:@le-space/iso-webauthn-varsig@^0.3.0",
|
|
149
153
|
"conf>ajv": "^8.18.0",
|
|
150
|
-
"ajv>fast-uri": "^3.1.
|
|
154
|
+
"ajv>fast-uri": "^3.1.9"
|
|
151
155
|
},
|
|
152
156
|
"onlyBuiltDependencies": [
|
|
153
157
|
"@ipshipyard/node-datachannel",
|
package/src/errors.js
CHANGED
|
@@ -11,6 +11,9 @@ export const ERROR_CODES = Object.freeze({
|
|
|
11
11
|
VAULT_LOCKED: 'VAULT_LOCKED',
|
|
12
12
|
VAULT_SLOT_EXISTS: 'VAULT_SLOT_EXISTS',
|
|
13
13
|
VAULT_LAST_SLOT: 'VAULT_LAST_SLOT',
|
|
14
|
+
VAULT_STORE_UNREADABLE: 'VAULT_STORE_UNREADABLE',
|
|
15
|
+
VAULT_SLOT_REMOVED: 'VAULT_SLOT_REMOVED',
|
|
16
|
+
VAULT_SLOT_IN_USE: 'VAULT_SLOT_IN_USE',
|
|
14
17
|
});
|
|
15
18
|
|
|
16
19
|
export class WebAuthnIdentityError extends Error {
|
|
@@ -96,6 +99,10 @@ export class PrfUnavailableError extends KeystoreEncryptionError {
|
|
|
96
99
|
* has no slot in it (`VAULT_NO_SLOT`), the key does not open it or it was
|
|
97
100
|
* altered (`VAULT_LOCKED`), the authenticator already has a slot
|
|
98
101
|
* (`VAULT_SLOT_EXISTS`), or the slot is the last one (`VAULT_LAST_SLOT`).
|
|
102
|
+
* From the vault keeper also: the vaults kept in a store cannot be read
|
|
103
|
+
* (`VAULT_STORE_UNREADABLE`), the authenticator was removed from the vault
|
|
104
|
+
* kept there (`VAULT_SLOT_REMOVED`), or it is the one that opened the vault
|
|
105
|
+
* and so cannot remove itself (`VAULT_SLOT_IN_USE`).
|
|
99
106
|
*/
|
|
100
107
|
export class VaultError extends KeystoreEncryptionError {
|
|
101
108
|
constructor(message, options = {}) {
|
package/src/index.js
CHANGED
|
@@ -335,6 +335,12 @@ export {
|
|
|
335
335
|
replacePayload,
|
|
336
336
|
slotIdFor,
|
|
337
337
|
} from './keystore/vault.js';
|
|
338
|
+
// Where the vaults are kept, and what every unlock does with them.
|
|
339
|
+
export {
|
|
340
|
+
createVaultKeeper,
|
|
341
|
+
rootPayload,
|
|
342
|
+
rootOf,
|
|
343
|
+
} from './keystore/vault-keeper.js';
|
|
338
344
|
// An identity that is nobody's passkey — the books every key of their owner
|
|
339
345
|
// writes as, its secret kept in a vault.
|
|
340
346
|
export { createSecretSigner } from './keystore/secret-signer.js';
|
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module VaultKeeper
|
|
3
|
+
* @description
|
|
4
|
+
* Where an application keeps its vaults, and what it does with them: on every
|
|
5
|
+
* unlock, find this authenticator's slot and open the vault, or make one; give
|
|
6
|
+
* a second key a slot; take a key out; renew the vault key.
|
|
7
|
+
*
|
|
8
|
+
* Le-Space/belege and Le-Space/invoice each wrote this for themselves around
|
|
9
|
+
* the same vault (`vault.js`), and the simple-todo chapters would have been
|
|
10
|
+
* the third copy. The rules are the same everywhere, and each is easy to get
|
|
11
|
+
* wrong once: an unreadable store is never overwritten, a removed key is
|
|
12
|
+
* refused instead of being given new, empty books, and a renewal leaves no
|
|
13
|
+
* key out that was meant to stay.
|
|
14
|
+
*
|
|
15
|
+
* The store is the application's: anything with `getItem` and `setItem`, as
|
|
16
|
+
* `localStorage` has them. Under one key it holds a JSON array, one record per
|
|
17
|
+
* vault:
|
|
18
|
+
*
|
|
19
|
+
* {
|
|
20
|
+
* "vault": { ...a version-1 vault, see vault.js },
|
|
21
|
+
* "slots": [
|
|
22
|
+
* { "kid": "<slot id>", "label": "YubiKey 5C",
|
|
23
|
+
* "credentialId": "<base64url>", "addedAt": "<ISO 8601>" }
|
|
24
|
+
* ],
|
|
25
|
+
* "removed": ["<slot id>"]
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* `slots` says in public which authenticator each slot belongs to, so an
|
|
29
|
+
* application can name them, and ask each one that stays when it renews the
|
|
30
|
+
* vault. `removed` remembers the authenticators taken out here. Fields the
|
|
31
|
+
* keeper does not know are kept as they are. This is the format Belege has
|
|
32
|
+
* kept since its vault (Le-Space/belege#406), so its records open unchanged.
|
|
33
|
+
*
|
|
34
|
+
* What goes into the vault is the application's too, as bytes. The simplest
|
|
35
|
+
* choice is one random root, with every key the application needs derived
|
|
36
|
+
* from it — `deriveSubkey(root, '<app>/<purpose>/v1')` — so the vault never
|
|
37
|
+
* has to change when the application needs one more key. `rootPayload` and
|
|
38
|
+
* `rootOf` write and read such a root, in the bytes Belege writes.
|
|
39
|
+
*
|
|
40
|
+
* What the keeper does not do:
|
|
41
|
+
* - It holds no secret. The PRF output and the vault key stay with the caller,
|
|
42
|
+
* in what `open` returns, for as long as the vault is open.
|
|
43
|
+
* - It decides nothing about where a record travels. `find` is the hook for a
|
|
44
|
+
* vault kept elsewhere — in a backup, in an index.
|
|
45
|
+
* - Two pages writing one store at once: the last write wins. A record is
|
|
46
|
+
* replaced whole, by its vault id.
|
|
47
|
+
*/
|
|
48
|
+
import { ERROR_CODES, VaultError } from '../errors.js';
|
|
49
|
+
import { deriveAesKey } from './prf-keys.js';
|
|
50
|
+
import {
|
|
51
|
+
addSlot,
|
|
52
|
+
createVault,
|
|
53
|
+
openVault,
|
|
54
|
+
removeSlotById,
|
|
55
|
+
replacePayload as replaceVaultPayload,
|
|
56
|
+
slotIdFor,
|
|
57
|
+
} from './vault.js';
|
|
58
|
+
|
|
59
|
+
const ROOT_VERSION = 1;
|
|
60
|
+
const ROOT_BYTES = 32;
|
|
61
|
+
const encoder = new TextEncoder();
|
|
62
|
+
const decoder = new TextDecoder();
|
|
63
|
+
|
|
64
|
+
/** @param {Uint8Array} bytes */
|
|
65
|
+
const toHex = (bytes) =>
|
|
66
|
+
Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
67
|
+
|
|
68
|
+
/** @param {string} hex */
|
|
69
|
+
function fromHex(hex) {
|
|
70
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
71
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
72
|
+
bytes[i] = parseInt(hex.slice(2 * i, 2 * i + 2), 16);
|
|
73
|
+
}
|
|
74
|
+
return bytes;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** @param {unknown} value @returns {Uint8Array} */
|
|
78
|
+
function bytesOf(value) {
|
|
79
|
+
if (value instanceof Uint8Array) return value;
|
|
80
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
81
|
+
if (ArrayBuffer.isView(value)) {
|
|
82
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
83
|
+
}
|
|
84
|
+
if (Array.isArray(value)) return Uint8Array.from(value);
|
|
85
|
+
throw new TypeError('rawCredentialId must be bytes');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** As WebAuthn writes a credential's id. @param {Uint8Array} bytes */
|
|
89
|
+
const base64url = (bytes) =>
|
|
90
|
+
btoa(String.fromCharCode(...bytes))
|
|
91
|
+
.replace(/\+/g, '-')
|
|
92
|
+
.replace(/\//g, '_')
|
|
93
|
+
.replace(/=+$/, '');
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* A root as vault payload: `{"version":1,"root":"<hex>"}`.
|
|
97
|
+
*
|
|
98
|
+
* @param {Uint8Array} root - 32 bytes, e.g. `crypto.getRandomValues`.
|
|
99
|
+
* @returns {Uint8Array}
|
|
100
|
+
*/
|
|
101
|
+
export function rootPayload(root) {
|
|
102
|
+
if (!(root instanceof Uint8Array) || root.length !== ROOT_BYTES) {
|
|
103
|
+
throw new TypeError(`root must be ${ROOT_BYTES} bytes`);
|
|
104
|
+
}
|
|
105
|
+
return encoder.encode(
|
|
106
|
+
JSON.stringify({ version: ROOT_VERSION, root: toHex(root) })
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The root a payload from `rootPayload` holds.
|
|
112
|
+
*
|
|
113
|
+
* @param {Uint8Array} payload
|
|
114
|
+
* @returns {Uint8Array} 32 bytes.
|
|
115
|
+
* @throws {VaultError} `VAULT_MALFORMED` for anything else.
|
|
116
|
+
*/
|
|
117
|
+
export function rootOf(payload) {
|
|
118
|
+
let parsed;
|
|
119
|
+
try {
|
|
120
|
+
parsed = JSON.parse(decoder.decode(payload));
|
|
121
|
+
} catch (cause) {
|
|
122
|
+
throw new VaultError('The vault holds no root', {
|
|
123
|
+
code: ERROR_CODES.VAULT_MALFORMED,
|
|
124
|
+
cause,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (
|
|
128
|
+
parsed?.version !== ROOT_VERSION ||
|
|
129
|
+
typeof parsed.root !== 'string' ||
|
|
130
|
+
!new RegExp(`^[0-9a-f]{${2 * ROOT_BYTES}}$`).test(parsed.root)
|
|
131
|
+
) {
|
|
132
|
+
throw new VaultError('The vault holds no root', {
|
|
133
|
+
code: ERROR_CODES.VAULT_MALFORMED,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return fromHex(parsed.root);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** @param {object} record @param {string} kid */
|
|
140
|
+
const holds = (record, kid) =>
|
|
141
|
+
record.vault.slots.some((slot) => slot?.kid === kid);
|
|
142
|
+
|
|
143
|
+
/** @param {unknown} record */
|
|
144
|
+
const isRecord = (record) =>
|
|
145
|
+
Boolean(record) &&
|
|
146
|
+
typeof record === 'object' &&
|
|
147
|
+
Boolean(record.vault) &&
|
|
148
|
+
typeof record.vault === 'object' &&
|
|
149
|
+
Array.isArray(record.vault.slots);
|
|
150
|
+
|
|
151
|
+
/** The record with `slots` and `removed` as arrays, everything else kept. */
|
|
152
|
+
const normalised = (record) => ({
|
|
153
|
+
...record,
|
|
154
|
+
slots: Array.isArray(record.slots) ? record.slots : [],
|
|
155
|
+
removed: Array.isArray(record.removed) ? record.removed.map(String) : [],
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
/** @param {Uint8Array|undefined} payload */
|
|
159
|
+
function payloadBytes(payload, what) {
|
|
160
|
+
if (!(payload instanceof Uint8Array)) {
|
|
161
|
+
throw new TypeError(`${what} must be the payload as bytes`);
|
|
162
|
+
}
|
|
163
|
+
return payload;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A keeper for the vaults kept under `key` in `storage`.
|
|
168
|
+
*
|
|
169
|
+
* @param {Object} options
|
|
170
|
+
* @param {Pick<Storage, 'getItem' | 'setItem'>} options.storage - Where the
|
|
171
|
+
* records are kept, e.g. `localStorage`.
|
|
172
|
+
* @param {string} options.key - The storage key, e.g. `myapp.vaults.v1`.
|
|
173
|
+
* @param {string} options.slotInfo - The `info` every slot key is derived
|
|
174
|
+
* under, e.g. `myapp/vault-slot/v1`. Part of the data format: changed, no
|
|
175
|
+
* slot opens any more.
|
|
176
|
+
* @param {() => Date} [options.now] - When a slot was added.
|
|
177
|
+
*/
|
|
178
|
+
export function createVaultKeeper({
|
|
179
|
+
storage,
|
|
180
|
+
key,
|
|
181
|
+
slotInfo,
|
|
182
|
+
now = () => new Date(),
|
|
183
|
+
} = {}) {
|
|
184
|
+
if (
|
|
185
|
+
!storage ||
|
|
186
|
+
typeof storage.getItem !== 'function' ||
|
|
187
|
+
typeof storage.setItem !== 'function'
|
|
188
|
+
) {
|
|
189
|
+
throw new TypeError(
|
|
190
|
+
'createVaultKeeper needs a storage with getItem and setItem'
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
if (typeof key !== 'string' || key === '') {
|
|
194
|
+
throw new TypeError(
|
|
195
|
+
'createVaultKeeper needs the key its vaults are kept under'
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (typeof slotInfo !== 'string' || slotInfo === '') {
|
|
199
|
+
throw new TypeError(
|
|
200
|
+
'createVaultKeeper needs the info slot keys are derived under, e.g. "myapp/vault-slot/v1"'
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const slotKeyOf = (prfOutput) => deriveAesKey(prfOutput, slotInfo);
|
|
205
|
+
|
|
206
|
+
const unreadable = (cause) =>
|
|
207
|
+
new VaultError(
|
|
208
|
+
`The vaults kept under "${key}" cannot be read; they are left as they are`,
|
|
209
|
+
{ code: ERROR_CODES.VAULT_STORE_UNREADABLE, cause }
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Every record in the store, read anew. An unreadable store is an error,
|
|
214
|
+
* never an empty one: it may hold the only copy of a vault.
|
|
215
|
+
*/
|
|
216
|
+
function records() {
|
|
217
|
+
const text = storage.getItem(key);
|
|
218
|
+
if (text === null || text === undefined) return [];
|
|
219
|
+
let parsed;
|
|
220
|
+
try {
|
|
221
|
+
parsed = JSON.parse(text);
|
|
222
|
+
} catch (cause) {
|
|
223
|
+
throw unreadable(cause);
|
|
224
|
+
}
|
|
225
|
+
if (!Array.isArray(parsed) || !parsed.every(isRecord)) throw unreadable();
|
|
226
|
+
return parsed.map(normalised);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Keep `record` in place of the one with vault id `replacing`, else add it. */
|
|
230
|
+
function save(record, replacing = record.vault.id) {
|
|
231
|
+
const all = records();
|
|
232
|
+
const at = all.findIndex((r) => r.vault.id === replacing);
|
|
233
|
+
if (at >= 0) all[at] = record;
|
|
234
|
+
else all.push(record);
|
|
235
|
+
storage.setItem(key, JSON.stringify(all));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** What the store says in public about one authenticator's slot. */
|
|
239
|
+
const slotEntry = (kid, rawCredentialId, label, addedAt) => ({
|
|
240
|
+
kid,
|
|
241
|
+
label: label ?? '',
|
|
242
|
+
credentialId: base64url(bytesOf(rawCredentialId)),
|
|
243
|
+
addedAt,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Is there a vault here for this authenticator — without asking it?
|
|
248
|
+
*
|
|
249
|
+
* @param {Uint8Array} rawCredentialId
|
|
250
|
+
* @returns {Promise<{ kid: string, here: boolean, removed: boolean }>}
|
|
251
|
+
* `here`: a vault kept here has its slot. `removed`: none has, and it was
|
|
252
|
+
* taken out of one.
|
|
253
|
+
*/
|
|
254
|
+
async function status(rawCredentialId) {
|
|
255
|
+
const kid = await slotIdFor(rawCredentialId);
|
|
256
|
+
const all = records();
|
|
257
|
+
const here = all.some((r) => holds(r, kid));
|
|
258
|
+
return {
|
|
259
|
+
kid,
|
|
260
|
+
here,
|
|
261
|
+
removed: !here && all.some((r) => r.removed.includes(kid)),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Open the vault this authenticator has a slot in, or make one.
|
|
267
|
+
*
|
|
268
|
+
* - A vault kept here with its slot: opened.
|
|
269
|
+
* - None, and it was removed from one here: refused, `VAULT_SLOT_REMOVED` —
|
|
270
|
+
* never given a new vault, which for books made before the vault would be
|
|
271
|
+
* a stale copy that looks like the real ones.
|
|
272
|
+
* - None, and `find` returns a record with its slot (from a backup, an
|
|
273
|
+
* index): opened, and kept here once it has opened, under this
|
|
274
|
+
* authenticator's `label`.
|
|
275
|
+
* - None at all: a new vault holding `create()`, this authenticator's slot
|
|
276
|
+
* on it. Without `create`, refused: `VAULT_NO_SLOT`.
|
|
277
|
+
*
|
|
278
|
+
* Pass `find` on every unlock where the vault may be kept elsewhere, not
|
|
279
|
+
* only on a restore path. A synced passkey on a new device otherwise makes
|
|
280
|
+
* a vault of its own, and from then on its slot is found here and `find` is
|
|
281
|
+
* never asked again (Le-Space/belege#524).
|
|
282
|
+
*
|
|
283
|
+
* @param {Object} unlock
|
|
284
|
+
* @param {Uint8Array} unlock.prfOutput - From `readPrfOutput`.
|
|
285
|
+
* @param {Uint8Array} unlock.rawCredentialId
|
|
286
|
+
* @param {string} [unlock.label] - The authenticator's name, kept in public.
|
|
287
|
+
* @param {() => Uint8Array | Promise<Uint8Array>} [unlock.create] - The
|
|
288
|
+
* payload of a new vault, e.g. `() => rootPayload(randomBytes)`.
|
|
289
|
+
* @param {(kid: string) => Promise<object|null>} [unlock.find] - A record
|
|
290
|
+
* kept elsewhere, by slot id.
|
|
291
|
+
* @returns {Promise<{ record: object, payload: Uint8Array,
|
|
292
|
+
* vaultKey: Uint8Array, kid: string, created: boolean, found: boolean }>}
|
|
293
|
+
* Keep it in memory while the vault is open; it holds the vault key.
|
|
294
|
+
* @throws {VaultError} `VAULT_STORE_UNREADABLE`, `VAULT_SLOT_REMOVED`,
|
|
295
|
+
* `VAULT_NO_SLOT`, or `VAULT_LOCKED` when the slot does not open with
|
|
296
|
+
* this PRF output.
|
|
297
|
+
*/
|
|
298
|
+
async function open({
|
|
299
|
+
prfOutput,
|
|
300
|
+
rawCredentialId,
|
|
301
|
+
label,
|
|
302
|
+
create,
|
|
303
|
+
find,
|
|
304
|
+
} = {}) {
|
|
305
|
+
const kid = await slotIdFor(rawCredentialId);
|
|
306
|
+
const all = records();
|
|
307
|
+
let record = all.find((r) => holds(r, kid));
|
|
308
|
+
if (!record && all.some((r) => r.removed.includes(kid))) {
|
|
309
|
+
throw new VaultError(
|
|
310
|
+
'This authenticator was removed from the vault kept here',
|
|
311
|
+
{ code: ERROR_CODES.VAULT_SLOT_REMOVED }
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
let found = false;
|
|
315
|
+
if (!record && find) {
|
|
316
|
+
const elsewhere = await find(kid);
|
|
317
|
+
if (isRecord(elsewhere) && holds(elsewhere, kid)) {
|
|
318
|
+
record = normalised(elsewhere);
|
|
319
|
+
found = true;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const slotKey = await slotKeyOf(prfOutput);
|
|
323
|
+
|
|
324
|
+
if (record) {
|
|
325
|
+
const { payload, vaultKey } = await openVault(record.vault, {
|
|
326
|
+
slotKey,
|
|
327
|
+
rawCredentialId,
|
|
328
|
+
});
|
|
329
|
+
if (found) {
|
|
330
|
+
const before = record.slots.find((slot) => slot?.kid === kid);
|
|
331
|
+
record = {
|
|
332
|
+
...record,
|
|
333
|
+
slots: [
|
|
334
|
+
...record.slots.filter((slot) => slot?.kid !== kid),
|
|
335
|
+
slotEntry(
|
|
336
|
+
kid,
|
|
337
|
+
rawCredentialId,
|
|
338
|
+
label ?? before?.label,
|
|
339
|
+
before?.addedAt ?? now().toISOString()
|
|
340
|
+
),
|
|
341
|
+
],
|
|
342
|
+
};
|
|
343
|
+
save(record);
|
|
344
|
+
}
|
|
345
|
+
return { record, payload, vaultKey, kid, created: false, found };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (!create) {
|
|
349
|
+
throw new VaultError(
|
|
350
|
+
'No vault kept here has a slot for this authenticator',
|
|
351
|
+
{ code: ERROR_CODES.VAULT_NO_SLOT }
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
const payload = payloadBytes(await create(), 'create()');
|
|
355
|
+
const { vault, vaultKey } = await createVault(payload, {
|
|
356
|
+
slotKey,
|
|
357
|
+
rawCredentialId,
|
|
358
|
+
});
|
|
359
|
+
record = {
|
|
360
|
+
vault,
|
|
361
|
+
slots: [slotEntry(kid, rawCredentialId, label, now().toISOString())],
|
|
362
|
+
removed: [],
|
|
363
|
+
};
|
|
364
|
+
save(record);
|
|
365
|
+
return { record, payload, vaultKey, kid, created: true, found: false };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Give one more authenticator a slot, while the vault is open: the new key
|
|
370
|
+
* is touched for its PRF output. A key taken out here before may come back.
|
|
371
|
+
*
|
|
372
|
+
* @param {object} opened - From `open`.
|
|
373
|
+
* @param {Object} passkey
|
|
374
|
+
* @param {Uint8Array} passkey.prfOutput - The new authenticator's.
|
|
375
|
+
* @param {Uint8Array} passkey.rawCredentialId
|
|
376
|
+
* @param {string} [passkey.label]
|
|
377
|
+
* @returns {Promise<object>} `opened`, with the new record.
|
|
378
|
+
* @throws {VaultError} `VAULT_SLOT_EXISTS` when it has a slot already, here
|
|
379
|
+
* or in another vault kept here — one key opens one vault per store.
|
|
380
|
+
*/
|
|
381
|
+
async function addPasskey(
|
|
382
|
+
opened,
|
|
383
|
+
{ prfOutput, rawCredentialId, label } = {}
|
|
384
|
+
) {
|
|
385
|
+
const kid = await slotIdFor(rawCredentialId);
|
|
386
|
+
if (
|
|
387
|
+
records().some(
|
|
388
|
+
(r) => r.vault.id !== opened.record.vault.id && holds(r, kid)
|
|
389
|
+
)
|
|
390
|
+
) {
|
|
391
|
+
throw new VaultError(
|
|
392
|
+
'This authenticator already opens another vault kept here',
|
|
393
|
+
{ code: ERROR_CODES.VAULT_SLOT_EXISTS }
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
const vault = await addSlot(opened.record.vault, opened.vaultKey, {
|
|
397
|
+
slotKey: await slotKeyOf(prfOutput),
|
|
398
|
+
rawCredentialId,
|
|
399
|
+
});
|
|
400
|
+
const record = {
|
|
401
|
+
...opened.record,
|
|
402
|
+
vault,
|
|
403
|
+
slots: [
|
|
404
|
+
...opened.record.slots.filter((slot) => slot?.kid !== kid),
|
|
405
|
+
slotEntry(kid, rawCredentialId, label, now().toISOString()),
|
|
406
|
+
],
|
|
407
|
+
removed: opened.record.removed.filter((k) => k !== kid),
|
|
408
|
+
};
|
|
409
|
+
save(record);
|
|
410
|
+
return { ...opened, record };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Take an authenticator's slot out — by its slot id, so a key that is not at
|
|
415
|
+
* hand can go too. It is refused here from then on. It has seen the vault
|
|
416
|
+
* key, and older copies of the record still carry its slot: for a key that
|
|
417
|
+
* may be in other hands, `renew` instead.
|
|
418
|
+
*
|
|
419
|
+
* @param {object} opened - From `open`.
|
|
420
|
+
* @param {string} kid - The slot id, as `record.slots` names it.
|
|
421
|
+
* @returns {Promise<object>} `opened`, with the new record.
|
|
422
|
+
* @throws {VaultError} `VAULT_SLOT_IN_USE` for the authenticator that opened
|
|
423
|
+
* the vault, `VAULT_NO_SLOT` for one without a slot.
|
|
424
|
+
*/
|
|
425
|
+
async function removePasskey(opened, kid) {
|
|
426
|
+
if (kid === opened.kid) {
|
|
427
|
+
throw new VaultError(
|
|
428
|
+
'The authenticator that opened the vault cannot remove itself',
|
|
429
|
+
{ code: ERROR_CODES.VAULT_SLOT_IN_USE }
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
const vault = removeSlotById(opened.record.vault, kid);
|
|
433
|
+
const record = {
|
|
434
|
+
...opened.record,
|
|
435
|
+
vault,
|
|
436
|
+
slots: opened.record.slots.filter((slot) => slot?.kid !== kid),
|
|
437
|
+
removed: [...new Set([...opened.record.removed, kid])],
|
|
438
|
+
};
|
|
439
|
+
save(record);
|
|
440
|
+
return { ...opened, record };
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Renew the vault: a new vault key and a new vault id, the same payload or
|
|
445
|
+
* `payload`, and a slot for every authenticator that stays. Every one of
|
|
446
|
+
* them answers — its PRF output, checked against its slot in the old vault —
|
|
447
|
+
* so none is left out by mistake; the ones in `removed` are taken out and
|
|
448
|
+
* refused here. The old record is replaced here; copies of it elsewhere
|
|
449
|
+
* still open with the old slots.
|
|
450
|
+
*
|
|
451
|
+
* @param {object} opened - From `open`.
|
|
452
|
+
* @param {Object} change
|
|
453
|
+
* @param {Array<{ prfOutput: Uint8Array, rawCredentialId: Uint8Array }>}
|
|
454
|
+
* change.answers - Every authenticator that stays, the one that opened
|
|
455
|
+
* the vault among them.
|
|
456
|
+
* @param {string[]} [change.removed] - Slot ids taken out.
|
|
457
|
+
* @param {Uint8Array} [change.payload] - A new payload, e.g. with keys that
|
|
458
|
+
* a removed authenticator must not know.
|
|
459
|
+
* @returns {Promise<object>} `opened`, with the new record, vault key and
|
|
460
|
+
* payload.
|
|
461
|
+
* @throws {VaultError} `VAULT_SLOT_IN_USE` when the authenticator that
|
|
462
|
+
* opened the vault would go; `VAULT_NO_SLOT` for a removed id without a
|
|
463
|
+
* slot; `VAULT_LOCKED` for an answer its slot does not open with.
|
|
464
|
+
*/
|
|
465
|
+
async function renew(opened, { answers, removed = [], payload } = {}) {
|
|
466
|
+
if (!Array.isArray(answers) || answers.length === 0) {
|
|
467
|
+
throw new TypeError(
|
|
468
|
+
'renew needs an answer from every authenticator that stays'
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
const kids = await Promise.all(
|
|
472
|
+
answers.map((answer) => slotIdFor(answer.rawCredentialId))
|
|
473
|
+
);
|
|
474
|
+
const out = new Set(removed);
|
|
475
|
+
if (!kids.includes(opened.kid) || out.has(opened.kid)) {
|
|
476
|
+
throw new VaultError(
|
|
477
|
+
'The authenticator that opened the vault has to stay',
|
|
478
|
+
{
|
|
479
|
+
code: ERROR_CODES.VAULT_SLOT_IN_USE,
|
|
480
|
+
}
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
const current = opened.record.vault.slots.map((slot) => slot.kid);
|
|
484
|
+
for (const kid of out) {
|
|
485
|
+
if (!current.includes(kid)) {
|
|
486
|
+
throw new VaultError(`No slot ${kid} to remove`, {
|
|
487
|
+
code: ERROR_CODES.VAULT_NO_SLOT,
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
const staying = current.filter((kid) => !out.has(kid));
|
|
492
|
+
if (
|
|
493
|
+
new Set(kids).size !== kids.length ||
|
|
494
|
+
staying.length !== kids.length ||
|
|
495
|
+
!staying.every((kid) => kids.includes(kid))
|
|
496
|
+
) {
|
|
497
|
+
throw new TypeError(
|
|
498
|
+
'renew needs one answer from every authenticator that stays, and the others in `removed`'
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
const slots = await Promise.all(
|
|
502
|
+
answers.map(async ({ prfOutput, rawCredentialId }) => {
|
|
503
|
+
const slot = { slotKey: await slotKeyOf(prfOutput), rawCredentialId };
|
|
504
|
+
await openVault(opened.record.vault, slot);
|
|
505
|
+
return slot;
|
|
506
|
+
})
|
|
507
|
+
);
|
|
508
|
+
const next =
|
|
509
|
+
payload === undefined
|
|
510
|
+
? opened.payload
|
|
511
|
+
: payloadBytes(payload, 'renew’s payload');
|
|
512
|
+
|
|
513
|
+
const [first, ...rest] = slots;
|
|
514
|
+
let { vault, vaultKey } = await createVault(next, first);
|
|
515
|
+
for (const slot of rest) vault = await addSlot(vault, vaultKey, slot);
|
|
516
|
+
|
|
517
|
+
const record = {
|
|
518
|
+
...opened.record,
|
|
519
|
+
vault,
|
|
520
|
+
slots: opened.record.slots.filter((slot) => !out.has(slot?.kid)),
|
|
521
|
+
removed: [...new Set([...opened.record.removed, ...out])],
|
|
522
|
+
};
|
|
523
|
+
save(record, opened.record.vault.id);
|
|
524
|
+
return { ...opened, record, payload: next, vaultKey };
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* A new payload under the same vault key and slots.
|
|
529
|
+
*
|
|
530
|
+
* @param {object} opened - From `open`.
|
|
531
|
+
* @param {Uint8Array} payload
|
|
532
|
+
* @returns {Promise<object>} `opened`, with the new record and payload.
|
|
533
|
+
*/
|
|
534
|
+
async function replacePayload(opened, payload) {
|
|
535
|
+
const bytes = payloadBytes(payload, 'payload');
|
|
536
|
+
const vault = await replaceVaultPayload(
|
|
537
|
+
opened.record.vault,
|
|
538
|
+
opened.vaultKey,
|
|
539
|
+
bytes
|
|
540
|
+
);
|
|
541
|
+
const record = { ...opened.record, vault };
|
|
542
|
+
save(record);
|
|
543
|
+
return { ...opened, record, payload: bytes };
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return Object.freeze({
|
|
547
|
+
records,
|
|
548
|
+
status,
|
|
549
|
+
open,
|
|
550
|
+
addPasskey,
|
|
551
|
+
removePasskey,
|
|
552
|
+
renew,
|
|
553
|
+
replacePayload,
|
|
554
|
+
});
|
|
555
|
+
}
|
package/src/keystore/vault.js
CHANGED
|
@@ -354,7 +354,21 @@ export async function addSlot(
|
|
|
354
354
|
*/
|
|
355
355
|
export async function removeSlot(vault, rawCredentialId) {
|
|
356
356
|
assertVault(vault);
|
|
357
|
-
|
|
357
|
+
return removeSlotById(vault, await slotIdFor(rawCredentialId));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* `removeSlot` by the slot id itself, for an authenticator only known by the
|
|
362
|
+
* id its slot is filed under — one that is not at hand. Used by the vault
|
|
363
|
+
* keeper; not part of the package's entries.
|
|
364
|
+
*
|
|
365
|
+
* @param {object} vault
|
|
366
|
+
* @param {string} kid - From `slotIdFor`.
|
|
367
|
+
* @returns {object} A new record; the one passed in is unchanged.
|
|
368
|
+
* @throws {VaultError} `VAULT_NO_SLOT` or `VAULT_LAST_SLOT`.
|
|
369
|
+
*/
|
|
370
|
+
export function removeSlotById(vault, kid) {
|
|
371
|
+
assertVault(vault);
|
|
358
372
|
if (!vault.slots.some((slot) => slot.kid === kid)) {
|
|
359
373
|
throw new VaultError('This authenticator has no slot in the vault', {
|
|
360
374
|
code: ERROR_CODES.VAULT_NO_SLOT,
|
package/src/standalone/index.js
CHANGED
|
@@ -46,6 +46,11 @@ export {
|
|
|
46
46
|
replacePayload,
|
|
47
47
|
slotIdFor,
|
|
48
48
|
} from '../keystore/vault.js';
|
|
49
|
+
export {
|
|
50
|
+
createVaultKeeper,
|
|
51
|
+
rootPayload,
|
|
52
|
+
rootOf,
|
|
53
|
+
} from '../keystore/vault-keeper.js';
|
|
49
54
|
export { PrfUnavailableError, VaultError } from '../errors.js';
|
|
50
55
|
export { createSecretSigner } from '../keystore/secret-signer.js';
|
|
51
56
|
|
|
@@ -200,28 +200,30 @@ export async function createWebAuthnP256Signer(options = {}) {
|
|
|
200
200
|
|
|
201
201
|
/**
|
|
202
202
|
* Upload-wall compatibility helper. Returns Ed25519 signer or P-256 fallback signer.
|
|
203
|
+
*
|
|
204
|
+
* A failed registration rejects — a cancelled prompt with the browser's
|
|
205
|
+
* `NotAllowedError`, an unknown `authenticatorType` with a `TypeError`. This
|
|
206
|
+
* used to promise `null` instead, from a `try`/`catch` that could never catch:
|
|
207
|
+
* it returned the registration's promise without awaiting it.
|
|
208
|
+
*
|
|
203
209
|
* @param {string} userId
|
|
204
210
|
* @param {string} displayName
|
|
205
211
|
* @param {{authenticatorType?: 'platform' | 'cross-platform' | 'any'}} [options]
|
|
206
|
-
* @returns {Promise<StandaloneWebAuthnVarsigSigner
|
|
212
|
+
* @returns {Promise<StandaloneWebAuthnVarsigSigner>}
|
|
207
213
|
*/
|
|
208
214
|
export async function createWebAuthnEd25519Credential(
|
|
209
215
|
userId,
|
|
210
216
|
displayName,
|
|
211
217
|
options = {}
|
|
212
218
|
) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
});
|
|
222
|
-
} catch {
|
|
223
|
-
return null;
|
|
224
|
-
}
|
|
219
|
+
const forceP256Hardware =
|
|
220
|
+
typeof window !== 'undefined' && Boolean(window.__FORCE_P256_HARDWARE__);
|
|
221
|
+
return createWebAuthnSigner({
|
|
222
|
+
userId,
|
|
223
|
+
displayName,
|
|
224
|
+
authenticatorType: options.authenticatorType || 'any',
|
|
225
|
+
forceP256: forceP256Hardware,
|
|
226
|
+
});
|
|
225
227
|
}
|
|
226
228
|
|
|
227
229
|
/**
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The passkey vault on its own: reading the PRF output, deriving keys from it,
|
|
3
|
+
* vaults any registered authenticator opens, and a keeper for them.
|
|
4
|
+
*
|
|
5
|
+
* Nothing here imports OrbitDB, ucanto, libp2p or any other package: it needs
|
|
6
|
+
* WebCrypto and `navigator.credentials`, and no more, so an application that
|
|
7
|
+
* keeps its data elsewhere pays for nothing else
|
|
8
|
+
* (`tests/vault-entry.test.js` holds it to that).
|
|
9
|
+
*/
|
|
10
|
+
export {
|
|
11
|
+
readPrfOutput,
|
|
12
|
+
deriveSubkey,
|
|
13
|
+
deriveAesKey,
|
|
14
|
+
} from '../keystore/prf-keys.js';
|
|
15
|
+
export {
|
|
16
|
+
createVault,
|
|
17
|
+
openVault,
|
|
18
|
+
addSlot,
|
|
19
|
+
removeSlot,
|
|
20
|
+
replacePayload,
|
|
21
|
+
slotIdFor,
|
|
22
|
+
} from '../keystore/vault.js';
|
|
23
|
+
export {
|
|
24
|
+
createVaultKeeper,
|
|
25
|
+
rootPayload,
|
|
26
|
+
rootOf,
|
|
27
|
+
} from '../keystore/vault-keeper.js';
|
|
28
|
+
export { PrfUnavailableError, VaultError } from '../errors.js';
|
package/types/index.d.ts
CHANGED
|
@@ -386,9 +386,69 @@ export function clearEncryptedKeystore(credentialId: string): Promise<void>;
|
|
|
386
386
|
|
|
387
387
|
export function checkExtensionSupport(): Promise<ExtensionSupport>;
|
|
388
388
|
|
|
389
|
+
/** Options for `WebAuthnVarsigProvider.createCredential`. */
|
|
390
|
+
export interface VarsigCredentialOptions {
|
|
391
|
+
/** Account label for the credential picker. Not an identifier. */
|
|
392
|
+
userId?: string;
|
|
393
|
+
displayName?: string;
|
|
394
|
+
/** Relying party id. Default: the page's hostname. */
|
|
395
|
+
domain?: string;
|
|
396
|
+
discoverableCredentials?: boolean;
|
|
397
|
+
/**
|
|
398
|
+
* Which authenticators the browser offers: the device's own, a security key
|
|
399
|
+
* or phone, or every kind. Default `'any'`: the request names none.
|
|
400
|
+
*/
|
|
401
|
+
authenticatorType?: AuthenticatorType;
|
|
402
|
+
/** The WebAuthn name for `authenticatorType`; the two must not disagree. */
|
|
403
|
+
authenticatorAttachment?: AuthenticatorAttachment;
|
|
404
|
+
/**
|
|
405
|
+
* Not read: a registration always requires user verification, because a
|
|
406
|
+
* varsig signature without the UV flag is refused.
|
|
407
|
+
*/
|
|
408
|
+
userVerification?: UserVerificationRequirement;
|
|
409
|
+
/** Offer only P-256 (ES256). */
|
|
410
|
+
forceP256?: boolean;
|
|
411
|
+
[key: string]: unknown;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** What `WebAuthnVarsigProvider.createCredential` returns. */
|
|
415
|
+
export interface VarsigCredential {
|
|
416
|
+
credentialId: Uint8Array;
|
|
417
|
+
publicKey: Uint8Array;
|
|
418
|
+
/** `did:key` of the public key. */
|
|
419
|
+
did: string;
|
|
420
|
+
algorithm: WebAuthnAlgorithm;
|
|
421
|
+
/** COSE key type, algorithm and curve from the attestation. */
|
|
422
|
+
cose: { kty: number; alg: number; crv: number };
|
|
423
|
+
/** Relying party id the credential is registered under. */
|
|
424
|
+
rpId: string;
|
|
425
|
+
[key: string]: unknown;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** How a varsig signature asks the authenticator: at construction or per call. */
|
|
429
|
+
export interface VarsigSignOptions {
|
|
430
|
+
/** Default `'preferred'`. */
|
|
431
|
+
userVerification?: UserVerificationRequirement;
|
|
432
|
+
mediation?: CredentialMediationRequirement;
|
|
433
|
+
discoverableCredentials?: boolean;
|
|
434
|
+
}
|
|
435
|
+
|
|
389
436
|
export class WebAuthnVarsigProvider {
|
|
390
|
-
constructor(credential: Record<string, unknown
|
|
391
|
-
|
|
437
|
+
constructor(credential: Record<string, unknown>, options?: VarsigSignOptions);
|
|
438
|
+
static isSupported(): boolean;
|
|
439
|
+
static createCredential(
|
|
440
|
+
options?: VarsigCredentialOptions
|
|
441
|
+
): Promise<VarsigCredential>;
|
|
442
|
+
signPayload(
|
|
443
|
+
payloadBytes: Uint8Array,
|
|
444
|
+
domainLabel?: string,
|
|
445
|
+
options?: VarsigSignOptions
|
|
446
|
+
): Promise<Uint8Array>;
|
|
447
|
+
sign(
|
|
448
|
+
data: string | Uint8Array,
|
|
449
|
+
domainLabel?: string,
|
|
450
|
+
options?: VarsigSignOptions
|
|
451
|
+
): Promise<Uint8Array>;
|
|
392
452
|
verify(
|
|
393
453
|
signature: Uint8Array,
|
|
394
454
|
publicKey: Uint8Array,
|
|
@@ -401,17 +461,42 @@ export function createWebAuthnVarsigIdentity(
|
|
|
401
461
|
options?: Record<string, unknown>
|
|
402
462
|
): Promise<unknown>;
|
|
403
463
|
|
|
464
|
+
/**
|
|
465
|
+
* An `Identities`-compatible object for OrbitDB, built synchronously around
|
|
466
|
+
* one varsig identity. `fallbackIdentities` verifies entries this cannot, as
|
|
467
|
+
* from a worker signer.
|
|
468
|
+
*/
|
|
404
469
|
export function createWebAuthnVarsigIdentities(
|
|
405
|
-
|
|
406
|
-
|
|
470
|
+
identity: unknown,
|
|
471
|
+
domainLabels?: Record<string, string>,
|
|
472
|
+
storage?: unknown,
|
|
473
|
+
fallbackIdentities?: unknown
|
|
474
|
+
): unknown;
|
|
407
475
|
|
|
408
|
-
|
|
409
|
-
export function
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
476
|
+
/** The identity's block: its hash (a CID string) and its bytes. */
|
|
477
|
+
export function encodeIdentityValue(
|
|
478
|
+
value: unknown
|
|
479
|
+
): Promise<{ hash: string; bytes: Uint8Array }>;
|
|
480
|
+
export function decodeVarsigIdentityFromBytes(
|
|
481
|
+
bytes: Uint8Array,
|
|
482
|
+
domainLabels?: Record<string, string>
|
|
483
|
+
): Promise<unknown>;
|
|
484
|
+
export function verifyVarsigIdentity(
|
|
485
|
+
identity: unknown,
|
|
486
|
+
domainLabels?: Record<string, string>
|
|
487
|
+
): Promise<boolean>;
|
|
488
|
+
/** `get` and `put` on the instance's blockstore; `null` without one. */
|
|
489
|
+
export function createIpfsIdentityStorage(ipfs?: unknown): {
|
|
490
|
+
/** Whatever the blockstore's `get` resolves to, `undefined` on failure. */
|
|
491
|
+
get(hash: string): Promise<unknown>;
|
|
492
|
+
put(hash: string, bytes: Uint8Array): Promise<void>;
|
|
493
|
+
} | null;
|
|
494
|
+
/** OrbitDB's identities, made to verify varsig entries as well. */
|
|
495
|
+
export function wrapWithVarsigVerification(
|
|
496
|
+
defaultIdentities: unknown,
|
|
497
|
+
ipfs?: unknown,
|
|
498
|
+
domainLabels?: Record<string, string>
|
|
413
499
|
): unknown;
|
|
414
|
-
export function wrapWithVarsigVerification(value: unknown): unknown;
|
|
415
500
|
|
|
416
501
|
export const DEFAULT_DOMAIN_LABELS: Record<string, string>;
|
|
417
502
|
|
|
@@ -619,6 +704,18 @@ export function replacePayload(
|
|
|
619
704
|
/** The slot id an authenticator is filed under. */
|
|
620
705
|
export function slotIdFor(rawCredentialId: Uint8Array): Promise<string>;
|
|
621
706
|
|
|
707
|
+
/** Where the vaults are kept, and what every unlock does with them. */
|
|
708
|
+
export {
|
|
709
|
+
createVaultKeeper,
|
|
710
|
+
rootPayload,
|
|
711
|
+
rootOf,
|
|
712
|
+
VaultKeeper,
|
|
713
|
+
VaultRecord,
|
|
714
|
+
VaultSlotInfo,
|
|
715
|
+
OpenedVault,
|
|
716
|
+
VaultPasskey,
|
|
717
|
+
} from './standalone';
|
|
718
|
+
|
|
622
719
|
/**
|
|
623
720
|
* An Ed25519 signer derived from a secret — an identity that is nobody's
|
|
624
721
|
* passkey, e.g. the books every key of their owner writes as. Same secret and
|
|
@@ -640,7 +737,10 @@ export type VaultErrorCode =
|
|
|
640
737
|
| 'VAULT_NO_SLOT'
|
|
641
738
|
| 'VAULT_LOCKED'
|
|
642
739
|
| 'VAULT_SLOT_EXISTS'
|
|
643
|
-
| 'VAULT_LAST_SLOT'
|
|
740
|
+
| 'VAULT_LAST_SLOT'
|
|
741
|
+
| 'VAULT_STORE_UNREADABLE'
|
|
742
|
+
| 'VAULT_SLOT_REMOVED'
|
|
743
|
+
| 'VAULT_SLOT_IN_USE';
|
|
644
744
|
|
|
645
745
|
declare const defaultExport: {
|
|
646
746
|
WebAuthnDIDProvider: typeof WebAuthnDIDProvider;
|
package/types/standalone.d.ts
CHANGED
|
@@ -98,11 +98,15 @@ export function createWebAuthnP256Signer(
|
|
|
98
98
|
options?: WebAuthnSignerOptions
|
|
99
99
|
): Promise<StandaloneWebAuthnVarsigSigner>;
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Rejects when the registration fails — a cancelled prompt, an unknown
|
|
103
|
+
* `authenticatorType`. It never resolved to `null`, though this said it would.
|
|
104
|
+
*/
|
|
101
105
|
export function createWebAuthnEd25519Credential(
|
|
102
106
|
userId: string,
|
|
103
107
|
displayName: string,
|
|
104
108
|
options?: { authenticatorType?: AuthenticatorType }
|
|
105
|
-
): Promise<StandaloneWebAuthnVarsigSigner
|
|
109
|
+
): Promise<StandaloneWebAuthnVarsigSigner>;
|
|
106
110
|
|
|
107
111
|
export function checkEd25519Support(): Promise<boolean>;
|
|
108
112
|
|
|
@@ -389,6 +393,110 @@ export function replacePayload(
|
|
|
389
393
|
/** The slot id an authenticator is filed under. */
|
|
390
394
|
export function slotIdFor(rawCredentialId: Uint8Array): Promise<string>;
|
|
391
395
|
|
|
396
|
+
/** What a store says in public about one authenticator's slot. */
|
|
397
|
+
export interface VaultSlotInfo {
|
|
398
|
+
/** `slotIdFor(rawCredentialId)`. */
|
|
399
|
+
kid: string;
|
|
400
|
+
/** The authenticator's name, as the application gave it. */
|
|
401
|
+
label: string;
|
|
402
|
+
/** The raw credential id, base64url, as WebAuthn writes it. */
|
|
403
|
+
credentialId: string;
|
|
404
|
+
/** ISO 8601. */
|
|
405
|
+
addedAt: string;
|
|
406
|
+
[field: string]: unknown;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** One vault as a keeper stores it. Fields it does not know are kept. */
|
|
410
|
+
export interface VaultRecord {
|
|
411
|
+
vault: Vault;
|
|
412
|
+
slots: VaultSlotInfo[];
|
|
413
|
+
/** Slot ids taken out here: refused, never given a new vault. */
|
|
414
|
+
removed: string[];
|
|
415
|
+
[field: string]: unknown;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** An open vault. Holds the vault key: keep it in memory only. */
|
|
419
|
+
export interface OpenedVault {
|
|
420
|
+
record: VaultRecord;
|
|
421
|
+
payload: Uint8Array;
|
|
422
|
+
vaultKey: Uint8Array;
|
|
423
|
+
/** The slot id of the authenticator that opened it. */
|
|
424
|
+
kid: string;
|
|
425
|
+
/** Made by this `open`. */
|
|
426
|
+
created: boolean;
|
|
427
|
+
/** Came from `find`, and is kept here now. */
|
|
428
|
+
found: boolean;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** An authenticator's answer: its PRF output, for its credential. */
|
|
432
|
+
export interface VaultPasskey {
|
|
433
|
+
prfOutput: Uint8Array;
|
|
434
|
+
rawCredentialId: Uint8Array;
|
|
435
|
+
label?: string;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export interface VaultKeeper {
|
|
439
|
+
/** Every record kept, read anew. Throws `VAULT_STORE_UNREADABLE`. */
|
|
440
|
+
records(): VaultRecord[];
|
|
441
|
+
/** Whether a vault here has this authenticator's slot, without asking it. */
|
|
442
|
+
status(
|
|
443
|
+
rawCredentialId: Uint8Array
|
|
444
|
+
): Promise<{ kid: string; here: boolean; removed: boolean }>;
|
|
445
|
+
/**
|
|
446
|
+
* Open the vault this authenticator has a slot in; else one `find` returns;
|
|
447
|
+
* else a new one holding `create()`. Throws `VAULT_SLOT_REMOVED` for an
|
|
448
|
+
* authenticator removed here, `VAULT_NO_SLOT` without `create`.
|
|
449
|
+
*/
|
|
450
|
+
open(
|
|
451
|
+
unlock: VaultPasskey & {
|
|
452
|
+
create?: () => Uint8Array | Promise<Uint8Array>;
|
|
453
|
+
find?: (kid: string) => Promise<VaultRecord | null | undefined>;
|
|
454
|
+
}
|
|
455
|
+
): Promise<OpenedVault>;
|
|
456
|
+
/** A slot for one more authenticator. Throws `VAULT_SLOT_EXISTS`. */
|
|
457
|
+
addPasskey(opened: OpenedVault, passkey: VaultPasskey): Promise<OpenedVault>;
|
|
458
|
+
/**
|
|
459
|
+
* Take a slot out by its id; refused here from then on. Revokes nothing
|
|
460
|
+
* already opened. Throws `VAULT_SLOT_IN_USE` for the opener's own.
|
|
461
|
+
*/
|
|
462
|
+
removePasskey(opened: OpenedVault, kid: string): Promise<OpenedVault>;
|
|
463
|
+
/**
|
|
464
|
+
* A new vault key and id, a slot for every authenticator that stays — each
|
|
465
|
+
* answers — and the same or a new payload.
|
|
466
|
+
*/
|
|
467
|
+
renew(
|
|
468
|
+
opened: OpenedVault,
|
|
469
|
+
change: {
|
|
470
|
+
answers: Array<Omit<VaultPasskey, 'label'>>;
|
|
471
|
+
removed?: string[];
|
|
472
|
+
payload?: Uint8Array;
|
|
473
|
+
}
|
|
474
|
+
): Promise<OpenedVault>;
|
|
475
|
+
/** A new payload, same vault key, same slots. */
|
|
476
|
+
replacePayload(
|
|
477
|
+
opened: OpenedVault,
|
|
478
|
+
payload: Uint8Array
|
|
479
|
+
): Promise<OpenedVault>;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Where an application keeps its vaults — anything with `getItem` and
|
|
484
|
+
* `setItem` — and what every unlock does with them. `slotInfo` is part of the
|
|
485
|
+
* data format, e.g. `myapp/vault-slot/v1`.
|
|
486
|
+
*/
|
|
487
|
+
export function createVaultKeeper(options: {
|
|
488
|
+
storage: Pick<Storage, 'getItem' | 'setItem'>;
|
|
489
|
+
key: string;
|
|
490
|
+
slotInfo: string;
|
|
491
|
+
now?: () => Date;
|
|
492
|
+
}): VaultKeeper;
|
|
493
|
+
|
|
494
|
+
/** A 32-byte root as vault payload: `{"version":1,"root":"<hex>"}`. */
|
|
495
|
+
export function rootPayload(root: Uint8Array): Uint8Array;
|
|
496
|
+
|
|
497
|
+
/** The root a `rootPayload` holds. Throws `VAULT_MALFORMED` for anything else. */
|
|
498
|
+
export function rootOf(payload: Uint8Array): Uint8Array;
|
|
499
|
+
|
|
392
500
|
/**
|
|
393
501
|
* An Ed25519 signer derived from a secret — an identity that is nobody's
|
|
394
502
|
* passkey, e.g. the books every key of their owner writes as. Same secret and
|
|
@@ -410,7 +518,10 @@ export type VaultErrorCode =
|
|
|
410
518
|
| 'VAULT_NO_SLOT'
|
|
411
519
|
| 'VAULT_LOCKED'
|
|
412
520
|
| 'VAULT_SLOT_EXISTS'
|
|
413
|
-
| 'VAULT_LAST_SLOT'
|
|
521
|
+
| 'VAULT_LAST_SLOT'
|
|
522
|
+
| 'VAULT_STORE_UNREADABLE'
|
|
523
|
+
| 'VAULT_SLOT_REMOVED'
|
|
524
|
+
| 'VAULT_SLOT_IN_USE';
|
|
414
525
|
|
|
415
526
|
/** The authenticator answered without a PRF output; nothing stands in for it. */
|
|
416
527
|
export class PrfUnavailableError extends Error {
|
package/types/vault.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@le-space/orbitdb-identity-provider-webauthn-did/vault`: the passkey vault
|
|
3
|
+
* on its own — PRF output, keys derived from it, vaults any registered
|
|
4
|
+
* authenticator opens, and a keeper for them. WebCrypto only.
|
|
5
|
+
*/
|
|
6
|
+
export {
|
|
7
|
+
readPrfOutput,
|
|
8
|
+
deriveSubkey,
|
|
9
|
+
deriveAesKey,
|
|
10
|
+
Vault,
|
|
11
|
+
VaultSlotKey,
|
|
12
|
+
createVault,
|
|
13
|
+
openVault,
|
|
14
|
+
addSlot,
|
|
15
|
+
removeSlot,
|
|
16
|
+
replacePayload,
|
|
17
|
+
slotIdFor,
|
|
18
|
+
createVaultKeeper,
|
|
19
|
+
rootPayload,
|
|
20
|
+
rootOf,
|
|
21
|
+
VaultKeeper,
|
|
22
|
+
VaultRecord,
|
|
23
|
+
VaultSlotInfo,
|
|
24
|
+
OpenedVault,
|
|
25
|
+
VaultPasskey,
|
|
26
|
+
VaultErrorCode,
|
|
27
|
+
PrfUnavailableError,
|
|
28
|
+
VaultError,
|
|
29
|
+
} from './standalone';
|