homebridge-melcloud-control 4.0.0-beta.403 → 4.0.0-beta.405
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 +155 -125
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.405",
|
|
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
|
@@ -7,177 +7,207 @@ import EventEmitter from 'events';
|
|
|
7
7
|
const MOBILE_USER_AGENT = 'MonitorAndControl.App.Mobile/35 CFNetwork/3860.100.1 Darwin/25.0.0';
|
|
8
8
|
const CLIENT_ID = 'homemobile';
|
|
9
9
|
const REDIRECT_URI = 'melcloudhome://';
|
|
10
|
-
const SCOPE = 'openid profile';
|
|
10
|
+
const SCOPE = 'openid profile email';
|
|
11
|
+
const AUTH_BASE = 'https://live-melcloudhome.auth.eu-west-1.amazoncognito.com';
|
|
12
|
+
const TOKEN_URL = `${AUTH_BASE}/oauth2/token`;
|
|
13
|
+
const AUTHORIZE_URL = `${AUTH_BASE}/login?`;
|
|
11
14
|
|
|
12
15
|
class MelCloudHomeToken extends EventEmitter {
|
|
13
16
|
constructor() {
|
|
14
17
|
super();
|
|
15
18
|
this.cookieJar = new CookieJar();
|
|
19
|
+
this.http = wrapper(axios.create({ jar: this.cookieJar }));
|
|
16
20
|
}
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const html = await this._get(authUrl);
|
|
28
|
-
const csrf = this._extractCsrf(html);
|
|
29
|
-
if (!csrf) throw new Error('Cannot find CSRF token');
|
|
30
|
-
|
|
31
|
-
console.log('[OAuth] Submitting credentials...');
|
|
32
|
-
|
|
33
|
-
const form = new URLSearchParams({
|
|
34
|
-
_csrf: csrf,
|
|
35
|
-
username: email,
|
|
36
|
-
password: password,
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const loginResp = await this._post('https://live-melcloudhome.auth.eu-west-1.amazoncognito.com/login', form);
|
|
40
|
-
|
|
41
|
-
if (loginResp.status === 302 && loginResp.headers.location) {
|
|
42
|
-
const nextUrl = this._resolveUrl('https://live-melcloudhome.auth.eu-west-1.amazoncognito.com', loginResp.headers.location);
|
|
43
|
-
console.log('[OAuth] Redirecting to:', nextUrl);
|
|
44
|
-
return await this._followRedirects(nextUrl, verifier);
|
|
45
|
-
} else if (loginResp.status === 200) {
|
|
46
|
-
throw new Error('Login failed — probably wrong credentials');
|
|
47
|
-
} else {
|
|
48
|
-
throw new Error(`Unexpected login status: ${loginResp.status}`);
|
|
49
|
-
}
|
|
22
|
+
// --- Utility: generate PKCE pair ---
|
|
23
|
+
_generatePkcePair() {
|
|
24
|
+
const verifier = crypto.randomBytes(32).toString('base64url');
|
|
25
|
+
const challenge = crypto
|
|
26
|
+
.createHash('sha256')
|
|
27
|
+
.update(verifier)
|
|
28
|
+
.digest('base64url');
|
|
29
|
+
return { verifier, challenge };
|
|
50
30
|
}
|
|
51
31
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
if (resp.status === 302 && resp.headers.location) {
|
|
58
|
-
const nextUrl = this._resolveUrl(url, resp.headers.location);
|
|
59
|
-
console.log(`[OAuth] Redirect (${resp.status}) to: ${nextUrl}`);
|
|
60
|
-
return await this._followRedirects(nextUrl, verifier, depth + 1);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (typeof resp.data === 'string') {
|
|
64
|
-
const code = this._extractAuthCode(resp.data);
|
|
65
|
-
if (code) {
|
|
66
|
-
console.log('[OAuth] Got authorization code');
|
|
67
|
-
return await this._exchangeToken(code, verifier);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
throw new Error(`No authorization code found (status ${resp.status})`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async _exchangeToken(code, verifier) {
|
|
75
|
-
console.log('[OAuth] Exchanging code for tokens...');
|
|
76
|
-
|
|
77
|
-
const tokenUrl = 'https://auth.melcloudhome.com/connect/token';
|
|
78
|
-
const data = new URLSearchParams({
|
|
79
|
-
grant_type: 'authorization_code',
|
|
80
|
-
client_id: CLIENT_ID,
|
|
81
|
-
code: code,
|
|
82
|
-
redirect_uri: REDIRECT_URI,
|
|
83
|
-
code_verifier: verifier,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const resp = await axios.post(tokenUrl, data.toString(), {
|
|
87
|
-
headers: {
|
|
88
|
-
'User-Agent': MOBILE_USER_AGENT,
|
|
89
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
90
|
-
'Authorization': 'Basic aG9tZW1vYmlsZTo=',
|
|
91
|
-
},
|
|
92
|
-
validateStatus: () => true,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
if (resp.status !== 200) {
|
|
96
|
-
console.error('[OAuth] Token exchange failed:', resp.status, resp.data);
|
|
97
|
-
throw new Error(`Token exchange failed with status ${resp.status}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
console.log('[OAuth] Tokens received!');
|
|
101
|
-
return {
|
|
102
|
-
accessToken: resp.data.access_token,
|
|
103
|
-
refreshToken: resp.data.refresh_token,
|
|
104
|
-
expiresIn: resp.data.expires_in,
|
|
105
|
-
};
|
|
32
|
+
// --- Utility: extract CSRF token from HTML ---
|
|
33
|
+
_extractCsrf(html) {
|
|
34
|
+
if (typeof html !== 'string') return null;
|
|
35
|
+
const match = html.match(/name=['"]_csrf['"][^>]*value=['"]([^'"]+)['"]/i);
|
|
36
|
+
return match ? match[1] : null;
|
|
106
37
|
}
|
|
107
38
|
|
|
108
|
-
|
|
39
|
+
// --- GET request with redirect + cookie management ---
|
|
40
|
+
async _get(url, depth = 0) {
|
|
109
41
|
const cookies = await this.cookieJar.getCookieString(url);
|
|
110
|
-
const resp = await
|
|
42
|
+
const resp = await this.http.get(url, {
|
|
111
43
|
headers: {
|
|
112
44
|
'User-Agent': MOBILE_USER_AGENT,
|
|
113
|
-
|
|
114
|
-
|
|
45
|
+
Accept:
|
|
46
|
+
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
47
|
+
Cookie: cookies,
|
|
115
48
|
},
|
|
116
|
-
validateStatus: () => true,
|
|
117
49
|
maxRedirects: 0,
|
|
50
|
+
validateStatus: () => true,
|
|
118
51
|
responseType: 'text',
|
|
119
|
-
transformResponse: [(
|
|
52
|
+
transformResponse: [(d) => d],
|
|
120
53
|
});
|
|
54
|
+
|
|
55
|
+
// Save cookies
|
|
121
56
|
if (resp.headers['set-cookie']) {
|
|
122
|
-
await
|
|
57
|
+
await Promise.all(
|
|
58
|
+
resp.headers['set-cookie'].map((c) =>
|
|
59
|
+
this.cookieJar.setCookie(c, url)
|
|
60
|
+
)
|
|
61
|
+
);
|
|
123
62
|
}
|
|
63
|
+
|
|
64
|
+
// Follow redirect manually
|
|
65
|
+
if (
|
|
66
|
+
resp.status >= 300 &&
|
|
67
|
+
resp.status < 400 &&
|
|
68
|
+
resp.headers.location &&
|
|
69
|
+
depth < 5
|
|
70
|
+
) {
|
|
71
|
+
const nextUrl = new URL(resp.headers.location, url).href;
|
|
72
|
+
console.log(`[OAuth] Redirect → ${nextUrl}`);
|
|
73
|
+
return this._get(nextUrl, depth + 1);
|
|
74
|
+
}
|
|
75
|
+
|
|
124
76
|
return resp;
|
|
125
77
|
}
|
|
126
78
|
|
|
127
|
-
|
|
79
|
+
// --- POST request helper ---
|
|
80
|
+
async _post(url, data, headers = {}) {
|
|
128
81
|
const cookies = await this.cookieJar.getCookieString(url);
|
|
129
|
-
const resp = await
|
|
82
|
+
const resp = await this.http.post(url, data, {
|
|
130
83
|
headers: {
|
|
131
84
|
'User-Agent': MOBILE_USER_AGENT,
|
|
132
85
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
'Content-Length': Buffer.byteLength(formData.toString()),
|
|
86
|
+
Cookie: cookies,
|
|
87
|
+
...headers,
|
|
136
88
|
},
|
|
137
|
-
validateStatus: () => true,
|
|
138
89
|
maxRedirects: 0,
|
|
90
|
+
validateStatus: () => true,
|
|
139
91
|
responseType: 'text',
|
|
140
|
-
transformResponse: [(
|
|
92
|
+
transformResponse: [(d) => d],
|
|
141
93
|
});
|
|
94
|
+
|
|
142
95
|
if (resp.headers['set-cookie']) {
|
|
143
|
-
await
|
|
96
|
+
await Promise.all(
|
|
97
|
+
resp.headers['set-cookie'].map((c) =>
|
|
98
|
+
this.cookieJar.setCookie(c, url)
|
|
99
|
+
)
|
|
100
|
+
);
|
|
144
101
|
}
|
|
102
|
+
|
|
145
103
|
return resp;
|
|
146
104
|
}
|
|
147
105
|
|
|
106
|
+
// --- Main login flow ---
|
|
107
|
+
async login(email, password) {
|
|
108
|
+
const { verifier, challenge } = this._generatePkcePair();
|
|
109
|
+
|
|
110
|
+
// Build authorize URL
|
|
111
|
+
const authUrl =
|
|
112
|
+
`${AUTH_BASE}/oauth2/authorize?` +
|
|
113
|
+
new URLSearchParams({
|
|
114
|
+
client_id: CLIENT_ID,
|
|
115
|
+
redirect_uri: REDIRECT_URI,
|
|
116
|
+
response_type: 'code',
|
|
117
|
+
scope: SCOPE,
|
|
118
|
+
code_challenge: challenge,
|
|
119
|
+
code_challenge_method: 'S256',
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
console.log(`[OAuth] Opening login page...`);
|
|
123
|
+
let resp = await this._get(authUrl);
|
|
124
|
+
if (resp.status >= 300 && resp.status < 400) {
|
|
125
|
+
const redirectUrl = new URL(resp.headers.location, AUTH_BASE).href;
|
|
126
|
+
console.log(`[OAuth] Redirected to: ${redirectUrl}`);
|
|
127
|
+
resp = await this._get(redirectUrl);
|
|
128
|
+
}
|
|
148
129
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
130
|
+
// --- Extract CSRF token ---
|
|
131
|
+
const csrf = this._extractCsrf(resp.data);
|
|
132
|
+
if (!csrf) {
|
|
133
|
+
console.error('[OAuth] ❌ Cannot find CSRF token!');
|
|
134
|
+
console.log(resp.data.substring(0, 400));
|
|
135
|
+
throw new Error('Cannot find CSRF token');
|
|
136
|
+
}
|
|
154
137
|
|
|
155
|
-
|
|
156
|
-
if (typeof html !== 'string') return null;
|
|
157
|
-
const urlMatch = html.match(/[?&]code=([^&]+)/);
|
|
158
|
-
if (urlMatch) return urlMatch[1];
|
|
159
|
-
const formMatch = html.match(/name="code"\s+value="([^"]+)"/);
|
|
160
|
-
return formMatch ? formMatch[1] : null;
|
|
161
|
-
}
|
|
138
|
+
console.log(`[OAuth] Found CSRF token: ${csrf}`);
|
|
162
139
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
140
|
+
// --- Submit login form ---
|
|
141
|
+
const loginUrl = resp.request.res.responseUrl || `${AUTH_BASE}/login`;
|
|
142
|
+
const formData = new URLSearchParams({
|
|
143
|
+
_csrf: csrf,
|
|
144
|
+
username: email,
|
|
145
|
+
password: password,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
console.log(`[OAuth] Submitting login form...`);
|
|
149
|
+
const loginResp = await this._post(loginUrl, formData.toString());
|
|
166
150
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
151
|
+
if (loginResp.status >= 300 && loginResp.status < 400) {
|
|
152
|
+
const redirectUrl = new URL(loginResp.headers.location, AUTH_BASE).href;
|
|
153
|
+
console.log(`[OAuth] Login redirect → ${redirectUrl}`);
|
|
154
|
+
|
|
155
|
+
// Extract code from redirect URL
|
|
156
|
+
const codeMatch = redirectUrl.match(/[?&]code=([^&]+)/);
|
|
157
|
+
if (!codeMatch) throw new Error('No code found after login redirect');
|
|
158
|
+
const code = codeMatch[1];
|
|
159
|
+
console.log(`[OAuth] ✅ Got authorization code: ${code}`);
|
|
160
|
+
|
|
161
|
+
// Exchange code for token
|
|
162
|
+
return this.exchangeToken(code, verifier);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
console.error('[OAuth] ❌ Unexpected status on login:', loginResp.status);
|
|
166
|
+
console.log(loginResp.data.substring(0, 400));
|
|
167
|
+
throw new Error('Unexpected status on login page');
|
|
170
168
|
}
|
|
171
169
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
170
|
+
// --- Exchange code for access token ---
|
|
171
|
+
async exchangeToken(code, verifier) {
|
|
172
|
+
console.log('[OAuth] Exchanging code for token...');
|
|
173
|
+
|
|
174
|
+
const body = new URLSearchParams({
|
|
175
|
+
grant_type: 'authorization_code',
|
|
176
|
+
client_id: CLIENT_ID,
|
|
177
|
+
redirect_uri: REDIRECT_URI,
|
|
178
|
+
code,
|
|
179
|
+
code_verifier: verifier,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const resp = await this.http.post(TOKEN_URL, body.toString(), {
|
|
183
|
+
headers: {
|
|
184
|
+
'User-Agent': MOBILE_USER_AGENT,
|
|
185
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
if (resp.status !== 200) {
|
|
190
|
+
console.error('[OAuth] ❌ Token exchange failed:', resp.status);
|
|
191
|
+
console.log(resp.data);
|
|
192
|
+
throw new Error('Token exchange failed');
|
|
177
193
|
}
|
|
194
|
+
|
|
195
|
+
console.log('[OAuth] ✅ Token response received');
|
|
196
|
+
return resp.data;
|
|
178
197
|
}
|
|
179
198
|
}
|
|
180
199
|
|
|
200
|
+
// --- Example usage ---
|
|
201
|
+
(async () => {
|
|
202
|
+
try {
|
|
203
|
+
const mel = new MelCloudHomeOAuth();
|
|
204
|
+
const token = await mel.login('YOUR_EMAIL', 'YOUR_PASSWORD');
|
|
205
|
+
console.log('✅ Final token:', token);
|
|
206
|
+
} catch (err) {
|
|
207
|
+
console.error('iDom Home, Connect error:', err.message);
|
|
208
|
+
}
|
|
209
|
+
})();
|
|
210
|
+
|
|
181
211
|
|
|
182
212
|
export default MelCloudHomeToken;
|
|
183
213
|
|