Skip to content

Commit 5c70a18

Browse files
committed
(reluctantly) exposing 'cacheLockKey' public API method for advanced use cases
1 parent 9b02e3f commit 5c70a18

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,19 @@ import { configure } from "..";
178178
configure({ cacheLifetime: 5 * 60 * 1000 });
179179
```
180180

181-
### Clear the passkey/keypair cache
181+
### Manually cache a lock-key
182+
183+
To manually cache a lock-key silently (without passkey prompt):
184+
185+
```js
186+
import { cacheLockKey } from "..";
187+
188+
cacheLockKey(existingLockKey);
189+
```
190+
191+
**WARNING:** This is generally not recommended; it's provided only for advanced use-cases, such as a lock-key being preserved (temporarily) to approximate a "login session" across multiple page loads. Avoid this approach unless you're certain you need it, as it can degrade some of the security assurances behind the design of this library.
192+
193+
### Clear the lock-key cache
182194

183195
To clear a cache entry (effectively, "logging out"):
184196

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@lo-fi/local-data-lock",
33
"description": "Protect local-first app data with encryption/decryption key secured in WebAuthn (biometric) passkeys",
4-
"version": "0.15.4",
4+
"version": "0.16.0",
55
"exports": {
66
".": "./dist/bundlers/ldl.mjs",
77
"./bundlers/astro": "./bundler-plugins/astro.mjs",

src/ldl.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export {
5252
// main library API:
5353
supportsWAUserVerification,
5454
listLocalIdentities,
55+
cacheLockKey,
5556
clearLockKeyCache,
5657
removeLocalAccount,
5758
getLockKey,
@@ -77,6 +78,7 @@ var publicAPI = {
7778
// main library API:
7879
supportsWAUserVerification,
7980
listLocalIdentities,
81+
cacheLockKey,
8082
clearLockKeyCache,
8183
removeLocalAccount,
8284
getLockKey,
@@ -116,7 +118,11 @@ function getCachedLockKey(localID) {
116118
}
117119
}
118120

119-
function cacheLockKey(localID,lockKey,forceUpdate = false) {
121+
function cacheLockKey(lockKey) {
122+
internalCacheLockKey(lockKey.localIdentity,checkLockKey(lockKey));
123+
}
124+
125+
function internalCacheLockKey(localID,lockKey,forceUpdate = false) {
120126
if (!(localID in lockKeyCache) || forceUpdate) {
121127
lockKeyCache[localID] = {
122128
...lockKey,
@@ -269,7 +275,7 @@ async function getLockKey(
269275
// registration succeeded, lock-key returned?
270276
else if (lockKey != null) {
271277
await storeLocalIdentities();
272-
cacheLockKey(localID,lockKey);
278+
internalCacheLockKey(localID,lockKey);
273279

274280
return Object.freeze({
275281
...lockKey,
@@ -376,7 +382,7 @@ async function getLockKey(
376382
}
377383
}
378384

379-
cacheLockKey(localID,lockKey);
385+
internalCacheLockKey(localID,lockKey);
380386
}
381387
else if (verify) {
382388
throw new Error("Auth verification requested but skipped, against unrecognized passkey (no matching local-identity)");
@@ -404,7 +410,7 @@ async function getLockKey(
404410
// registration succeeded, lock-key returned?
405411
if (record != null && lockKey != null) {
406412
localIdentities[localID] = record;
407-
cacheLockKey(localID,lockKey);
413+
internalCacheLockKey(localID,lockKey);
408414
await storeLocalIdentities();
409415

410416
return Object.freeze({
@@ -478,7 +484,7 @@ async function getLockKey(
478484
let lockKey = deriveLockKey(
479485
authResult.response.userID.slice(0,IV_BYTE_LENGTH)
480486
);
481-
cacheLockKey(localID,lockKey);
487+
internalCacheLockKey(localID,lockKey);
482488
return lockKey;
483489
}
484490
else {
@@ -571,7 +577,10 @@ function checkLockKey(lockKeyCandidate) {
571577
isByteArray(lockKeyCandidate.iv) &&
572578
lockKeyCandidate.iv.byteLength == IV_BYTE_LENGTH
573579
) {
574-
return deriveLockKey(lockKeyCandidate.iv);
580+
return {
581+
localIdentity: lockKeyCandidate.localIdentity,
582+
...deriveLockKey(lockKeyCandidate.iv),
583+
};
575584
}
576585
}
577586
throw new Error("Unrecongnized lock-key");

0 commit comments

Comments
 (0)