Skip to content

Commit

Permalink
Merge fa3cc6b into e0bf975
Browse files Browse the repository at this point in the history
  • Loading branch information
johncrisostomo committed Sep 17, 2018
2 parents e0bf975 + fa3cc6b commit 40b90c9
Show file tree
Hide file tree
Showing 5 changed files with 410 additions and 490 deletions.
30 changes: 29 additions & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@
[![Build Status](https://travis-ci.org/johncrisostomo/get-ssl-certificate.svg?branch=master)](https://travis-ci.org/johncrisostomo/get-ssl-certificate)
[![Coverage Status](https://coveralls.io/repos/github/johncrisostomo/get-ssl-certificate/badge.svg?branch=master)](https://coveralls.io/github/johncrisostomo/get-ssl-certificate?branch=master)
[![Code Climate](https://codeclimate.com/github/johncrisostomo/get-ssl-certificate/badges/gpa.svg)](https://codeclimate.com/github/johncrisostomo/get-ssl-certificate)
[![npm](https://img.shields.io/badge/npm-v2.1.2-blue.svg)](https://www.npmjs.com/package/get-ssl-certificate)
[![npm](https://img.shields.io/badge/npm-v2.2.0-blue.svg)](https://www.npmjs.com/package/get-ssl-certificate)

### Installation

Expand Down Expand Up @@ -49,6 +49,34 @@ sslCertificate.get('nodejs.org').then(function (certificate) {
});
```

#### Optional: Pass timeout (in ms)

```
sslCertificate.get('nodejs.org', 250).then(function (certificate) {
console.log(certificate)
// certificate is a JavaScript object
console.log(certificate.issuer)
// { C: 'GB',
// ST: 'Greater Manchester',
// L: 'Salford',
// O: 'COMODO CA Limited',
// CN: 'COMODO RSA Domain Validation Secure Server CA' }
console.log(certificate.valid_from)
// 'Aug 14 00:00:00 2017 GMT'
console.log(certificate.valid_to)
// 'Nov 20 23:59:59 2019 GMT'
// If there was a certificate.raw attribute, then you can access certificate.pemEncoded
console.log(certificate.pemEncoded)
// -----BEGIN CERTIFICATE-----
// ...
// -----END CERTIFICATE-----
});
```

## License

MIT
52 changes: 30 additions & 22 deletions index.js
@@ -1,63 +1,71 @@
var https = require('https')
var https = require('https');

function isEmpty(object) {
for (var prop in object) {
if (object.hasOwnProperty(prop)) return false
if (object.hasOwnProperty(prop)) return false;
}

return true
return true;
}

function pemEncode(str, n) {
var ret = []
var ret = [];

for (var i = 1; i <= str.length; i++) {
ret.push(str[i - 1])
var mod = i % n
ret.push(str[i - 1]);
var mod = i % n;

if (mod === 0) {
ret.push('\n')
ret.push('\n');
}
}

var returnString = `-----BEGIN CERTIFICATE-----\n${ret.join('')}\n-----END CERTIFICATE-----`
var returnString = `-----BEGIN CERTIFICATE-----\n${ret.join('')}\n-----END CERTIFICATE-----`;

return returnString
return returnString;
}

function get(url) {
function get(url, timeout) {
if (url.length <= 0 || typeof url !== 'string') {
throw Error('A valid URL is required')
throw Error('A valid URL is required');
}

var options = {
hostname: url,
agent: false,
rejectUnauthorized: false,
ciphers: 'ALL'
}
};

return new Promise(function(resolve, reject) {
var req = https.get(options, function(res) {
var certificate = res.socket.getPeerCertificate()
var certificate = res.socket.getPeerCertificate();

if (isEmpty(certificate) || certificate === null) {
reject({ message: 'The website did not provide a certificate' })
reject({ message: 'The website did not provide a certificate' });
} else {
if (certificate.raw) {
certificate.pemEncoded = pemEncode(certificate.raw.toString('base64'), 64)
certificate.pemEncoded = pemEncode(certificate.raw.toString('base64'), 64);
}
resolve(certificate)
resolve(certificate);
}
})
});

if (timeout) {
req.setTimeout(timeout, function() {
reject({ message: 'Request timed out.' });
req.abort();
});
}

req.on('error', function(e) {
reject(e)
})
reject(e);
});

req.end()
})
req.end();
});
}

module.exports = {
get: get
}
};

0 comments on commit 40b90c9

Please sign in to comment.