homebridge-melcloud-control 4.0.0-beta.248 → 4.0.0-beta.249

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.248",
4
+ "version": "4.0.0-beta.249",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -93,7 +93,7 @@ class MelCloudHomeToken {
93
93
 
94
94
  // --- Submit login and handle form_post ---
95
95
  async submitLogin(form, email, password, cookies, loginPageUrl) {
96
- console.log('Submitting login credentials...');
96
+ console.log('Submitting login credentials...');
97
97
 
98
98
  // Step 1: Collect all hidden inputs
99
99
  const formData = new URLSearchParams();
@@ -149,30 +149,49 @@ class MelCloudHomeToken {
149
149
 
150
150
  // --- Exchange code for tokens ---
151
151
  async exchangeCodeForTokens(code, codeVerifier) {
152
- console.log('Exchanging authorization code for tokens...');
152
+ console.log('Exchanging authorization code for tokens...');
153
153
 
154
+ // Dane POST do token endpoint
154
155
  const tokenData = new URLSearchParams({
155
156
  grant_type: 'authorization_code',
156
157
  code,
157
- redirect_uri: REDIRECT_URI,
158
+ redirect_uri: REDIRECT_URI, // musi być dokładnie ten sam co w authorize URL
158
159
  client_id: CLIENT_ID,
159
160
  code_verifier: codeVerifier,
160
161
  });
161
162
 
163
+ // Wysyłamy POST bez Basic Auth (PKCE wystarczy)
162
164
  const resp = await this.httpsRequest(TOKEN_ENDPOINT, {
163
165
  method: 'POST',
164
166
  headers: {
165
167
  'Content-Type': 'application/x-www-form-urlencoded',
166
168
  'Content-Length': tokenData.toString().length,
167
- 'Authorization': 'Basic aG9tZW1vYmlsZTo=', // base64(homemobile:)
168
169
  }
169
170
  }, tokenData.toString());
170
171
 
171
- const tokens = JSON.parse(resp.html);
172
- if (tokens.error) throw new Error(`Token exchange failed: ${tokens.error_description || tokens.error}`);
173
- return tokens;
172
+ // Parsujemy odpowiedź
173
+ let tokens;
174
+ try {
175
+ tokens = JSON.parse(resp.html);
176
+ } catch (err) {
177
+ console.log('Failed to parse token response:', resp.html);
178
+ throw err;
179
+ }
180
+
181
+ if (tokens.error) {
182
+ console.log('Token response error:', tokens);
183
+ throw new Error(`Token exchange failed: ${tokens.error_description || tokens.error}`);
184
+ }
185
+
186
+ return {
187
+ accessToken: tokens.access_token,
188
+ refreshToken: tokens.refresh_token,
189
+ expiresIn: tokens.expires_in,
190
+ idToken: tokens.id_token,
191
+ };
174
192
  }
175
193
 
194
+
176
195
  // --- Main flow ---
177
196
  async getTokensWithCredentials(email, password) {
178
197
  console.log('Obtaining OAuth tokens automatically...');
@@ -197,7 +216,7 @@ class MelCloudHomeToken {
197
216
 
198
217
  // 3️⃣ Exchange code for tokens
199
218
  const tokens = await this.exchangeCodeForTokens(authCode, pkce.verifier);
200
- console.log('✓ Successfully obtained OAuth tokens');
219
+ console.log('✓ Successfully obtained OAuth tokens');
201
220
  return tokens;
202
221
  }
203
222
  }