homebridge-melcloud-control 4.0.0-beta.373 → 4.0.0-beta.375
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/melcloudhometoken.js +64 -152
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.375",
|
|
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/melcloudhometoken.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
import crypto from 'crypto';
|
|
3
3
|
import { JSDOM } from 'jsdom';
|
|
4
|
+
import { wrapper } from 'axios-cookiejar-support';
|
|
5
|
+
import { CookieJar } from 'tough-cookie';
|
|
4
6
|
import EventEmitter from 'events';
|
|
5
7
|
|
|
6
8
|
const MOBILE_USER_AGENT = 'MonitorAndControl.App.Mobile/35 CFNetwork/3860.100.1 Darwin/25.0.0';
|
|
@@ -17,7 +19,14 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
17
19
|
this.passwd = config.passwd;
|
|
18
20
|
this.logWarn = config.logWarn;
|
|
19
21
|
this.logError = config.logError;
|
|
20
|
-
|
|
22
|
+
|
|
23
|
+
const jar = new CookieJar();
|
|
24
|
+
this.axiosInstance = wrapper(axios.create({
|
|
25
|
+
headers: { 'User-Agent': MOBILE_USER_AGENT },
|
|
26
|
+
jar,
|
|
27
|
+
withCredentials: true,
|
|
28
|
+
maxRedirects: 0,
|
|
29
|
+
}));
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
generatePKCE() {
|
|
@@ -27,178 +36,80 @@ class MelCloudHomeToken extends EventEmitter {
|
|
|
27
36
|
}
|
|
28
37
|
|
|
29
38
|
generateState() {
|
|
30
|
-
return crypto.randomBytes(
|
|
39
|
+
return crypto.randomBytes(16).toString('hex');
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
async getLoginPage(authUrl) {
|
|
34
|
-
const res = await this.axiosInstance(
|
|
35
|
-
method: 'GET',
|
|
36
|
-
baseURL: authUrl,
|
|
37
|
-
maxRedirects: 10
|
|
38
|
-
});
|
|
43
|
+
const res = await this.axiosInstance.get(authUrl);
|
|
39
44
|
return { url: res.request.res.responseUrl };
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
async extractCodeFromHtml(html, cookies = '') {
|
|
43
|
-
try {
|
|
44
|
-
// Code w URL
|
|
45
|
-
const urlMatch = html.match(/[?&]code=([^&]+)/);
|
|
46
|
-
if (urlMatch) {
|
|
47
|
-
return urlMatch[1];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Hidden form (form_post)
|
|
51
|
-
const formCodeMatch = html.match(/name="code"\s+value="([^"]+)"/);
|
|
52
|
-
const formStateMatch = html.match(/name="state"\s+value="([^"]+)"/);
|
|
53
|
-
const formActionMatch = html.match(/action="([^"]+)"/);
|
|
54
|
-
|
|
55
|
-
if (formCodeMatch && formStateMatch && formActionMatch) {
|
|
56
|
-
const code = await this.submitFormPost(
|
|
57
|
-
formActionMatch[1],
|
|
58
|
-
formCodeMatch[1],
|
|
59
|
-
formStateMatch[1],
|
|
60
|
-
cookies
|
|
61
|
-
);
|
|
62
|
-
return code;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// melcloudhome:// w body
|
|
66
|
-
const bodyCodeMatch = html.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
|
|
67
|
-
if (bodyCodeMatch) {
|
|
68
|
-
return bodyCodeMatch[1];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// data-url (np. z błędnej strony 500)
|
|
72
|
-
const dataUrlMatch = html.match(/data-url="[^"]*code=([^&"']+)/);
|
|
73
|
-
if (dataUrlMatch) {
|
|
74
|
-
return dataUrlMatch[1];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
console.warn('Authorization code not found in HTML response');
|
|
78
|
-
return null;
|
|
79
|
-
} catch (err) {
|
|
80
|
-
console.error('extractCodeFromHtml error:', err);
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
47
|
async getTokens(code, codeVerifier) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
const tokenResponse = await this.axiosInstance(tokenData.toString(), {
|
|
96
|
-
method: 'POST',
|
|
97
|
-
baseURL: TOKEN_ENDPOINT,
|
|
98
|
-
headers: {
|
|
99
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
100
|
-
'Content-Length': tokenData.toString().length,
|
|
101
|
-
'Authorization': 'Basic aG9tZW1vYmlsZTo=',
|
|
102
|
-
},
|
|
103
|
-
});
|
|
48
|
+
const tokenData = new URLSearchParams({
|
|
49
|
+
grant_type: 'authorization_code',
|
|
50
|
+
code,
|
|
51
|
+
redirect_uri: REDIRECT_URI,
|
|
52
|
+
client_id: CLIENT_ID,
|
|
53
|
+
code_verifier: codeVerifier,
|
|
54
|
+
});
|
|
104
55
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
56
|
+
try {
|
|
57
|
+
const res = await this.axiosInstance.post(
|
|
58
|
+
TOKEN_ENDPOINT,
|
|
59
|
+
tokenData.toString(),
|
|
60
|
+
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
this.emit('warn', `Tokens: ${JSON.stringify(res.data)}`);
|
|
64
|
+
return res.data;
|
|
108
65
|
} catch (error) {
|
|
109
|
-
throw new Error(`
|
|
66
|
+
throw new Error(`Token request failed: ${error.response?.data || error.message}`);
|
|
110
67
|
}
|
|
111
68
|
}
|
|
112
69
|
|
|
113
70
|
async loginToMelCloudHome(url) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const cookies = getResp.headers['set-cookie'] || [];
|
|
126
|
-
const dom = new JSDOM(getResp.data);
|
|
127
|
-
const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.value;
|
|
128
|
-
if (!csrf) {
|
|
129
|
-
if (this.logWarn) this.emit('warn', `CSRF token not found`);
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Step 2: Przygotuj dane logowania
|
|
134
|
-
const formData = new URLSearchParams({
|
|
135
|
-
_csrf: csrf,
|
|
136
|
-
username: this.user,
|
|
137
|
-
password: this.passwd,
|
|
138
|
-
});
|
|
71
|
+
const resp = await this.axiosInstance.get(url);
|
|
72
|
+
const dom = new JSDOM(resp.data);
|
|
73
|
+
const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.value;
|
|
74
|
+
if (!csrf) throw new Error('No CSRF token found');
|
|
75
|
+
|
|
76
|
+
const formData = new URLSearchParams({
|
|
77
|
+
_csrf: csrf,
|
|
78
|
+
username: this.user,
|
|
79
|
+
password: this.passwd,
|
|
80
|
+
});
|
|
139
81
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
baseURL: url,
|
|
146
|
-
headers: {
|
|
147
|
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
148
|
-
'Accept-Language': 'en-US,en;q=0.9',
|
|
149
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
150
|
-
'Content-Length': formData.toString().length,
|
|
151
|
-
'Cookie': cookies,
|
|
152
|
-
'Origin': 'https://live-melcloudhome.auth.eu-west-1.amazoncognito.com',
|
|
153
|
-
'Referer': url,
|
|
154
|
-
},
|
|
155
|
-
maxRedirects: 10,
|
|
156
|
-
validateStatus: status => [200, 302, 400].includes(status),
|
|
82
|
+
let next = url;
|
|
83
|
+
for (let i = 0; i < 10 && next; i++) {
|
|
84
|
+
const res = await this.axiosInstance.post(next, formData.toString(), {
|
|
85
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
86
|
+
validateStatus: s => [200, 302].includes(s),
|
|
157
87
|
});
|
|
158
88
|
|
|
159
|
-
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (this.logWarn) this.emit('warn', `Response headers: ${response.headers}`);
|
|
165
|
-
|
|
166
|
-
const match = redirectUrl.match(/[?&]code=([^&]+)/);
|
|
167
|
-
if (match) {
|
|
168
|
-
const code = match[1];
|
|
169
|
-
if (this.logWarn) this.emit('warn', `Redirect URL found code: ${code}`);
|
|
170
|
-
return code;
|
|
171
|
-
} else {
|
|
172
|
-
if (this.logWarn) this.emit('warn', `Redirect URL found, but no "code=" param: ${redirectUrl}`);
|
|
173
|
-
return null;
|
|
174
|
-
}
|
|
89
|
+
next = res.headers.location;
|
|
90
|
+
if (next && next.includes('code=')) {
|
|
91
|
+
const code = new URL(next).searchParams.get('code');
|
|
92
|
+
this.emit('warn', `Code found: ${code}`);
|
|
93
|
+
return code;
|
|
175
94
|
}
|
|
176
|
-
|
|
177
|
-
return null;
|
|
178
|
-
} catch (error) {
|
|
179
|
-
throw new Error(`Login to MELCloud Home error: ${error}`);
|
|
180
95
|
}
|
|
96
|
+
throw new Error('Authorization code not found');
|
|
181
97
|
}
|
|
182
98
|
|
|
183
99
|
async buildAuthorizeUrl() {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const data = { codeVerifier: pkce.verifier, url: loginPage.url }
|
|
198
|
-
return data;
|
|
199
|
-
} catch (error) {
|
|
200
|
-
throw new Error(`Failed to obtain OAuth link: ${error}`);
|
|
201
|
-
}
|
|
100
|
+
const pkce = this.generatePKCE();
|
|
101
|
+
const state = this.generateState();
|
|
102
|
+
const authUrl = new URL(AUTHORIZE_ENDPOINT);
|
|
103
|
+
authUrl.searchParams.set('client_id', CLIENT_ID);
|
|
104
|
+
authUrl.searchParams.set('redirect_uri', REDIRECT_URI);
|
|
105
|
+
authUrl.searchParams.set('response_type', 'code');
|
|
106
|
+
authUrl.searchParams.set('scope', SCOPE);
|
|
107
|
+
authUrl.searchParams.set('code_challenge', pkce.challenge);
|
|
108
|
+
authUrl.searchParams.set('code_challenge_method', 'S256');
|
|
109
|
+
authUrl.searchParams.set('state', state);
|
|
110
|
+
|
|
111
|
+
const loginPage = await this.getLoginPage(authUrl.toString());
|
|
112
|
+
return { codeVerifier: pkce.verifier, url: loginPage.url };
|
|
202
113
|
}
|
|
203
114
|
}
|
|
204
115
|
|
|
@@ -208,3 +119,4 @@ export default MelCloudHomeToken;
|
|
|
208
119
|
|
|
209
120
|
|
|
210
121
|
|
|
122
|
+
|