Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading array of organizations in a CSR #125

Merged
merged 5 commits into from
Sep 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,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 @@ -845,6 +845,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