@aiam/ciba 0.8.6 → 0.8.7

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.
Files changed (2) hide show
  1. package/ciba.mjs +14 -4
  2. package/package.json +1 -1
package/ciba.mjs CHANGED
@@ -468,18 +468,28 @@ function startDaemon(provider, deviceDoc, privateKey, serverUrl) {
468
468
  if (cachedName) {
469
469
  const cachedMap = deviceDoc.getMap(cachedName);
470
470
  const exp = cachedMap.get('exp');
471
- if (exp && exp > Math.floor(Date.now() / 1000) + 60) {
471
+ const now = Math.floor(Date.now() / 1000);
472
+ // Return cached token if: no exp set (server manages expiry),
473
+ // or exp is still valid. Only skip if we know it's expired.
474
+ const isExpired = exp && exp < now + 60;
475
+ if (!isExpired) {
472
476
  const token = decryptFromTokenMap(cachedMap);
473
477
  if (token) { conn.end(JSON.stringify({ token })); return; }
474
478
  }
475
479
  }
476
480
 
481
+ const prevTokenMapName = resourcesMap.get(requestedResource);
477
482
  const newRid = randomBytes(8).toString('base64url');
478
- dlog(`cache miss; writing requests[${newRid}] attrs=${JSON.stringify(attrs)}`);
483
+ dlog(`cache miss; writing requests[${newRid}] attrs=${JSON.stringify(attrs)} resources=${JSON.stringify([...resourcesMap.entries()])}`);
479
484
  requests.set(newRid, { ...attrs, status: 'pending', created_at: new Date().toISOString() });
480
485
 
481
- dlog(`resources map: ${JSON.stringify([...resourcesMap.entries()])} requested=${requestedResource}`);
482
- const prevTokenMapName = resourcesMap.get(requestedResource);
486
+ // Immediate re-check: token may have landed between cache check and now.
487
+ const postWriteName = resourcesMap.get(requestedResource);
488
+ if (postWriteName && postWriteName !== prevTokenMapName) {
489
+ const m = deviceDoc.getMap(postWriteName);
490
+ const token = decryptFromTokenMap(m);
491
+ if (token) { conn.end(JSON.stringify({ token })); return; }
492
+ }
483
493
  const newTokenMap = deviceDoc.getMap(`token:${newRid}`);
484
494
 
485
495
  const viaRid = firstInYMap(newTokenMap, (key) => key === 'ciphertext' || key === 'error', 30_000)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiam/ciba",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "description": "OAuth 2.0 Device Authorization Grant CLI with cross-device push approval (Yjs sync, ECDH-encrypted token delivery, persistent device id)",
5
5
  "type": "module",
6
6
  "bin": {