Skip to content

Commit

Permalink
Move all functions into helper file
Browse files Browse the repository at this point in the history
  • Loading branch information
frdmn committed Sep 4, 2016
1 parent 9bdf176 commit 62f1313
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 75 deletions.
68 changes: 67 additions & 1 deletion lib/helper.js
Expand Up @@ -9,7 +9,8 @@ var colors = require('colors'),
sh = require('shelljs'),
path = require('path'),
request = require('request'),
dns = require('dns');
dns = require('dns'),
openssl = require('openssl-cert-tools');

module.exports = {
/**
Expand Down Expand Up @@ -200,5 +201,70 @@ module.exports = {
return cb(response);
});
});
},

/**
* Display decoded certificate information to stdout
* @param {String} certificate
* @param {Function} callback
* @return {String|Bool} result
*/
displayCrtInformation: function (cert, cb){
openssl.getCertificateInfo(cert, function(err, data){
if (err === undefined) {
// Print out info
module.exports.out('Certificate Request:'.bold);
module.exports.out(data.certificate);
module.exports.out('Issuer:'.bold);

// For each issuer element
for(var issuerElement in data.issuer){
module.exports.out(' - ' + issuerElement + ': ' + data.issuer[issuerElement]);
}

module.exports.out('Subject:'.bold);

// For each subject element
for(var subjectElement in data.subject){
module.exports.out(' - ' + subjectElement + ': ' + data.subject[subjectElement]);
}

module.exports.out('Valid from:'.bold + ' ' + data.validFrom);
module.exports.out('Valid to:'.bold + ' ' + data.validTo);
module.exports.out('Remaining days:'.bold + ' ' + data.remainingDays);

return cb(true);
} else {
// console.log(err.message);
return cb(false);
}
});
},

/**
* Display decoded CSR information to stdout
* @param {String} certificate request
* @param {Function} callback
* @return {Bool} result
*/
displayCsrInformation: function(certRequest, cb){
openssl.getCertificateRequestInfo(certRequest, function(err, data){
if (err === undefined) {
// Print out info
module.exports.out('Certificate Request:'.bold);
module.exports.out(data.certificate);
module.exports.out('Subject:'.bold);

// For each subject element
for(var subjectElement in data.subject){
module.exports.out(' - ' + subjectElement + ': ' + data.subject[subjectElement]);
}

return cb(true);
} else {
// console.log(err.message);
return cb(false);
}
});
}
};
46 changes: 3 additions & 43 deletions tls-crt.js
Expand Up @@ -9,46 +9,6 @@ var cmdr = require('commander'),
helpers = require('./lib/helper'),
openssl = require('openssl-cert-tools');

/* Functions */

/**
* Display decoded certificate information to stdout
* @param {String} certificate
* @param {Function} callback
* @return {String|Bool} result
*/
var displayCrtInformation = function (cert, cb){
openssl.getCertificateInfo(cert, function(err, data){
if (err === undefined) {
// Print out info
helpers.out('Certificate Request:'.bold);
helpers.out(data.certificate);
helpers.out('Issuer:'.bold);

// For each issuer element
for(var issuerElement in data.issuer){
helpers.out(' - ' + issuerElement + ': ' + data.issuer[issuerElement]);
}

helpers.out('Subject:'.bold);

// For each subject element
for(var subjectElement in data.subject){
helpers.out(' - ' + subjectElement + ': ' + data.subject[subjectElement]);
}

helpers.out('Valid from:'.bold + ' ' + data.validFrom);
helpers.out('Valid to:'.bold + ' ' + data.validTo);
helpers.out('Remaining days:'.bold + ' ' + data.remainingDays);

return cb(true);
} else {
// console.log(err.message);
return cb(false);
}
});
}

/* Logic */

// Setup sub command options
Expand Down Expand Up @@ -83,7 +43,7 @@ if(cmdr.hostname){
}

// Call displayCrtInformation()
displayCrtInformation(crt, function(data){
helpers.displayCrtInformation(crt, function(data){
helpers.quit(0);
});
});
Expand All @@ -102,7 +62,7 @@ if(cmdr.hostname){
}

// Call displayCrtInformation()
displayCrtInformation(searchResult, function(data){
helpers.displayCrtInformation(searchResult, function(data){
helpers.quit(0);
});
});
Expand All @@ -123,7 +83,7 @@ if(cmdr.hostname){
}

// Call displayCrtInformation()
displayCrtInformation(searchResult, function(data){
helpers.displayCrtInformation(searchResult, function(data){
helpers.quit(0);
});
});
Expand Down
33 changes: 2 additions & 31 deletions tls-csr.js
Expand Up @@ -9,35 +9,6 @@ var cmdr = require('commander'),
helpers = require('./lib/helper'),
openssl = require('openssl-cert-tools');

/* Functions */

/**
* Display decoded CSR information to stdout
* @param {String} certificate request
* @param {Function} callback
* @return {Bool} result
*/
var displayCsrInformation = function(certRequest, cb){
openssl.getCertificateRequestInfo(certRequest, function(err, data){
if (err === undefined) {
// Print out info
helpers.out('Certificate Request:'.bold);
helpers.out(data.certificate);
helpers.out('Subject:'.bold);

// For each subject element
for(var subjectElement in data.subject){
helpers.out(' - ' + subjectElement + ': ' + data.subject[subjectElement]);
}

return cb(true);
} else {
// console.log(err.message);
return cb(false);
}
});
}

/* Logic */

// Setup sub command options
Expand All @@ -58,7 +29,7 @@ if (cmdr.filename) {
helpers.searchForCertificateRequest(haystack, function(searchResult){
if (searchResult !== false) {
// Call displayCrtInformation()
displayCsrInformation(searchResult, function(data){
helpers.displayCsrInformation(searchResult, function(data){
helpers.quit(0);
});
} else {
Expand All @@ -75,7 +46,7 @@ if (cmdr.filename) {
helpers.searchForCertificateRequest(haystack, function(searchResult){
if (searchResult !== false) {
// Call displayCrtInformation()
displayCsrInformation(searchResult, function(data){
helpers.displayCsrInformation(searchResult, function(data){
helpers.quit(0);
});
} else {
Expand Down

0 comments on commit 62f1313

Please sign in to comment.