@camstack/addon-export-alexa 1.2.194 → 1.2.196

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.
@@ -186,6 +186,7 @@ your skill's locale: NA → `us-east-1`, EU/IN → `eu-west-1`, FE/AU → `us-we
186
186
  | `DEBUG_DIRECTIVES` | `1` = one `directive_trace` log line per Smart Home directive: namespace / name / endpointId / payloadVersion / payload keys asked, and the answered event (ErrorResponse type and message, StateReport property names and value shapes). Never the bearer token or SDP bodies. Diagnostic; default off. |
187
187
  | `ALLOW_MIGRATION` | `0` = refuse `/broker/migrate`. Default on until every hub has migrated. |
188
188
  | `HUB_INGRESS_TOKEN` | Optional defense-in-depth header (`X-CamStack-Ingress`), copied from the addon's **Ingress token** setting. |
189
+ | `HUB_INSTANCE_ID` | Optional; the hub skill instance this Lambda serves (its Export panel shows the id). `/authorize` then links the account to it (`instance=<id>`), and a token without an `instanceId` claim is sent to `/<route>/<id>`. A token WITH the claim (hubs from D1047) always goes to `/addon/export-alexa/instances/<id>/<route>`, so one Lambda can serve accounts linked to different instances. Unset = the default skill. |
189
190
 
190
191
  Legacy, read only when the matching `*_PARAM` is unset: `LWA_CLIENT_ID`,
191
192
  `LWA_CLIENT_SECRET`, `SKILL_CLIENT_ID`, `SKILL_CLIENT_SECRET`. Remove them once
@@ -265,6 +265,11 @@ async function handleAuthorizePage(event, deps) {
265
265
  }
266
266
  const query = new URLSearchParams(forwarded)
267
267
  query.set('integration', INTEGRATION_ID)
268
+ // A Lambda dedicated to one skill of a hub that runs several links the account to it (D1047).
269
+ const instanceId = deps.env && deps.env.HUB_INSTANCE_ID
270
+ if (typeof instanceId === 'string' && instanceId.length > 0 && !query.has('instance')) {
271
+ query.set('instance', instanceId)
272
+ }
268
273
  return {
269
274
  statusCode: 302,
270
275
  headers: {
@@ -36,9 +36,14 @@
36
36
  * HUB_INGRESS_TOKEN Optional defense-in-depth header for hub calls.
37
37
  * HUB_INSTANCE_ID Optional; the id of this skill's instance on the hub
38
38
  * (shown in its Export panel). Set when the hub runs
39
- * several skills: calls then go to `/directive/<id>`,
40
- * `/oauth/broker-link/<id>` and `/oauth/lwa-tokens/<id>`.
41
- * Unset = the hub's first skill, as before.
39
+ * several skills: /authorize then asks the hub to link
40
+ * the account to that instance (`instance=<id>`), and a
41
+ * token that does not name an instance is sent to
42
+ * `/directive/<id>`, `/oauth/broker-link/<id>` and
43
+ * `/oauth/lwa-tokens/<id>`. Unset = the hub's default
44
+ * skill, as before. A token that names its instance (the
45
+ * `instanceId` claim, hubs from D1047 on) is always sent
46
+ * to `/addon/export-alexa/instances/<id>/…` instead.
42
47
  * DEBUG_DIRECTIVES "1" = log one `directive_trace` line per directive
43
48
  * (debug-trace.js; never the bearer). Default off.
44
49
  * Legacy (read only when the *_PARAM is unset): LWA_CLIENT_ID,
@@ -296,7 +301,7 @@ async function handleAcceptGrant(event, deps) {
296
301
  const hubUrl = normaliseHubUrl(claims.hubUrl)
297
302
 
298
303
  if (brokerAvailable(deps)) {
299
- const outcome = await linkViaBroker(deps, { hubUrl, granteeToken, tokens })
304
+ const outcome = await linkViaBroker(deps, { hubUrl, granteeToken, tokens, claims })
300
305
  if (outcome.kind === 'linked') return acceptGrantResponse(header)
301
306
  if (outcome.kind !== 'hub-without-broker' || deps.env.LEGACY_HUB_FALLBACK === '0') {
302
307
  return acceptGrantErrorEnvelope(header, outcome.message)
@@ -305,7 +310,7 @@ async function handleAcceptGrant(event, deps) {
305
310
  'accept_grant: hub has no broker route; falling back to the legacy token hand-off',
306
311
  )
307
312
  }
308
- return deliverTokensToLegacyHub(deps, header, { hubUrl, granteeToken, tokens })
313
+ return deliverTokensToLegacyHub(deps, header, { hubUrl, granteeToken, tokens, claims })
309
314
  }
310
315
 
311
316
  /** The path suffix that names this skill on a hub that runs several (`HUB_INSTANCE_ID`). */
@@ -314,6 +319,24 @@ function hubInstanceSuffix(deps) {
314
319
  return typeof id === 'string' && id.length > 0 ? `/${encodeURIComponent(id)}` : ''
315
320
  }
316
321
 
322
+ /** An instance id as the hub accepts it in a path (D1046). */
323
+ const INSTANCE_ID_RE = /^[A-Za-z0-9._-]{1,64}$/
324
+
325
+ /**
326
+ * The hub path of one of the addon's Lambda routes (`/directive`, `/oauth/broker-link`, …) for
327
+ * the account the token was linked for. A token that names its instance (the `instanceId`
328
+ * claim, D1047) is routed to that instance (`/instances/<id>/…`, D1046) — this is how one
329
+ * Lambda serves accounts linked to different skills of a hub. A token without the claim keeps
330
+ * the address it had: `HUB_INSTANCE_ID`'s suffix, or the default skill.
331
+ */
332
+ function hubRoutePath(deps, claims, route) {
333
+ const claimed = claims && typeof claims.instanceId === 'string' ? claims.instanceId : ''
334
+ if (INSTANCE_ID_RE.test(claimed)) {
335
+ return `/addon/export-alexa/instances/${claimed}${route}`
336
+ }
337
+ return `/addon/export-alexa${route}${hubInstanceSuffix(deps)}`
338
+ }
339
+
317
340
  /**
318
341
  * The D753 link: store the grant here, give the hub only a credential and a
319
342
  * first access token. The item stays `pending` until the hub accepted it, so a
@@ -329,7 +352,7 @@ async function linkViaBroker(deps, input) {
329
352
  let resp
330
353
  try {
331
354
  resp = await deps.fetch(
332
- `${input.hubUrl}/addon/export-alexa/oauth/broker-link${hubInstanceSuffix(deps)}`,
355
+ `${input.hubUrl}${hubRoutePath(deps, input.claims, '/oauth/broker-link')}`,
333
356
  {
334
357
  method: 'POST',
335
358
  headers: hubHeaders(deps, input.granteeToken),
@@ -359,7 +382,7 @@ async function linkViaBroker(deps, input) {
359
382
  /** Pre-D753 hand-off: the hub receives the refresh token itself. */
360
383
  async function deliverTokensToLegacyHub(deps, header, input) {
361
384
  const callbackResp = await deps.fetch(
362
- `${input.hubUrl}/addon/export-alexa/oauth/lwa-tokens${hubInstanceSuffix(deps)}`,
385
+ `${input.hubUrl}${hubRoutePath(deps, input.claims, '/oauth/lwa-tokens')}`,
363
386
  {
364
387
  method: 'POST',
365
388
  headers: hubHeaders(deps, input.granteeToken),
@@ -407,7 +430,7 @@ async function forwardDirective(event, deps) {
407
430
  endpoint,
408
431
  )
409
432
  }
410
- const target = `${normaliseHubUrl(claims.hubUrl)}/addon/export-alexa/directive${hubInstanceSuffix(deps)}`
433
+ const target = `${normaliseHubUrl(claims.hubUrl)}${hubRoutePath(deps, claims, '/directive')}`
411
434
  let resp
412
435
  try {
413
436
  resp = await deps.fetch(target, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camstack/addon-export-alexa",
3
- "version": "1.2.194",
3
+ "version": "1.2.196",
4
4
  "description": "Alexa Smart Home Skill export — hub-side OAuth provider + directive handler. The companion AWS Lambda forwards Alexa directives to this addon's /addon/export-alexa/directive endpoint.",
5
5
  "keywords": [
6
6
  "camstack",
@@ -59,7 +59,7 @@
59
59
  }
60
60
  ],
61
61
  "requires": {
62
- "server": ">=1.2.841"
62
+ "server": ">=1.2.850"
63
63
  }
64
64
  },
65
65
  "files": [