@noy-db/on-webauthn 0.1.0-pre.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/index.cjs +303 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +219 -0
- package/dist/index.d.ts +219 -0
- package/dist/index.js +270 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 vLannaAi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @noy-db/on-webauthn
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@noy-db/on-webauthn)
|
|
4
|
+
|
|
5
|
+
> WebAuthn hardware-key keyrings for noy-db
|
|
6
|
+
|
|
7
|
+
Part of [**`@noy-db/hub`**](https://www.npmjs.com/package/@noy-db/hub) — the zero-knowledge, offline-first, encrypted document store.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @noy-db/hub @noy-db/on-webauthn
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## What it is
|
|
16
|
+
|
|
17
|
+
WebAuthn hardware-key keyrings for noy-db — Touch ID, Face ID, Windows Hello, YubiKey, FIDO2 passkeys
|
|
18
|
+
|
|
19
|
+
## Status
|
|
20
|
+
|
|
21
|
+
**Pre-release** (`0.1.0-pre.1`). API may change before `1.0`.
|
|
22
|
+
|
|
23
|
+
## Documentation
|
|
24
|
+
|
|
25
|
+
See the [main repository](https://github.com/vLannaAi/noy-db#readme) for setup, examples, and the full subsystem catalog.
|
|
26
|
+
|
|
27
|
+
- Source — [`packages/on-webauthn`](https://github.com/vLannaAi/noy-db/tree/main/packages/on-webauthn)
|
|
28
|
+
- Issues — [github.com/vLannaAi/noy-db/issues](https://github.com/vLannaAi/noy-db/issues)
|
|
29
|
+
- Spec — [`SPEC.md`](https://github.com/vLannaAi/noy-db/blob/main/SPEC.md)
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
[MIT](./LICENSE) © vLannaAi
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ValidationError: () => import_hub3.ValidationError,
|
|
24
|
+
WebAuthnCancelledError: () => WebAuthnCancelledError,
|
|
25
|
+
WebAuthnMultiDeviceError: () => WebAuthnMultiDeviceError,
|
|
26
|
+
WebAuthnNotAvailableError: () => WebAuthnNotAvailableError,
|
|
27
|
+
WebAuthnPRFUnavailableError: () => WebAuthnPRFUnavailableError,
|
|
28
|
+
enrollWebAuthn: () => enrollWebAuthn,
|
|
29
|
+
isValidEnrollment: () => isValidEnrollment,
|
|
30
|
+
isWebAuthnAvailable: () => isWebAuthnAvailable,
|
|
31
|
+
unlockWebAuthn: () => unlockWebAuthn
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
var import_hub = require("@noy-db/hub");
|
|
35
|
+
var import_hub2 = require("@noy-db/hub");
|
|
36
|
+
var import_hub3 = require("@noy-db/hub");
|
|
37
|
+
var WebAuthnNotAvailableError = class extends Error {
|
|
38
|
+
code = "WEBAUTHN_NOT_AVAILABLE";
|
|
39
|
+
constructor() {
|
|
40
|
+
super("WebAuthn is not available in this environment. A browser with navigator.credentials support is required.");
|
|
41
|
+
this.name = "WebAuthnNotAvailableError";
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var WebAuthnCancelledError = class extends Error {
|
|
45
|
+
code = "WEBAUTHN_CANCELLED";
|
|
46
|
+
constructor(op) {
|
|
47
|
+
super(`WebAuthn ${op} was cancelled by the user.`);
|
|
48
|
+
this.name = "WebAuthnCancelledError";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var WebAuthnMultiDeviceError = class extends Error {
|
|
52
|
+
code = "WEBAUTHN_MULTI_DEVICE";
|
|
53
|
+
constructor() {
|
|
54
|
+
super(
|
|
55
|
+
"This credential is backup-eligible (BE flag set) and may be synced across devices. The vault requires a single-device credential (requireSingleDevice: true). Please use a hardware security key (YubiKey, Titan, SoloKey) or a platform authenticator that does not sync credentials across devices."
|
|
56
|
+
);
|
|
57
|
+
this.name = "WebAuthnMultiDeviceError";
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var WebAuthnPRFUnavailableError = class extends Error {
|
|
61
|
+
code = "WEBAUTHN_PRF_UNAVAILABLE";
|
|
62
|
+
constructor() {
|
|
63
|
+
super(
|
|
64
|
+
"The PRF extension is not available on this authenticator. Enrollment will fall back to rawId-based key derivation. This provides weaker binding to the specific authenticator."
|
|
65
|
+
);
|
|
66
|
+
this.name = "WebAuthnPRFUnavailableError";
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function isWebAuthnAvailable() {
|
|
70
|
+
return typeof window !== "undefined" && typeof window.PublicKeyCredential !== "undefined" && typeof navigator !== "undefined" && typeof navigator.credentials !== "undefined";
|
|
71
|
+
}
|
|
72
|
+
var PRF_SALT = new TextEncoder().encode("noydb-webauthn-kek-derive");
|
|
73
|
+
async function deriveKeyFromPRF(prfOutput) {
|
|
74
|
+
const keyMaterial = await globalThis.crypto.subtle.importKey(
|
|
75
|
+
"raw",
|
|
76
|
+
prfOutput,
|
|
77
|
+
"HKDF",
|
|
78
|
+
false,
|
|
79
|
+
["deriveKey"]
|
|
80
|
+
);
|
|
81
|
+
return globalThis.crypto.subtle.deriveKey(
|
|
82
|
+
{
|
|
83
|
+
name: "HKDF",
|
|
84
|
+
hash: "SHA-256",
|
|
85
|
+
salt: PRF_SALT,
|
|
86
|
+
info: new TextEncoder().encode("noydb-kek-wrap-v1")
|
|
87
|
+
},
|
|
88
|
+
keyMaterial,
|
|
89
|
+
{ name: "AES-GCM", length: 256 },
|
|
90
|
+
false,
|
|
91
|
+
["encrypt", "decrypt"]
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
async function deriveKeyFromRawId(rawId) {
|
|
95
|
+
const keyMaterial = await globalThis.crypto.subtle.importKey(
|
|
96
|
+
"raw",
|
|
97
|
+
rawId,
|
|
98
|
+
"HKDF",
|
|
99
|
+
false,
|
|
100
|
+
["deriveKey"]
|
|
101
|
+
);
|
|
102
|
+
return globalThis.crypto.subtle.deriveKey(
|
|
103
|
+
{
|
|
104
|
+
name: "HKDF",
|
|
105
|
+
hash: "SHA-256",
|
|
106
|
+
salt: new TextEncoder().encode("noydb-webauthn-rawid-fallback"),
|
|
107
|
+
info: new TextEncoder().encode("noydb-kek-wrap-v1")
|
|
108
|
+
},
|
|
109
|
+
keyMaterial,
|
|
110
|
+
{ name: "AES-GCM", length: 256 },
|
|
111
|
+
false,
|
|
112
|
+
["encrypt", "decrypt"]
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
function extractBEFlag(authData) {
|
|
116
|
+
const bytes = new Uint8Array(authData);
|
|
117
|
+
if (bytes.length < 33) return false;
|
|
118
|
+
const flagsByte = bytes[32];
|
|
119
|
+
return (flagsByte & 8) !== 0;
|
|
120
|
+
}
|
|
121
|
+
async function wrapKeyringSummary(keyring, wrappingKey) {
|
|
122
|
+
const dekMap = {};
|
|
123
|
+
for (const [collName, dek] of keyring.deks) {
|
|
124
|
+
const raw = await globalThis.crypto.subtle.exportKey("raw", dek);
|
|
125
|
+
dekMap[collName] = (0, import_hub.bufferToBase64)(raw);
|
|
126
|
+
}
|
|
127
|
+
const payload = JSON.stringify({
|
|
128
|
+
userId: keyring.userId,
|
|
129
|
+
displayName: keyring.displayName,
|
|
130
|
+
role: keyring.role,
|
|
131
|
+
permissions: keyring.permissions,
|
|
132
|
+
deks: dekMap,
|
|
133
|
+
salt: (0, import_hub.bufferToBase64)(keyring.salt)
|
|
134
|
+
});
|
|
135
|
+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
|
|
136
|
+
const encrypted = await globalThis.crypto.subtle.encrypt(
|
|
137
|
+
{ name: "AES-GCM", iv },
|
|
138
|
+
wrappingKey,
|
|
139
|
+
new TextEncoder().encode(payload)
|
|
140
|
+
);
|
|
141
|
+
return { wrappedPayload: (0, import_hub.bufferToBase64)(encrypted), wrapIv: (0, import_hub.bufferToBase64)(iv) };
|
|
142
|
+
}
|
|
143
|
+
async function unwrapKeyringSummary(enrollment, wrappingKey) {
|
|
144
|
+
const iv = (0, import_hub.base64ToBuffer)(enrollment.wrapIv);
|
|
145
|
+
const ciphertext = (0, import_hub.base64ToBuffer)(enrollment.wrappedPayload);
|
|
146
|
+
let plaintext;
|
|
147
|
+
try {
|
|
148
|
+
plaintext = await globalThis.crypto.subtle.decrypt(
|
|
149
|
+
{ name: "AES-GCM", iv },
|
|
150
|
+
wrappingKey,
|
|
151
|
+
ciphertext
|
|
152
|
+
);
|
|
153
|
+
} catch {
|
|
154
|
+
throw new import_hub2.ValidationError("WebAuthn decryption failed \u2014 the authenticator may have changed or the enrollment may be corrupt.");
|
|
155
|
+
}
|
|
156
|
+
const parsed = JSON.parse(new TextDecoder().decode(plaintext));
|
|
157
|
+
const deks = /* @__PURE__ */ new Map();
|
|
158
|
+
for (const [collName, rawBase64] of Object.entries(parsed.deks)) {
|
|
159
|
+
const dek = await globalThis.crypto.subtle.importKey(
|
|
160
|
+
"raw",
|
|
161
|
+
(0, import_hub.base64ToBuffer)(rawBase64),
|
|
162
|
+
{ name: "AES-GCM", length: 256 },
|
|
163
|
+
true,
|
|
164
|
+
["encrypt", "decrypt"]
|
|
165
|
+
);
|
|
166
|
+
deks.set(collName, dek);
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
userId: parsed.userId,
|
|
170
|
+
displayName: parsed.displayName,
|
|
171
|
+
role: parsed.role,
|
|
172
|
+
permissions: parsed.permissions,
|
|
173
|
+
deks,
|
|
174
|
+
kek: null,
|
|
175
|
+
salt: (0, import_hub.base64ToBuffer)(parsed.salt)
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
async function enrollWebAuthn(keyring, vault, options = {}) {
|
|
179
|
+
if (!isWebAuthnAvailable()) {
|
|
180
|
+
throw new WebAuthnNotAvailableError();
|
|
181
|
+
}
|
|
182
|
+
const rpId = options.rp?.id ?? (typeof window !== "undefined" ? window.location.hostname : "localhost");
|
|
183
|
+
const rpName = options.rp?.name ?? "NOYDB";
|
|
184
|
+
const timeout = options.timeout ?? 6e4;
|
|
185
|
+
const challenge = globalThis.crypto.getRandomValues(new Uint8Array(32));
|
|
186
|
+
const userIdBytes = new TextEncoder().encode(keyring.userId);
|
|
187
|
+
const authenticatorSelection = {
|
|
188
|
+
userVerification: "required",
|
|
189
|
+
residentKey: "preferred"
|
|
190
|
+
};
|
|
191
|
+
if (options.preferCrossPlatform === true) {
|
|
192
|
+
authenticatorSelection.authenticatorAttachment = "cross-platform";
|
|
193
|
+
} else if (options.preferCrossPlatform === false) {
|
|
194
|
+
authenticatorSelection.authenticatorAttachment = "platform";
|
|
195
|
+
}
|
|
196
|
+
const extensionsInput = {
|
|
197
|
+
prf: { eval: { first: PRF_SALT } }
|
|
198
|
+
};
|
|
199
|
+
const credential = await navigator.credentials.create({
|
|
200
|
+
publicKey: {
|
|
201
|
+
challenge,
|
|
202
|
+
rp: { id: rpId, name: rpName },
|
|
203
|
+
user: {
|
|
204
|
+
id: userIdBytes,
|
|
205
|
+
name: keyring.userId,
|
|
206
|
+
displayName: keyring.displayName
|
|
207
|
+
},
|
|
208
|
+
pubKeyCredParams: [
|
|
209
|
+
{ type: "public-key", alg: -7 },
|
|
210
|
+
// ES256
|
|
211
|
+
{ type: "public-key", alg: -257 },
|
|
212
|
+
// RS256
|
|
213
|
+
{ type: "public-key", alg: -8 }
|
|
214
|
+
// EdDSA
|
|
215
|
+
],
|
|
216
|
+
authenticatorSelection,
|
|
217
|
+
extensions: extensionsInput,
|
|
218
|
+
timeout
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
if (!credential) {
|
|
222
|
+
throw new WebAuthnCancelledError("enrollment");
|
|
223
|
+
}
|
|
224
|
+
const authData = credential.response.getAuthenticatorData();
|
|
225
|
+
const beFlag = extractBEFlag(authData);
|
|
226
|
+
if (options.requireSingleDevice && beFlag) {
|
|
227
|
+
throw new WebAuthnMultiDeviceError();
|
|
228
|
+
}
|
|
229
|
+
const extensions = credential.getClientExtensionResults();
|
|
230
|
+
const prfOutput = extensions.prf?.results?.first;
|
|
231
|
+
const prfUsed = !!prfOutput;
|
|
232
|
+
const wrappingKey = prfOutput ? await deriveKeyFromPRF(prfOutput) : await deriveKeyFromRawId(credential.rawId);
|
|
233
|
+
const { wrappedPayload, wrapIv } = await wrapKeyringSummary(keyring, wrappingKey);
|
|
234
|
+
return {
|
|
235
|
+
_noydb_webauthn: 1,
|
|
236
|
+
vault,
|
|
237
|
+
userId: keyring.userId,
|
|
238
|
+
credentialId: (0, import_hub.bufferToBase64)(credential.rawId),
|
|
239
|
+
prfUsed,
|
|
240
|
+
beFlag,
|
|
241
|
+
requireSingleDevice: options.requireSingleDevice ?? false,
|
|
242
|
+
wrappedPayload,
|
|
243
|
+
wrapIv,
|
|
244
|
+
enrolledAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
async function unlockWebAuthn(enrollment, options = {}) {
|
|
248
|
+
if (!isWebAuthnAvailable()) {
|
|
249
|
+
throw new WebAuthnNotAvailableError();
|
|
250
|
+
}
|
|
251
|
+
const timeout = options.timeout ?? 6e4;
|
|
252
|
+
const credentialId = (0, import_hub.base64ToBuffer)(enrollment.credentialId);
|
|
253
|
+
const extensionsInput = enrollment.prfUsed ? { prf: { eval: { first: PRF_SALT } } } : {};
|
|
254
|
+
const assertion = await navigator.credentials.get({
|
|
255
|
+
publicKey: {
|
|
256
|
+
challenge: globalThis.crypto.getRandomValues(new Uint8Array(32)),
|
|
257
|
+
allowCredentials: [{ type: "public-key", id: credentialId }],
|
|
258
|
+
userVerification: "required",
|
|
259
|
+
extensions: extensionsInput,
|
|
260
|
+
timeout
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
if (!assertion) {
|
|
264
|
+
throw new WebAuthnCancelledError("assertion");
|
|
265
|
+
}
|
|
266
|
+
const authData = assertion.response.authenticatorData;
|
|
267
|
+
const beFlag = extractBEFlag(authData);
|
|
268
|
+
if (enrollment.requireSingleDevice && beFlag) {
|
|
269
|
+
throw new WebAuthnMultiDeviceError();
|
|
270
|
+
}
|
|
271
|
+
let wrappingKey;
|
|
272
|
+
if (enrollment.prfUsed) {
|
|
273
|
+
const extensions = assertion.getClientExtensionResults();
|
|
274
|
+
const prfOutput = extensions.prf?.results?.first;
|
|
275
|
+
if (!prfOutput) {
|
|
276
|
+
throw new import_hub2.ValidationError(
|
|
277
|
+
"PRF extension output not available at assertion time. The authenticator may not support PRF. Re-enroll without PRF support."
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
wrappingKey = await deriveKeyFromPRF(prfOutput);
|
|
281
|
+
} else {
|
|
282
|
+
wrappingKey = await deriveKeyFromRawId(assertion.rawId);
|
|
283
|
+
}
|
|
284
|
+
return unwrapKeyringSummary(enrollment, wrappingKey);
|
|
285
|
+
}
|
|
286
|
+
function isValidEnrollment(value) {
|
|
287
|
+
if (!value || typeof value !== "object") return false;
|
|
288
|
+
const e = value;
|
|
289
|
+
return e._noydb_webauthn === 1 && typeof e.vault === "string" && typeof e.userId === "string" && typeof e.credentialId === "string" && typeof e.wrappedPayload === "string" && typeof e.wrapIv === "string";
|
|
290
|
+
}
|
|
291
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
292
|
+
0 && (module.exports = {
|
|
293
|
+
ValidationError,
|
|
294
|
+
WebAuthnCancelledError,
|
|
295
|
+
WebAuthnMultiDeviceError,
|
|
296
|
+
WebAuthnNotAvailableError,
|
|
297
|
+
WebAuthnPRFUnavailableError,
|
|
298
|
+
enrollWebAuthn,
|
|
299
|
+
isValidEnrollment,
|
|
300
|
+
isWebAuthnAvailable,
|
|
301
|
+
unlockWebAuthn
|
|
302
|
+
});
|
|
303
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @noy-db/on-webauthn —\n *\n * Hardware-key keyring for noy-db using the WebAuthn API.\n *\n * Covers every form factor:\n * - Platform authenticators: Touch ID, Face ID, Windows Hello, Android biometric\n * - Roaming authenticators: YubiKey (5C NFC, Bio), SoloKey, Titan, any FIDO2 key\n * - Passkey-capable platform authenticators: iCloud Keychain, Google Password Manager\n *\n * Key derivation model\n * ────────────────────\n * This package uses the **PRF (Pseudo-Random Function) extension** when\n * available to derive a deterministic wrapping key from the WebAuthn\n * credential. The PRF output is consistent across assertions on the same\n * device/credential, enabling unlock-without-passphrase while keeping the\n * derived key bound to the physical authenticator.\n *\n * When PRF is not supported by the authenticator (common on older hardware),\n * the package falls back to HKDF-SHA256 over the credential's `rawId` —\n * the same approach as the pre-existing `@noy-db/core` biometric module.\n *\n * The derived key is NEVER persisted. It exists only in memory during the\n * unlock operation. What IS persisted (in the noy-db adapter, not in browser\n * storage) is the wrapped KEK: `encrypt(KEK, derivedKey)`.\n *\n * BE-flag guards\n * ──────────────\n * The backup-eligibility (BE) flag in a WebAuthn authenticator data signals\n * that the credential is (or can be) synced across devices — e.g. stored in\n * iCloud Keychain. For single-device security policies (air-gapped USB sticks,\n * high-security terminals), this is a threat: the credential is available on\n * any device where the user's iCloud account is signed in.\n *\n * The `requireSingleDevice: true` option rejects credentials with the BE flag\n * set during enrollment. Existing enrollments are checked at assertion time —\n * if the authenticator data shows BE=1 but `requireSingleDevice` was set at\n * enrollment, the assertion throws `WebAuthnMultiDeviceError`.\n *\n * Enrollment flow\n * ───────────────\n * 1. User is already authenticated (passphrase or existing session).\n * 2. Call `enrollWebAuthn(keyring, options)`.\n * 3. WebAuthn credential is created; PRF or rawId-derived key wraps the KEK.\n * 4. Returns a `WebAuthnEnrollment` — persist this to the noy-db adapter\n * via `saveEnrollment()`, or store it yourself in any encrypted collection.\n *\n * Unlock flow\n * ───────────\n * 1. Load the `WebAuthnEnrollment` via `loadEnrollment()`.\n * 2. Call `unlockWebAuthn(enrollment, keyring)` — triggers the WebAuthn\n * assertion prompt.\n * 3. On success, returns the unwrapped `CryptoKey` (the KEK) — use it to\n * re-hydrate the session via `createSession()`.\n */\n\nimport { bufferToBase64, base64ToBuffer } from '@noy-db/hub'\nimport { ValidationError } from '@noy-db/hub'\nimport type { UnlockedKeyring, Role } from '@noy-db/hub'\n\n// Re-export from core for convenience\nexport { ValidationError } from '@noy-db/hub'\n\n// ─── Error types ──────────────────────────────────────────────────────\n\n/**\n * Thrown when the WebAuthn API is not available in the current environment.\n *\n * Check `isWebAuthnAvailable()` before calling `enrollWebAuthn()` or\n * `unlockWebAuthn()` and show a fallback UI (passphrase entry) if this\n * returns false. Common scenarios: Node.js environments, older browsers,\n * non-HTTPS origins (WebAuthn requires a Secure Context).\n */\nexport class WebAuthnNotAvailableError extends Error {\n readonly code = 'WEBAUTHN_NOT_AVAILABLE'\n constructor() {\n super('WebAuthn is not available in this environment. A browser with navigator.credentials support is required.')\n this.name = 'WebAuthnNotAvailableError'\n }\n}\n\n/**\n * Thrown when the user dismisses the WebAuthn prompt without completing it.\n *\n * The `op` field distinguishes enrollment cancellation (user chose not to\n * enroll a hardware key) from assertion cancellation (user dismissed the\n * unlock prompt). Treat this as a user-initiated action, not an error — show\n * a \"use passphrase instead\" option rather than an error message.\n */\nexport class WebAuthnCancelledError extends Error {\n readonly code = 'WEBAUTHN_CANCELLED'\n constructor(op: 'enrollment' | 'assertion') {\n super(`WebAuthn ${op} was cancelled by the user.`)\n this.name = 'WebAuthnCancelledError'\n }\n}\n\n/**\n * Thrown when the authenticator has the backup-eligible (BE) flag set but\n * the vault requires a single-device credential (`requireSingleDevice: true`).\n *\n * A BE credential is synced across devices (e.g. iCloud Keychain, Google\n * Password Manager), which violates the single-device security model. The\n * user must enroll a hardware security key (YubiKey, Titan, SoloKey) instead.\n */\nexport class WebAuthnMultiDeviceError extends Error {\n readonly code = 'WEBAUTHN_MULTI_DEVICE'\n constructor() {\n super(\n 'This credential is backup-eligible (BE flag set) and may be synced across devices. ' +\n 'The vault requires a single-device credential (requireSingleDevice: true). ' +\n 'Please use a hardware security key (YubiKey, Titan, SoloKey) or a platform ' +\n 'authenticator that does not sync credentials across devices.',\n )\n this.name = 'WebAuthnMultiDeviceError'\n }\n}\n\n/**\n * Thrown (as a non-fatal warning, caught internally) when the PRF extension\n * is not supported by the authenticator.\n *\n * NOYDB prefers PRF for key derivation because it produces a\n * credential-bound output that is deterministic and not extractable from\n * the authenticator. When PRF is unavailable, enrollment falls back to\n * HKDF over the credential's `rawId` — weaker binding, but still functional.\n * This error is caught at enrollment time; callers only see it if they\n * explicitly opt into strict PRF-only mode.\n */\nexport class WebAuthnPRFUnavailableError extends Error {\n readonly code = 'WEBAUTHN_PRF_UNAVAILABLE'\n constructor() {\n super(\n 'The PRF extension is not available on this authenticator. ' +\n 'Enrollment will fall back to rawId-based key derivation. ' +\n 'This provides weaker binding to the specific authenticator.',\n )\n this.name = 'WebAuthnPRFUnavailableError'\n }\n}\n\n// ─── Types ────────────────────────────────────────────────────────────\n\n/**\n * A persisted WebAuthn enrollment record. Store this in a noy-db\n * collection (encrypted like any other record) or return it from\n * `saveEnrollment()` / `loadEnrollment()` helpers.\n */\nexport interface WebAuthnEnrollment {\n /** Enrollment format version. */\n readonly _noydb_webauthn: 1\n /** The vault this enrollment was created for. */\n readonly vault: string\n /** The user ID this enrollment belongs to. */\n readonly userId: string\n /** WebAuthn credential ID (base64). Use for allowCredentials in assertions. */\n readonly credentialId: string\n /** Whether PRF was used for key derivation (vs rawId HKDF fallback). */\n readonly prfUsed: boolean\n /** Whether the BE (backup-eligibility) flag was present at enrollment time. */\n readonly beFlag: boolean\n /** Whether single-device was required at enrollment time. */\n readonly requireSingleDevice: boolean\n /** The wrapped KEK: encrypt(exportedDekMap, derivedKey). Base64. */\n readonly wrappedPayload: string\n /** IV used for the wrapping. Base64. */\n readonly wrapIv: string\n /** ISO timestamp of enrollment. */\n readonly enrolledAt: string\n}\n\n/** Options for `enrollWebAuthn()`. */\nexport interface WebAuthnEnrollOptions {\n /**\n * Relying party ID and name for the WebAuthn credential.\n * Defaults to `{ id: window.location.hostname, name: 'NOYDB' }`.\n */\n rp?: { id?: string; name: string }\n /**\n * If `true`, refuse to enroll credentials with the BE flag set\n * (multi-device / syncable passkeys). Defaults to `false`.\n *\n * Set to `true` for high-security deployments where the credential\n * must be bound to a single physical device (YubiKey, Titan, etc.).\n */\n requireSingleDevice?: boolean\n /**\n * WebAuthn timeout in milliseconds. Default: 60_000.\n */\n timeout?: number\n /**\n * If `true`, prefer a cross-platform authenticator (roaming security key).\n * If `false`, prefer a platform authenticator (Touch ID, Face ID).\n * If undefined, let the browser choose.\n */\n preferCrossPlatform?: boolean\n}\n\n/** Options for `unlockWebAuthn()`. */\nexport interface WebAuthnUnlockOptions {\n /** WebAuthn timeout in milliseconds. Default: 60_000. */\n timeout?: number\n}\n\n// ─── Environment check ─────────────────────────────────────────────────\n\n/**\n * Returns `true` if WebAuthn is available and can be used for enrollment or unlock.\n *\n * Checks for `navigator.credentials`, `window.PublicKeyCredential`, and a\n * Secure Context (`window.isSecureContext`). Call this before rendering the\n * \"Register hardware key\" button to avoid showing options that will fail.\n */\nexport function isWebAuthnAvailable(): boolean {\n return (\n typeof window !== 'undefined' &&\n typeof window.PublicKeyCredential !== 'undefined' &&\n typeof navigator !== 'undefined' &&\n typeof navigator.credentials !== 'undefined'\n )\n}\n\n// ─── PRF salt ─────────────────────────────────────────────────────────\n\nconst PRF_SALT = new TextEncoder().encode('noydb-webauthn-kek-derive')\n\n// ─── Key derivation helpers ────────────────────────────────────────────\n\n/**\n * Derive a wrapping key from PRF output.\n * PRF output is 32 bytes of authenticator-bound pseudo-random data.\n */\nasync function deriveKeyFromPRF(prfOutput: ArrayBuffer): Promise<CryptoKey> {\n const keyMaterial = await globalThis.crypto.subtle.importKey(\n 'raw',\n prfOutput,\n 'HKDF',\n false,\n ['deriveKey'],\n )\n return globalThis.crypto.subtle.deriveKey(\n {\n name: 'HKDF',\n hash: 'SHA-256',\n salt: PRF_SALT,\n info: new TextEncoder().encode('noydb-kek-wrap-v1'),\n },\n keyMaterial,\n { name: 'AES-GCM', length: 256 },\n false,\n ['encrypt', 'decrypt'],\n )\n}\n\n/**\n * Derive a wrapping key from the credential's rawId (fallback when PRF unavailable).\n * Weaker than PRF (rawId may be observable to the server) but universally supported.\n */\nasync function deriveKeyFromRawId(rawId: ArrayBuffer): Promise<CryptoKey> {\n const keyMaterial = await globalThis.crypto.subtle.importKey(\n 'raw',\n rawId,\n 'HKDF',\n false,\n ['deriveKey'],\n )\n return globalThis.crypto.subtle.deriveKey(\n {\n name: 'HKDF',\n hash: 'SHA-256',\n salt: new TextEncoder().encode('noydb-webauthn-rawid-fallback'),\n info: new TextEncoder().encode('noydb-kek-wrap-v1'),\n },\n keyMaterial,\n { name: 'AES-GCM', length: 256 },\n false,\n ['encrypt', 'decrypt'],\n )\n}\n\n// ─── BE flag extraction ────────────────────────────────────────────────\n\n/**\n * Extract the BE (backup-eligibility) flag from WebAuthn authenticator data.\n * Authenticator data byte layout (CTAP2 spec):\n * bytes 0-31: rpIdHash\n * byte 32: flags byte\n * bytes 33-36: signCount\n * ...\n *\n * Flags byte bit layout (bit 0 = LSB):\n * bit 0 (UP): user presence\n * bit 2 (UV): user verification\n * bit 3 (BE): backup eligibility\n * bit 4 (BS): backup state\n * bit 6 (AT): attested credential data present\n * bit 7 (ED): extension data present\n */\nfunction extractBEFlag(authData: ArrayBuffer): boolean {\n const bytes = new Uint8Array(authData)\n if (bytes.length < 33) return false\n const flagsByte = bytes[32]!\n return (flagsByte & 0b00001000) !== 0 // bit 3\n}\n\n// ─── Payload wrap/unwrap ───────────────────────────────────────────────\n\n/**\n * Serialize and encrypt the DEK map from `keyring` using `wrappingKey`.\n * The wrapped payload is what gets stored in the enrollment record.\n */\nasync function wrapKeyringSummary(\n keyring: UnlockedKeyring,\n wrappingKey: CryptoKey,\n): Promise<{ wrappedPayload: string; wrapIv: string }> {\n const dekMap: Record<string, string> = {}\n for (const [collName, dek] of keyring.deks) {\n const raw = await globalThis.crypto.subtle.exportKey('raw', dek)\n dekMap[collName] = bufferToBase64(raw)\n }\n\n const payload = JSON.stringify({\n userId: keyring.userId,\n displayName: keyring.displayName,\n role: keyring.role,\n permissions: keyring.permissions,\n deks: dekMap,\n salt: bufferToBase64(keyring.salt),\n })\n\n const iv = globalThis.crypto.getRandomValues(new Uint8Array(12))\n const encrypted = await globalThis.crypto.subtle.encrypt(\n { name: 'AES-GCM', iv },\n wrappingKey,\n new TextEncoder().encode(payload),\n )\n\n return { wrappedPayload: bufferToBase64(encrypted), wrapIv: bufferToBase64(iv) }\n}\n\n/**\n * Decrypt and deserialize the keyring payload using `wrappingKey`.\n */\nasync function unwrapKeyringSummary(\n enrollment: WebAuthnEnrollment,\n wrappingKey: CryptoKey,\n): Promise<UnlockedKeyring> {\n const iv = base64ToBuffer(enrollment.wrapIv)\n const ciphertext = base64ToBuffer(enrollment.wrappedPayload)\n\n let plaintext: ArrayBuffer\n try {\n plaintext = await globalThis.crypto.subtle.decrypt(\n { name: 'AES-GCM', iv },\n wrappingKey,\n ciphertext,\n )\n } catch {\n throw new ValidationError('WebAuthn decryption failed — the authenticator may have changed or the enrollment may be corrupt.')\n }\n\n const parsed = JSON.parse(new TextDecoder().decode(plaintext)) as {\n userId: string\n displayName: string\n role: Role\n permissions: Record<string, 'rw' | 'ro'>\n deks: Record<string, string>\n salt: string\n }\n\n const deks = new Map<string, CryptoKey>()\n for (const [collName, rawBase64] of Object.entries(parsed.deks)) {\n const dek = await globalThis.crypto.subtle.importKey(\n 'raw',\n base64ToBuffer(rawBase64),\n { name: 'AES-GCM', length: 256 },\n true,\n ['encrypt', 'decrypt'],\n )\n deks.set(collName, dek)\n }\n\n return {\n userId: parsed.userId,\n displayName: parsed.displayName,\n role: parsed.role,\n permissions: parsed.permissions,\n deks,\n kek: null as unknown as CryptoKey,\n salt: base64ToBuffer(parsed.salt),\n }\n}\n\n// ─── Public API ────────────────────────────────────────────────────────\n\n/**\n * Enroll a WebAuthn credential for the given keyring.\n *\n * The caller must already have an unlocked keyring (from passphrase auth or\n * an existing session). The WebAuthn credential creation prompt is triggered\n * by this call.\n *\n * Returns a `WebAuthnEnrollment` that should be persisted — typically via\n * `saveEnrollment()` into a noy-db collection.\n *\n * @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.\n * @throws `WebAuthnCancelledError` if the user cancels the credential creation.\n * @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` is true and the\n * authenticator returned a credential with the BE flag set.\n */\nexport async function enrollWebAuthn(\n keyring: UnlockedKeyring,\n vault: string,\n options: WebAuthnEnrollOptions = {},\n): Promise<WebAuthnEnrollment> {\n if (!isWebAuthnAvailable()) {\n throw new WebAuthnNotAvailableError()\n }\n\n const rpId = options.rp?.id ?? (typeof window !== 'undefined' ? window.location.hostname : 'localhost')\n const rpName = options.rp?.name ?? 'NOYDB'\n const timeout = options.timeout ?? 60_000\n\n const challenge = globalThis.crypto.getRandomValues(new Uint8Array(32))\n const userIdBytes = new TextEncoder().encode(keyring.userId)\n\n const authenticatorSelection: AuthenticatorSelectionCriteria = {\n userVerification: 'required',\n residentKey: 'preferred',\n }\n if (options.preferCrossPlatform === true) {\n authenticatorSelection.authenticatorAttachment = 'cross-platform'\n } else if (options.preferCrossPlatform === false) {\n authenticatorSelection.authenticatorAttachment = 'platform'\n }\n\n // Request PRF extension for deterministic key derivation\n const extensionsInput = {\n prf: { eval: { first: PRF_SALT } },\n } as AuthenticationExtensionsClientInputs\n\n const credential = await navigator.credentials.create({\n publicKey: {\n challenge,\n rp: { id: rpId, name: rpName },\n user: {\n id: userIdBytes,\n name: keyring.userId,\n displayName: keyring.displayName,\n },\n pubKeyCredParams: [\n { type: 'public-key', alg: -7 }, // ES256\n { type: 'public-key', alg: -257 }, // RS256\n { type: 'public-key', alg: -8 }, // EdDSA\n ],\n authenticatorSelection,\n extensions: extensionsInput,\n timeout,\n },\n }) as PublicKeyCredential | null\n\n if (!credential) {\n throw new WebAuthnCancelledError('enrollment')\n }\n\n const authData = (credential.response as AuthenticatorAttestationResponse).getAuthenticatorData()\n const beFlag = extractBEFlag(authData)\n\n if (options.requireSingleDevice && beFlag) {\n throw new WebAuthnMultiDeviceError()\n }\n\n // Try to get PRF output from extensions\n const extensions = credential.getClientExtensionResults() as {\n prf?: { results?: { first?: ArrayBuffer } }\n }\n const prfOutput = extensions.prf?.results?.first\n const prfUsed = !!prfOutput\n\n const wrappingKey = prfOutput\n ? await deriveKeyFromPRF(prfOutput)\n : await deriveKeyFromRawId(credential.rawId)\n\n const { wrappedPayload, wrapIv } = await wrapKeyringSummary(keyring, wrappingKey)\n\n return {\n _noydb_webauthn: 1,\n vault,\n userId: keyring.userId,\n credentialId: bufferToBase64(credential.rawId),\n prfUsed,\n beFlag,\n requireSingleDevice: options.requireSingleDevice ?? false,\n wrappedPayload,\n wrapIv,\n enrolledAt: new Date().toISOString(),\n }\n}\n\n/**\n * Unlock a vault using a previously enrolled WebAuthn credential.\n *\n * Triggers the WebAuthn assertion prompt. On success, decrypts the keyring\n * payload from the enrollment record and returns an `UnlockedKeyring`.\n *\n * The returned keyring has the same DEKs as at enrollment time. If DEKs\n * have been rotated since enrollment, this will return stale DEKs — the\n * caller should detect decryption failures and prompt for re-enrollment.\n *\n * @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.\n * @throws `WebAuthnCancelledError` if the user cancels the assertion.\n * @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` was set at\n * enrollment and the authenticator data now shows BE=1.\n * @throws `ValidationError` if decryption of the keyring payload fails.\n */\nexport async function unlockWebAuthn(\n enrollment: WebAuthnEnrollment,\n options: WebAuthnUnlockOptions = {},\n): Promise<UnlockedKeyring> {\n if (!isWebAuthnAvailable()) {\n throw new WebAuthnNotAvailableError()\n }\n\n const timeout = options.timeout ?? 60_000\n const credentialId = base64ToBuffer(enrollment.credentialId)\n\n const extensionsInput = (enrollment.prfUsed\n ? { prf: { eval: { first: PRF_SALT } } }\n : {}\n ) as AuthenticationExtensionsClientInputs\n\n const assertion = await navigator.credentials.get({\n publicKey: {\n challenge: globalThis.crypto.getRandomValues(new Uint8Array(32)),\n allowCredentials: [{ type: 'public-key', id: credentialId as BufferSource }],\n userVerification: 'required',\n extensions: extensionsInput,\n timeout,\n },\n }) as PublicKeyCredential | null\n\n if (!assertion) {\n throw new WebAuthnCancelledError('assertion')\n }\n\n // BE-flag guard at assertion time\n const authData = (assertion.response as AuthenticatorAssertionResponse).authenticatorData\n const beFlag = extractBEFlag(authData)\n if (enrollment.requireSingleDevice && beFlag) {\n throw new WebAuthnMultiDeviceError()\n }\n\n // Derive the wrapping key using the same method as enrollment\n let wrappingKey: CryptoKey\n if (enrollment.prfUsed) {\n const extensions = assertion.getClientExtensionResults() as {\n prf?: { results?: { first?: ArrayBuffer } }\n }\n const prfOutput = extensions.prf?.results?.first\n if (!prfOutput) {\n throw new ValidationError(\n 'PRF extension output not available at assertion time. ' +\n 'The authenticator may not support PRF. Re-enroll without PRF support.',\n )\n }\n wrappingKey = await deriveKeyFromPRF(prfOutput)\n } else {\n wrappingKey = await deriveKeyFromRawId(assertion.rawId)\n }\n\n return unwrapKeyringSummary(enrollment, wrappingKey)\n}\n\n/**\n * Check whether a `WebAuthnEnrollment` record looks well-formed.\n * Does not perform any cryptographic verification.\n */\nexport function isValidEnrollment(value: unknown): value is WebAuthnEnrollment {\n if (!value || typeof value !== 'object') return false\n const e = value as Record<string, unknown>\n return (\n e._noydb_webauthn === 1 &&\n typeof e.vault === 'string' &&\n typeof e.userId === 'string' &&\n typeof e.credentialId === 'string' &&\n typeof e.wrappedPayload === 'string' &&\n typeof e.wrapIv === 'string'\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwDA,iBAA+C;AAC/C,IAAAA,cAAgC;AAIhC,IAAAA,cAAgC;AAYzB,IAAM,4BAAN,cAAwC,MAAM;AAAA,EAC1C,OAAO;AAAA,EAChB,cAAc;AACZ,UAAM,0GAA0G;AAChH,SAAK,OAAO;AAAA,EACd;AACF;AAUO,IAAM,yBAAN,cAAqC,MAAM;AAAA,EACvC,OAAO;AAAA,EAChB,YAAY,IAAgC;AAC1C,UAAM,YAAY,EAAE,6BAA6B;AACjD,SAAK,OAAO;AAAA,EACd;AACF;AAUO,IAAM,2BAAN,cAAuC,MAAM;AAAA,EACzC,OAAO;AAAA,EAChB,cAAc;AACZ;AAAA,MACE;AAAA,IAIF;AACA,SAAK,OAAO;AAAA,EACd;AACF;AAaO,IAAM,8BAAN,cAA0C,MAAM;AAAA,EAC5C,OAAO;AAAA,EAChB,cAAc;AACZ;AAAA,MACE;AAAA,IAGF;AACA,SAAK,OAAO;AAAA,EACd;AACF;AA0EO,SAAS,sBAA+B;AAC7C,SACE,OAAO,WAAW,eAClB,OAAO,OAAO,wBAAwB,eACtC,OAAO,cAAc,eACrB,OAAO,UAAU,gBAAgB;AAErC;AAIA,IAAM,WAAW,IAAI,YAAY,EAAE,OAAO,2BAA2B;AAQrE,eAAe,iBAAiB,WAA4C;AAC1E,QAAM,cAAc,MAAM,WAAW,OAAO,OAAO;AAAA,IACjD;AAAA,IACA;AAAA,IACA;AAAA,IACA;A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,uBAAuB,WAAW;AAAA,EAC9C;AAGA,QAAM,WAAY,UAAU,SAA4C;AACxE,QAAM,SAAS,cAAc,QAAQ;AACrC,MAAI,WAAW,uBAAuB,QAAQ;AAC5C,UAAM,IAAI,yBAAyB;AAAA,EACrC;AAGA,MAAI;AACJ,MAAI,WAAW,SAAS;AACtB,UAAM,aAAa,UAAU,0BAA0B;AAGvD,UAAM,YAAY,WAAW,KAAK,SAAS;AAC3C,QAAI,CAAC,WAAW;AACd,YAAM,IAAI;AAAA,QACR;AAAA,MAEF;AAAA,IACF;AACA,kBAAc,MAAM,iBAAiB,SAAS;AAAA,EAChD,OAAO;AACL,kBAAc,MAAM,mBAAmB,UAAU,KAAK;AAAA,EACxD;AAEA,SAAO,qBAAqB,YAAY,WAAW;AACrD;AAMO,SAAS,kBAAkB,OAA6C;AAC7E,MAAI,CAAC,SAAS,OAAO,UAAU,SAAU,QAAO;AAChD,QAAM,IAAI;AACV,SACE,EAAE,oBAAoB,KACtB,OAAO,EAAE,UAAU,YACnB,OAAO,EAAE,WAAW,YACpB,OAAO,EAAE,iBAAiB,YAC1B,OAAO,EAAE,mBAAmB,YAC5B,OAAO,EAAE,WAAW;AAExB;","names":["import_hub"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { UnlockedKeyring } from '@noy-db/hub';
|
|
2
|
+
export { ValidationError } from '@noy-db/hub';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @noy-db/on-webauthn —
|
|
6
|
+
*
|
|
7
|
+
* Hardware-key keyring for noy-db using the WebAuthn API.
|
|
8
|
+
*
|
|
9
|
+
* Covers every form factor:
|
|
10
|
+
* - Platform authenticators: Touch ID, Face ID, Windows Hello, Android biometric
|
|
11
|
+
* - Roaming authenticators: YubiKey (5C NFC, Bio), SoloKey, Titan, any FIDO2 key
|
|
12
|
+
* - Passkey-capable platform authenticators: iCloud Keychain, Google Password Manager
|
|
13
|
+
*
|
|
14
|
+
* Key derivation model
|
|
15
|
+
* ────────────────────
|
|
16
|
+
* This package uses the **PRF (Pseudo-Random Function) extension** when
|
|
17
|
+
* available to derive a deterministic wrapping key from the WebAuthn
|
|
18
|
+
* credential. The PRF output is consistent across assertions on the same
|
|
19
|
+
* device/credential, enabling unlock-without-passphrase while keeping the
|
|
20
|
+
* derived key bound to the physical authenticator.
|
|
21
|
+
*
|
|
22
|
+
* When PRF is not supported by the authenticator (common on older hardware),
|
|
23
|
+
* the package falls back to HKDF-SHA256 over the credential's `rawId` —
|
|
24
|
+
* the same approach as the pre-existing `@noy-db/core` biometric module.
|
|
25
|
+
*
|
|
26
|
+
* The derived key is NEVER persisted. It exists only in memory during the
|
|
27
|
+
* unlock operation. What IS persisted (in the noy-db adapter, not in browser
|
|
28
|
+
* storage) is the wrapped KEK: `encrypt(KEK, derivedKey)`.
|
|
29
|
+
*
|
|
30
|
+
* BE-flag guards
|
|
31
|
+
* ──────────────
|
|
32
|
+
* The backup-eligibility (BE) flag in a WebAuthn authenticator data signals
|
|
33
|
+
* that the credential is (or can be) synced across devices — e.g. stored in
|
|
34
|
+
* iCloud Keychain. For single-device security policies (air-gapped USB sticks,
|
|
35
|
+
* high-security terminals), this is a threat: the credential is available on
|
|
36
|
+
* any device where the user's iCloud account is signed in.
|
|
37
|
+
*
|
|
38
|
+
* The `requireSingleDevice: true` option rejects credentials with the BE flag
|
|
39
|
+
* set during enrollment. Existing enrollments are checked at assertion time —
|
|
40
|
+
* if the authenticator data shows BE=1 but `requireSingleDevice` was set at
|
|
41
|
+
* enrollment, the assertion throws `WebAuthnMultiDeviceError`.
|
|
42
|
+
*
|
|
43
|
+
* Enrollment flow
|
|
44
|
+
* ───────────────
|
|
45
|
+
* 1. User is already authenticated (passphrase or existing session).
|
|
46
|
+
* 2. Call `enrollWebAuthn(keyring, options)`.
|
|
47
|
+
* 3. WebAuthn credential is created; PRF or rawId-derived key wraps the KEK.
|
|
48
|
+
* 4. Returns a `WebAuthnEnrollment` — persist this to the noy-db adapter
|
|
49
|
+
* via `saveEnrollment()`, or store it yourself in any encrypted collection.
|
|
50
|
+
*
|
|
51
|
+
* Unlock flow
|
|
52
|
+
* ───────────
|
|
53
|
+
* 1. Load the `WebAuthnEnrollment` via `loadEnrollment()`.
|
|
54
|
+
* 2. Call `unlockWebAuthn(enrollment, keyring)` — triggers the WebAuthn
|
|
55
|
+
* assertion prompt.
|
|
56
|
+
* 3. On success, returns the unwrapped `CryptoKey` (the KEK) — use it to
|
|
57
|
+
* re-hydrate the session via `createSession()`.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Thrown when the WebAuthn API is not available in the current environment.
|
|
62
|
+
*
|
|
63
|
+
* Check `isWebAuthnAvailable()` before calling `enrollWebAuthn()` or
|
|
64
|
+
* `unlockWebAuthn()` and show a fallback UI (passphrase entry) if this
|
|
65
|
+
* returns false. Common scenarios: Node.js environments, older browsers,
|
|
66
|
+
* non-HTTPS origins (WebAuthn requires a Secure Context).
|
|
67
|
+
*/
|
|
68
|
+
declare class WebAuthnNotAvailableError extends Error {
|
|
69
|
+
readonly code = "WEBAUTHN_NOT_AVAILABLE";
|
|
70
|
+
constructor();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Thrown when the user dismisses the WebAuthn prompt without completing it.
|
|
74
|
+
*
|
|
75
|
+
* The `op` field distinguishes enrollment cancellation (user chose not to
|
|
76
|
+
* enroll a hardware key) from assertion cancellation (user dismissed the
|
|
77
|
+
* unlock prompt). Treat this as a user-initiated action, not an error — show
|
|
78
|
+
* a "use passphrase instead" option rather than an error message.
|
|
79
|
+
*/
|
|
80
|
+
declare class WebAuthnCancelledError extends Error {
|
|
81
|
+
readonly code = "WEBAUTHN_CANCELLED";
|
|
82
|
+
constructor(op: 'enrollment' | 'assertion');
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Thrown when the authenticator has the backup-eligible (BE) flag set but
|
|
86
|
+
* the vault requires a single-device credential (`requireSingleDevice: true`).
|
|
87
|
+
*
|
|
88
|
+
* A BE credential is synced across devices (e.g. iCloud Keychain, Google
|
|
89
|
+
* Password Manager), which violates the single-device security model. The
|
|
90
|
+
* user must enroll a hardware security key (YubiKey, Titan, SoloKey) instead.
|
|
91
|
+
*/
|
|
92
|
+
declare class WebAuthnMultiDeviceError extends Error {
|
|
93
|
+
readonly code = "WEBAUTHN_MULTI_DEVICE";
|
|
94
|
+
constructor();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Thrown (as a non-fatal warning, caught internally) when the PRF extension
|
|
98
|
+
* is not supported by the authenticator.
|
|
99
|
+
*
|
|
100
|
+
* NOYDB prefers PRF for key derivation because it produces a
|
|
101
|
+
* credential-bound output that is deterministic and not extractable from
|
|
102
|
+
* the authenticator. When PRF is unavailable, enrollment falls back to
|
|
103
|
+
* HKDF over the credential's `rawId` — weaker binding, but still functional.
|
|
104
|
+
* This error is caught at enrollment time; callers only see it if they
|
|
105
|
+
* explicitly opt into strict PRF-only mode.
|
|
106
|
+
*/
|
|
107
|
+
declare class WebAuthnPRFUnavailableError extends Error {
|
|
108
|
+
readonly code = "WEBAUTHN_PRF_UNAVAILABLE";
|
|
109
|
+
constructor();
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A persisted WebAuthn enrollment record. Store this in a noy-db
|
|
113
|
+
* collection (encrypted like any other record) or return it from
|
|
114
|
+
* `saveEnrollment()` / `loadEnrollment()` helpers.
|
|
115
|
+
*/
|
|
116
|
+
interface WebAuthnEnrollment {
|
|
117
|
+
/** Enrollment format version. */
|
|
118
|
+
readonly _noydb_webauthn: 1;
|
|
119
|
+
/** The vault this enrollment was created for. */
|
|
120
|
+
readonly vault: string;
|
|
121
|
+
/** The user ID this enrollment belongs to. */
|
|
122
|
+
readonly userId: string;
|
|
123
|
+
/** WebAuthn credential ID (base64). Use for allowCredentials in assertions. */
|
|
124
|
+
readonly credentialId: string;
|
|
125
|
+
/** Whether PRF was used for key derivation (vs rawId HKDF fallback). */
|
|
126
|
+
readonly prfUsed: boolean;
|
|
127
|
+
/** Whether the BE (backup-eligibility) flag was present at enrollment time. */
|
|
128
|
+
readonly beFlag: boolean;
|
|
129
|
+
/** Whether single-device was required at enrollment time. */
|
|
130
|
+
readonly requireSingleDevice: boolean;
|
|
131
|
+
/** The wrapped KEK: encrypt(exportedDekMap, derivedKey). Base64. */
|
|
132
|
+
readonly wrappedPayload: string;
|
|
133
|
+
/** IV used for the wrapping. Base64. */
|
|
134
|
+
readonly wrapIv: string;
|
|
135
|
+
/** ISO timestamp of enrollment. */
|
|
136
|
+
readonly enrolledAt: string;
|
|
137
|
+
}
|
|
138
|
+
/** Options for `enrollWebAuthn()`. */
|
|
139
|
+
interface WebAuthnEnrollOptions {
|
|
140
|
+
/**
|
|
141
|
+
* Relying party ID and name for the WebAuthn credential.
|
|
142
|
+
* Defaults to `{ id: window.location.hostname, name: 'NOYDB' }`.
|
|
143
|
+
*/
|
|
144
|
+
rp?: {
|
|
145
|
+
id?: string;
|
|
146
|
+
name: string;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* If `true`, refuse to enroll credentials with the BE flag set
|
|
150
|
+
* (multi-device / syncable passkeys). Defaults to `false`.
|
|
151
|
+
*
|
|
152
|
+
* Set to `true` for high-security deployments where the credential
|
|
153
|
+
* must be bound to a single physical device (YubiKey, Titan, etc.).
|
|
154
|
+
*/
|
|
155
|
+
requireSingleDevice?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* WebAuthn timeout in milliseconds. Default: 60_000.
|
|
158
|
+
*/
|
|
159
|
+
timeout?: number;
|
|
160
|
+
/**
|
|
161
|
+
* If `true`, prefer a cross-platform authenticator (roaming security key).
|
|
162
|
+
* If `false`, prefer a platform authenticator (Touch ID, Face ID).
|
|
163
|
+
* If undefined, let the browser choose.
|
|
164
|
+
*/
|
|
165
|
+
preferCrossPlatform?: boolean;
|
|
166
|
+
}
|
|
167
|
+
/** Options for `unlockWebAuthn()`. */
|
|
168
|
+
interface WebAuthnUnlockOptions {
|
|
169
|
+
/** WebAuthn timeout in milliseconds. Default: 60_000. */
|
|
170
|
+
timeout?: number;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Returns `true` if WebAuthn is available and can be used for enrollment or unlock.
|
|
174
|
+
*
|
|
175
|
+
* Checks for `navigator.credentials`, `window.PublicKeyCredential`, and a
|
|
176
|
+
* Secure Context (`window.isSecureContext`). Call this before rendering the
|
|
177
|
+
* "Register hardware key" button to avoid showing options that will fail.
|
|
178
|
+
*/
|
|
179
|
+
declare function isWebAuthnAvailable(): boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Enroll a WebAuthn credential for the given keyring.
|
|
182
|
+
*
|
|
183
|
+
* The caller must already have an unlocked keyring (from passphrase auth or
|
|
184
|
+
* an existing session). The WebAuthn credential creation prompt is triggered
|
|
185
|
+
* by this call.
|
|
186
|
+
*
|
|
187
|
+
* Returns a `WebAuthnEnrollment` that should be persisted — typically via
|
|
188
|
+
* `saveEnrollment()` into a noy-db collection.
|
|
189
|
+
*
|
|
190
|
+
* @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.
|
|
191
|
+
* @throws `WebAuthnCancelledError` if the user cancels the credential creation.
|
|
192
|
+
* @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` is true and the
|
|
193
|
+
* authenticator returned a credential with the BE flag set.
|
|
194
|
+
*/
|
|
195
|
+
declare function enrollWebAuthn(keyring: UnlockedKeyring, vault: string, options?: WebAuthnEnrollOptions): Promise<WebAuthnEnrollment>;
|
|
196
|
+
/**
|
|
197
|
+
* Unlock a vault using a previously enrolled WebAuthn credential.
|
|
198
|
+
*
|
|
199
|
+
* Triggers the WebAuthn assertion prompt. On success, decrypts the keyring
|
|
200
|
+
* payload from the enrollment record and returns an `UnlockedKeyring`.
|
|
201
|
+
*
|
|
202
|
+
* The returned keyring has the same DEKs as at enrollment time. If DEKs
|
|
203
|
+
* have been rotated since enrollment, this will return stale DEKs — the
|
|
204
|
+
* caller should detect decryption failures and prompt for re-enrollment.
|
|
205
|
+
*
|
|
206
|
+
* @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.
|
|
207
|
+
* @throws `WebAuthnCancelledError` if the user cancels the assertion.
|
|
208
|
+
* @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` was set at
|
|
209
|
+
* enrollment and the authenticator data now shows BE=1.
|
|
210
|
+
* @throws `ValidationError` if decryption of the keyring payload fails.
|
|
211
|
+
*/
|
|
212
|
+
declare function unlockWebAuthn(enrollment: WebAuthnEnrollment, options?: WebAuthnUnlockOptions): Promise<UnlockedKeyring>;
|
|
213
|
+
/**
|
|
214
|
+
* Check whether a `WebAuthnEnrollment` record looks well-formed.
|
|
215
|
+
* Does not perform any cryptographic verification.
|
|
216
|
+
*/
|
|
217
|
+
declare function isValidEnrollment(value: unknown): value is WebAuthnEnrollment;
|
|
218
|
+
|
|
219
|
+
export { WebAuthnCancelledError, type WebAuthnEnrollOptions, type WebAuthnEnrollment, WebAuthnMultiDeviceError, WebAuthnNotAvailableError, WebAuthnPRFUnavailableError, type WebAuthnUnlockOptions, enrollWebAuthn, isValidEnrollment, isWebAuthnAvailable, unlockWebAuthn };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { UnlockedKeyring } from '@noy-db/hub';
|
|
2
|
+
export { ValidationError } from '@noy-db/hub';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @noy-db/on-webauthn —
|
|
6
|
+
*
|
|
7
|
+
* Hardware-key keyring for noy-db using the WebAuthn API.
|
|
8
|
+
*
|
|
9
|
+
* Covers every form factor:
|
|
10
|
+
* - Platform authenticators: Touch ID, Face ID, Windows Hello, Android biometric
|
|
11
|
+
* - Roaming authenticators: YubiKey (5C NFC, Bio), SoloKey, Titan, any FIDO2 key
|
|
12
|
+
* - Passkey-capable platform authenticators: iCloud Keychain, Google Password Manager
|
|
13
|
+
*
|
|
14
|
+
* Key derivation model
|
|
15
|
+
* ────────────────────
|
|
16
|
+
* This package uses the **PRF (Pseudo-Random Function) extension** when
|
|
17
|
+
* available to derive a deterministic wrapping key from the WebAuthn
|
|
18
|
+
* credential. The PRF output is consistent across assertions on the same
|
|
19
|
+
* device/credential, enabling unlock-without-passphrase while keeping the
|
|
20
|
+
* derived key bound to the physical authenticator.
|
|
21
|
+
*
|
|
22
|
+
* When PRF is not supported by the authenticator (common on older hardware),
|
|
23
|
+
* the package falls back to HKDF-SHA256 over the credential's `rawId` —
|
|
24
|
+
* the same approach as the pre-existing `@noy-db/core` biometric module.
|
|
25
|
+
*
|
|
26
|
+
* The derived key is NEVER persisted. It exists only in memory during the
|
|
27
|
+
* unlock operation. What IS persisted (in the noy-db adapter, not in browser
|
|
28
|
+
* storage) is the wrapped KEK: `encrypt(KEK, derivedKey)`.
|
|
29
|
+
*
|
|
30
|
+
* BE-flag guards
|
|
31
|
+
* ──────────────
|
|
32
|
+
* The backup-eligibility (BE) flag in a WebAuthn authenticator data signals
|
|
33
|
+
* that the credential is (or can be) synced across devices — e.g. stored in
|
|
34
|
+
* iCloud Keychain. For single-device security policies (air-gapped USB sticks,
|
|
35
|
+
* high-security terminals), this is a threat: the credential is available on
|
|
36
|
+
* any device where the user's iCloud account is signed in.
|
|
37
|
+
*
|
|
38
|
+
* The `requireSingleDevice: true` option rejects credentials with the BE flag
|
|
39
|
+
* set during enrollment. Existing enrollments are checked at assertion time —
|
|
40
|
+
* if the authenticator data shows BE=1 but `requireSingleDevice` was set at
|
|
41
|
+
* enrollment, the assertion throws `WebAuthnMultiDeviceError`.
|
|
42
|
+
*
|
|
43
|
+
* Enrollment flow
|
|
44
|
+
* ───────────────
|
|
45
|
+
* 1. User is already authenticated (passphrase or existing session).
|
|
46
|
+
* 2. Call `enrollWebAuthn(keyring, options)`.
|
|
47
|
+
* 3. WebAuthn credential is created; PRF or rawId-derived key wraps the KEK.
|
|
48
|
+
* 4. Returns a `WebAuthnEnrollment` — persist this to the noy-db adapter
|
|
49
|
+
* via `saveEnrollment()`, or store it yourself in any encrypted collection.
|
|
50
|
+
*
|
|
51
|
+
* Unlock flow
|
|
52
|
+
* ───────────
|
|
53
|
+
* 1. Load the `WebAuthnEnrollment` via `loadEnrollment()`.
|
|
54
|
+
* 2. Call `unlockWebAuthn(enrollment, keyring)` — triggers the WebAuthn
|
|
55
|
+
* assertion prompt.
|
|
56
|
+
* 3. On success, returns the unwrapped `CryptoKey` (the KEK) — use it to
|
|
57
|
+
* re-hydrate the session via `createSession()`.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Thrown when the WebAuthn API is not available in the current environment.
|
|
62
|
+
*
|
|
63
|
+
* Check `isWebAuthnAvailable()` before calling `enrollWebAuthn()` or
|
|
64
|
+
* `unlockWebAuthn()` and show a fallback UI (passphrase entry) if this
|
|
65
|
+
* returns false. Common scenarios: Node.js environments, older browsers,
|
|
66
|
+
* non-HTTPS origins (WebAuthn requires a Secure Context).
|
|
67
|
+
*/
|
|
68
|
+
declare class WebAuthnNotAvailableError extends Error {
|
|
69
|
+
readonly code = "WEBAUTHN_NOT_AVAILABLE";
|
|
70
|
+
constructor();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Thrown when the user dismisses the WebAuthn prompt without completing it.
|
|
74
|
+
*
|
|
75
|
+
* The `op` field distinguishes enrollment cancellation (user chose not to
|
|
76
|
+
* enroll a hardware key) from assertion cancellation (user dismissed the
|
|
77
|
+
* unlock prompt). Treat this as a user-initiated action, not an error — show
|
|
78
|
+
* a "use passphrase instead" option rather than an error message.
|
|
79
|
+
*/
|
|
80
|
+
declare class WebAuthnCancelledError extends Error {
|
|
81
|
+
readonly code = "WEBAUTHN_CANCELLED";
|
|
82
|
+
constructor(op: 'enrollment' | 'assertion');
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Thrown when the authenticator has the backup-eligible (BE) flag set but
|
|
86
|
+
* the vault requires a single-device credential (`requireSingleDevice: true`).
|
|
87
|
+
*
|
|
88
|
+
* A BE credential is synced across devices (e.g. iCloud Keychain, Google
|
|
89
|
+
* Password Manager), which violates the single-device security model. The
|
|
90
|
+
* user must enroll a hardware security key (YubiKey, Titan, SoloKey) instead.
|
|
91
|
+
*/
|
|
92
|
+
declare class WebAuthnMultiDeviceError extends Error {
|
|
93
|
+
readonly code = "WEBAUTHN_MULTI_DEVICE";
|
|
94
|
+
constructor();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Thrown (as a non-fatal warning, caught internally) when the PRF extension
|
|
98
|
+
* is not supported by the authenticator.
|
|
99
|
+
*
|
|
100
|
+
* NOYDB prefers PRF for key derivation because it produces a
|
|
101
|
+
* credential-bound output that is deterministic and not extractable from
|
|
102
|
+
* the authenticator. When PRF is unavailable, enrollment falls back to
|
|
103
|
+
* HKDF over the credential's `rawId` — weaker binding, but still functional.
|
|
104
|
+
* This error is caught at enrollment time; callers only see it if they
|
|
105
|
+
* explicitly opt into strict PRF-only mode.
|
|
106
|
+
*/
|
|
107
|
+
declare class WebAuthnPRFUnavailableError extends Error {
|
|
108
|
+
readonly code = "WEBAUTHN_PRF_UNAVAILABLE";
|
|
109
|
+
constructor();
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A persisted WebAuthn enrollment record. Store this in a noy-db
|
|
113
|
+
* collection (encrypted like any other record) or return it from
|
|
114
|
+
* `saveEnrollment()` / `loadEnrollment()` helpers.
|
|
115
|
+
*/
|
|
116
|
+
interface WebAuthnEnrollment {
|
|
117
|
+
/** Enrollment format version. */
|
|
118
|
+
readonly _noydb_webauthn: 1;
|
|
119
|
+
/** The vault this enrollment was created for. */
|
|
120
|
+
readonly vault: string;
|
|
121
|
+
/** The user ID this enrollment belongs to. */
|
|
122
|
+
readonly userId: string;
|
|
123
|
+
/** WebAuthn credential ID (base64). Use for allowCredentials in assertions. */
|
|
124
|
+
readonly credentialId: string;
|
|
125
|
+
/** Whether PRF was used for key derivation (vs rawId HKDF fallback). */
|
|
126
|
+
readonly prfUsed: boolean;
|
|
127
|
+
/** Whether the BE (backup-eligibility) flag was present at enrollment time. */
|
|
128
|
+
readonly beFlag: boolean;
|
|
129
|
+
/** Whether single-device was required at enrollment time. */
|
|
130
|
+
readonly requireSingleDevice: boolean;
|
|
131
|
+
/** The wrapped KEK: encrypt(exportedDekMap, derivedKey). Base64. */
|
|
132
|
+
readonly wrappedPayload: string;
|
|
133
|
+
/** IV used for the wrapping. Base64. */
|
|
134
|
+
readonly wrapIv: string;
|
|
135
|
+
/** ISO timestamp of enrollment. */
|
|
136
|
+
readonly enrolledAt: string;
|
|
137
|
+
}
|
|
138
|
+
/** Options for `enrollWebAuthn()`. */
|
|
139
|
+
interface WebAuthnEnrollOptions {
|
|
140
|
+
/**
|
|
141
|
+
* Relying party ID and name for the WebAuthn credential.
|
|
142
|
+
* Defaults to `{ id: window.location.hostname, name: 'NOYDB' }`.
|
|
143
|
+
*/
|
|
144
|
+
rp?: {
|
|
145
|
+
id?: string;
|
|
146
|
+
name: string;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* If `true`, refuse to enroll credentials with the BE flag set
|
|
150
|
+
* (multi-device / syncable passkeys). Defaults to `false`.
|
|
151
|
+
*
|
|
152
|
+
* Set to `true` for high-security deployments where the credential
|
|
153
|
+
* must be bound to a single physical device (YubiKey, Titan, etc.).
|
|
154
|
+
*/
|
|
155
|
+
requireSingleDevice?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* WebAuthn timeout in milliseconds. Default: 60_000.
|
|
158
|
+
*/
|
|
159
|
+
timeout?: number;
|
|
160
|
+
/**
|
|
161
|
+
* If `true`, prefer a cross-platform authenticator (roaming security key).
|
|
162
|
+
* If `false`, prefer a platform authenticator (Touch ID, Face ID).
|
|
163
|
+
* If undefined, let the browser choose.
|
|
164
|
+
*/
|
|
165
|
+
preferCrossPlatform?: boolean;
|
|
166
|
+
}
|
|
167
|
+
/** Options for `unlockWebAuthn()`. */
|
|
168
|
+
interface WebAuthnUnlockOptions {
|
|
169
|
+
/** WebAuthn timeout in milliseconds. Default: 60_000. */
|
|
170
|
+
timeout?: number;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Returns `true` if WebAuthn is available and can be used for enrollment or unlock.
|
|
174
|
+
*
|
|
175
|
+
* Checks for `navigator.credentials`, `window.PublicKeyCredential`, and a
|
|
176
|
+
* Secure Context (`window.isSecureContext`). Call this before rendering the
|
|
177
|
+
* "Register hardware key" button to avoid showing options that will fail.
|
|
178
|
+
*/
|
|
179
|
+
declare function isWebAuthnAvailable(): boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Enroll a WebAuthn credential for the given keyring.
|
|
182
|
+
*
|
|
183
|
+
* The caller must already have an unlocked keyring (from passphrase auth or
|
|
184
|
+
* an existing session). The WebAuthn credential creation prompt is triggered
|
|
185
|
+
* by this call.
|
|
186
|
+
*
|
|
187
|
+
* Returns a `WebAuthnEnrollment` that should be persisted — typically via
|
|
188
|
+
* `saveEnrollment()` into a noy-db collection.
|
|
189
|
+
*
|
|
190
|
+
* @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.
|
|
191
|
+
* @throws `WebAuthnCancelledError` if the user cancels the credential creation.
|
|
192
|
+
* @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` is true and the
|
|
193
|
+
* authenticator returned a credential with the BE flag set.
|
|
194
|
+
*/
|
|
195
|
+
declare function enrollWebAuthn(keyring: UnlockedKeyring, vault: string, options?: WebAuthnEnrollOptions): Promise<WebAuthnEnrollment>;
|
|
196
|
+
/**
|
|
197
|
+
* Unlock a vault using a previously enrolled WebAuthn credential.
|
|
198
|
+
*
|
|
199
|
+
* Triggers the WebAuthn assertion prompt. On success, decrypts the keyring
|
|
200
|
+
* payload from the enrollment record and returns an `UnlockedKeyring`.
|
|
201
|
+
*
|
|
202
|
+
* The returned keyring has the same DEKs as at enrollment time. If DEKs
|
|
203
|
+
* have been rotated since enrollment, this will return stale DEKs — the
|
|
204
|
+
* caller should detect decryption failures and prompt for re-enrollment.
|
|
205
|
+
*
|
|
206
|
+
* @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.
|
|
207
|
+
* @throws `WebAuthnCancelledError` if the user cancels the assertion.
|
|
208
|
+
* @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` was set at
|
|
209
|
+
* enrollment and the authenticator data now shows BE=1.
|
|
210
|
+
* @throws `ValidationError` if decryption of the keyring payload fails.
|
|
211
|
+
*/
|
|
212
|
+
declare function unlockWebAuthn(enrollment: WebAuthnEnrollment, options?: WebAuthnUnlockOptions): Promise<UnlockedKeyring>;
|
|
213
|
+
/**
|
|
214
|
+
* Check whether a `WebAuthnEnrollment` record looks well-formed.
|
|
215
|
+
* Does not perform any cryptographic verification.
|
|
216
|
+
*/
|
|
217
|
+
declare function isValidEnrollment(value: unknown): value is WebAuthnEnrollment;
|
|
218
|
+
|
|
219
|
+
export { WebAuthnCancelledError, type WebAuthnEnrollOptions, type WebAuthnEnrollment, WebAuthnMultiDeviceError, WebAuthnNotAvailableError, WebAuthnPRFUnavailableError, type WebAuthnUnlockOptions, enrollWebAuthn, isValidEnrollment, isWebAuthnAvailable, unlockWebAuthn };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { bufferToBase64, base64ToBuffer } from "@noy-db/hub";
|
|
3
|
+
import { ValidationError } from "@noy-db/hub";
|
|
4
|
+
import { ValidationError as ValidationError2 } from "@noy-db/hub";
|
|
5
|
+
var WebAuthnNotAvailableError = class extends Error {
|
|
6
|
+
code = "WEBAUTHN_NOT_AVAILABLE";
|
|
7
|
+
constructor() {
|
|
8
|
+
super("WebAuthn is not available in this environment. A browser with navigator.credentials support is required.");
|
|
9
|
+
this.name = "WebAuthnNotAvailableError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var WebAuthnCancelledError = class extends Error {
|
|
13
|
+
code = "WEBAUTHN_CANCELLED";
|
|
14
|
+
constructor(op) {
|
|
15
|
+
super(`WebAuthn ${op} was cancelled by the user.`);
|
|
16
|
+
this.name = "WebAuthnCancelledError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var WebAuthnMultiDeviceError = class extends Error {
|
|
20
|
+
code = "WEBAUTHN_MULTI_DEVICE";
|
|
21
|
+
constructor() {
|
|
22
|
+
super(
|
|
23
|
+
"This credential is backup-eligible (BE flag set) and may be synced across devices. The vault requires a single-device credential (requireSingleDevice: true). Please use a hardware security key (YubiKey, Titan, SoloKey) or a platform authenticator that does not sync credentials across devices."
|
|
24
|
+
);
|
|
25
|
+
this.name = "WebAuthnMultiDeviceError";
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var WebAuthnPRFUnavailableError = class extends Error {
|
|
29
|
+
code = "WEBAUTHN_PRF_UNAVAILABLE";
|
|
30
|
+
constructor() {
|
|
31
|
+
super(
|
|
32
|
+
"The PRF extension is not available on this authenticator. Enrollment will fall back to rawId-based key derivation. This provides weaker binding to the specific authenticator."
|
|
33
|
+
);
|
|
34
|
+
this.name = "WebAuthnPRFUnavailableError";
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
function isWebAuthnAvailable() {
|
|
38
|
+
return typeof window !== "undefined" && typeof window.PublicKeyCredential !== "undefined" && typeof navigator !== "undefined" && typeof navigator.credentials !== "undefined";
|
|
39
|
+
}
|
|
40
|
+
var PRF_SALT = new TextEncoder().encode("noydb-webauthn-kek-derive");
|
|
41
|
+
async function deriveKeyFromPRF(prfOutput) {
|
|
42
|
+
const keyMaterial = await globalThis.crypto.subtle.importKey(
|
|
43
|
+
"raw",
|
|
44
|
+
prfOutput,
|
|
45
|
+
"HKDF",
|
|
46
|
+
false,
|
|
47
|
+
["deriveKey"]
|
|
48
|
+
);
|
|
49
|
+
return globalThis.crypto.subtle.deriveKey(
|
|
50
|
+
{
|
|
51
|
+
name: "HKDF",
|
|
52
|
+
hash: "SHA-256",
|
|
53
|
+
salt: PRF_SALT,
|
|
54
|
+
info: new TextEncoder().encode("noydb-kek-wrap-v1")
|
|
55
|
+
},
|
|
56
|
+
keyMaterial,
|
|
57
|
+
{ name: "AES-GCM", length: 256 },
|
|
58
|
+
false,
|
|
59
|
+
["encrypt", "decrypt"]
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
async function deriveKeyFromRawId(rawId) {
|
|
63
|
+
const keyMaterial = await globalThis.crypto.subtle.importKey(
|
|
64
|
+
"raw",
|
|
65
|
+
rawId,
|
|
66
|
+
"HKDF",
|
|
67
|
+
false,
|
|
68
|
+
["deriveKey"]
|
|
69
|
+
);
|
|
70
|
+
return globalThis.crypto.subtle.deriveKey(
|
|
71
|
+
{
|
|
72
|
+
name: "HKDF",
|
|
73
|
+
hash: "SHA-256",
|
|
74
|
+
salt: new TextEncoder().encode("noydb-webauthn-rawid-fallback"),
|
|
75
|
+
info: new TextEncoder().encode("noydb-kek-wrap-v1")
|
|
76
|
+
},
|
|
77
|
+
keyMaterial,
|
|
78
|
+
{ name: "AES-GCM", length: 256 },
|
|
79
|
+
false,
|
|
80
|
+
["encrypt", "decrypt"]
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
function extractBEFlag(authData) {
|
|
84
|
+
const bytes = new Uint8Array(authData);
|
|
85
|
+
if (bytes.length < 33) return false;
|
|
86
|
+
const flagsByte = bytes[32];
|
|
87
|
+
return (flagsByte & 8) !== 0;
|
|
88
|
+
}
|
|
89
|
+
async function wrapKeyringSummary(keyring, wrappingKey) {
|
|
90
|
+
const dekMap = {};
|
|
91
|
+
for (const [collName, dek] of keyring.deks) {
|
|
92
|
+
const raw = await globalThis.crypto.subtle.exportKey("raw", dek);
|
|
93
|
+
dekMap[collName] = bufferToBase64(raw);
|
|
94
|
+
}
|
|
95
|
+
const payload = JSON.stringify({
|
|
96
|
+
userId: keyring.userId,
|
|
97
|
+
displayName: keyring.displayName,
|
|
98
|
+
role: keyring.role,
|
|
99
|
+
permissions: keyring.permissions,
|
|
100
|
+
deks: dekMap,
|
|
101
|
+
salt: bufferToBase64(keyring.salt)
|
|
102
|
+
});
|
|
103
|
+
const iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
|
|
104
|
+
const encrypted = await globalThis.crypto.subtle.encrypt(
|
|
105
|
+
{ name: "AES-GCM", iv },
|
|
106
|
+
wrappingKey,
|
|
107
|
+
new TextEncoder().encode(payload)
|
|
108
|
+
);
|
|
109
|
+
return { wrappedPayload: bufferToBase64(encrypted), wrapIv: bufferToBase64(iv) };
|
|
110
|
+
}
|
|
111
|
+
async function unwrapKeyringSummary(enrollment, wrappingKey) {
|
|
112
|
+
const iv = base64ToBuffer(enrollment.wrapIv);
|
|
113
|
+
const ciphertext = base64ToBuffer(enrollment.wrappedPayload);
|
|
114
|
+
let plaintext;
|
|
115
|
+
try {
|
|
116
|
+
plaintext = await globalThis.crypto.subtle.decrypt(
|
|
117
|
+
{ name: "AES-GCM", iv },
|
|
118
|
+
wrappingKey,
|
|
119
|
+
ciphertext
|
|
120
|
+
);
|
|
121
|
+
} catch {
|
|
122
|
+
throw new ValidationError("WebAuthn decryption failed \u2014 the authenticator may have changed or the enrollment may be corrupt.");
|
|
123
|
+
}
|
|
124
|
+
const parsed = JSON.parse(new TextDecoder().decode(plaintext));
|
|
125
|
+
const deks = /* @__PURE__ */ new Map();
|
|
126
|
+
for (const [collName, rawBase64] of Object.entries(parsed.deks)) {
|
|
127
|
+
const dek = await globalThis.crypto.subtle.importKey(
|
|
128
|
+
"raw",
|
|
129
|
+
base64ToBuffer(rawBase64),
|
|
130
|
+
{ name: "AES-GCM", length: 256 },
|
|
131
|
+
true,
|
|
132
|
+
["encrypt", "decrypt"]
|
|
133
|
+
);
|
|
134
|
+
deks.set(collName, dek);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
userId: parsed.userId,
|
|
138
|
+
displayName: parsed.displayName,
|
|
139
|
+
role: parsed.role,
|
|
140
|
+
permissions: parsed.permissions,
|
|
141
|
+
deks,
|
|
142
|
+
kek: null,
|
|
143
|
+
salt: base64ToBuffer(parsed.salt)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
async function enrollWebAuthn(keyring, vault, options = {}) {
|
|
147
|
+
if (!isWebAuthnAvailable()) {
|
|
148
|
+
throw new WebAuthnNotAvailableError();
|
|
149
|
+
}
|
|
150
|
+
const rpId = options.rp?.id ?? (typeof window !== "undefined" ? window.location.hostname : "localhost");
|
|
151
|
+
const rpName = options.rp?.name ?? "NOYDB";
|
|
152
|
+
const timeout = options.timeout ?? 6e4;
|
|
153
|
+
const challenge = globalThis.crypto.getRandomValues(new Uint8Array(32));
|
|
154
|
+
const userIdBytes = new TextEncoder().encode(keyring.userId);
|
|
155
|
+
const authenticatorSelection = {
|
|
156
|
+
userVerification: "required",
|
|
157
|
+
residentKey: "preferred"
|
|
158
|
+
};
|
|
159
|
+
if (options.preferCrossPlatform === true) {
|
|
160
|
+
authenticatorSelection.authenticatorAttachment = "cross-platform";
|
|
161
|
+
} else if (options.preferCrossPlatform === false) {
|
|
162
|
+
authenticatorSelection.authenticatorAttachment = "platform";
|
|
163
|
+
}
|
|
164
|
+
const extensionsInput = {
|
|
165
|
+
prf: { eval: { first: PRF_SALT } }
|
|
166
|
+
};
|
|
167
|
+
const credential = await navigator.credentials.create({
|
|
168
|
+
publicKey: {
|
|
169
|
+
challenge,
|
|
170
|
+
rp: { id: rpId, name: rpName },
|
|
171
|
+
user: {
|
|
172
|
+
id: userIdBytes,
|
|
173
|
+
name: keyring.userId,
|
|
174
|
+
displayName: keyring.displayName
|
|
175
|
+
},
|
|
176
|
+
pubKeyCredParams: [
|
|
177
|
+
{ type: "public-key", alg: -7 },
|
|
178
|
+
// ES256
|
|
179
|
+
{ type: "public-key", alg: -257 },
|
|
180
|
+
// RS256
|
|
181
|
+
{ type: "public-key", alg: -8 }
|
|
182
|
+
// EdDSA
|
|
183
|
+
],
|
|
184
|
+
authenticatorSelection,
|
|
185
|
+
extensions: extensionsInput,
|
|
186
|
+
timeout
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (!credential) {
|
|
190
|
+
throw new WebAuthnCancelledError("enrollment");
|
|
191
|
+
}
|
|
192
|
+
const authData = credential.response.getAuthenticatorData();
|
|
193
|
+
const beFlag = extractBEFlag(authData);
|
|
194
|
+
if (options.requireSingleDevice && beFlag) {
|
|
195
|
+
throw new WebAuthnMultiDeviceError();
|
|
196
|
+
}
|
|
197
|
+
const extensions = credential.getClientExtensionResults();
|
|
198
|
+
const prfOutput = extensions.prf?.results?.first;
|
|
199
|
+
const prfUsed = !!prfOutput;
|
|
200
|
+
const wrappingKey = prfOutput ? await deriveKeyFromPRF(prfOutput) : await deriveKeyFromRawId(credential.rawId);
|
|
201
|
+
const { wrappedPayload, wrapIv } = await wrapKeyringSummary(keyring, wrappingKey);
|
|
202
|
+
return {
|
|
203
|
+
_noydb_webauthn: 1,
|
|
204
|
+
vault,
|
|
205
|
+
userId: keyring.userId,
|
|
206
|
+
credentialId: bufferToBase64(credential.rawId),
|
|
207
|
+
prfUsed,
|
|
208
|
+
beFlag,
|
|
209
|
+
requireSingleDevice: options.requireSingleDevice ?? false,
|
|
210
|
+
wrappedPayload,
|
|
211
|
+
wrapIv,
|
|
212
|
+
enrolledAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
async function unlockWebAuthn(enrollment, options = {}) {
|
|
216
|
+
if (!isWebAuthnAvailable()) {
|
|
217
|
+
throw new WebAuthnNotAvailableError();
|
|
218
|
+
}
|
|
219
|
+
const timeout = options.timeout ?? 6e4;
|
|
220
|
+
const credentialId = base64ToBuffer(enrollment.credentialId);
|
|
221
|
+
const extensionsInput = enrollment.prfUsed ? { prf: { eval: { first: PRF_SALT } } } : {};
|
|
222
|
+
const assertion = await navigator.credentials.get({
|
|
223
|
+
publicKey: {
|
|
224
|
+
challenge: globalThis.crypto.getRandomValues(new Uint8Array(32)),
|
|
225
|
+
allowCredentials: [{ type: "public-key", id: credentialId }],
|
|
226
|
+
userVerification: "required",
|
|
227
|
+
extensions: extensionsInput,
|
|
228
|
+
timeout
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
if (!assertion) {
|
|
232
|
+
throw new WebAuthnCancelledError("assertion");
|
|
233
|
+
}
|
|
234
|
+
const authData = assertion.response.authenticatorData;
|
|
235
|
+
const beFlag = extractBEFlag(authData);
|
|
236
|
+
if (enrollment.requireSingleDevice && beFlag) {
|
|
237
|
+
throw new WebAuthnMultiDeviceError();
|
|
238
|
+
}
|
|
239
|
+
let wrappingKey;
|
|
240
|
+
if (enrollment.prfUsed) {
|
|
241
|
+
const extensions = assertion.getClientExtensionResults();
|
|
242
|
+
const prfOutput = extensions.prf?.results?.first;
|
|
243
|
+
if (!prfOutput) {
|
|
244
|
+
throw new ValidationError(
|
|
245
|
+
"PRF extension output not available at assertion time. The authenticator may not support PRF. Re-enroll without PRF support."
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
wrappingKey = await deriveKeyFromPRF(prfOutput);
|
|
249
|
+
} else {
|
|
250
|
+
wrappingKey = await deriveKeyFromRawId(assertion.rawId);
|
|
251
|
+
}
|
|
252
|
+
return unwrapKeyringSummary(enrollment, wrappingKey);
|
|
253
|
+
}
|
|
254
|
+
function isValidEnrollment(value) {
|
|
255
|
+
if (!value || typeof value !== "object") return false;
|
|
256
|
+
const e = value;
|
|
257
|
+
return e._noydb_webauthn === 1 && typeof e.vault === "string" && typeof e.userId === "string" && typeof e.credentialId === "string" && typeof e.wrappedPayload === "string" && typeof e.wrapIv === "string";
|
|
258
|
+
}
|
|
259
|
+
export {
|
|
260
|
+
ValidationError2 as ValidationError,
|
|
261
|
+
WebAuthnCancelledError,
|
|
262
|
+
WebAuthnMultiDeviceError,
|
|
263
|
+
WebAuthnNotAvailableError,
|
|
264
|
+
WebAuthnPRFUnavailableError,
|
|
265
|
+
enrollWebAuthn,
|
|
266
|
+
isValidEnrollment,
|
|
267
|
+
isWebAuthnAvailable,
|
|
268
|
+
unlockWebAuthn
|
|
269
|
+
};
|
|
270
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @noy-db/on-webauthn —\n *\n * Hardware-key keyring for noy-db using the WebAuthn API.\n *\n * Covers every form factor:\n * - Platform authenticators: Touch ID, Face ID, Windows Hello, Android biometric\n * - Roaming authenticators: YubiKey (5C NFC, Bio), SoloKey, Titan, any FIDO2 key\n * - Passkey-capable platform authenticators: iCloud Keychain, Google Password Manager\n *\n * Key derivation model\n * ────────────────────\n * This package uses the **PRF (Pseudo-Random Function) extension** when\n * available to derive a deterministic wrapping key from the WebAuthn\n * credential. The PRF output is consistent across assertions on the same\n * device/credential, enabling unlock-without-passphrase while keeping the\n * derived key bound to the physical authenticator.\n *\n * When PRF is not supported by the authenticator (common on older hardware),\n * the package falls back to HKDF-SHA256 over the credential's `rawId` —\n * the same approach as the pre-existing `@noy-db/core` biometric module.\n *\n * The derived key is NEVER persisted. It exists only in memory during the\n * unlock operation. What IS persisted (in the noy-db adapter, not in browser\n * storage) is the wrapped KEK: `encrypt(KEK, derivedKey)`.\n *\n * BE-flag guards\n * ──────────────\n * The backup-eligibility (BE) flag in a WebAuthn authenticator data signals\n * that the credential is (or can be) synced across devices — e.g. stored in\n * iCloud Keychain. For single-device security policies (air-gapped USB sticks,\n * high-security terminals), this is a threat: the credential is available on\n * any device where the user's iCloud account is signed in.\n *\n * The `requireSingleDevice: true` option rejects credentials with the BE flag\n * set during enrollment. Existing enrollments are checked at assertion time —\n * if the authenticator data shows BE=1 but `requireSingleDevice` was set at\n * enrollment, the assertion throws `WebAuthnMultiDeviceError`.\n *\n * Enrollment flow\n * ───────────────\n * 1. User is already authenticated (passphrase or existing session).\n * 2. Call `enrollWebAuthn(keyring, options)`.\n * 3. WebAuthn credential is created; PRF or rawId-derived key wraps the KEK.\n * 4. Returns a `WebAuthnEnrollment` — persist this to the noy-db adapter\n * via `saveEnrollment()`, or store it yourself in any encrypted collection.\n *\n * Unlock flow\n * ───────────\n * 1. Load the `WebAuthnEnrollment` via `loadEnrollment()`.\n * 2. Call `unlockWebAuthn(enrollment, keyring)` — triggers the WebAuthn\n * assertion prompt.\n * 3. On success, returns the unwrapped `CryptoKey` (the KEK) — use it to\n * re-hydrate the session via `createSession()`.\n */\n\nimport { bufferToBase64, base64ToBuffer } from '@noy-db/hub'\nimport { ValidationError } from '@noy-db/hub'\nimport type { UnlockedKeyring, Role } from '@noy-db/hub'\n\n// Re-export from core for convenience\nexport { ValidationError } from '@noy-db/hub'\n\n// ─── Error types ──────────────────────────────────────────────────────\n\n/**\n * Thrown when the WebAuthn API is not available in the current environment.\n *\n * Check `isWebAuthnAvailable()` before calling `enrollWebAuthn()` or\n * `unlockWebAuthn()` and show a fallback UI (passphrase entry) if this\n * returns false. Common scenarios: Node.js environments, older browsers,\n * non-HTTPS origins (WebAuthn requires a Secure Context).\n */\nexport class WebAuthnNotAvailableError extends Error {\n readonly code = 'WEBAUTHN_NOT_AVAILABLE'\n constructor() {\n super('WebAuthn is not available in this environment. A browser with navigator.credentials support is required.')\n this.name = 'WebAuthnNotAvailableError'\n }\n}\n\n/**\n * Thrown when the user dismisses the WebAuthn prompt without completing it.\n *\n * The `op` field distinguishes enrollment cancellation (user chose not to\n * enroll a hardware key) from assertion cancellation (user dismissed the\n * unlock prompt). Treat this as a user-initiated action, not an error — show\n * a \"use passphrase instead\" option rather than an error message.\n */\nexport class WebAuthnCancelledError extends Error {\n readonly code = 'WEBAUTHN_CANCELLED'\n constructor(op: 'enrollment' | 'assertion') {\n super(`WebAuthn ${op} was cancelled by the user.`)\n this.name = 'WebAuthnCancelledError'\n }\n}\n\n/**\n * Thrown when the authenticator has the backup-eligible (BE) flag set but\n * the vault requires a single-device credential (`requireSingleDevice: true`).\n *\n * A BE credential is synced across devices (e.g. iCloud Keychain, Google\n * Password Manager), which violates the single-device security model. The\n * user must enroll a hardware security key (YubiKey, Titan, SoloKey) instead.\n */\nexport class WebAuthnMultiDeviceError extends Error {\n readonly code = 'WEBAUTHN_MULTI_DEVICE'\n constructor() {\n super(\n 'This credential is backup-eligible (BE flag set) and may be synced across devices. ' +\n 'The vault requires a single-device credential (requireSingleDevice: true). ' +\n 'Please use a hardware security key (YubiKey, Titan, SoloKey) or a platform ' +\n 'authenticator that does not sync credentials across devices.',\n )\n this.name = 'WebAuthnMultiDeviceError'\n }\n}\n\n/**\n * Thrown (as a non-fatal warning, caught internally) when the PRF extension\n * is not supported by the authenticator.\n *\n * NOYDB prefers PRF for key derivation because it produces a\n * credential-bound output that is deterministic and not extractable from\n * the authenticator. When PRF is unavailable, enrollment falls back to\n * HKDF over the credential's `rawId` — weaker binding, but still functional.\n * This error is caught at enrollment time; callers only see it if they\n * explicitly opt into strict PRF-only mode.\n */\nexport class WebAuthnPRFUnavailableError extends Error {\n readonly code = 'WEBAUTHN_PRF_UNAVAILABLE'\n constructor() {\n super(\n 'The PRF extension is not available on this authenticator. ' +\n 'Enrollment will fall back to rawId-based key derivation. ' +\n 'This provides weaker binding to the specific authenticator.',\n )\n this.name = 'WebAuthnPRFUnavailableError'\n }\n}\n\n// ─── Types ────────────────────────────────────────────────────────────\n\n/**\n * A persisted WebAuthn enrollment record. Store this in a noy-db\n * collection (encrypted like any other record) or return it from\n * `saveEnrollment()` / `loadEnrollment()` helpers.\n */\nexport interface WebAuthnEnrollment {\n /** Enrollment format version. */\n readonly _noydb_webauthn: 1\n /** The vault this enrollment was created for. */\n readonly vault: string\n /** The user ID this enrollment belongs to. */\n readonly userId: string\n /** WebAuthn credential ID (base64). Use for allowCredentials in assertions. */\n readonly credentialId: string\n /** Whether PRF was used for key derivation (vs rawId HKDF fallback). */\n readonly prfUsed: boolean\n /** Whether the BE (backup-eligibility) flag was present at enrollment time. */\n readonly beFlag: boolean\n /** Whether single-device was required at enrollment time. */\n readonly requireSingleDevice: boolean\n /** The wrapped KEK: encrypt(exportedDekMap, derivedKey). Base64. */\n readonly wrappedPayload: string\n /** IV used for the wrapping. Base64. */\n readonly wrapIv: string\n /** ISO timestamp of enrollment. */\n readonly enrolledAt: string\n}\n\n/** Options for `enrollWebAuthn()`. */\nexport interface WebAuthnEnrollOptions {\n /**\n * Relying party ID and name for the WebAuthn credential.\n * Defaults to `{ id: window.location.hostname, name: 'NOYDB' }`.\n */\n rp?: { id?: string; name: string }\n /**\n * If `true`, refuse to enroll credentials with the BE flag set\n * (multi-device / syncable passkeys). Defaults to `false`.\n *\n * Set to `true` for high-security deployments where the credential\n * must be bound to a single physical device (YubiKey, Titan, etc.).\n */\n requireSingleDevice?: boolean\n /**\n * WebAuthn timeout in milliseconds. Default: 60_000.\n */\n timeout?: number\n /**\n * If `true`, prefer a cross-platform authenticator (roaming security key).\n * If `false`, prefer a platform authenticator (Touch ID, Face ID).\n * If undefined, let the browser choose.\n */\n preferCrossPlatform?: boolean\n}\n\n/** Options for `unlockWebAuthn()`. */\nexport interface WebAuthnUnlockOptions {\n /** WebAuthn timeout in milliseconds. Default: 60_000. */\n timeout?: number\n}\n\n// ─── Environment check ─────────────────────────────────────────────────\n\n/**\n * Returns `true` if WebAuthn is available and can be used for enrollment or unlock.\n *\n * Checks for `navigator.credentials`, `window.PublicKeyCredential`, and a\n * Secure Context (`window.isSecureContext`). Call this before rendering the\n * \"Register hardware key\" button to avoid showing options that will fail.\n */\nexport function isWebAuthnAvailable(): boolean {\n return (\n typeof window !== 'undefined' &&\n typeof window.PublicKeyCredential !== 'undefined' &&\n typeof navigator !== 'undefined' &&\n typeof navigator.credentials !== 'undefined'\n )\n}\n\n// ─── PRF salt ─────────────────────────────────────────────────────────\n\nconst PRF_SALT = new TextEncoder().encode('noydb-webauthn-kek-derive')\n\n// ─── Key derivation helpers ────────────────────────────────────────────\n\n/**\n * Derive a wrapping key from PRF output.\n * PRF output is 32 bytes of authenticator-bound pseudo-random data.\n */\nasync function deriveKeyFromPRF(prfOutput: ArrayBuffer): Promise<CryptoKey> {\n const keyMaterial = await globalThis.crypto.subtle.importKey(\n 'raw',\n prfOutput,\n 'HKDF',\n false,\n ['deriveKey'],\n )\n return globalThis.crypto.subtle.deriveKey(\n {\n name: 'HKDF',\n hash: 'SHA-256',\n salt: PRF_SALT,\n info: new TextEncoder().encode('noydb-kek-wrap-v1'),\n },\n keyMaterial,\n { name: 'AES-GCM', length: 256 },\n false,\n ['encrypt', 'decrypt'],\n )\n}\n\n/**\n * Derive a wrapping key from the credential's rawId (fallback when PRF unavailable).\n * Weaker than PRF (rawId may be observable to the server) but universally supported.\n */\nasync function deriveKeyFromRawId(rawId: ArrayBuffer): Promise<CryptoKey> {\n const keyMaterial = await globalThis.crypto.subtle.importKey(\n 'raw',\n rawId,\n 'HKDF',\n false,\n ['deriveKey'],\n )\n return globalThis.crypto.subtle.deriveKey(\n {\n name: 'HKDF',\n hash: 'SHA-256',\n salt: new TextEncoder().encode('noydb-webauthn-rawid-fallback'),\n info: new TextEncoder().encode('noydb-kek-wrap-v1'),\n },\n keyMaterial,\n { name: 'AES-GCM', length: 256 },\n false,\n ['encrypt', 'decrypt'],\n )\n}\n\n// ─── BE flag extraction ────────────────────────────────────────────────\n\n/**\n * Extract the BE (backup-eligibility) flag from WebAuthn authenticator data.\n * Authenticator data byte layout (CTAP2 spec):\n * bytes 0-31: rpIdHash\n * byte 32: flags byte\n * bytes 33-36: signCount\n * ...\n *\n * Flags byte bit layout (bit 0 = LSB):\n * bit 0 (UP): user presence\n * bit 2 (UV): user verification\n * bit 3 (BE): backup eligibility\n * bit 4 (BS): backup state\n * bit 6 (AT): attested credential data present\n * bit 7 (ED): extension data present\n */\nfunction extractBEFlag(authData: ArrayBuffer): boolean {\n const bytes = new Uint8Array(authData)\n if (bytes.length < 33) return false\n const flagsByte = bytes[32]!\n return (flagsByte & 0b00001000) !== 0 // bit 3\n}\n\n// ─── Payload wrap/unwrap ───────────────────────────────────────────────\n\n/**\n * Serialize and encrypt the DEK map from `keyring` using `wrappingKey`.\n * The wrapped payload is what gets stored in the enrollment record.\n */\nasync function wrapKeyringSummary(\n keyring: UnlockedKeyring,\n wrappingKey: CryptoKey,\n): Promise<{ wrappedPayload: string; wrapIv: string }> {\n const dekMap: Record<string, string> = {}\n for (const [collName, dek] of keyring.deks) {\n const raw = await globalThis.crypto.subtle.exportKey('raw', dek)\n dekMap[collName] = bufferToBase64(raw)\n }\n\n const payload = JSON.stringify({\n userId: keyring.userId,\n displayName: keyring.displayName,\n role: keyring.role,\n permissions: keyring.permissions,\n deks: dekMap,\n salt: bufferToBase64(keyring.salt),\n })\n\n const iv = globalThis.crypto.getRandomValues(new Uint8Array(12))\n const encrypted = await globalThis.crypto.subtle.encrypt(\n { name: 'AES-GCM', iv },\n wrappingKey,\n new TextEncoder().encode(payload),\n )\n\n return { wrappedPayload: bufferToBase64(encrypted), wrapIv: bufferToBase64(iv) }\n}\n\n/**\n * Decrypt and deserialize the keyring payload using `wrappingKey`.\n */\nasync function unwrapKeyringSummary(\n enrollment: WebAuthnEnrollment,\n wrappingKey: CryptoKey,\n): Promise<UnlockedKeyring> {\n const iv = base64ToBuffer(enrollment.wrapIv)\n const ciphertext = base64ToBuffer(enrollment.wrappedPayload)\n\n let plaintext: ArrayBuffer\n try {\n plaintext = await globalThis.crypto.subtle.decrypt(\n { name: 'AES-GCM', iv },\n wrappingKey,\n ciphertext,\n )\n } catch {\n throw new ValidationError('WebAuthn decryption failed — the authenticator may have changed or the enrollment may be corrupt.')\n }\n\n const parsed = JSON.parse(new TextDecoder().decode(plaintext)) as {\n userId: string\n displayName: string\n role: Role\n permissions: Record<string, 'rw' | 'ro'>\n deks: Record<string, string>\n salt: string\n }\n\n const deks = new Map<string, CryptoKey>()\n for (const [collName, rawBase64] of Object.entries(parsed.deks)) {\n const dek = await globalThis.crypto.subtle.importKey(\n 'raw',\n base64ToBuffer(rawBase64),\n { name: 'AES-GCM', length: 256 },\n true,\n ['encrypt', 'decrypt'],\n )\n deks.set(collName, dek)\n }\n\n return {\n userId: parsed.userId,\n displayName: parsed.displayName,\n role: parsed.role,\n permissions: parsed.permissions,\n deks,\n kek: null as unknown as CryptoKey,\n salt: base64ToBuffer(parsed.salt),\n }\n}\n\n// ─── Public API ────────────────────────────────────────────────────────\n\n/**\n * Enroll a WebAuthn credential for the given keyring.\n *\n * The caller must already have an unlocked keyring (from passphrase auth or\n * an existing session). The WebAuthn credential creation prompt is triggered\n * by this call.\n *\n * Returns a `WebAuthnEnrollment` that should be persisted — typically via\n * `saveEnrollment()` into a noy-db collection.\n *\n * @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.\n * @throws `WebAuthnCancelledError` if the user cancels the credential creation.\n * @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` is true and the\n * authenticator returned a credential with the BE flag set.\n */\nexport async function enrollWebAuthn(\n keyring: UnlockedKeyring,\n vault: string,\n options: WebAuthnEnrollOptions = {},\n): Promise<WebAuthnEnrollment> {\n if (!isWebAuthnAvailable()) {\n throw new WebAuthnNotAvailableError()\n }\n\n const rpId = options.rp?.id ?? (typeof window !== 'undefined' ? window.location.hostname : 'localhost')\n const rpName = options.rp?.name ?? 'NOYDB'\n const timeout = options.timeout ?? 60_000\n\n const challenge = globalThis.crypto.getRandomValues(new Uint8Array(32))\n const userIdBytes = new TextEncoder().encode(keyring.userId)\n\n const authenticatorSelection: AuthenticatorSelectionCriteria = {\n userVerification: 'required',\n residentKey: 'preferred',\n }\n if (options.preferCrossPlatform === true) {\n authenticatorSelection.authenticatorAttachment = 'cross-platform'\n } else if (options.preferCrossPlatform === false) {\n authenticatorSelection.authenticatorAttachment = 'platform'\n }\n\n // Request PRF extension for deterministic key derivation\n const extensionsInput = {\n prf: { eval: { first: PRF_SALT } },\n } as AuthenticationExtensionsClientInputs\n\n const credential = await navigator.credentials.create({\n publicKey: {\n challenge,\n rp: { id: rpId, name: rpName },\n user: {\n id: userIdBytes,\n name: keyring.userId,\n displayName: keyring.displayName,\n },\n pubKeyCredParams: [\n { type: 'public-key', alg: -7 }, // ES256\n { type: 'public-key', alg: -257 }, // RS256\n { type: 'public-key', alg: -8 }, // EdDSA\n ],\n authenticatorSelection,\n extensions: extensionsInput,\n timeout,\n },\n }) as PublicKeyCredential | null\n\n if (!credential) {\n throw new WebAuthnCancelledError('enrollment')\n }\n\n const authData = (credential.response as AuthenticatorAttestationResponse).getAuthenticatorData()\n const beFlag = extractBEFlag(authData)\n\n if (options.requireSingleDevice && beFlag) {\n throw new WebAuthnMultiDeviceError()\n }\n\n // Try to get PRF output from extensions\n const extensions = credential.getClientExtensionResults() as {\n prf?: { results?: { first?: ArrayBuffer } }\n }\n const prfOutput = extensions.prf?.results?.first\n const prfUsed = !!prfOutput\n\n const wrappingKey = prfOutput\n ? await deriveKeyFromPRF(prfOutput)\n : await deriveKeyFromRawId(credential.rawId)\n\n const { wrappedPayload, wrapIv } = await wrapKeyringSummary(keyring, wrappingKey)\n\n return {\n _noydb_webauthn: 1,\n vault,\n userId: keyring.userId,\n credentialId: bufferToBase64(credential.rawId),\n prfUsed,\n beFlag,\n requireSingleDevice: options.requireSingleDevice ?? false,\n wrappedPayload,\n wrapIv,\n enrolledAt: new Date().toISOString(),\n }\n}\n\n/**\n * Unlock a vault using a previously enrolled WebAuthn credential.\n *\n * Triggers the WebAuthn assertion prompt. On success, decrypts the keyring\n * payload from the enrollment record and returns an `UnlockedKeyring`.\n *\n * The returned keyring has the same DEKs as at enrollment time. If DEKs\n * have been rotated since enrollment, this will return stale DEKs — the\n * caller should detect decryption failures and prompt for re-enrollment.\n *\n * @throws `WebAuthnNotAvailableError` if the environment doesn't support WebAuthn.\n * @throws `WebAuthnCancelledError` if the user cancels the assertion.\n * @throws `WebAuthnMultiDeviceError` if `requireSingleDevice` was set at\n * enrollment and the authenticator data now shows BE=1.\n * @throws `ValidationError` if decryption of the keyring payload fails.\n */\nexport async function unlockWebAuthn(\n enrollment: WebAuthnEnrollment,\n options: WebAuthnUnlockOptions = {},\n): Promise<UnlockedKeyring> {\n if (!isWebAuthnAvailable()) {\n throw new WebAuthnNotAvailableError()\n }\n\n const timeout = options.timeout ?? 60_000\n const credentialId = base64ToBuffer(enrollment.credentialId)\n\n const extensionsInput = (enrollment.prfUsed\n ? { prf: { eval: { first: PRF_SALT } } }\n : {}\n ) as AuthenticationExtensionsClientInputs\n\n const assertion = await navigator.credentials.get({\n publicKey: {\n challenge: globalThis.crypto.getRandomValues(new Uint8Array(32)),\n allowCredentials: [{ type: 'public-key', id: credentialId as BufferSource }],\n userVerification: 'required',\n extensions: extensionsInput,\n timeout,\n },\n }) as PublicKeyCredential | null\n\n if (!assertion) {\n throw new WebAuthnCancelledError('assertion')\n }\n\n // BE-flag guard at assertion time\n const authData = (assertion.response as AuthenticatorAssertionResponse).authenticatorData\n const beFlag = extractBEFlag(authData)\n if (enrollment.requireSingleDevice && beFlag) {\n throw new WebAuthnMultiDeviceError()\n }\n\n // Derive the wrapping key using the same method as enrollment\n let wrappingKey: CryptoKey\n if (enrollment.prfUsed) {\n const extensions = assertion.getClientExtensionResults() as {\n prf?: { results?: { first?: ArrayBuffer } }\n }\n const prfOutput = extensions.prf?.results?.first\n if (!prfOutput) {\n throw new ValidationError(\n 'PRF extension output not available at assertion time. ' +\n 'The authenticator may not support PRF. Re-enroll without PRF support.',\n )\n }\n wrappingKey = await deriveKeyFromPRF(prfOutput)\n } else {\n wrappingKey = await deriveKeyFromRawId(assertion.rawId)\n }\n\n return unwrapKeyringSummary(enrollment, wrappingKey)\n}\n\n/**\n * Check whether a `WebAuthnEnrollment` record looks well-formed.\n * Does not perform any cryptographic verification.\n */\nexport function isValidEnrollment(value: unknown): value is WebAuthnEnrollment {\n if (!value || typeof value !== 'object') return false\n const e = value as Record<string, unknown>\n return (\n e._noydb_webauthn === 1 &&\n typeof e.vault === 'string' &&\n typeof e.userId === 'string' &&\n typeof e.credentialId === 'string' &&\n typeof e.wrappedPayload === 'string' &&\n typeof e.wrapIv === 'string'\n )\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AEF;AAAA,IACF;AACA,kBAAc,MAAM,iBAAiB,SAAS;AAAA,EAChD,OAAO;AACL,kBAAc,MAAM,mBAAmB,UAAU,KAAK;AAAA,EACxD;AAEA,SAAO,qBAAqB,YAAY,WAAW;AACrD;AAMO,SAAS,kBAAkB,OAA6C;AAC7E,MAAI,CAAC,SAAS,OAAO,UAAU,SAAU,QAAO;AAChD,QAAM,IAAI;AACV,SACE,EAAE,oBAAoB,KACtB,OAAO,EAAE,UAAU,YACnB,OAAO,EAAE,WAAW,YACpB,OAAO,EAAE,iBAAiB,YAC1B,OAAO,EAAE,mBAAmB,YAC5B,OAAO,EAAE,WAAW;AAExB;","names":["ValidationError"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@noy-db/on-webauthn",
|
|
3
|
+
"version": "0.1.0-pre.3",
|
|
4
|
+
"description": "WebAuthn hardware-key keyrings for noy-db — Touch ID, Face ID, Windows Hello, YubiKey, FIDO2 passkeys",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "vLannaAi <vicio@lanna.ai>",
|
|
7
|
+
"homepage": "https://github.com/vLannaAi/noy-db/tree/main/packages/on-webauthn#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/vLannaAi/noy-db.git",
|
|
11
|
+
"directory": "packages/on-webauthn"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/vLannaAi/noy-db/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/index.d.cts",
|
|
26
|
+
"default": "./dist/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.cjs",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@noy-db/hub": "0.1.0-pre.3"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@noy-db/hub": "0.1.0-pre.3"
|
|
46
|
+
},
|
|
47
|
+
"keywords": [
|
|
48
|
+
"noy-db",
|
|
49
|
+
"auth",
|
|
50
|
+
"webauthn",
|
|
51
|
+
"passkey",
|
|
52
|
+
"biometric",
|
|
53
|
+
"fido2",
|
|
54
|
+
"yubikey",
|
|
55
|
+
"touch-id",
|
|
56
|
+
"face-id",
|
|
57
|
+
"windows-hello",
|
|
58
|
+
"hardware-key",
|
|
59
|
+
"zero-knowledge",
|
|
60
|
+
"encryption"
|
|
61
|
+
],
|
|
62
|
+
"publishConfig": {
|
|
63
|
+
"access": "public",
|
|
64
|
+
"tag": "latest"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsup",
|
|
68
|
+
"test": "vitest run",
|
|
69
|
+
"lint": "eslint src/",
|
|
70
|
+
"typecheck": "tsc --noEmit"
|
|
71
|
+
}
|
|
72
|
+
}
|