@gooin/garmin-connect 1.6.9 → 1.6.11
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/dist/common/HttpClient.d.ts +38 -37
- package/dist/common/HttpClient.js +360 -332
- package/dist/common/HttpClient.js.map +1 -1
- package/dist/garmin/GarminConnect.d.ts +73 -84
- package/dist/garmin/GarminConnect.js +467 -470
- package/dist/garmin/GarminConnect.js.map +1 -1
- package/dist/garmin/UrlClass.d.ts +43 -43
- package/dist/garmin/UrlClass.js +124 -124
- package/dist/garmin/common/DateUtils.d.ts +6 -6
- package/dist/garmin/common/DateUtils.js +37 -37
- package/dist/garmin/common/HydrationUtils.d.ts +2 -2
- package/dist/garmin/common/HydrationUtils.js +15 -15
- package/dist/garmin/common/WeightUtils.d.ts +1 -1
- package/dist/garmin/common/WeightUtils.js +8 -8
- package/dist/garmin/types/activity.d.ts +408 -408
- package/dist/garmin/types/activity.js +26 -26
- package/dist/garmin/types/course.d.ts +146 -146
- package/dist/garmin/types/course.js +2 -2
- package/dist/garmin/types/golf.d.ts +67 -67
- package/dist/garmin/types/golf.js +2 -2
- package/dist/garmin/types/heartrate.d.ts +22 -22
- package/dist/garmin/types/heartrate.js +1 -1
- package/dist/garmin/types/hydration.d.ts +28 -28
- package/dist/garmin/types/hydration.js +1 -1
- package/dist/garmin/types/index.d.ts +558 -541
- package/dist/garmin/types/index.js +35 -16
- package/dist/garmin/types/index.js.map +1 -1
- package/dist/garmin/types/sleep.d.ts +120 -120
- package/dist/garmin/types/sleep.js +2 -2
- package/dist/garmin/types/weight.d.ts +42 -42
- package/dist/garmin/types/weight.js +2 -2
- package/dist/garmin/workouts/Running.d.ts +16 -16
- package/dist/garmin/workouts/Running.js +46 -46
- package/dist/garmin/workouts/templates/RunningTemplate.d.ts +68 -68
- package/dist/garmin/workouts/templates/RunningTemplate.js +77 -77
- package/dist/index.d.ts +1 -1
- package/dist/index.js +8 -8
- package/dist/utils.d.ts +3 -3
- package/dist/utils.js +36 -36
- package/package.json +3 -2
- package/dist/common/CFClient.d.ts +0 -22
- package/dist/common/CFClient.js +0 -137
- package/dist/common/CFClient.js.map +0 -1
- package/dist/common/DateUtils.d.ts +0 -1
- package/dist/common/DateUtils.js +0 -11
- package/dist/common/DateUtils.js.map +0 -1
- package/dist/garmin/GarminConnect copy.d.ts +0 -76
- package/dist/garmin/GarminConnect copy.js +0 -397
- package/dist/garmin/GarminConnect copy.js.map +0 -1
- package/dist/garmin/GarminConnect_new.d.ts +0 -76
- package/dist/garmin/GarminConnect_new.js +0 -397
- package/dist/garmin/GarminConnect_new.js.map +0 -1
- package/dist/garmin/GarminConnect_old.d.ts +0 -75
- package/dist/garmin/GarminConnect_old.js +0 -386
- package/dist/garmin/GarminConnect_old.js.map +0 -1
- package/dist/garmin/Urls copy.d.ts +0 -66
- package/dist/garmin/Urls copy.js +0 -109
- package/dist/garmin/Urls copy.js.map +0 -1
- package/dist/garmin/Urls.d.ts +0 -64
- package/dist/garmin/Urls.js +0 -104
- package/dist/garmin/Urls.js.map +0 -1
- package/dist/garmin/types.d.ts +0 -927
- package/dist/garmin/types.js +0 -17
- package/dist/garmin/types.js.map +0 -1
|
@@ -1,333 +1,361 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.HttpClient = void 0;
|
|
7
|
-
const axios_1 = __importDefault(require("axios"));
|
|
8
|
-
const form_data_1 = __importDefault(require("form-data"));
|
|
9
|
-
const lodash_1 = __importDefault(require("lodash"));
|
|
10
|
-
const luxon_1 = require("luxon");
|
|
11
|
-
const oauth_1_0a_1 = __importDefault(require("oauth-1.0a"));
|
|
12
|
-
const qs_1 = __importDefault(require("qs"));
|
|
13
|
-
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
14
|
-
const CSRF_RE = new RegExp('name="_csrf"\\s+value="(.+?)"');
|
|
15
|
-
const TICKET_RE = new RegExp('ticket=([^"]+)"');
|
|
16
|
-
const ACCOUNT_LOCKED_RE = new RegExp('var statuss*=s*"([^"]*)"');
|
|
17
|
-
const PAGE_TITLE_RE = new RegExp('<title>([^<]*)</title>');
|
|
18
|
-
const USER_AGENT_CONNECTMOBILE = 'com.garmin.android.apps.connectmobile';
|
|
19
|
-
const USER_AGENT_BROWSER = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36';
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
'
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (this.oauth2Token
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
async
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
// console.log('login -
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.HttpClient = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const form_data_1 = __importDefault(require("form-data"));
|
|
9
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
10
|
+
const luxon_1 = require("luxon");
|
|
11
|
+
const oauth_1_0a_1 = __importDefault(require("oauth-1.0a"));
|
|
12
|
+
const qs_1 = __importDefault(require("qs"));
|
|
13
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
14
|
+
const CSRF_RE = new RegExp('name="_csrf"\\s+value="(.+?)"');
|
|
15
|
+
const TICKET_RE = new RegExp('ticket=([^"]+)"');
|
|
16
|
+
const ACCOUNT_LOCKED_RE = new RegExp('var statuss*=s*"([^"]*)"');
|
|
17
|
+
const PAGE_TITLE_RE = new RegExp('<title>([^<]*)</title>');
|
|
18
|
+
const USER_AGENT_CONNECTMOBILE = 'com.garmin.android.apps.connectmobile';
|
|
19
|
+
const USER_AGENT_BROWSER = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36';
|
|
20
|
+
const USER_AGENT_BROWSER_MAC = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
|
21
|
+
const OAUTH_CONSUMER_URL = 'https://thegarth.s3.amazonaws.com/oauth_consumer.json';
|
|
22
|
+
const HTTP_STATUS = {
|
|
23
|
+
UNAUTHORIZED: 401
|
|
24
|
+
};
|
|
25
|
+
let tokenRefreshPromise = null;
|
|
26
|
+
let refreshSubscribers = [];
|
|
27
|
+
class HttpClient {
|
|
28
|
+
constructor(url, config) {
|
|
29
|
+
var _a, _b;
|
|
30
|
+
this.url = url;
|
|
31
|
+
this.client = axios_1.default.create({
|
|
32
|
+
timeout: (_a = config === null || config === void 0 ? void 0 : config.timeout) !== null && _a !== void 0 ? _a : 5000,
|
|
33
|
+
timeoutErrorMessage: `Request Timeout: > ${(_b = config === null || config === void 0 ? void 0 : config.timeout) !== null && _b !== void 0 ? _b : 5000} ms`
|
|
34
|
+
/**
|
|
35
|
+
* Charles debugger: uncomment `proxy` and `httpsAgent`, then run bellow command.
|
|
36
|
+
* NODE_TLS_REJECT_UNAUTHORIZED=0 node test/sync.js
|
|
37
|
+
*/
|
|
38
|
+
// proxy: {
|
|
39
|
+
// host: '127.0.0.1',
|
|
40
|
+
// port: 8888,
|
|
41
|
+
// protocol: 'http'
|
|
42
|
+
// },
|
|
43
|
+
// httpsAgent: new (require('https').Agent)({
|
|
44
|
+
// rejectUnauthorized: false
|
|
45
|
+
// })
|
|
46
|
+
});
|
|
47
|
+
this.config = config;
|
|
48
|
+
this.client.interceptors.response.use((response) => response, async (error) => {
|
|
49
|
+
var _a;
|
|
50
|
+
if (axios_1.default.isAxiosError(error) &&
|
|
51
|
+
error.code === 'ECONNABORTED') {
|
|
52
|
+
throw new Error(error.message || 'Request Timeout');
|
|
53
|
+
}
|
|
54
|
+
const originalRequest = error.config;
|
|
55
|
+
if (((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === HTTP_STATUS.UNAUTHORIZED &&
|
|
56
|
+
!(originalRequest === null || originalRequest === void 0 ? void 0 : originalRequest._retry)) {
|
|
57
|
+
if (!this.oauth2Token) {
|
|
58
|
+
throw new Error('No OAuth2 token available');
|
|
59
|
+
}
|
|
60
|
+
originalRequest._retry = true;
|
|
61
|
+
try {
|
|
62
|
+
if (!tokenRefreshPromise) {
|
|
63
|
+
tokenRefreshPromise =
|
|
64
|
+
this.refreshOauth2Token().finally(() => {
|
|
65
|
+
tokenRefreshPromise = null;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
await tokenRefreshPromise;
|
|
69
|
+
originalRequest.headers.Authorization = `Bearer ${this.oauth2Token.access_token}`;
|
|
70
|
+
return this.client(originalRequest);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
console.error('Token refresh failed:', err);
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (axios_1.default.isAxiosError(error) && error.response) {
|
|
78
|
+
this.handleError(error.response);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// 处理没有response的情况
|
|
82
|
+
throw new Error('Network error or unknown error occurred');
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
});
|
|
86
|
+
this.client.interceptors.request.use(async (config) => {
|
|
87
|
+
if (this.oauth2Token) {
|
|
88
|
+
config.headers.Authorization =
|
|
89
|
+
'Bearer ' + this.oauth2Token.access_token;
|
|
90
|
+
}
|
|
91
|
+
return config;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
async fetchOauthConsumer() {
|
|
95
|
+
const response = await axios_1.default.get(OAUTH_CONSUMER_URL);
|
|
96
|
+
this.OAUTH_CONSUMER = {
|
|
97
|
+
key: response.data.consumer_key,
|
|
98
|
+
secret: response.data.consumer_secret
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async checkTokenVaild() {
|
|
102
|
+
if (this.oauth2Token) {
|
|
103
|
+
if (this.oauth2Token.expires_at < luxon_1.DateTime.now().toSeconds()) {
|
|
104
|
+
console.error('Token expired!');
|
|
105
|
+
await this.refreshOauth2Token();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async get(url, config) {
|
|
110
|
+
const response = await this.client.get(url, config);
|
|
111
|
+
return response === null || response === void 0 ? void 0 : response.data;
|
|
112
|
+
}
|
|
113
|
+
async post(url, data, config) {
|
|
114
|
+
const response = await this.client.post(url, data, config);
|
|
115
|
+
return response === null || response === void 0 ? void 0 : response.data;
|
|
116
|
+
}
|
|
117
|
+
async put(url, data, config) {
|
|
118
|
+
const response = await this.client.put(url, data, config);
|
|
119
|
+
return response === null || response === void 0 ? void 0 : response.data;
|
|
120
|
+
}
|
|
121
|
+
async delete(url, config) {
|
|
122
|
+
const response = await this.client.post(url, null, {
|
|
123
|
+
...config,
|
|
124
|
+
headers: {
|
|
125
|
+
...config === null || config === void 0 ? void 0 : config.headers,
|
|
126
|
+
'X-Http-Method-Override': 'DELETE'
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return response === null || response === void 0 ? void 0 : response.data;
|
|
130
|
+
}
|
|
131
|
+
setCommonHeader(headers) {
|
|
132
|
+
lodash_1.default.each(headers, (headerValue, key) => {
|
|
133
|
+
this.client.defaults.headers.common[key] = headerValue;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
handleError(response) {
|
|
137
|
+
this.handleHttpError(response);
|
|
138
|
+
}
|
|
139
|
+
handleHttpError(response) {
|
|
140
|
+
const { status, statusText, data } = response;
|
|
141
|
+
const errorMessage = {
|
|
142
|
+
status,
|
|
143
|
+
statusText,
|
|
144
|
+
data: typeof data === 'object' ? JSON.stringify(data) : data
|
|
145
|
+
};
|
|
146
|
+
console.error('HTTP Error:', errorMessage);
|
|
147
|
+
throw new Error(`HTTP Error (${status}): ${statusText}`);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Login to Garmin Connect
|
|
151
|
+
* @param username
|
|
152
|
+
* @param password
|
|
153
|
+
* @returns {Promise<HttpClient>}
|
|
154
|
+
*/
|
|
155
|
+
async login(username, password) {
|
|
156
|
+
await this.fetchOauthConsumer();
|
|
157
|
+
// Step1-3: Get ticket from page.
|
|
158
|
+
const ticket = await this.getLoginTicket(username, password);
|
|
159
|
+
// Step4: Oauth1
|
|
160
|
+
const oauth1 = await this.getOauth1Token(ticket);
|
|
161
|
+
// TODO: Handle MFA
|
|
162
|
+
// Step 5: Oauth2
|
|
163
|
+
await this.exchange(oauth1);
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
async getLoginTicket(username, password) {
|
|
167
|
+
// Step1: Set cookie
|
|
168
|
+
const step1Params = {
|
|
169
|
+
clientId: 'GarminConnect',
|
|
170
|
+
locale: 'en',
|
|
171
|
+
service: this.url.GC_MODERN
|
|
172
|
+
};
|
|
173
|
+
const step1Url = `${this.url.GARMIN_SSO_EMBED}?${qs_1.default.stringify(step1Params)}`;
|
|
174
|
+
// console.log('login - step1Url:', step1Url);
|
|
175
|
+
await this.client.get(step1Url);
|
|
176
|
+
// Step2 Get _csrf
|
|
177
|
+
const step2Params = {
|
|
178
|
+
id: 'gauth-widget',
|
|
179
|
+
embedWidget: true,
|
|
180
|
+
locale: 'en',
|
|
181
|
+
gauthHost: this.url.GARMIN_SSO_EMBED
|
|
182
|
+
};
|
|
183
|
+
const step2Url = `${this.url.SIGNIN_URL}?${qs_1.default.stringify(step2Params)}`;
|
|
184
|
+
// console.log('login - step2Url:', step2Url);
|
|
185
|
+
const step2Result = await this.get(step2Url);
|
|
186
|
+
// console.log('login - step2Result:', step2Result)
|
|
187
|
+
const csrfRegResult = CSRF_RE.exec(step2Result);
|
|
188
|
+
if (!csrfRegResult) {
|
|
189
|
+
throw new Error('login - csrf not found');
|
|
190
|
+
}
|
|
191
|
+
const csrf_token = csrfRegResult[1];
|
|
192
|
+
// console.log('login - csrf:', csrf_token);
|
|
193
|
+
// Step3 Get ticket
|
|
194
|
+
const signinParams = {
|
|
195
|
+
id: 'gauth-widget',
|
|
196
|
+
embedWidget: true,
|
|
197
|
+
clientId: 'GarminConnect',
|
|
198
|
+
locale: 'en',
|
|
199
|
+
gauthHost: this.url.GARMIN_SSO_EMBED,
|
|
200
|
+
service: this.url.GARMIN_SSO_EMBED,
|
|
201
|
+
source: this.url.GARMIN_SSO_EMBED,
|
|
202
|
+
redirectAfterAccountLoginUrl: this.url.GARMIN_SSO_EMBED,
|
|
203
|
+
redirectAfterAccountCreationUrl: this.url.GARMIN_SSO_EMBED
|
|
204
|
+
};
|
|
205
|
+
const step3Url = `${this.url.SIGNIN_URL}?${qs_1.default.stringify(signinParams)}`;
|
|
206
|
+
// console.log('login - step3Url:', step3Url);
|
|
207
|
+
const step3Form = new form_data_1.default();
|
|
208
|
+
step3Form.append('username', username);
|
|
209
|
+
step3Form.append('password', password);
|
|
210
|
+
step3Form.append('embed', 'true');
|
|
211
|
+
step3Form.append('_csrf', csrf_token);
|
|
212
|
+
const step3Result = await this.post(step3Url, step3Form, {
|
|
213
|
+
headers: {
|
|
214
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
215
|
+
Dnt: 1,
|
|
216
|
+
Origin: this.url.GARMIN_SSO_ORIGIN,
|
|
217
|
+
Referer: this.url.SIGNIN_URL,
|
|
218
|
+
'User-Agent': USER_AGENT_BROWSER
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
// console.log('step3Result:', step3Result)
|
|
222
|
+
this.handleAccountLocked(step3Result);
|
|
223
|
+
this.handlePageTitle(step3Result);
|
|
224
|
+
this.handleMFA(step3Result);
|
|
225
|
+
const ticketRegResult = TICKET_RE.exec(step3Result);
|
|
226
|
+
if (!ticketRegResult) {
|
|
227
|
+
throw new Error('login failed (Ticket not found or MFA), please check username and password');
|
|
228
|
+
}
|
|
229
|
+
const ticket = ticketRegResult[1];
|
|
230
|
+
return ticket;
|
|
231
|
+
}
|
|
232
|
+
// TODO: Handle MFA
|
|
233
|
+
handleMFA(htmlStr) { }
|
|
234
|
+
// TODO: Handle Phone number
|
|
235
|
+
handlePageTitle(htmlStr) {
|
|
236
|
+
const pageTitileRegResult = PAGE_TITLE_RE.exec(htmlStr);
|
|
237
|
+
if (pageTitileRegResult) {
|
|
238
|
+
const title = pageTitileRegResult[1];
|
|
239
|
+
console.log('login page title:', title);
|
|
240
|
+
if (lodash_1.default.includes(title, 'Update Phone Number')) {
|
|
241
|
+
// current I don't know where to update it
|
|
242
|
+
// See: https://github.com/matin/garth/issues/19
|
|
243
|
+
throw new Error('login failed (Update Phone number), please update your phone number, See: https://github.com/matin/garth/issues/19');
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
handleAccountLocked(htmlStr) {
|
|
248
|
+
const accountLockedRegResult = ACCOUNT_LOCKED_RE.exec(htmlStr);
|
|
249
|
+
if (accountLockedRegResult) {
|
|
250
|
+
const msg = accountLockedRegResult[1];
|
|
251
|
+
console.error(msg);
|
|
252
|
+
throw new Error('login failed (AccountLocked), please open connect web page to unlock your account');
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
async refreshOauth2Token() {
|
|
256
|
+
try {
|
|
257
|
+
if (!this.OAUTH_CONSUMER) {
|
|
258
|
+
await this.fetchOauthConsumer();
|
|
259
|
+
}
|
|
260
|
+
if (!this.oauth2Token || !this.oauth1Token) {
|
|
261
|
+
throw new Error('Missing required tokens for refresh');
|
|
262
|
+
}
|
|
263
|
+
const oauth1 = {
|
|
264
|
+
oauth: this.getOauthClient(this.OAUTH_CONSUMER),
|
|
265
|
+
token: this.oauth1Token
|
|
266
|
+
};
|
|
267
|
+
await this.exchange(oauth1);
|
|
268
|
+
console.log(`「${this.config.username}」in「${this.url.domain}」 OAuth2 token refreshed successfully`);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
console.error('Failed to refresh OAuth2 token:', error);
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async getOauth1Token(ticket) {
|
|
276
|
+
if (!this.OAUTH_CONSUMER) {
|
|
277
|
+
throw new Error('No OAUTH_CONSUMER');
|
|
278
|
+
}
|
|
279
|
+
const params = {
|
|
280
|
+
ticket,
|
|
281
|
+
'login-url': this.url.GARMIN_SSO_EMBED,
|
|
282
|
+
'accepts-mfa-tokens': true
|
|
283
|
+
};
|
|
284
|
+
const url = `${this.url.OAUTH_URL}/preauthorized?${qs_1.default.stringify(params)}`;
|
|
285
|
+
const oauth = this.getOauthClient(this.OAUTH_CONSUMER);
|
|
286
|
+
const step4RequestData = {
|
|
287
|
+
url: url,
|
|
288
|
+
method: 'GET'
|
|
289
|
+
};
|
|
290
|
+
const headers = oauth.toHeader(oauth.authorize(step4RequestData));
|
|
291
|
+
// console.log('getOauth1Token - headers:', headers);
|
|
292
|
+
const response = await this.get(url, {
|
|
293
|
+
headers: {
|
|
294
|
+
...headers,
|
|
295
|
+
'User-Agent': USER_AGENT_CONNECTMOBILE
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
// console.log('getOauth1Token - response:', response);
|
|
299
|
+
const token = qs_1.default.parse(response);
|
|
300
|
+
// console.log('getOauth1Token - token:', token);
|
|
301
|
+
this.oauth1Token = token;
|
|
302
|
+
return { token, oauth };
|
|
303
|
+
}
|
|
304
|
+
getOauthClient(consumer) {
|
|
305
|
+
const oauth = new oauth_1_0a_1.default({
|
|
306
|
+
consumer: consumer,
|
|
307
|
+
signature_method: 'HMAC-SHA1',
|
|
308
|
+
hash_function(base_string, key) {
|
|
309
|
+
return node_crypto_1.default
|
|
310
|
+
.createHmac('sha1', key)
|
|
311
|
+
.update(base_string)
|
|
312
|
+
.digest('base64');
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
return oauth;
|
|
316
|
+
}
|
|
317
|
+
//
|
|
318
|
+
async exchange(oauth1) {
|
|
319
|
+
const token = {
|
|
320
|
+
key: oauth1.token.oauth_token,
|
|
321
|
+
secret: oauth1.token.oauth_token_secret
|
|
322
|
+
};
|
|
323
|
+
// console.log('exchange - token:', token);
|
|
324
|
+
const baseUrl = `${this.url.OAUTH_URL}/exchange/user/2.0`;
|
|
325
|
+
const requestData = {
|
|
326
|
+
url: baseUrl,
|
|
327
|
+
method: 'POST',
|
|
328
|
+
data: null
|
|
329
|
+
};
|
|
330
|
+
const step5AuthData = oauth1.oauth.authorize(requestData, token);
|
|
331
|
+
// console.log('login - step5AuthData:', step5AuthData);
|
|
332
|
+
const url = `${baseUrl}?${qs_1.default.stringify(step5AuthData)}`;
|
|
333
|
+
// console.log('exchange - url:', url);
|
|
334
|
+
this.oauth2Token = undefined;
|
|
335
|
+
const response = await this.post(url, null, {
|
|
336
|
+
headers: {
|
|
337
|
+
'User-Agent': USER_AGENT_CONNECTMOBILE,
|
|
338
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
// console.log('exchange - response:', response);
|
|
342
|
+
this.oauth2Token = this.setOauth2TokenExpiresAt(response);
|
|
343
|
+
// console.log('exchange - oauth2Token:', this.oauth2Token);
|
|
344
|
+
}
|
|
345
|
+
setOauth2TokenExpiresAt(token) {
|
|
346
|
+
const now = luxon_1.DateTime.now();
|
|
347
|
+
const expiresAt = now.plus({ seconds: token.expires_in });
|
|
348
|
+
const refreshTokenExpiresAt = now.plus({
|
|
349
|
+
seconds: token.refresh_token_expires_in
|
|
350
|
+
});
|
|
351
|
+
return {
|
|
352
|
+
...token,
|
|
353
|
+
last_update_date: now.toLocal().toString(),
|
|
354
|
+
expires_date: expiresAt.toLocal().toString(),
|
|
355
|
+
expires_at: expiresAt.toSeconds(),
|
|
356
|
+
refresh_token_expires_at: refreshTokenExpiresAt.toSeconds()
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
exports.HttpClient = HttpClient;
|
|
333
361
|
//# sourceMappingURL=HttpClient.js.map
|