Skip to content

Commit

Permalink
Support reading array of organizations in a CSR (#125)
Browse files Browse the repository at this point in the history
* initial test and support for multiple orgs on csr reading

* added tests and removed logs

* removed padding aroun org regex

* changed arrow function to regular function
  • Loading branch information
andresfvilla authored and Dexus committed Sep 3, 2017
1 parent 2eae57c commit 061a695
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,8 @@ function fetchCertificateData(certData, callback) {
tmp = subject2.match(/\sL=([^\n].*?)[\n]/);
certValues.locality = tmp && tmp[1] || '';
// organization
tmp = subject2.match(/\sO=([^\n].*?)[\n]/);
certValues.organization = tmp && tmp[1] || '';
tmp = subject2.matchAll(/\sO=([^\n].*)/g);
certValues.organization = tmp ? tmp.length>1 ? tmp.map(function(x) {return x[1];}) : tmp[0][1] : '';
// unit
tmp = subject2.match(/\sOU=([^\n].*?)[\n]/);
certValues.organizationUnit = tmp && tmp[1] || '';
Expand Down Expand Up @@ -935,6 +935,17 @@ function fetchCertificateData(certData, callback) {
callback(null, certValues);
}

String.prototype.matchAll = function(regexp) {
var matches = [];
this.replace(regexp, function() {
var arr = ([]).slice.call(arguments, 0);
var extras = arr.splice(-2);
arr.index = extras[0];
arr.input = extras[1];
matches.push(arr);
});
return matches.length ? matches : null;
};

function linebrakes(content) {
var helper_x, subject, type;
Expand Down
50 changes: 50 additions & 0 deletions test/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,56 @@ exports['General Tests'] = {
});
},

'Create CSR with multiple organizations using config file': function(test) {
var certInfo = {
issuer : {},
country: 'EE',
state: 'Harjumaa',
locality: 'Tallinn',
organization: ['Node.ee', 'Node2.ee'],
organizationUnit: 'test',
commonName: 'www.node.ee',
emailAddress: 'andris@node.ee',
dc: '',
signatureAlgorithm: 'sha256WithRSAEncryption',
publicKeyAlgorithm: 'rsaEncryption',
publicKeySize: '2048 bit'
};

var expectedCertInfo = {
issuer : {},
country: 'EE',
state: 'Harjumaa',
locality: 'Tallinn',
organization: ['Node.ee', 'Node2.ee'],
organizationUnit: 'test',
commonName: 'www.node.ee',
emailAddress: 'andris@node.ee',
dc: '',
signatureAlgorithm: 'sha256WithRSAEncryption',
publicKeyAlgorithm: 'rsaEncryption',
publicKeySize: '2048 bit'
};

pem.createCSR(certInfo, function(error, data) {
var csr = (data && data.csr || '').toString();
test.ifError(error);
test.ok(csr);
test.ok(csr.match(/^\n*\-\-\-\-\-BEGIN CERTIFICATE REQUEST\-\-\-\-\-\n/));
test.ok(csr.match(/\n\-\-\-\-\-END CERTIFICATE REQUEST\-\-\-\-\-\n*$/));

test.ok(data && data.clientKey);
test.ok(fs.readdirSync('./tmp').length === 0);

pem.readCertificateInfo(csr, function(error, data) {
test.ifError(error);
test.deepEqual(data, expectedCertInfo);
test.ok(fs.readdirSync('./tmp').length === 0);
test.done();
});
});
},

'Create CSR with own key': function(test) {
pem.createPrivateKey(function(error, data) {
var key = (data && data.key || '').toString();
Expand Down

0 comments on commit 061a695

Please sign in to comment.