Skip to content

Commit

Permalink
Add assertion attributes to child object on profile (node-saml#543)
Browse files Browse the repository at this point in the history
This attributes are also mounted to profile directly in a non
conflicting way.
  • Loading branch information
kriss1897 committed May 19, 2021
1 parent d17e55a commit 4e19f64
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
18 changes: 12 additions & 6 deletions src/node-saml/saml.ts
Expand Up @@ -1170,13 +1170,21 @@ class SAML {
};

if (attributes) {
const profileAttributes: Record<string, unknown> = {};

attributes.forEach((attribute) => {
if (!Object.prototype.hasOwnProperty.call(attribute, "AttributeValue")) {
// if attributes has no AttributeValue child, continue
return;
}

const name = attribute.$.Name;
const value = attribute.AttributeValue;
const value =
attribute.AttributeValue.length === 1
? attrValueMapper(attribute.AttributeValue[0])
: attribute.AttributeValue.map(attrValueMapper);

profileAttributes[name] = value;

// If any property is already present in profile and is also present
// in attributes, then skip the one from attributes. Handle this
Expand All @@ -1185,12 +1193,10 @@ class SAML {
return;
}

if (value.length === 1) {
profile[name] = attrValueMapper(value[0]);
} else {
profile[name] = value.map(attrValueMapper);
}
profile[name] = value;
});

profile.attributes = profileAttributes;
}
}

Expand Down
6 changes: 1 addition & 5 deletions test/node-saml/tests.spec.ts
Expand Up @@ -1992,11 +1992,6 @@ describe("node-saml /", function () {
'<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0">' +
"<saml:Issuer>http://idp.example.com/metadata.php</saml:Issuer>" +
"<saml2:AttributeStatement>" +
'<saml2:Attribute Name="attributeName" ' +
'NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified">' +
'<saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xsi:type="xs:string"/>' +
"</saml2:Attribute>" +
'<saml2:Attribute Name="issuer" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">' +
'<saml2:AttributeValue xsi:type="xs:string">test</saml2:AttributeValue>' +
Expand All @@ -2010,6 +2005,7 @@ describe("node-saml /", function () {
});

should(profile!.issuer).not.be.equal("test");
should(profile!.attributes).containEql({ issuer: "test" });
});
});

Expand Down

0 comments on commit 4e19f64

Please sign in to comment.