@aiam/ciba 0.9.3 → 0.9.5
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/package.json +1 -1
- package/token.mjs +27 -5
package/package.json
CHANGED
package/token.mjs
CHANGED
|
@@ -30,11 +30,22 @@ const args = process.argv.slice(2);
|
|
|
30
30
|
const get = (flag) => { const i = args.indexOf(flag); return i !== -1 ? args[i + 1] : undefined; };
|
|
31
31
|
const has = (flag) => args.includes(flag);
|
|
32
32
|
|
|
33
|
-
const resource
|
|
34
|
-
const jsonOut
|
|
35
|
-
const envOut
|
|
33
|
+
const resource = get('--resource') ?? `urn:sap:destination:${process.env.CIBA_DEFAULT_DESTINATION || 'WEBAGENTS_BACKEND'}`;
|
|
34
|
+
const jsonOut = has('--json');
|
|
35
|
+
const envOut = has('--env');
|
|
36
36
|
const isRefresh = has('--refresh');
|
|
37
37
|
|
|
38
|
+
// Collect all unrecognised --key value pairs as extra request attrs.
|
|
39
|
+
// Server uses them for login-hint, tenant, scope, app_tid, etc.
|
|
40
|
+
const KNOWN = new Set(['--resource', '--url', '--json', '--env', '--refresh']);
|
|
41
|
+
const extraAttrs = {};
|
|
42
|
+
for (let i = 0; i < args.length; i++) {
|
|
43
|
+
if (args[i].startsWith('--') && !KNOWN.has(args[i]) && i + 1 < args.length && !args[i + 1].startsWith('--')) {
|
|
44
|
+
extraAttrs[args[i].slice(2).replace(/-/g, '_')] = args[i + 1];
|
|
45
|
+
i++;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
38
49
|
// ─── Config ───────────────────────────────────────────────────────────────────
|
|
39
50
|
|
|
40
51
|
function loadConfig() {
|
|
@@ -198,7 +209,7 @@ if (isRefresh) {
|
|
|
198
209
|
const requests = deviceDoc.getMap('requests');
|
|
199
210
|
const newRid = randomBytes(8).toString('base64url');
|
|
200
211
|
log(`→ refresh — writing request ${newRid}`);
|
|
201
|
-
requests.set(newRid, { resource, status: 'pending', created_at: new Date().toISOString() });
|
|
212
|
+
requests.set(newRid, { ...extraAttrs, resource, status: 'pending', created_at: new Date().toISOString() });
|
|
202
213
|
provider.destroy();
|
|
203
214
|
process.exit(0);
|
|
204
215
|
}
|
|
@@ -217,13 +228,14 @@ if (found) {
|
|
|
217
228
|
const requests = deviceDoc.getMap('requests');
|
|
218
229
|
const newRid = randomBytes(8).toString('base64url');
|
|
219
230
|
log(`→ cache miss — writing request ${newRid} resource=${resolvedResource}`);
|
|
220
|
-
requests.set(newRid, { resource: resolvedResource, status: 'pending', created_at: new Date().toISOString() });
|
|
231
|
+
requests.set(newRid, { ...extraAttrs, resource: resolvedResource, status: 'pending', created_at: new Date().toISOString() });
|
|
221
232
|
|
|
222
233
|
// Wait for resources to update
|
|
223
234
|
log('→ waiting for token...');
|
|
224
235
|
const token = await new Promise((resolve, reject) => {
|
|
225
236
|
const timer = setTimeout(() => {
|
|
226
237
|
resourcesMap.unobserve(observer);
|
|
238
|
+
log(`→ timeout — resources at that point: ${JSON.stringify([...resourcesMap.entries()])}`);
|
|
227
239
|
reject(new Error('Timeout waiting for token'));
|
|
228
240
|
}, 30_000);
|
|
229
241
|
|
|
@@ -237,6 +249,16 @@ const token = await new Promise((resolve, reject) => {
|
|
|
237
249
|
}
|
|
238
250
|
};
|
|
239
251
|
resourcesMap.observe(observer);
|
|
252
|
+
// Also observe provider synced events in case doc re-syncs
|
|
253
|
+
provider.on('synced', () => {
|
|
254
|
+
log(`→ re-synced — resources: ${JSON.stringify([...resourcesMap.entries()])}`);
|
|
255
|
+
const found = tryGet(resourcesMap, deviceDoc, resolvedResource);
|
|
256
|
+
if (found) {
|
|
257
|
+
clearTimeout(timer);
|
|
258
|
+
resourcesMap.unobserve(observer);
|
|
259
|
+
resolve(decrypt(found));
|
|
260
|
+
}
|
|
261
|
+
});
|
|
240
262
|
});
|
|
241
263
|
|
|
242
264
|
provider.destroy();
|