Skip to content

Commit

Permalink
fix: replaced request with axios
Browse files Browse the repository at this point in the history
  • Loading branch information
lostinauth0 committed Jul 28, 2023
1 parent 6807e45 commit ec262fd
Show file tree
Hide file tree
Showing 9 changed files with 2,021 additions and 1,102 deletions.
File renamed without changes.
635 changes: 380 additions & 255 deletions admin/server.js

Large diffs are not rendered by default.

423 changes: 231 additions & 192 deletions connector-setup/index.js

Large diffs are not rendered by default.

89 changes: 52 additions & 37 deletions connector-setup/steps/configureConnection.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
var urlJoin = require('url-join');
var request = require('request');
var fs = require('fs');
var path = require('path');
const axios = require('axios');
var urlJoin = require('url-join');
var fs = require('fs');
var path = require('path');
var thumbprint = require('@auth0/thumbprint');
var nconf = require('nconf');
var os = require('os');
var nconf = require('nconf');
var os = require('os');

var pemToCert = function(pem) {
var cert = /-----BEGIN CERTIFICATE-----([^-]*)-----END CERTIFICATE-----/g.exec(pem.toString());
var pemToCert = function (pem) {
var cert =
/-----BEGIN CERTIFICATE-----([^-]*)-----END CERTIFICATE-----/g.exec(
pem.toString()
);
if (cert.length > 0) {
return cert[1].replace(/[\n|\r\n]/g, '');
}
Expand All @@ -20,49 +23,61 @@ var getCurrentThumbprint = function (workingPath) {
return thumbprint.calculate(pemToCert(nconf.get('AUTH_CERT')));
}

var cert = pemToCert(fs.readFileSync(path.join(workingPath, 'certs', 'cert.pem')).toString());
var cert = pemToCert(
fs.readFileSync(path.join(workingPath, 'certs', 'cert.pem')).toString()
);
return thumbprint.calculate(cert);
};

module.exports = function (program, workingPath, connectionInfo, ticket, cb) {
var serverUrl = nconf.get('SERVER_URL') ||
('http://' + os.hostname() + ':' + (nconf.get('PORT') || 4000));
var serverUrl =
nconf.get('SERVER_URL') ||
'http://' + os.hostname() + ':' + (nconf.get('PORT') || 4000);

var signInEndpoint = urlJoin(serverUrl, '/wsfed');
var pem = nconf.get('AUTH_CERT') || fs.readFileSync(path.join(workingPath, 'certs', 'cert.pem')).toString();
var pem =
nconf.get('AUTH_CERT') ||
fs.readFileSync(path.join(workingPath, 'certs', 'cert.pem')).toString();
var cert = pemToCert(pem);

console.log(('Configuring connection ' + connectionInfo.connectionName + '.').yellow);
console.log(
('Configuring connection ' + connectionInfo.connectionName + '.').yellow
);
console.log(' > Posting certificates and signInEndpoint: ' + signInEndpoint);

request.post({
url: ticket,
json: {
certs: [cert],
axios
.post(ticket, {
certs: [cert],
signInEndpoint: signInEndpoint,
agentMode: nconf.get('AGENT_MODE'),
agentVersion: require('../../package').version
}
}, function (err, response, body) {
if (err) {
agentMode: nconf.get('AGENT_MODE'),
agentVersion: require('../../package').version,
})
.then((response) => {
if (response.status !== 200) {
console.log(
'Unexpected status while configuring connection: ' + response.status
);
return cb(new Error(response.data));
}

nconf.set('SERVER_URL', serverUrl);
nconf.set('LAST_SENT_THUMBPRINT', getCurrentThumbprint(workingPath));
nconf.set('TENANT_SIGNING_KEY', response.data.signingKey || '');

console.log(
('Connection ' + connectionInfo.connectionName + ' configured.').green
);
cb();
})
.catch((err) => {
if (err.code === 'ECONNREFUSED') {
console.log('Unable to reach Auth0 at ' + ticket);
} else {
console.log('Unexpected error while configuring connection: ' + (err.code || err.message));
console.log(
'Unexpected error while configuring connection: ' +
(err.code || err.message)
);
}
return cb(err);
}

if (response.statusCode !== 200) {
console.log('Unexpected status while configuring connection: ' + response.statusCode);
return cb(new Error(body));
}

nconf.set('SERVER_URL', serverUrl);
nconf.set('LAST_SENT_THUMBPRINT', getCurrentThumbprint(workingPath));
nconf.set('TENANT_SIGNING_KEY', response.body.signingKey || '');

console.log(('Connection ' + connectionInfo.connectionName + ' configured.').green);
cb();
});
});
};
87 changes: 49 additions & 38 deletions latency_test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var latency_test = module.exports;
var request = require('request');
var _ = require('lodash');
var async = require('async');
var nconf = require('nconf');
var url = require('url');
var test_url = 'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
var exit = require('./lib/exit');
var latency_test = module.exports;
const axios = require('axios');
const _ = require('lodash');
const async = require('async');
const nconf = require('nconf');
const url = require('url');
const test_url =
'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
const exit = require('./lib/exit');

/**
* test the url and return the ns
Expand All @@ -15,47 +16,57 @@ var exit = require('./lib/exit');
*/
latency_test.run = function (done) {
var start = process.hrtime();
request.get(test_url, function(err){
if (err) return done(err);
var took = process.hrtime(start);
done(null, took[0] * 1e9 + took[1]);
});
axios
.get(test_url)
.then((response) => {
var took = process.hrtime(start);
done(null, took[0] * 1e9 + took[1]);
})
.catch((err) => done(err));
};

latency_test.run_many = function (n, done) {
async.mapSeries(_.range(n), function (n, callback) {
latency_test.run(callback);
}, function (err, times){
if (err) {
console.log('Error when doing the latency test, exiting.');
exit(1);
}
async.mapSeries(
_.range(n),
function (n, callback) {
latency_test.run(callback);
},
function (err, times) {
if (err) {
console.log('Error when doing the latency test, exiting.');
exit(1);
}

var sum = times.reduce(function (prev, curr) {
return prev + curr;
}, 0);
var sum = times.reduce(function (prev, curr) {
return prev + curr;
}, 0);

var max = times.reduce(function (prev, curr) {
return Math.max(prev, curr);
}, 0);
var max = times.reduce(function (prev, curr) {
return Math.max(prev, curr);
}, 0);

var min = times.reduce(function (prev, curr) {
return Math.min(prev, curr);
}, Infinity);
var min = times.reduce(function (prev, curr) {
return Math.min(prev, curr);
}, Infinity);

var result = [sum / n, max, min].map(function (nanos) {
return (nanos / 1e6).toFixed(2);
});
var result = [sum / n, max, min].map(function (nanos) {
return (nanos / 1e6).toFixed(2);
});

console.log('latency test took avg: %d ms, max: %d ms, min: %d ms',
result[0], result[1], result[2]);
console.log(
'latency test took avg: %d ms, max: %d ms, min: %d ms',
result[0],
result[1],
result[2]
);

if (done) {
done(null, result);
if (done) {
done(null, result);
}
}
});
);
};

if (require.main === module) {
latency_test.run_many(10);
}
}
57 changes: 31 additions & 26 deletions lib/clock_skew_detector.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
var request = require('request');
var nconf = require('nconf');
var url = require('url');
var test_url = 'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
var exit = require('./exit');
const axios = require('axios');
const nconf = require('nconf');
const url = require('url');
const test_url =
'https://' + url.parse(nconf.get('PROVISIONING_TICKET')).host + '/test';
const exit = require('./exit');

function check (callback) {
request.get({
uri: test_url,
json: true
}, function (err, resp, body) {
if (err || !body || !body.clock) {
return callback();
}
function check(callback) {
axios
.get(test_url)
.then((response) => {
const body = response.data;

var auth0_time = body.clock;
var local_time = new Date().getTime();
var dif = Math.abs(auth0_time - local_time);
if (!body || !body.clock) {
return callback();
}

if (dif > 5000) {
var message = [ 'Clock skew detected.',
'- Local time: ' + new Date(local_time),
'- Auth0 time: ' + new Date(auth0_time)].join('\n');
const auth0_time = body.clock;
const local_time = new Date().getTime();
const dif = Math.abs(auth0_time - local_time);

return callback(new Error(message));
}
if (dif > 5000) {
var message = [
'Clock skew detected.',
'- Local time: ' + new Date(local_time),
'- Auth0 time: ' + new Date(auth0_time),
].join('\n');

callback();
});
return callback(new Error(message));
}
})
.finally(() => {
return callback();
});
}

function schedule () {
function schedule() {
setTimeout(function () {
check(function (err) {
if (err) {
Expand All @@ -41,4 +46,4 @@ function schedule () {
}, 5000);
}

schedule();
schedule();
Loading

0 comments on commit ec262fd

Please sign in to comment.