homebridge-melcloud-control 4.0.0-beta.290 → 4.0.0-beta.291
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 +40 -22
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.291",
|
|
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
|
@@ -96,44 +96,62 @@ class MelCloudHomeToken {
|
|
|
96
96
|
*/
|
|
97
97
|
async loginWithCredentials(email, password) {
|
|
98
98
|
const authorizeUrl = await this.buildAuthorizeUrl();
|
|
99
|
-
|
|
99
|
+
console.log('Fetching login page...');
|
|
100
100
|
const res = await this.client.get(authorizeUrl);
|
|
101
101
|
|
|
102
102
|
const $ = cheerio.load(res.data);
|
|
103
103
|
const form = $('form');
|
|
104
|
-
|
|
104
|
+
if (!form.length) throw new Error('Login form not found on page');
|
|
105
|
+
|
|
106
|
+
// ✅ dynamicznie pobieramy action z formularza
|
|
107
|
+
let action = form.attr('action');
|
|
108
|
+
if (!action) throw new Error('Form action not found');
|
|
109
|
+
if (!action.startsWith('http')) {
|
|
110
|
+
action = new URL(action, 'https://auth.melcloudhome.com').toString();
|
|
111
|
+
}
|
|
112
|
+
|
|
105
113
|
const requestVerificationToken = $('input[name="__RequestVerificationToken"]').val();
|
|
106
114
|
const returnUrl = $('input[name="ReturnUrl"]').val() || '';
|
|
107
115
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
116
|
+
const body = new URLSearchParams({
|
|
117
|
+
Username: email,
|
|
118
|
+
Password: password,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (requestVerificationToken) {
|
|
122
|
+
body.set('__RequestVerificationToken', requestVerificationToken);
|
|
123
|
+
}
|
|
124
|
+
if (returnUrl) {
|
|
125
|
+
body.set('ReturnUrl', returnUrl);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
console.log(`Submitting credentials to: ${action}`);
|
|
129
|
+
|
|
130
|
+
const loginRes = await this.client.post(action, body, {
|
|
131
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
132
|
+
maxRedirects: 0,
|
|
133
|
+
validateStatus: status => status < 400 || status === 302 || status === 500,
|
|
134
|
+
});
|
|
123
135
|
|
|
124
136
|
let html = loginRes.data;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
137
|
+
|
|
138
|
+
if (loginRes.headers.location) {
|
|
139
|
+
console.log(`Redirecting to: ${loginRes.headers.location}`);
|
|
140
|
+
const redirectRes = await this.client.get(loginRes.headers.location);
|
|
141
|
+
html = redirectRes.data;
|
|
128
142
|
}
|
|
129
143
|
|
|
130
144
|
const code = await this.extractCodeFromHtml(html);
|
|
131
|
-
if (!code)
|
|
145
|
+
if (!code) {
|
|
146
|
+
console.log('Authorization code not found in HTML');
|
|
147
|
+
throw new Error('Authorization code not found in HTML');
|
|
148
|
+
}
|
|
132
149
|
|
|
133
|
-
|
|
150
|
+
console.log(`Found authorization code: ${code}`);
|
|
134
151
|
|
|
135
152
|
return await this.exchangeCodeForTokens(code);
|
|
136
153
|
}
|
|
154
|
+
|
|
137
155
|
}
|
|
138
156
|
|
|
139
157
|
export default MelCloudHomeToken;
|