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

Validate that DiscoHints elements only appears on IdP #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
123 changes: 89 additions & 34 deletions samlmetajs/mdreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ MDEntityDescriptor.prototype.hasCertOfType = function (type) {
/*
* Check if the current entity has a specified property.
*/
MDEntityDescriptor.prototype.hasProperty = function (property) {
var entity;
if (hasProp(this, 'saml2sp')) {
entity = this.saml2sp;
} else {
entity = this.saml2idp;
}
MDEntityDescriptor.prototype.hasProperty = function (property, entity) {
if (!entity) {
return false;
if (hasProp(this, 'saml2sp')) {
entity = this.saml2sp;
} else {
entity = this.saml2idp;
}
if (!entity) {
return false;
}
}

return hasProp(entity, property);
Expand All @@ -103,16 +104,16 @@ MDEntityDescriptor.prototype.hasProperty = function (property) {
/*
* Return the specified property from the current entity.
*/
MDEntityDescriptor.prototype.getProperty = function (property) {
var entity;
if (hasProp(this, 'saml2sp')) {
entity = this.saml2sp;
} else {
entity = this.saml2idp;
}
MDEntityDescriptor.prototype.getProperty = function (property, entity) {
if (!entity) {
this.saml2sp = {};
entity = this.saml2sp;
if (hasProp(this, 'saml2sp')) {
entity = this.saml2sp;
} else if (hasProp(this, 'saml2idp')) {
entity = this.saml2idp;
} else {
this.saml2sp = {};
entity = this.saml2sp;
}
}

if (!hasProp(entity, property)) {
Expand All @@ -125,11 +126,11 @@ MDEntityDescriptor.prototype.getProperty = function (property) {
/*
* Look for a MDUI property in any language.
*/
MDEntityDescriptor.prototype.hasMDUIProperty = function (property) {
var mdui = this.hasProperty('mdui'),
MDEntityDescriptor.prototype.hasMDUIProperty = function (property, entity) {
var mdui = this.hasProperty('mdui', entity),
result = false;
if (mdui) {
mdui = this.getProperty('mdui');
mdui = this.getProperty('mdui', entity);
result = !isEmpty(mdui) && hasProp(mdui, property) && !isEmpty(mdui[property]);
};
return result;
Expand Down Expand Up @@ -165,7 +166,7 @@ MDEntityDescriptor.prototype.addLogo = function (lang, location, width, height)
* Look for location.
*/
MDEntityDescriptor.prototype.hasLocation = function () {
return this.hasMDUIProperty('location');
return this.hasMDUIProperty('location', this.saml2idp);
};

/*
Expand All @@ -175,7 +176,7 @@ MDEntityDescriptor.prototype.getLocation = function () {
if (!this.hasLocation()) {
return null;
} else {
return this.getProperty('mdui').location;
return this.getProperty('mdui', this.saml2idp).location;
}
};

Expand All @@ -184,7 +185,7 @@ MDEntityDescriptor.prototype.getLocation = function () {
* nested structures as needed.
*/
MDEntityDescriptor.prototype.setLocation = function (location) {
var mdui = this.getProperty('mdui');
var mdui = this.getProperty('mdui', this.saml2idp);
mdui.location = location;
};

Expand Down Expand Up @@ -790,9 +791,7 @@ parseFromString = function(xmlstring) {
}


function parseUIInfo(node) {

var mdui = {};
function parseUIInfo(node, mdui) {

expectNode(node, 'UIInfo', constants.ns.mdui);

Expand Down Expand Up @@ -832,12 +831,6 @@ parseFromString = function(xmlstring) {
}
}
},
{
namespace: constants.ns.mdui, name: 'GeolocationHint',
callback: function(n) {
mdui.location = nodeGetTextRecursive(n).substr(4);
}
},
{
namespace: constants.ns.mdui, name: 'Keywords',
callback: function (n) {
Expand Down Expand Up @@ -878,10 +871,27 @@ parseFromString = function(xmlstring) {
processTest(new TestResult('mduiunknownchild', 'Parsing of this child element of MDUI not yet implemented [' + nodeName(n) + ']'));
});

return mdui;
}

function parseDiscoHintsInfo(node, mdui) {

expectNode(node, 'DiscoHints', constants.ns.mdui);

nodeProcessChildren(node, [
{
namespace: constants.ns.mdui, name: 'GeolocationHint',
callback: function(n) {
mdui.location = nodeGetTextRecursive(n).substr(4);
}
}
// Fallback
], function(n) {
processTest(new TestResult('mduiunknownchild', 'Parsing of this child element of MDUI:DiscoHint not yet implemented [' + nodeName(n) + ']'));
});

}


function parseSPSSODescriptorExtensions(node, saml2sp) {
expectNode(node, 'Extensions', constants.ns.md);

Expand All @@ -890,7 +900,16 @@ parseFromString = function(xmlstring) {
{
namespace: constants.ns.mdui, name: 'UIInfo',
callback: function(n) {
saml2sp.mdui = parseUIInfo(n);
if (!saml2sp.mdui) {
saml2sp.mdui = {};
}
parseUIInfo(n, saml2sp.mdui);
}
},
{
namespace: constants.ns.mdui, name: 'DiscoHints',
callback: function(n) {
processTest(new TestResult('extillegalelementdiscohints', 'Illegal element (DiscoHints) in MDUI Extensions at SPSSODescriptor [' + nodeName(n) + ']', 0, 2));
}
},
{
Expand Down Expand Up @@ -1010,6 +1029,36 @@ parseFromString = function(xmlstring) {
return saml2sp;
}

function parseIDPSSODescriptorExtensions(node, saml2idp) {
expectNode(node, 'Extensions', constants.ns.md);

// Process children of Extensions
nodeProcessChildren(node, [
{
namespace: constants.ns.mdui, name: 'UIInfo',
callback: function(n) {
if (!saml2idp.mdui) {
saml2idp.mdui = {};
}
parseUIInfo(n, saml2idp.mdui);
}
},
{
namespace: constants.ns.mdui, name: 'DiscoHints',
callback: function(n) {
if (!saml2idp.mdui) {
saml2idp.mdui = {};
}
parseDiscoHintsInfo(n, saml2idp.mdui);
}
}
// Fallback
], function(n) {
processTest(new TestResult('notimplementedssoext', 'Parsing Extensions at IDPSSODescriptor with [' + nodeName(n) + '] not implemented'));
// console.log('Parsing Extensions at SPSSODescriptor with [' + nodeName(n) + '] not implemented...');
});
}

function parseSAML2IDP(node) {

var
Expand All @@ -1020,6 +1069,12 @@ parseFromString = function(xmlstring) {

// Process children of IDPSSODescriptor
nodeProcessChildren(node, [
{
namespace: constants.ns.md, name: 'Extensions',
callback: function(n) {
parseIDPSSODescriptorExtensions(n, saml2idp);
}
},
{
namespace: constants.ns.md, name: 'KeyDescriptor',
callback: function(n) {
Expand Down
3 changes: 1 addition & 2 deletions samlmetajs/samlmeta.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ SAMLmetaJS.xmlupdater = function(xmlstring) {
entitydescriptor.hasLogo() ||
entitydescriptor.hasInformationURL() ||
entitydescriptor.hasPrivacyStatementURL() ||
entitydescriptor.hasLocation() ||
entitydescriptor.hasKeywords
) {
extensions = this.addIfNotExtensions(spdescriptor);
Expand Down Expand Up @@ -339,7 +338,7 @@ SAMLmetaJS.xmlupdater = function(xmlstring) {
}
}
SAMLmetaJS.XML.wipeChildren(node.parentNode, SAMLmetaJS.Constants.ns.mdui, 'DiscoHints');
if (entitydescriptor.hasLocation()) {
if (endpoint === "saml2idp" && entitydescriptor.hasLocation()) {
this.addMDUILocation(node.parentNode, entitydescriptor.getLocation());
}
},
Expand Down