forked from johncrisostomo/get-ssl-certificate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (28 loc) · 813 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var https = require('https');
function isEmpty(object) {
for(var prop in object) {
if(object.hasOwnProperty(prop))
return false;
}
return true;
}
module.exports = function(url, callback) {
if (url.length <= 0 || typeof url !== 'string') {
throw Error("A valid URL is required");
}
if (typeof callback !== "function") {
throw Error("Callback function is required");
}
var req = https.get({hostname: url, agent: false}, function (res) {
var certificate = res.socket.getPeerCertificate();
if(isEmpty(certificate) || certificate === null) {
return callback({message: 'The website did not provide a certificate'}, null);
} else {
return callback(null, certificate);
}
});
req.on('error', function(e) {
callback(e, null)
});
req.end();
}