-
Notifications
You must be signed in to change notification settings - Fork 138
/
carelink.js
325 lines (282 loc) · 9.59 KB
/
carelink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
/* jshint node: true */
"use strict";
var _ = require('lodash'),
common = require('common'),
request = require('request');
var logger = require('./logger');
var CARELINK_EU = process.env['MMCONNECT_SERVER'] === 'EU';
var DEFAULT_MAX_RETRY_DURATION = module.exports.defaultMaxRetryDuration = 512;
var carelinkServerAddress = CARELINK_EU ? "carelink.minimed.eu" : "carelink.minimed.com";
var CARELINKEU_SERVER_ADDRESS = 'https://' + carelinkServerAddress;
var CARELINKEU_LOGIN1_URL = 'https://' + carelinkServerAddress + '/patient/sso/login?country=gb&lang=en';
var CARELINKEU_LOGIN3_URL = 'https://mdtlogin.medtronic.com/mmcl/auth/oauth/v2/authorize/login?country=gb&lang=en';
var CARELINKEU_LOGIN4_URL = 'https://mdtlogin.medtronic.com/mmcl/auth/oauth/v2/authorize/consent';
var CARELINKEU_JSON_BASE_URL = 'https://' + carelinkServerAddress + '/patient/connect/data?cpSerialNumber=NONE&msgType=last24hours&requestTime=';
var CARELINKEU_LOGIN_COOKIE = 'auth_tmp_token';
var CARELINK_SECURITY_URL = 'https://' + carelinkServerAddress + '/patient/j_security_check';
var CARELINK_AFTER_LOGIN_URL = 'https://' + carelinkServerAddress + '/patient/main/login.do';
var CARELINK_JSON_BASE_URL = 'https://' + carelinkServerAddress + '/patient/connect/ConnectViewerServlet?cpSerialNumber=NONE&msgType=last24hours&requestTime=';
var CARELINK_LOGIN_COOKIE = '_WL_AUTHCOOKIE_JSESSIONID';
var carelinkJsonUrlNow = function () {
return (CARELINK_EU ? CARELINKEU_JSON_BASE_URL : CARELINK_JSON_BASE_URL) + Date.now();
};
function reqOptions(extra) {
var defaults = {
jar: true,
followRedirect: false,
headers: {
Host: carelinkServerAddress,
Connection: 'keep-alive',
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8'
}
};
return _.merge(defaults, extra);
}
function haveLoginCookie(jar) {
if (CARELINK_EU)
return _.some(jar.getCookies(CARELINKEU_SERVER_ADDRESS), {key: CARELINKEU_LOGIN_COOKIE});
else
return _.some(jar.getCookies(CARELINK_SECURITY_URL), {key: CARELINK_LOGIN_COOKIE});
}
function responseAsError(response) {
if (!(response.statusCode >= 200 && response.statusCode < 400)) {
return new Error(
"Bad response from CareLink: " +
JSON.stringify(_.merge(response, {'body': '<redacted>'}))
);
} else {
return null;
}
}
function checkResponseThen(fn) {
return function (err, response) {
err = err || responseAsError(response);
fn.apply(this, [err].concat(Array.prototype.slice.call(arguments, 1)));
};
}
function retryDurationOnAttempt(n) {
return Math.pow(2, n);
}
function totalDurationAfterNextRetry(n) {
var sum = 0;
for (var i = 0; i <= n; i++) {
sum += retryDurationOnAttempt(i);
}
return sum;
}
var Client = exports.Client = function (options) {
if (!(this instanceof Client)) {
return new Client(arguments[0]);
}
var jar = request.jar();
if (options.maxRetryDuration === undefined) {
options.maxRetryDuration = DEFAULT_MAX_RETRY_DURATION;
}
function doLogin(next) {
logger.log('POST ' + CARELINK_SECURITY_URL);
request.post(
CARELINK_SECURITY_URL,
reqOptions({
jar: jar,
form: {j_username: options.username, j_password: options.password, j_character_encoding: "UTF-8"}
}),
checkResponseThen(next)
);
}
function doFetchCookie(response, next) {
logger.log('GET ' + CARELINK_AFTER_LOGIN_URL);
request.get(
CARELINK_AFTER_LOGIN_URL,
reqOptions({jar: jar}),
checkResponseThen(next)
);
}
var params = function (url) {
let q = url.split('?'), result = {};
if (q.length >= 1) {
q[q.length >= 2 ? 1 : 0].split('&').forEach((item) => {
try {
result[item.split('=')[0]] = item.split('=')[1];
} catch (e) {
result[item.split('=')[0]] = '';
}
})
}
return result;
}
function doLoginEu1(next) {
logger.log('GET ' + CARELINKEU_LOGIN1_URL);
request.get(
CARELINKEU_LOGIN1_URL,
reqOptions({
jar: jar,
rejectUnauthorized: false,
}),
checkResponseThen(next)
);
}
function doLoginEu2(response, next) {
let url = response.headers.location;
logger.log('GET ' + url);
request.get(
url,
reqOptions({
jar: jar,
rejectUnauthorized: false,
}),
checkResponseThen(next)
);
}
function doLoginEu3(response, next) {
logger.log('POST ' + CARELINKEU_LOGIN3_URL);
let ps = params(response.headers.location);
request.post(
CARELINKEU_LOGIN3_URL,
reqOptions({
jar: jar,
rejectUnauthorized: false,
changeOrigin: true,
gzip: true,
form: {
sessionID: ps.sessionID,
sessionData: ps.sessionData,
locale: "en",
action: "login",
username: options.username,
password: options.password,
actionButton: "Log in",
}
}),
checkResponseThen(next)
);
}
function doLoginEu4(response, next) {
logger.log('GET ' + CARELINKEU_LOGIN4_URL);
let ps = params(response.request.body);
const regex = /(<input type="hidden" name="sessionData" value=")(.*)"/gm;
ps.sessionData = regex.exec(response.body)[2];
request.post(
CARELINKEU_LOGIN4_URL,
reqOptions({
jar: jar,
rejectUnauthorized: false,
changeOrigin: true,
form: {
action: "consent",
sessionID: ps.sessionID,
sessionData: ps.sessionData,
response_type: "code",
response_mode: "query",
}
}),
checkResponseThen(next)
);
}
function doLoginEu5(response, next) {
let url = response.headers.location;
logger.log('GET ' + url);
request.get(
url,
reqOptions({
jar: jar,
rejectUnauthorized: false,
changeOrigin: true,
}),
checkResponseThen(next)
);
}
function getConnectData(response, next, retryCount) {
var url = carelinkJsonUrlNow();
logger.log('GET ' + url);
var reqO = {jar: jar, gzip: true};
if (CARELINK_EU) {
var cookie = _.find(jar.getCookies(CARELINKEU_SERVER_ADDRESS), { key: 'auth_tmp_token' });
reqO.headers = {
Authorization: "Bearer " + cookie.value,
};
}
var resp = request.get(
url,
reqOptions(reqO),
checkResponseThen(function (err, response) {
if (err) {
logger.log(err);
if (retryCount === undefined) {
retryCount = 0;
} else if (totalDurationAfterNextRetry(retryCount) >= options.maxRetryDuration) {
logger.log('Retried for too long (' + totalDurationAfterNextRetry(retryCount - 1) + ' seconds).');
next(err);
}
var timeout = retryDurationOnAttempt(retryCount);
logger.log('Trying again in ' + timeout + ' second(s)...');
setTimeout(function () {
getConnectData(response, next, retryCount + 1);
}, 1000 * timeout);
} else {
next(null, response);
}
})
);
}
function parseData(response, next) {
var parsed;
try {
parsed = JSON.parse(response.body);
} catch (e) {
next(e);
}
next(null, parsed);
}
function firstFetch(callback) {
var funcs = [
getConnectData,
parseData,
callback.bind(null, null),
];
if (CARELINK_EU) {
funcs = [
doLoginEu1,
doLoginEu2,
doLoginEu3,
doLoginEu4,
doLoginEu5,
...funcs];
} else {
funcs = [
doLogin,
doFetchCookie,
...funcs];
}
common.step(
funcs,
callback
);
}
function fetchLoggedIn(callback) {
common.step(
[
getConnectData,
parseData,
callback.bind(null, null),
],
function onError(err) {
logger.log('Fetch JSON failed; logging in again');
firstFetch(callback);
}
);
}
function fetch(callback) {
if (haveLoginCookie(jar)) {
fetchLoggedIn(callback);
} else {
logger.log('Logging in to CareLink');
firstFetch(callback);
}
}
return {
fetch: fetch
};
};