Skip to content

Commit 2a1699b

Browse files
authored
Add assertion attributes to child object on profile (#593)
* Fix: Conflicting profile properties between profile and attributes (#543) * Add assertion attributes to child object on profile (#543) This attributes are also mounted to profile directly in a non conflicting way. Co-authored-by: Shashank Singh Solanki <[email protected]>
1 parent c7b7226 commit 2a1699b

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ node_modules/
55
yarn-error.log
66
.DS_Store
77
.eslintcache
8-
.dir-locals.el
8+
.dir-locals.el
9+
10+
## Local VS code settings and debug profiles
11+
.vscode

src/node-saml/saml.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -1170,18 +1170,33 @@ class SAML {
11701170
};
11711171

11721172
if (attributes) {
1173+
const profileAttributes: Record<string, unknown> = {};
1174+
11731175
attributes.forEach((attribute) => {
11741176
if (!Object.prototype.hasOwnProperty.call(attribute, "AttributeValue")) {
11751177
// if attributes has no AttributeValue child, continue
11761178
return;
11771179
}
1178-
const value = attribute.AttributeValue;
1179-
if (value.length === 1) {
1180-
profile[attribute.$.Name] = attrValueMapper(value[0]);
1181-
} else {
1182-
profile[attribute.$.Name] = value.map(attrValueMapper);
1180+
1181+
const name = attribute.$.Name;
1182+
const value =
1183+
attribute.AttributeValue.length === 1
1184+
? attrValueMapper(attribute.AttributeValue[0])
1185+
: attribute.AttributeValue.map(attrValueMapper);
1186+
1187+
profileAttributes[name] = value;
1188+
1189+
// If any property is already present in profile and is also present
1190+
// in attributes, then skip the one from attributes. Handle this
1191+
// conflict gracefully without returning any error
1192+
if (Object.prototype.hasOwnProperty.call(profile, name)) {
1193+
return;
11831194
}
1195+
1196+
profile[name] = value;
11841197
});
1198+
1199+
profile.attributes = profileAttributes;
11851200
}
11861201
}
11871202

test/node-saml/tests.spec.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1904,10 +1904,13 @@ describe("node-saml /", function () {
19041904
});
19051905
});
19061906
describe("validatePostRequest()", function () {
1907+
const signingKey: any = fs.readFileSync(__dirname + "/../static/key.pem", "ascii");
1908+
const signingCert: any = fs.readFileSync(__dirname + "/../static/cert.pem", "ascii");
19071909
let samlObj: SAML;
1910+
19081911
beforeEach(function () {
19091912
samlObj = new SAML({
1910-
cert: fs.readFileSync(__dirname + "/../static/cert.pem", "ascii"),
1913+
cert: signingCert,
19111914
});
19121915
});
19131916

@@ -1981,7 +1984,31 @@ describe("node-saml /", function () {
19811984
sessionIndex: "1",
19821985
});
19831986
});
1987+
1988+
it("check conflicting profile fields with data from attributes", async () => {
1989+
const testSAMLObj = new SAML({ cert: signingCert, issuer: "okta" });
1990+
const xml =
1991+
'<Response xmlns="urn:oasis:names:tc:SAML:2.0:protocol" ID="response0">' +
1992+
'<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0">' +
1993+
"<saml:Issuer>http://idp.example.com/metadata.php</saml:Issuer>" +
1994+
"<saml2:AttributeStatement>" +
1995+
"</saml2:Attribute>" +
1996+
'<saml2:Attribute Name="issuer" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">' +
1997+
'<saml2:AttributeValue xsi:type="xs:string">test</saml2:AttributeValue>' +
1998+
"</saml2:Attribute>" +
1999+
"</saml2:AttributeStatement>" +
2000+
"</saml2:Assertion>" +
2001+
"</Response>";
2002+
const signedXml = signXmlResponse(xml, { privateKey: signingKey });
2003+
const { profile } = await testSAMLObj.validatePostResponseAsync({
2004+
SAMLResponse: Buffer.from(signedXml).toString("base64"),
2005+
});
2006+
2007+
should(profile!.issuer).not.be.equal("test");
2008+
should(profile!.attributes).containEql({ issuer: "test" });
2009+
});
19842010
});
2011+
19852012
it("validatePostRequest errors for encrypted nameID with wrong decryptionPvk", async () => {
19862013
const samlObj = new SAML({
19872014
cert: fs.readFileSync(__dirname + "/../static/cert.pem", "ascii"),

0 commit comments

Comments
 (0)