homebridge-melcloud-control 4.0.0-beta.295 → 4.0.0-beta.296
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/src/melcloud.js +31 -1
- package/src/melcloudhometoken.js +39 -17
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"displayName": "MELCloud Control",
|
|
3
3
|
"name": "homebridge-melcloud-control",
|
|
4
|
-
"version": "4.0.0-beta.
|
|
4
|
+
"version": "4.0.0-beta.296",
|
|
5
5
|
"description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "grzegorz914",
|
package/src/melcloud.js
CHANGED
|
@@ -305,7 +305,37 @@ class MelCloud extends EventEmitter {
|
|
|
305
305
|
const code = await melCloudHomeToken.extractCodeFromHtml(html);
|
|
306
306
|
|
|
307
307
|
console.log(code);
|
|
308
|
-
|
|
308
|
+
return
|
|
309
|
+
// Check for form_post response (HTML with hidden form)
|
|
310
|
+
const formCodeMatch = html.match(/name="code"\s+value="([^"]+)"/);
|
|
311
|
+
const formStateMatch = html.match(/name="state"\s+value="([^"]+)"/);
|
|
312
|
+
const formActionMatch = html.match(/action="([^"]+)"/);
|
|
313
|
+
|
|
314
|
+
if (formCodeMatch && formStateMatch && formActionMatch) {
|
|
315
|
+
console.log('Found form_post response, submitting to callback endpoint...');
|
|
316
|
+
this.submitFormPost(formActionMatch[1], formCodeMatch[1], formStateMatch[1], cookies)
|
|
317
|
+
.then(code => resolve(code))
|
|
318
|
+
.catch(err => reject(err));
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Check for melcloudhome:// redirect in JS
|
|
323
|
+
const bodyCodeMatch = html.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
|
|
324
|
+
if (bodyCodeMatch) {
|
|
325
|
+
console.log('Found authorization code in response body (JS)');
|
|
326
|
+
resolve(bodyCodeMatch[1]);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// 🆕 NEW: Check for data-url with code parameter (like in 500 error pages)
|
|
331
|
+
const dataUrlMatch = html.match(/data-url="[^"]*code=([^&"']+)/);
|
|
332
|
+
if (dataUrlMatch) {
|
|
333
|
+
console.log('Found authorization code in data-url attribute');
|
|
334
|
+
resolve(dataUrlMatch[1]);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
reject(new Error('Authorization code not found in URL or HTML'));
|
|
309
339
|
|
|
310
340
|
// 5. Wymień code na access_token
|
|
311
341
|
const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({
|
package/src/melcloudhometoken.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { wrapper } from 'axios-cookiejar-support';
|
|
3
|
+
import { CookieJar } from 'tough-cookie';
|
|
1
4
|
import crypto from 'crypto';
|
|
2
5
|
|
|
3
6
|
const MOBILE_USER_AGENT = 'MonitorAndControl.App.Mobile/35 CFNetwork/3860.100.1 Darwin/25.0.0';
|
|
@@ -6,11 +9,13 @@ const REDIRECT_URI = 'melcloudhome://';
|
|
|
6
9
|
const SCOPE = 'openid profile email offline_access IdentityServerApi';
|
|
7
10
|
const TOKEN_ENDPOINT = 'https://auth.melcloudhome.com/connect/token';
|
|
8
11
|
const AUTHORIZE_ENDPOINT = 'https://auth.melcloudhome.com/connect/authorize';
|
|
9
|
-
const LOGIN_ENDPOINT = 'https://auth.melcloudhome.com/Account/Login';
|
|
10
12
|
|
|
11
13
|
class MelCloudHomeToken {
|
|
12
14
|
constructor(log = console) {
|
|
13
15
|
this.log = log;
|
|
16
|
+
const jar = new CookieJar();
|
|
17
|
+
this.client = wrapper(axios.create({ jar, withCredentials: true }));
|
|
18
|
+
this.client.defaults.headers['User-Agent'] = MOBILE_USER_AGENT;
|
|
14
19
|
}
|
|
15
20
|
|
|
16
21
|
generatePKCE() {
|
|
@@ -23,27 +28,22 @@ class MelCloudHomeToken {
|
|
|
23
28
|
return crypto.randomBytes(32).toString('hex');
|
|
24
29
|
}
|
|
25
30
|
|
|
26
|
-
async
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
const state = this.generateState();
|
|
30
|
-
this.state = state;
|
|
31
|
-
|
|
32
|
-
const authUrl = new URL(AUTHORIZE_ENDPOINT);
|
|
33
|
-
authUrl.searchParams.set('client_id', CLIENT_ID);
|
|
34
|
-
authUrl.searchParams.set('redirect_uri', REDIRECT_URI);
|
|
35
|
-
authUrl.searchParams.set('response_type', 'code');
|
|
36
|
-
authUrl.searchParams.set('scope', SCOPE);
|
|
37
|
-
authUrl.searchParams.set('code_challenge', pkce.challenge);
|
|
38
|
-
authUrl.searchParams.set('code_challenge_method', 'S256');
|
|
39
|
-
authUrl.searchParams.set('state', state);
|
|
40
|
-
|
|
41
|
-
return authUrl.toString();
|
|
31
|
+
async getLoginPage(authUrl) {
|
|
32
|
+
const res = await this.client.get(authUrl, { maxRedirects: 10 });
|
|
33
|
+
return { url: res.request.res.responseUrl };
|
|
42
34
|
}
|
|
43
35
|
|
|
44
36
|
async extractCodeFromHtml(html) {
|
|
45
37
|
let code = null;
|
|
46
38
|
|
|
39
|
+
// 1️⃣ Sprawdzenie, czy code jest w URL
|
|
40
|
+
const url1 = page.url();
|
|
41
|
+
const urlMatch = url1.match(/[?&]code=([^&]+)/);
|
|
42
|
+
if (urlMatch) {
|
|
43
|
+
console.log('Authorization code found in URL');
|
|
44
|
+
return urlMatch[1];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
47
|
// Check for form_post response (HTML with hidden form)
|
|
48
48
|
const formCodeMatch = html.match(/name="code"\s+value="([^"]+)"/);
|
|
49
49
|
const formStateMatch = html.match(/name="state"\s+value="([^"]+)"/);
|
|
@@ -75,6 +75,28 @@ class MelCloudHomeToken {
|
|
|
75
75
|
|
|
76
76
|
return code;
|
|
77
77
|
}
|
|
78
|
+
|
|
79
|
+
async buildAuthorizeUrl() {
|
|
80
|
+
try {
|
|
81
|
+
const pkce = this.generatePKCE();
|
|
82
|
+
const state = this.generateState();
|
|
83
|
+
const authUrl = new URL(AUTHORIZE_ENDPOINT);
|
|
84
|
+
authUrl.searchParams.set('client_id', CLIENT_ID);
|
|
85
|
+
authUrl.searchParams.set('redirect_uri', REDIRECT_URI);
|
|
86
|
+
authUrl.searchParams.set('response_type', 'code');
|
|
87
|
+
authUrl.searchParams.set('scope', SCOPE);
|
|
88
|
+
authUrl.searchParams.set('code_challenge', pkce.challenge);
|
|
89
|
+
authUrl.searchParams.set('code_challenge_method', 'S256');
|
|
90
|
+
authUrl.searchParams.set('state', state);
|
|
91
|
+
|
|
92
|
+
const loginPage = await this.getLoginPage(authUrl.toString());
|
|
93
|
+
|
|
94
|
+
return loginPage.url;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.error('Failed to obtain OAuth tokens:', error);
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
78
100
|
}
|
|
79
101
|
|
|
80
102
|
export default MelCloudHomeToken;
|