homebridge-melcloud-control 4.0.0-beta.360 → 4.0.0-beta.361

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.0.0-beta.360",
4
+ "version": "4.0.0-beta.361",
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
@@ -298,7 +298,7 @@ class MelCloud extends EventEmitter {
298
298
  const url = data.url;
299
299
  const codeVerifier = data.codeVerifier;
300
300
  const code = await melCloudHomeToken.loginToMelCloudHome(url);
301
- //const token = await melCloudHomeToken.getTokens(code, codeVerifier);
301
+ const token = await melCloudHomeToken.getTokens(code, codeVerifier);
302
302
 
303
303
  const accountInfo = { ContextKey: code, UseFahrenheit: false };
304
304
  this.contextKey = code;
@@ -94,7 +94,7 @@ class MelCloudHomeToken extends EventEmitter {
94
94
  code_verifier: codeVerifier,
95
95
  });
96
96
 
97
- const tokenResponse = await axios.post(TOKEN_ENDPOINT, tokenData.toString(), {
97
+ const tokenResponse = await this.client.post(TOKEN_ENDPOINT, tokenData.toString(), {
98
98
  headers: {
99
99
  'Content-Type': 'application/x-www-form-urlencoded',
100
100
  'Authorization': 'Basic aG9tZW1vYmlsZTo=',
@@ -111,58 +111,70 @@ class MelCloudHomeToken extends EventEmitter {
111
111
 
112
112
  async loginToMelCloudHome(url) {
113
113
  try {
114
-
115
- const jar = new CookieJar();
116
- const client = wrapper(axios.create({ jar, withCredentials: true }));
117
- client.defaults.headers['User-Agent'] = MOBILE_USER_AGENT;
118
-
119
- // --- Krok 1: GET strony logowania ---
120
- const getResp = await client.get(url, {
121
- headers: { 'User-Agent': MOBILE_USER_AGENT }
114
+ // Step 1: Pobierz stronę logowania, żeby uzyskać sesję + token _csrf
115
+ const getResp = await this.client.get(url, {
116
+ headers: {
117
+ 'User-Agent': MOBILE_USER_AGENT,
118
+ 'Accept': 'text/html',
119
+ },
120
+ withCredentials: true,
122
121
  });
123
122
 
123
+ const cookies = getResp.headers['set-cookie'] || [];
124
124
  const dom = new JSDOM(getResp.data);
125
- const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.getAttribute('value');
126
- if (!csrf) throw new Error('CSRF token not found');
125
+ const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.value;
126
+ if (!csrf) {
127
+ if (this.logWarn) this.emit('warn', `CSRF token not found`);
128
+ return null;
129
+ }
127
130
 
128
- // --- Krok 2: POST login ---
131
+ // Step 2: Przygotuj dane logowania
129
132
  const formData = new URLSearchParams({
130
133
  _csrf: csrf,
131
134
  username: this.user,
132
- password: this.passwd
133
- }).toString();
135
+ password: this.passwd,
136
+ });
137
+
138
+ if (this.logWarn) this.emit('warn', `Redirect URL: ${url}`);
134
139
 
135
- const postResp = await client.post(url, formData, {
140
+ // Step 3: Wyślij POST z danymi logowania
141
+ const response = await this.client.post(url, formData.toString(), {
136
142
  headers: {
137
143
  'User-Agent': MOBILE_USER_AGENT,
144
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
145
+ 'Accept-Language': 'en-US,en;q=0.9',
138
146
  'Content-Type': 'application/x-www-form-urlencoded',
147
+ 'Content-Length': formData.toString().length,
148
+ 'Cookie': cookies,
139
149
  'Origin': 'https://live-melcloudhome.auth.eu-west-1.amazoncognito.com',
140
150
  'Referer': url,
141
151
  },
142
152
  maxRedirects: 0,
143
- validateStatus: status => [200, 302, 400].includes(status)
153
+ validateStatus: status => [200, 302, 400].includes(status),
144
154
  });
145
155
 
146
- if (postResp.status === 400) throw new Error('Login failed');
147
-
148
- // --- Krok 3: GET dashboard po zalogowaniu ---
149
- const dashboardResp = await client.get('https://melcloudhome.com/dashboard', {
150
- headers: { 'User-Agent': MOBILE_USER_AGENT }
151
- });
152
156
 
153
- this.emit('warn', `1 ${dashboardResp.data}, ${dashboardResp.headers}`);
157
+ if (response.status === 302 && response.headers.location) {
158
+ // Login success — mamy redirect z "code="
159
+ const redirectUrl = response.headers.location;
160
+ if (this.logWarn) this.emit('warn', `Redirect URL: ${redirectUrl}`);
154
161
 
155
- // --- Krok 4: Pobierz cookies z dashboard ---
156
- const dashboardCookies = await jar.getCookies('https://melcloudhome.com/dashboard');
157
- this.emit('warn', `2 ${dashboardCookies}`);
158
162
 
159
- // Możemy zwrócić jako string do użycia w nagłówku:
160
- const cookieHeader = dashboardCookies.map(c => `${c.key}=${c.value}`).join('; ');
161
163
 
162
- this.emit('warn', `Cookies ${cookieHeader}`);
164
+ const match = redirectUrl.match(/[?&]code=([^&]+)/);
165
+ if (match) {
166
+ const code = match[1];
167
+ if (this.logWarn) this.emit('warn', `Redirect URL found code: ${code}`);
168
+ return code;
169
+ } else {
170
+ if (this.logWarn) this.emit('warn', `Redirect URL found, but no "code=" param: ${redirectUrl}`);
171
+ return null;
172
+ }
173
+ }
163
174
 
164
- return dashboardCookies.map(c => `${c.key}=${c.value}`);
175
+ if (response.status === 400) if (this.logWarn) this.emit('warn', `Login failed (bad request or invalid credentials: ${response.data}`);
165
176
 
177
+ return null;
166
178
  } catch (error) {
167
179
  throw new Error(`Login to MELCloud Home error: ${error}`);
168
180
  }