From 62f1313956e05654daed88e35faa5a0514fa996b Mon Sep 17 00:00:00 2001 From: Jonas Friedmann Date: Sun, 4 Sep 2016 13:22:32 +0200 Subject: [PATCH] Move all functions into helper file --- lib/helper.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++- tls-crt.js | 46 +++------------------------------- tls-csr.js | 33 ++----------------------- 3 files changed, 72 insertions(+), 75 deletions(-) diff --git a/lib/helper.js b/lib/helper.js index 684c7d5..4554fd5 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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 = { /** @@ -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); + } + }); } }; diff --git a/tls-crt.js b/tls-crt.js index 1b125db..f643e10 100644 --- a/tls-crt.js +++ b/tls-crt.js @@ -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 @@ -83,7 +43,7 @@ if(cmdr.hostname){ } // Call displayCrtInformation() - displayCrtInformation(crt, function(data){ + helpers.displayCrtInformation(crt, function(data){ helpers.quit(0); }); }); @@ -102,7 +62,7 @@ if(cmdr.hostname){ } // Call displayCrtInformation() - displayCrtInformation(searchResult, function(data){ + helpers.displayCrtInformation(searchResult, function(data){ helpers.quit(0); }); }); @@ -123,7 +83,7 @@ if(cmdr.hostname){ } // Call displayCrtInformation() - displayCrtInformation(searchResult, function(data){ + helpers.displayCrtInformation(searchResult, function(data){ helpers.quit(0); }); }); diff --git a/tls-csr.js b/tls-csr.js index 65ec567..8aed72b 100644 --- a/tls-csr.js +++ b/tls-csr.js @@ -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 @@ -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 { @@ -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 {