Skip to content

Commit

Permalink
Trying to solve ERR_TLS_CERT_ALTNAME_INVALID error3
Browse files Browse the repository at this point in the history
  • Loading branch information
Frigyes Bartha committed Jun 30, 2020
1 parent cae311e commit 76a25b3
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions carelink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
);
Expand All @@ -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)
);
Expand All @@ -164,6 +185,9 @@ var Client = exports.Client = function (options) {
url,
reqOptions({
jar: jar,
headers: {
Host: getHost(url),
},
}),
checkResponseThen(next)
);
Expand All @@ -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'),
Expand Down Expand Up @@ -210,6 +237,9 @@ var Client = exports.Client = function (options) {
url,
reqOptions({
jar: jar,
headers: {
Host: getHost(url),
},
form: {
action: "consent",
sessionID: ps.sessionID,
Expand All @@ -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', ''),
},
}),
Expand All @@ -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(
Expand All @@ -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 {
Expand Down

0 comments on commit 76a25b3

Please sign in to comment.