Skip to content

Commit b6ffac8

Browse files
author
Marcos Castany
committed
Added prefix option and fixed moment warning
1 parent c260e4b commit b6ffac8

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

lib/saml11.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ exports.create = function(options, callback) {
6666

6767
if (options.lifetimeInSeconds) {
6868
conditions[0].setAttribute('NotBefore', now.format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
69-
conditions[0].setAttribute('NotOnOrAfter', now.add('seconds', options.lifetimeInSeconds).format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
69+
conditions[0].setAttribute('NotOnOrAfter', now.add(options.lifetimeInSeconds, 'seconds').format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
7070
}
7171

7272
if (options.audiences) {

lib/saml20.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ exports.create = function(options, callback) {
6767

6868
options.includeAttributeNameFormat = (typeof options.includeAttributeNameFormat !== 'undefined') ? options.includeAttributeNameFormat : true;
6969
options.typedAttributes = (typeof options.typedAttributes !== 'undefined') ? options.typedAttributes : true;
70-
70+
options.prefix = options.prefix && (typeof options.prefix === 'string') ? options.prefix : '' ;
7171

7272
var cert = utils.pemToCert(options.cert);
7373

@@ -79,8 +79,9 @@ exports.create = function(options, callback) {
7979
sig.signingKey = options.key;
8080

8181
sig.keyInfoProvider = {
82-
getKeyInfo: function () {
83-
return "<X509Data><X509Certificate>" + cert + "</X509Certificate></X509Data>";
82+
getKeyInfo: function (key, prefix) {
83+
prefix = prefix ? prefix + ':' : prefix;
84+
return "<" + prefix + "X509Data><" + prefix + "X509Certificate>" + cert + "</" + prefix + "X509Certificate></" + prefix + "X509Data>";
8485
}
8586
};
8687

@@ -104,9 +105,9 @@ exports.create = function(options, callback) {
104105

105106
if (options.lifetimeInSeconds) {
106107
conditions[0].setAttribute('NotBefore', now.format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
107-
conditions[0].setAttribute('NotOnOrAfter', now.clone().add('seconds', options.lifetimeInSeconds).format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
108+
conditions[0].setAttribute('NotOnOrAfter', now.clone().add(options.lifetimeInSeconds, 'seconds').format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
108109

109-
confirmationData[0].setAttribute('NotOnOrAfter', now.clone().add('seconds', options.lifetimeInSeconds).format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
110+
confirmationData[0].setAttribute('NotOnOrAfter', now.clone().add(options.lifetimeInSeconds, 'seconds').format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
110111
}
111112

112113
if (options.audiences) {
@@ -188,7 +189,8 @@ exports.create = function(options, callback) {
188189
location: {
189190
reference: options.xpathToNodeBeforeSignature || "//*[local-name(.)='Issuer']",
190191
action: 'after'
191-
}
192+
},
193+
prefix: options.prefix
192194
};
193195

194196
sig.computeSignature(token, opts);

test/saml20.tests.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,56 @@ describe('saml 2.0', function () {
364364
assert.equal('saml:Conditions', signature[0].previousSibling.nodeName);
365365
});
366366

367+
it('should place signature with prefix where specified', function () {
368+
var options = {
369+
cert: fs.readFileSync(__dirname + '/test-auth0.pem'),
370+
key: fs.readFileSync(__dirname + '/test-auth0.key'),
371+
xpathToNodeBeforeSignature: "//*[local-name(.)='Conditions']",
372+
prefix: 'anyprefix',
373+
attributes: {
374+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress': '[email protected]',
375+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Foo Bar',
376+
'http://example.org/claims/testemptyarray': [], // should dont include empty arrays
377+
'http://example.org/claims/testaccent': 'fóo', // should supports accents
378+
'http://undefinedattribute/ws/com.com': undefined
379+
}
380+
};
381+
382+
var signedAssertion = saml.create(options);
383+
384+
var isValid = utils.isValidSignature(signedAssertion, options.cert);
385+
assert.equal(true, isValid);
386+
387+
var doc = new xmldom.DOMParser().parseFromString(signedAssertion);
388+
var signature = doc.documentElement.getElementsByTagName(options.prefix + ':Signature');
389+
assert.equal('saml:Conditions', signature[0].previousSibling.nodeName);
390+
});
391+
392+
it('should ignore prefix if not a string', function () {
393+
var options = {
394+
cert: fs.readFileSync(__dirname + '/test-auth0.pem'),
395+
key: fs.readFileSync(__dirname + '/test-auth0.key'),
396+
xpathToNodeBeforeSignature: "//*[local-name(.)='Conditions']",
397+
prefix: 123,
398+
attributes: {
399+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress': '[email protected]',
400+
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Foo Bar',
401+
'http://example.org/claims/testemptyarray': [], // should dont include empty arrays
402+
'http://example.org/claims/testaccent': 'fóo', // should supports accents
403+
'http://undefinedattribute/ws/com.com': undefined
404+
}
405+
};
406+
407+
var signedAssertion = saml.create(options);
408+
409+
var isValid = utils.isValidSignature(signedAssertion, options.cert);
410+
assert.equal(true, isValid);
411+
412+
var doc = new xmldom.DOMParser().parseFromString(signedAssertion);
413+
var signature = doc.documentElement.getElementsByTagName('Signature');
414+
assert.equal('saml:Conditions', signature[0].previousSibling.nodeName);
415+
});
416+
367417
describe('encryption', function () {
368418

369419
it('should create a saml 2.0 signed and encrypted assertion', function (done) {

0 commit comments

Comments
 (0)