Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for WSSecurity XML signing with x509 certificates.
updated readme updated package.json with latest ursa version change minimum node requirement to 0.10 since many dependencies break with engine-strict=true under node 0.8
- Loading branch information
Jon Ciccone
authored and
CT Arrington
committed
Feb 25, 2016
1 parent
34d42a0
commit 06d90f7
Showing
10 changed files
with
198 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"use strict"; | ||
|
||
var ursa = require('ursa'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var ejs = require('ejs'); | ||
var SignedXml = require('xml-crypto').SignedXml; | ||
var uuid = require('node-uuid'); | ||
var wsseSecurityHeaderTemplate = ejs.compile(fs.readFileSync(path.join(__dirname, 'templates', 'wsse-security-header.ejs')).toString()); | ||
var wsseSecurityTokenTemplate = ejs.compile(fs.readFileSync(path.join(__dirname, 'templates', 'wsse-security-token.ejs')).toString()); | ||
|
||
function addMinutes(date, minutes) { | ||
return new Date(date.getTime() + minutes * 60000); | ||
} | ||
|
||
function dateStringForSOAP(date) { | ||
return date.getUTCFullYear() + '-' + ('0' + (date.getUTCMonth() + 1)).slice(-2) + '-' + | ||
('0' + date.getUTCDate()).slice(-2) + 'T' + ('0' + date.getUTCHours()).slice(-2) + ":" + | ||
('0' + date.getUTCMinutes()).slice(-2) + ":" + ('0' + date.getUTCSeconds()).slice(-2) + "Z"; | ||
} | ||
|
||
function generateCreated() { | ||
return dateStringForSOAP(new Date()); | ||
} | ||
|
||
function generateExpires() { | ||
return dateStringForSOAP(addMinutes(new Date(), 10)); | ||
} | ||
|
||
function insertStr(src, dst, pos) { | ||
return [dst.slice(0, pos), src, dst.slice(pos)].join(''); | ||
} | ||
|
||
function generateId() { | ||
return uuid.v4().replace(/-/gm, ''); | ||
} | ||
|
||
function WSSecurityCert(privatePEM, publicP12PEM, password, encoding) { | ||
|
||
this.privateKey = ursa.createPrivateKey(privatePEM, password, encoding); | ||
this.publicP12PEM = publicP12PEM.toString().replace('-----BEGIN CERTIFICATE-----', '').replace('-----END CERTIFICATE-----', '').replace(/(\r\n|\n|\r)/gm, ''); | ||
|
||
this.signer = new SignedXml(); | ||
this.signer.signingKey = this.privateKey.toPrivatePem(); | ||
this.x509Id = "x509-" + generateId(); | ||
|
||
var references = ["http://www.w3.org/2000/09/xmldsig#enveloped-signature", | ||
"http://www.w3.org/2001/10/xml-exc-c14n#"]; | ||
|
||
this.signer.addReference("//*[local-name(.)='Body']", references); | ||
this.signer.addReference("//*[local-name(.)='Timestamp']", references); | ||
|
||
var _this = this; | ||
this.signer.keyInfoProvider = {}; | ||
this.signer.keyInfoProvider.getKeyInfo = function (key) { | ||
return wsseSecurityTokenTemplate({ x509Id: _this.x509Id }); | ||
}; | ||
} | ||
|
||
WSSecurityCert.prototype.postProcess = function (xml) { | ||
this.created = generateCreated(); | ||
this.expires = generateExpires(); | ||
|
||
var secHeader = wsseSecurityHeaderTemplate({ | ||
binaryToken: this.publicP12PEM, | ||
created: this.created, | ||
expires: this.expires, | ||
id: this.x509Id | ||
}); | ||
|
||
var xmlWithSec = insertStr(secHeader, xml, xml.indexOf('</soap:Header>')); | ||
|
||
this.signer.computeSignature(xmlWithSec); | ||
|
||
return insertStr(this.signer.getSignatureXml(), xmlWithSec, xmlWithSec.indexOf('</wsse:Security>')); | ||
}; | ||
|
||
module.exports = WSSecurityCert; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" | ||
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" | ||
soap:mustUnderstand="true"> | ||
<wsse:BinarySecurityToken | ||
EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" | ||
ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" | ||
wsu:Id="<%-id%>"><%-binaryToken%></wsse:BinarySecurityToken> | ||
<Timestamp xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" Id="_1"> | ||
<Created><%-created%></Created> | ||
<Expires><%-expires%></Expires> | ||
</Timestamp> | ||
</wsse:Security> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<wsse:SecurityTokenReference> | ||
<wsse:Reference URI="<%-x509Id%>" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/> | ||
</wsse:SecurityTokenReference> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'), | ||
join = require('path').join; | ||
|
||
describe('WSSecurityCert', function() { | ||
var WSSecurityCert = require('../../').WSSecurityCert; | ||
var cert = fs.readFileSync(join(__dirname, '..', 'certs', 'agent2-cert.pem')); | ||
var key = fs.readFileSync(join(__dirname, '..', 'certs', 'agent2-key.pem')); | ||
|
||
it('is a function', function() { | ||
WSSecurityCert.should.be.type('function'); | ||
}); | ||
|
||
it('should accept valid constructor variables', function() { | ||
var instance = new WSSecurityCert(key, cert, '', 'utf8'); | ||
instance.should.have.property('privateKey'); | ||
instance.should.have.property('publicP12PEM'); | ||
instance.should.have.property('signer'); | ||
instance.should.have.property('x509Id'); | ||
}); | ||
|
||
it('should not accept invalid constructor variables', function() { | ||
var passed = true; | ||
|
||
try { | ||
new WSSecurityCert('*****', cert, '', 'utf8'); | ||
} catch(e) { | ||
passed = false; | ||
} | ||
|
||
if (passed) { | ||
throw new Error('bad private key'); | ||
} | ||
|
||
passed = true; | ||
|
||
try { | ||
new WSSecurityCert(key, cert, '', 'bob'); | ||
} catch(e) { | ||
passed = false; | ||
} | ||
|
||
if (passed) { | ||
throw new Error('bad encoding'); | ||
} | ||
}); | ||
|
||
it('should insert a WSSecurity signing block when postProcess is called', function() { | ||
var instance = new WSSecurityCert(key, cert, '', 'utf8'); | ||
var xml = instance.postProcess('<soap:Header></soap:Header><soap:Body></soap:Body>'); | ||
|
||
xml.should.containEql('<wsse:Security'); | ||
xml.should.containEql('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'); | ||
xml.should.containEql('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'); | ||
xml.should.containEql('soap:mustUnderstand="true"'); | ||
xml.should.containEql('<wsse:BinarySecurityToken'); | ||
xml.should.containEql('EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'); | ||
xml.should.containEql('ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"'); | ||
xml.should.containEql('wsu:Id="' + instance.x509Id); | ||
xml.should.containEql('</wsse:BinarySecurityToken>'); | ||
xml.should.containEql('<Timestamp xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" Id="_1">'); | ||
xml.should.containEql('<Created>' + instance.created); | ||
xml.should.containEql('<Expires>' + instance.expires); | ||
xml.should.containEql('<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">'); | ||
xml.should.containEql('<wsse:SecurityTokenReference>'); | ||
xml.should.containEql('<wsse:Reference URI="' + instance.x509Id); | ||
xml.should.containEql('ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>'); | ||
xml.should.containEql(instance.publicP12PEM); | ||
xml.should.containEql(instance.signer.getSignatureXml()); | ||
}); | ||
}); |