@aiam/ciba 0.8.9 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/ciba.mjs +2 -2
  2. package/package.json +1 -1
  3. package/token.mjs +29 -4
package/ciba.mjs CHANGED
@@ -656,7 +656,7 @@ const tokenCmd = defineCommand({
656
656
  ...outputArgs,
657
657
  },
658
658
  async run({ args }) {
659
- const tokenScript = join(_dirname(_fileURLToPath(import.meta.url)), 'token.mjs');
659
+ const tokenScript = join(_pkgDir, "token.mjs");
660
660
  const argv = [tokenScript];
661
661
  if (args.resource) argv.push('--resource', args.resource);
662
662
  if (args.json) argv.push('--json');
@@ -677,7 +677,7 @@ const refreshCmd = defineCommand({
677
677
  async run({ args }) {
678
678
  // refresh writes to device-doc requests and returns immediately.
679
679
  // token.mjs will pick up the new token when it arrives.
680
- const tokenScript = join(_dirname(_fileURLToPath(import.meta.url)), 'token.mjs');
680
+ const tokenScript = join(_pkgDir, "token.mjs");
681
681
  const argv = [tokenScript, '--refresh'];
682
682
  if (args.resource) argv.push('--resource', args.resource);
683
683
  if (args.json) argv.push('--json');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiam/ciba",
3
- "version": "0.8.9",
3
+ "version": "0.9.1",
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": {
package/token.mjs CHANGED
@@ -167,6 +167,31 @@ const meta = deviceDoc.getMap('meta');
167
167
  if (meta.get('public_key') !== publicKey) meta.set('public_key', publicKey);
168
168
 
169
169
  const resourcesMap = deviceDoc.getMap('resources');
170
+ const optionsMap = deviceDoc.getMap('options');
171
+
172
+ // Read server options — resolve short name or use as-is.
173
+ // options.resources = { webagents: { default: true, ... }, a2a: { ... }, ... }
174
+ const serverResources = optionsMap.get('resources') ?? {};
175
+ const resourceNames = Object.keys(serverResources);
176
+
177
+ // Resolve the requested resource:
178
+ // 1. If resource matches a short name directly → use it
179
+ // 2. If resource is a URN → use it as-is
180
+ // 3. If no resource → use default from options
181
+ let resolvedResource = resource;
182
+ if (!resource.includes(':') && !resourceNames.includes(resource)) {
183
+ // Try to find a match case-insensitively
184
+ const match = resourceNames.find(n => n.toLowerCase() === resource.toLowerCase());
185
+ if (match) resolvedResource = match;
186
+ }
187
+ if (!resource) {
188
+ // Use default
189
+ const def = resourceNames.find(n => serverResources[n]?.default);
190
+ resolvedResource = def ?? resource;
191
+ }
192
+
193
+ log(`→ options: ${JSON.stringify(serverResources)}`);
194
+ log(`→ resolved: ${resolvedResource}`);
170
195
  log(`→ resources: ${JSON.stringify([...resourcesMap.entries()])}`);
171
196
 
172
197
  // Refresh mode: write request and exit immediately.
@@ -180,7 +205,7 @@ if (isRefresh) {
180
205
  }
181
206
 
182
207
  // tryGet immediately
183
- const found = tryGet(resourcesMap, deviceDoc, resource);
208
+ const found = tryGet(resourcesMap, deviceDoc, resolvedResource);
184
209
  if (found) {
185
210
  log('→ cache hit');
186
211
  const token = decrypt(found);
@@ -192,8 +217,8 @@ if (found) {
192
217
  // Cache miss — write request
193
218
  const requests = deviceDoc.getMap('requests');
194
219
  const newRid = randomBytes(8).toString('base64url');
195
- log(`→ cache miss — writing request ${newRid}`);
196
- requests.set(newRid, { resource, status: 'pending', created_at: new Date().toISOString() });
220
+ log(`→ cache miss — writing request ${newRid} resource=${resolvedResource}`);
221
+ requests.set(newRid, { resource: resolvedResource, status: 'pending', created_at: new Date().toISOString() });
197
222
 
198
223
  // Wait for resources to update
199
224
  log('→ waiting for token...');
@@ -205,7 +230,7 @@ const token = await new Promise((resolve, reject) => {
205
230
 
206
231
  const observer = () => {
207
232
  log(`→ resources updated: ${JSON.stringify([...resourcesMap.entries()])}`);
208
- const found = tryGet(resourcesMap, deviceDoc, resource);
233
+ const found = tryGet(resourcesMap, deviceDoc, resolvedResource);
209
234
  if (found) {
210
235
  clearTimeout(timer);
211
236
  resourcesMap.unobserve(observer);