diff --git a/carelink.js b/carelink.js index 6cdc985..15d509d 100644 --- a/carelink.js +++ b/carelink.js @@ -13,7 +13,7 @@ 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_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'; @@ -90,6 +90,10 @@ var Client = exports.Client = function (options) { var jar = request.jar(); + if (options.maxRetryDuration === undefined) { + options.maxRetryDuration = DEFAULT_MAX_RETRY_DURATION; + } + function getCookies() { return jar.getCookies(CARELINK_EU ? CARELINKEU_SERVER_ADDRESS : CARELINK_SECURITY_URL); } @@ -102,28 +106,41 @@ var Client = exports.Client = function (options) { return _.find(getCookies(), {key: cookieName}); } - if (options.maxRetryDuration === undefined) { - options.maxRetryDuration = DEFAULT_MAX_RETRY_DURATION; + function getHost(url) { + return new URL(url).host; } function doLogin(next) { - logger.log('POST ' + CARELINK_SECURITY_URL); + let url = CARELINK_SECURITY_URL; + logger.log('POST ' + url); + request.post( - CARELINK_SECURITY_URL, + url, reqOptions({ jar: jar, - form: {j_username: options.username, j_password: options.password, j_character_encoding: "UTF-8"} + headers: { + Host: getHost(url), + }, + 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); + let url = CARELINK_AFTER_LOGIN_URL; + logger.log('GET ' + url); request.get( - CARELINK_AFTER_LOGIN_URL, + url, reqOptions({ - jar: jar + jar: jar, + headers: { + Host: getHost(url), + }, }), checkResponseThen(next) ); @@ -144,12 +161,16 @@ var Client = exports.Client = function (options) { } function doLoginEu1(next) { - logger.log('GET ' + CARELINKEU_LOGIN1_URL); + let url = CARELINKEU_LOGIN_URL; + logger.log('GET ' + url); request.get( - CARELINKEU_LOGIN1_URL, + url, reqOptions({ jar: jar, + headers: { + Host: getHost(url), + }, }), checkResponseThen(next) ); @@ -164,6 +185,9 @@ var Client = exports.Client = function (options) { url, reqOptions({ jar: jar, + headers: { + Host: getHost(url), + }, }), checkResponseThen(next) ); @@ -181,6 +205,9 @@ var Client = exports.Client = function (options) { reqOptions({ jar: jar, gzip: true, + headers: { + host: getHost(url), + }, form: { sessionID: uriParam.get('sessionID'), sessionData: uriParam.get('sessionData'), @@ -210,6 +237,9 @@ var Client = exports.Client = function (options) { url, reqOptions({ jar: jar, + headers: { + Host: getHost(url), + }, form: { action: "consent", sessionID: ps.sessionID, @@ -231,21 +261,26 @@ var Client = exports.Client = function (options) { url, reqOptions({ jar: jar, + headers: { + Host: getHost(url), + }, }), checkResponseThen(next) ); } function refreshTokenEu(next) { + let url = CARELINKEU_REFRESH_TOKEN_URL; logger.log('Refresh auth token'); request.post( - CARELINKEU_REFRESH_TOKEN_URL, + url, reqOptions({ jar: jar, gzip: true, json: true, headers: { + Host: getHost(url), Authorization: "Bearer " + _.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', ''), }, }), @@ -269,12 +304,13 @@ var Client = exports.Client = function (options) { var reqO = { jar: jar, - gzip: true + gzip: true, + headers: { + Host: getHost(url), + }, }; if (CARELINK_EU) { - reqO.headers = { - Authorization: "Bearer " + _.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', ''), - }; + reqO.headers.Authorization = "Bearer " + _.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', ''); } var resp = request.get( @@ -293,7 +329,7 @@ var Client = exports.Client = function (options) { logger.log('Trying again in ' + timeout + ' second(s)...'); setTimeout(function () { if (CARELINK_EU) { - refreshTokenEu(function() { + refreshTokenEu(function () { getConnectData(response, next, retryCount + 1); }); } else {