homebridge-nuheat2 1.2.22 → 1.2.23
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/CHANGELOG.md +8 -0
- package/lib/NuHeatAPI.js +166 -28
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,14 @@ All notable changes to this project should be documented in this file
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [1.2.23] - 2026-07-08
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Follow Nuheat's updated OAuth browser redirects during login and consent so PKCE token exchange no longer fails with `invalid_grant`
|
|
12
|
+
- Preserve Nuheat consent cookies through the final authorization callback and select the correct login/consent forms when the identity page includes multiple forms
|
|
13
|
+
- Add regression tests for the Conductor/NAM OAuth login and consent flow
|
|
14
|
+
|
|
7
15
|
## [1.2.22] - 2026-07-07
|
|
8
16
|
|
|
9
17
|
### Fixed
|
package/lib/NuHeatAPI.js
CHANGED
|
@@ -272,6 +272,110 @@ class NuHeatAPI {
|
|
|
272
272
|
return "";
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
|
+
findForm(html, selector) {
|
|
276
|
+
return (html
|
|
277
|
+
.querySelectorAll("form")
|
|
278
|
+
.find((form) => form.querySelector(selector)) || html);
|
|
279
|
+
}
|
|
280
|
+
getFormAction(form, baseUrl, fallbackUrl) {
|
|
281
|
+
return new URL(form.getAttribute?.("action") || fallbackUrl, baseUrl).toString();
|
|
282
|
+
}
|
|
283
|
+
getInputValue(html, name) {
|
|
284
|
+
return (html.querySelector("input[name=" + name + "]")?.getAttribute("value") ||
|
|
285
|
+
"");
|
|
286
|
+
}
|
|
287
|
+
getCheckedInputValues(html, name) {
|
|
288
|
+
const values = [];
|
|
289
|
+
for (const input of html.querySelectorAll("input[name=" + name + "]")) {
|
|
290
|
+
const type = (input.getAttribute("type") || "").toLowerCase();
|
|
291
|
+
const value = input.getAttribute("value") || "";
|
|
292
|
+
if (value &&
|
|
293
|
+
((type === "checkbox" && input.hasAttribute("checked")) ||
|
|
294
|
+
type === "hidden")) {
|
|
295
|
+
if (!values.includes(value)) {
|
|
296
|
+
values.push(value);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return values;
|
|
301
|
+
}
|
|
302
|
+
getHtmlMetaRefreshUrl(htmlText, baseUrl) {
|
|
303
|
+
const html = parse(htmlText);
|
|
304
|
+
const metaRefresh = html
|
|
305
|
+
.querySelectorAll("meta")
|
|
306
|
+
.find((node) => (node.getAttribute("http-equiv") || "").toLowerCase() === "refresh");
|
|
307
|
+
const refreshContent = metaRefresh?.getAttribute("content") || "";
|
|
308
|
+
const redirectMatch = refreshContent.match(/url\s*=\s*([^;]+)/i);
|
|
309
|
+
if (!redirectMatch) {
|
|
310
|
+
return "";
|
|
311
|
+
}
|
|
312
|
+
const redirectUrl = redirectMatch[1]
|
|
313
|
+
.trim()
|
|
314
|
+
.replace(/^['"]|['"]$/g, "");
|
|
315
|
+
return new URL(redirectUrl, baseUrl).toString();
|
|
316
|
+
}
|
|
317
|
+
mergeCookies(...cookies) {
|
|
318
|
+
const cookiePairs = new Map();
|
|
319
|
+
for (const cookie of cookies) {
|
|
320
|
+
if (!cookie) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
for (const cookiePair of cookie.split(/;\s*/)) {
|
|
324
|
+
const cookieName = cookiePair.split("=")[0];
|
|
325
|
+
if (cookieName) {
|
|
326
|
+
cookiePairs.set(cookieName, cookiePair);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return Array.from(cookiePairs.values()).join("; ");
|
|
331
|
+
}
|
|
332
|
+
async followHtmlMetaRefresh(response, cookie) {
|
|
333
|
+
if (response.headers.get("location")) {
|
|
334
|
+
return response;
|
|
335
|
+
}
|
|
336
|
+
const redirectUrl = this.getHtmlMetaRefreshUrl(await response.text(), response.url);
|
|
337
|
+
if (!redirectUrl) {
|
|
338
|
+
return response;
|
|
339
|
+
}
|
|
340
|
+
this.log.debug("NuHeatAPI: Following OAuth browser redirect to " +
|
|
341
|
+
new URL(redirectUrl).pathname +
|
|
342
|
+
".");
|
|
343
|
+
const redirectResponse = await this.fetch(redirectUrl, {
|
|
344
|
+
headers: {
|
|
345
|
+
Cookie: cookie,
|
|
346
|
+
},
|
|
347
|
+
redirect: "manual",
|
|
348
|
+
});
|
|
349
|
+
if (!redirectResponse) {
|
|
350
|
+
this.log.error("NuHeatAPI: Unable to follow the OAuth browser redirect.");
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
return redirectResponse;
|
|
354
|
+
}
|
|
355
|
+
getRedirectUrl(response) {
|
|
356
|
+
const location = response.headers.get("location");
|
|
357
|
+
if (!location) {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
return new URL(location, settings_1.NUHEAT_API_AUTHORIZE_URI);
|
|
361
|
+
}
|
|
362
|
+
isFinalAuthorizationRedirect(response) {
|
|
363
|
+
const redirectUrl = this.getRedirectUrl(response);
|
|
364
|
+
if (!redirectUrl) {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
return (redirectUrl.searchParams.has("code") ||
|
|
368
|
+
redirectUrl.searchParams.has("error"));
|
|
369
|
+
}
|
|
370
|
+
needsOAuthConsent(response) {
|
|
371
|
+
const redirectUrl = this.getRedirectUrl(response);
|
|
372
|
+
if (!redirectUrl || this.isFinalAuthorizationRedirect(response)) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
const redirectPath = redirectUrl.pathname.toLowerCase();
|
|
376
|
+
return (redirectPath === "/connect/authorize/callback" ||
|
|
377
|
+
redirectPath === "/consent");
|
|
378
|
+
}
|
|
275
379
|
async oauthGetAuthPage() {
|
|
276
380
|
const authEndpoint = new URL(settings_1.NUHEAT_API_AUTHORIZE_URI);
|
|
277
381
|
authEndpoint.searchParams.set("response_type", "code");
|
|
@@ -300,19 +404,20 @@ class NuHeatAPI {
|
|
|
300
404
|
if (cookie) {
|
|
301
405
|
const htmlText = await authPage.text();
|
|
302
406
|
const loginPageHtml = parse(htmlText);
|
|
303
|
-
const
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
.querySelector("input[name=ReturnUrl]")
|
|
308
|
-
?.getAttribute("value") || "";
|
|
407
|
+
const loginForm = this.findForm(loginPageHtml, "input[name=Password]");
|
|
408
|
+
const requestVerificationToken = this.getInputValue(loginForm, "__RequestVerificationToken");
|
|
409
|
+
const requestReturnURL = this.getInputValue(loginForm, "ReturnUrl") ||
|
|
410
|
+
this.getInputValue(loginPageHtml, "ReturnUrl");
|
|
309
411
|
if (!requestVerificationToken) {
|
|
310
412
|
this.log.error("NuHeatAPI: Unable to complete OAuth login. The verification token could not be retrieved.");
|
|
311
413
|
return null;
|
|
312
414
|
}
|
|
415
|
+
const usernameField = loginForm.querySelector("input[name=Email]")
|
|
416
|
+
? "Email"
|
|
417
|
+
: "Username";
|
|
313
418
|
const loginBody = new URLSearchParams({
|
|
314
419
|
ReturnUrl: requestReturnURL,
|
|
315
|
-
|
|
420
|
+
[usernameField]: this.email,
|
|
316
421
|
Password: this.password,
|
|
317
422
|
button: "login",
|
|
318
423
|
__RequestVerificationToken: requestVerificationToken,
|
|
@@ -341,26 +446,48 @@ class NuHeatAPI {
|
|
|
341
446
|
return null;
|
|
342
447
|
}
|
|
343
448
|
async oauthConfirm(authPage, sessionCookie) {
|
|
344
|
-
const redirectUrl =
|
|
345
|
-
|
|
449
|
+
const redirectUrl = this.getRedirectUrl(authPage);
|
|
450
|
+
if (!redirectUrl) {
|
|
451
|
+
this.log.error("NuHeatAPI: Unable to complete the OAuth login redirect.");
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
let confirmPage = await this.fetch(redirectUrl.toString(), {
|
|
346
455
|
headers: {
|
|
347
456
|
Cookie: sessionCookie,
|
|
348
457
|
},
|
|
458
|
+
redirect: "manual",
|
|
349
459
|
});
|
|
350
460
|
if (!confirmPage) {
|
|
351
461
|
this.log.error("NuHeatAPI: Unable to complete the OAuth login redirect.");
|
|
352
462
|
return null;
|
|
353
463
|
}
|
|
464
|
+
const callbackCookie = this.trimSetCookie(confirmPage.headers.raw?.()["set-cookie"]);
|
|
465
|
+
const callbackSessionCookie = this.mergeCookies(sessionCookie, callbackCookie);
|
|
466
|
+
if (this.isFinalAuthorizationRedirect(confirmPage)) {
|
|
467
|
+
return confirmPage;
|
|
468
|
+
}
|
|
469
|
+
const confirmRedirectUrl = this.getRedirectUrl(confirmPage);
|
|
470
|
+
if (confirmRedirectUrl?.pathname.toLowerCase() === "/consent") {
|
|
471
|
+
confirmPage = await this.fetch(confirmRedirectUrl.toString(), {
|
|
472
|
+
headers: {
|
|
473
|
+
Cookie: callbackSessionCookie,
|
|
474
|
+
},
|
|
475
|
+
redirect: "manual",
|
|
476
|
+
});
|
|
477
|
+
if (!confirmPage) {
|
|
478
|
+
this.log.error("NuHeatAPI: Unable to access the OAuth consent confirmation page.");
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
354
482
|
const cookie = this.trimSetCookie(confirmPage.headers.raw?.()["set-cookie"]);
|
|
355
|
-
|
|
483
|
+
const consentSessionCookie = this.mergeCookies(callbackSessionCookie, cookie);
|
|
484
|
+
if (consentSessionCookie) {
|
|
356
485
|
const htmlText = await confirmPage.text();
|
|
357
486
|
const loginPageHtml = parse(htmlText);
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
.querySelector("input[name=ReturnUrl]")
|
|
363
|
-
?.getAttribute("value") || "";
|
|
487
|
+
const consentForm = this.findForm(loginPageHtml, "input[name=ScopesConsented]");
|
|
488
|
+
const requestVerificationToken = this.getInputValue(consentForm, "__RequestVerificationToken");
|
|
489
|
+
const requestReturnURL = this.getInputValue(consentForm, "ReturnUrl") ||
|
|
490
|
+
this.getInputValue(loginPageHtml, "ReturnUrl");
|
|
364
491
|
if (!requestVerificationToken) {
|
|
365
492
|
this.log.error("NuHeatAPI: Unable to complete OAuth login. The api access couldn't be confirmed.");
|
|
366
493
|
return null;
|
|
@@ -371,14 +498,19 @@ class NuHeatAPI {
|
|
|
371
498
|
RememberConsent: "true",
|
|
372
499
|
__RequestVerificationToken: requestVerificationToken,
|
|
373
500
|
});
|
|
501
|
+
const checkedScopes = this.getCheckedInputValues(consentForm, "ScopesConsented");
|
|
502
|
+
const scopes = checkedScopes.length > 0 ? checkedScopes : [...OAUTH_SCOPES];
|
|
503
|
+
for (const scope of scopes) {
|
|
504
|
+
loginBody.append("ScopesConsented", scope);
|
|
505
|
+
}
|
|
374
506
|
this.log.debug("NuHeatAPI: OAuth consent required. Confirming scopes: " +
|
|
375
507
|
this.getRequestedScope());
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
508
|
+
const consentUrl = this.getFormAction(consentForm, confirmPage.url, settings_1.NUHEAT_API_CONSENT_URI);
|
|
509
|
+
const response = await this.fetch(consentUrl, {
|
|
510
|
+
body: loginBody.toString(),
|
|
379
511
|
headers: {
|
|
380
512
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
381
|
-
Cookie:
|
|
513
|
+
Cookie: consentSessionCookie,
|
|
382
514
|
},
|
|
383
515
|
method: "POST",
|
|
384
516
|
redirect: "manual",
|
|
@@ -387,17 +519,18 @@ class NuHeatAPI {
|
|
|
387
519
|
this.log.error("NuHeatAPI: Unable to complete OAuth login. API access confirmation not completed.");
|
|
388
520
|
return null;
|
|
389
521
|
}
|
|
390
|
-
|
|
522
|
+
const responseCookie = this.trimSetCookie(response.headers.raw?.()["set-cookie"]);
|
|
523
|
+
return await this.followHtmlMetaRefresh(response, this.mergeCookies(consentSessionCookie, responseCookie));
|
|
391
524
|
}
|
|
392
525
|
return null;
|
|
393
526
|
}
|
|
394
527
|
async oauthRedirect(loginResponse, sessionCookie) {
|
|
395
528
|
const redirectUrl = new URL(loginResponse.headers.get("location") || "", settings_1.NUHEAT_API_AUTHORIZE_URI);
|
|
396
|
-
const cookie = this.trimSetCookie(loginResponse.headers.raw?.()["set-cookie"]);
|
|
529
|
+
const cookie = this.mergeCookies(sessionCookie, this.trimSetCookie(loginResponse.headers.raw?.()["set-cookie"]));
|
|
397
530
|
if (cookie) {
|
|
398
531
|
const response = await this.fetch(redirectUrl.toString(), {
|
|
399
532
|
headers: {
|
|
400
|
-
Cookie: cookie
|
|
533
|
+
Cookie: cookie,
|
|
401
534
|
},
|
|
402
535
|
redirect: "manual",
|
|
403
536
|
});
|
|
@@ -420,16 +553,21 @@ class NuHeatAPI {
|
|
|
420
553
|
}
|
|
421
554
|
const sessionCookie = this.trimSetCookie(response.headers.raw?.()["set-cookie"]);
|
|
422
555
|
if (sessionCookie) {
|
|
423
|
-
|
|
424
|
-
|
|
556
|
+
response = await this.followHtmlMetaRefresh(response, sessionCookie);
|
|
557
|
+
if (!response) {
|
|
558
|
+
return null;
|
|
559
|
+
}
|
|
560
|
+
if (this.needsOAuthConsent(response)) {
|
|
425
561
|
response = await this.oauthConfirm(response, sessionCookie);
|
|
426
562
|
if (!response) {
|
|
427
563
|
return null;
|
|
428
564
|
}
|
|
429
565
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
566
|
+
if (!this.isFinalAuthorizationRedirect(response)) {
|
|
567
|
+
response = await this.oauthRedirect(response, sessionCookie);
|
|
568
|
+
if (!response) {
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
433
571
|
}
|
|
434
572
|
const redirectUrl = new URL(response.headers.get("location") || "");
|
|
435
573
|
const requestBody = this.buildAuthorizationCodeTokenRequest(redirectUrl);
|