Skip to content

Commit

Permalink
Merge pull request #9 from polymorf/master
Browse files Browse the repository at this point in the history
Add certificate validity information to readCertificateInfo callback
  • Loading branch information
andris9 committed Jun 10, 2013
2 parents 15afc53 + 8aff869 commit 5ec9c90
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Use `readCertificateInfo` for reading subject data from a certificate or a CSR
Where

* **certificate** is a PEM encoded CSR or a certificate
* **callback** is a callback function with an error object and `{country, state, locality, organization, organizationUnit, commonName, emailAddress}`
* **callback** is a callback function with an error object and `{country, state, locality, organization, organizationUnit, commonName, emailAddress, validity{start, end} }`

### Get fingerprint

Expand Down
8 changes: 8 additions & 0 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ function fetchCertificateData(certData, callback){
certData = (certData || "").toString();

var subject, extra, tmp, certValues = {};
var validity = {};

if((subject = certData.match(/Subject:([^\n]*)\n/)) && subject.length>1){
subject = subject[1];
Expand Down Expand Up @@ -358,6 +359,13 @@ function fetchCertificateData(certData, callback){
tmp = extra.match(/emailAddress=([^,\n\/].*?)[,\n\/]/);
certValues.emailAddress = tmp && tmp[1] || "";
}
if ((tmp = certData.match(/Not Before\s?:\s?([^\n]*)\n/)) && tmp.length>1)
validity.start = Date.parse(tmp && tmp[1] || "");
if ((tmp = certData.match(/Not After\s?:\s?([^\n]*)\n/)) && tmp.length>1)
validity.end = Date.parse(tmp && tmp[1] || "");
if (validity.start && validity.end)
certValues.validity = validity;

callback(null, certValues);
}

Expand Down
4 changes: 4 additions & 0 deletions test/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ exports["General Tests"] = {

pem.readCertificateInfo(certificate, function(error, data){
test.ifError(error);
if(data.validity)
delete data.validity;
test.deepEqual(data,{
country: '',
state: '',
Expand Down Expand Up @@ -172,6 +174,8 @@ exports["General Tests"] = {

pem.readCertificateInfo(certificate, function(error, data){
test.ifError(error);
if(data.validity)
delete data.validity;
test.deepEqual(data, certInfo);
test.done();
});
Expand Down

0 comments on commit 5ec9c90

Please sign in to comment.