-
Notifications
You must be signed in to change notification settings - Fork 138
/
carelink.js
336 lines (278 loc) · 12.2 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
326
327
328
329
330
331
332
333
334
335
336
/* jshint node: true */
"use strict";
var _ = require('lodash'),
axios = require('axios').default,
axiosCookieJarSupport = require('axios-cookiejar-support').default,
tough = require('tough-cookie'),
urllib = require('url'),
software = require('./package.json'),
qs = require('qs');
var logger = require('./logger');
var MMCONNECT_SERVER = process.env['MMCONNECT_SERVER'];
var CARELINK_EU = MMCONNECT_SERVER === 'EU';
var MMCONNECT_SERVERNAME = process.env['MMCONNECT_SERVERNAME'];
var DEFAULT_CARELINKSERVERADDRESS = MMCONNECT_SERVERNAME || (CARELINK_EU ? "carelink.minimed.eu" : "carelink.minimed.com");
var DEFAULT_COUNTRYCODE = process.env['MMCONNECT_COUNTRYCODE'] || 'gb';
var DEFAULT_LANGCODE = process.env['MMCONNECT_LANGCODE'] || 'en';
var Client = exports.Client = function (options) {
var CARELINKEU_LOGIN_LOCALE = { country: options.countrycode || DEFAULT_COUNTRYCODE
, lang: options.lang || DEFAULT_LANGCODE };
var DEFAULT_MAX_RETRY_DURATION = module.exports.defaultMaxRetryDuration = 512;
if (options.server && options.server.toUpperCase( ) == 'EU') { options.server = 'carelink.minimed.eu'; }
var serverName = options.server || DEFAULT_CARELINKSERVERADDRESS;
var carelinkServerAddress = serverName;
var CARELINKEU_LOGIN_URL = 'https://' + carelinkServerAddress + '/patient/sso/login?country=gb&lang=en';
var CARELINKEU_REFRESH_TOKEN_URL = 'https://' + carelinkServerAddress + '/patient/sso/reauth';
var CARELINKEU_JSON_BASE_URL = 'https://' + carelinkServerAddress + '/patient/connect/data?cpSerialNumber=NONE&msgType=last24hours&requestTime=';
var CARELINKEU_TOKEN_COOKIE = 'auth_tmp_token';
var CARELINKEU_TOKENEXPIRE_COOKIE = 'c_token_valid_to';
var CARELINK_ME_URL = 'https://' + carelinkServerAddress + '/patient/users/me';
var CARELINK_COUNTRY_SETTINGS_URL = 'https://' + carelinkServerAddress + '/patient/countries/settings?countryCode='+(options.countrycode || DEFAULT_COUNTRYCODE)+'&language='+(options.lang || DEFAULT_LANGCODE);
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 user_agent_string = [software.name, software.version, software.bugs.url].join(' // ');
var getCurrentRole = async function() {
var resp = (await axiosInstance.get(CARELINK_ME_URL));
if (resp && resp.data && resp.data.role) {
return resp.data.role.toUpperCase( );
}
}
var carelinkJsonUrlNow = async function () {
return (1 || CARELINK_EU ? CARELINKEU_JSON_BASE_URL : CARELINK_JSON_BASE_URL) + Date.now();
};
let requestCount = 0;
if (!(this instanceof Client)) {
return new Client(arguments[0]);
}
const axiosInstance = axios.create({});
axiosCookieJarSupport(axiosInstance);
axiosInstance.defaults.jar = new tough.CookieJar();
axiosInstance.defaults.maxRedirects = 0;
axiosInstance.defaults.timeout = 10 * 1000;
axiosInstance.defaults.withCredentials = true;
axiosInstance.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
if (error.response && error.response.status >= 200 && error.response.status < 400) {
return error.response;
} else {
// Do something with response error
return Promise.reject(error);
}
});
axiosInstance.interceptors.request.use((config) => {
requestCount++;
if (requestCount > 10)
throw new Error("Request count exceeds the maximum in one fetch!");
config.headers['User-Agent'] = user_agent_string;
return config;
});
if (options.maxRetryDuration === undefined) {
options.maxRetryDuration = DEFAULT_MAX_RETRY_DURATION;
}
function retryDurationOnAttempt(n) {
return Math.pow(2, n);
}
function getCookies() {
let cookies = [];
axiosInstance.defaults.jar.store.getAllCookies(function (err, cookieArray) {
if (err)
cookies = [];
cookies = cookieArray;
});
return cookies.filter(c => c.domain === carelinkServerAddress);
}
function haveCookie(cookieName) {
return _.some(getCookies(), {key: cookieName});
}
function getCookie(cookieName) {
return _.find(getCookies(), {key: cookieName});
}
function deleteCookies() {
return axiosInstance.defaults.jar.removeAllCookiesSync();
}
function removeCookie(domain, path, key) {
return axiosInstance.defaults.jar.store.removeCookie(domain, path, key, function () {
});
}
function setCookie(domain, path, key, value) {
axiosInstance.defaults.jar.setCookieSync(`${key}=${value}`, `https://${domain}${path}`);
}
async function doLogin() {
return await axiosInstance.post(
CARELINK_SECURITY_URL,
qs.stringify({
j_username: options.username,
j_password: options.password,
j_character_encoding: "UTF-8"
}));
}
async function doFetchCookie() {
return await axiosInstance.get(CARELINK_AFTER_LOGIN_URL);
}
async function doLoginEu1() {
deleteCookies();
logger.log('EU login 1');
let url = urllib.parse(CARELINKEU_LOGIN_URL);
var query = _.merge(qs.parse(url.query), CARELINKEU_LOGIN_LOCALE);
url = urllib.format(_.merge(url, { search: null, query: query }));
deleteCookies();
logger.log('EU login 1', url);
return await axiosInstance.get(url);
}
async function doLoginEu2(response) {
logger.log(`EU login 2 (url: ${response.headers.location})`);
return await axiosInstance.get(response.headers.location);
}
async function doLoginEu3(response) {
let uri = new URL(response.headers.location);
let uriParam = uri.searchParams;
let url = `${uri.origin}${uri.pathname}?locale=${uriParam.get('locale')}&countrycode=${uriParam.get('countrycode')}`;
logger.log(`EU login 3 (url: ${url})`);
response = await axiosInstance.post(url, qs.stringify({
sessionID: uriParam.get('sessionID'),
sessionData: uriParam.get('sessionData'),
locale: "en",
action: "login",
username: options.username,
password: options.password,
actionButton: "Log in",
}));
if (_.get(response, 'data', '').includes(uri.pathname))
throw new Error('Carelink invalid username or password');
return response;
}
async function doLoginEu4(response) {
let regex = /(<form action=")(.*)" method="POST"/gm;
let url = (regex.exec(response.data) || [])[2] || '';
// Session data is changed, need to get it from the html body form
regex = /(<input type="hidden" name="sessionID" value=")(.*)"/gm;
let sessionId = (regex.exec(response.data) || [])[2] || '';
regex = /(<input type="hidden" name="sessionData" value=")(.*)"/gm;
let sessionData = (regex.exec(response.data)[2] || []) || '';
logger.log(`EU login 4 (url: ${url}, sessionID: ${sessionId}, sessionData: ${sessionData})`);
return await axiosInstance.post(url, qs.stringify({
action: "consent",
sessionID: sessionId,
sessionData: sessionData,
response_type: "code",
response_mode: "query",
}), {
maxRedirects: 0,
});
}
async function doLoginEu5(response) {
logger.log(`EU login 5 (url: ${response.headers.location})`);
await axiosInstance.get(response.headers.location, {maxRedirects: 0});
removeCookie('carelink.minimed.eu', '/', 'codeVerifier')
axiosInstance.defaults.headers.common = {
'Authorization': `Bearer ${_.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', '')}`,
};
}
async function refreshTokenEu() {
logger.log('Refresh EU token');
return await axiosInstance
.post(CARELINKEU_REFRESH_TOKEN_URL)
.then(response => {
axiosInstance.defaults.headers.common = {
'Authorization': `Bearer ${_.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', '')}`,
'Cookie': ''
};
})
.catch(async function (error) {
console.error(`[MMConnect] Refresh EU token failed (${error})`);
deleteCookies();
await checkLogin(true);
});
}
async function getConnectData() {
var currentRole=await getCurrentRole();
if(currentRole === "CARE_PARTNER_OUS" || currentRole === "CARE_PARTNER") {
var dataRetrievalUrl = null;
var resp = (await axiosInstance.get(CARELINK_COUNTRY_SETTINGS_URL));
if (resp && resp.data && resp.data.blePereodicDataEndpoint) {
dataRetrievalUrl = resp.data.blePereodicDataEndpoint;
}
if(dataRetrievalUrl) {
// HOTFIX
// https://github.com/nightscout/minimed-connect-to-nightscout/issues/39
dataRetrievalUrl = dataRetrievalUrl.replace('/carepartner/v6/display/message', '/carepartner/v5/display/message');
logger.log('GET data (as carepartner) ' + dataRetrievalUrl);
var body = {
username: options.username,
role: "carepartner",
patientId: options.patientId
};
return await axiosInstance.post(dataRetrievalUrl,body,{
});
} else {
throw new Error('Unable to retrieve data retrieval url for carepartner account');
}
} else {
var url = await carelinkJsonUrlNow();
logger.log('GET data ' + url);
return await axiosInstance.get(url);
}
}
async function checkLogin(relogin = false) {
if (1 || CARELINK_EU) {
// EU - SSO method
if (!relogin && (haveCookie(CARELINKEU_TOKEN_COOKIE) || haveCookie(CARELINKEU_TOKENEXPIRE_COOKIE))) {
let expire = new Date(Date.parse(_.get(getCookie(CARELINKEU_TOKENEXPIRE_COOKIE), 'value')));
// Refresh token if expires in 6 minutes
if (expire < new Date(Date.now() + 6 * 60 * 1000))
await refreshTokenEu();
} else {
logger.log('Logging in to CareLink');
let response = await doLoginEu1();
response = await doLoginEu2(response);
response = await doLoginEu3(response);
response = await doLoginEu4(response);
await doLoginEu5(response);
}
} else {
// US - Cookie method
if (!haveCookie(CARELINK_LOGIN_COOKIE)) {
logger.log('Logging in to CareLink');
let response = await doLogin()
await doFetchCookie(response)
}
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function fetch(callback) {
requestCount = 0;
let data = null;
let error = null;
try {
let maxRetry = 1; // No retry
for (let i = 1; i <= maxRetry; i++) {
try {
await checkLogin();
data = (await getConnectData()).data;
break;
} catch (e1) {
deleteCookies();
if (i === maxRetry)
throw e1;
let timeout = retryDurationOnAttempt(i);
await sleep(1000 * timeout);
}
}
} catch (e) {
error = `${e.toString()}\nstack: ${e.stack}`;
} finally {
callback(error, data);
}
}
return {
fetch: fetch
};
};