homebridge-melcloud-control 4.0.0-beta.247 → 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 +1 -1
- package/src/melcloudhometoken.js +29 -10
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.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",
|
package/src/melcloudhometoken.js
CHANGED
|
@@ -75,7 +75,7 @@ class MelCloudHomeToken {
|
|
|
75
75
|
|
|
76
76
|
// --- Get login page ---
|
|
77
77
|
async getLoginPage(authUrl) {
|
|
78
|
-
|
|
78
|
+
console.log('Getting login page...');
|
|
79
79
|
const resp = await this.httpsRequest(authUrl, {
|
|
80
80
|
method: 'GET',
|
|
81
81
|
headers: {
|
|
@@ -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
|
-
|
|
96
|
+
console.log('Submitting login credentials...');
|
|
97
97
|
|
|
98
98
|
// Step 1: Collect all hidden inputs
|
|
99
99
|
const formData = new URLSearchParams();
|
|
@@ -149,33 +149,52 @@ class MelCloudHomeToken {
|
|
|
149
149
|
|
|
150
150
|
// --- Exchange code for tokens ---
|
|
151
151
|
async exchangeCodeForTokens(code, codeVerifier) {
|
|
152
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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...');
|
|
179
198
|
|
|
180
199
|
const pkce = this.generatePKCE();
|
|
181
200
|
const state = this.generateState();
|
|
@@ -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
|
-
|
|
219
|
+
console.log('✓ Successfully obtained OAuth tokens');
|
|
201
220
|
return tokens;
|
|
202
221
|
}
|
|
203
222
|
}
|