Skip to content

Commit

Permalink
fix: add xmlns:xs and nmlns:xsi attribute (#271)
Browse files Browse the repository at this point in the history
* fix: add xmlns:xs and nmlns:xsi attribute

* fix: won't generate saml attribute valueXmlnsXs valueXmlnsXsi if not provided

* refact: add xmlns:xs and xmlns:xsi and set default value

* fix unit tests
  • Loading branch information
yelexin authored and tngan committed Jun 25, 2019
1 parent 371612e commit b1956d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/libsaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export interface LoginResponseAttribute {
nameFormat: string; //
valueXsiType: string; //
valueTag: string;
valueXmlnsXs?: string;
valueXmlnsXsi?: string;
}

export interface BaseSamlTemplate {
Expand Down Expand Up @@ -232,8 +234,10 @@ const libSaml = () => {
* @return {string}
*/
attributeStatementBuilder(attributes: LoginResponseAttribute[]): string {
const attr = attributes.map(({ name, nameFormat, valueTag, valueXsiType }) => {
return `<saml:Attribute Name="${name}" NameFormat="${nameFormat}"><saml:AttributeValue xsi:type="${valueXsiType}">{${tagging('attr', valueTag)}}</saml:AttributeValue></saml:Attribute>`;
const attr = attributes.map(({ name, nameFormat, valueTag, valueXsiType, valueXmlnsXs, valueXmlnsXsi }) => {
const defaultValueXmlnsXs = 'http://www.w3.org/2001/XMLSchema'
const defaultValueXmlnsXsi = 'http://www.w3.org/2001/XMLSchema-instance'
return `<saml:Attribute Name="${name}" NameFormat="${nameFormat}"><saml:AttributeValue xmlns:xs="${valueXmlnsXs ? valueXmlnsXs : defaultValueXmlnsXs}" xmlns:xsi="${valueXmlnsXsi ? valueXmlnsXsi : defaultValueXmlnsXsi}" xsi:type="${valueXsiType}">{${tagging('attr', valueTag)}}</saml:AttributeValue></saml:Attribute>`;
}).join('');
return `<saml:AttributeStatement>${attr}</saml:AttributeStatement>`;
},
Expand Down
6 changes: 3 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ test('getAssertionConsumerService with two bindings', t => {
name: 'email',
valueTag: 'user.email',
nameFormat: 'urn:oasis:names:tc:SAML:2.0:attrname-format:basic',
valueXsiType: 'xs:string',
valueXsiType: 'xs:string'
}];
const expectedStatement = '<saml:AttributeStatement><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">{attrUserEmail}</saml:AttributeValue></saml:Attribute></saml:AttributeStatement>';
const expectedStatement = '<saml:AttributeStatement><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">{attrUserEmail}</saml:AttributeValue></saml:Attribute></saml:AttributeStatement>';

t.is(libsaml.attributeStatementBuilder(attributes), expectedStatement);
});
Expand All @@ -303,7 +303,7 @@ test('getAssertionConsumerService with two bindings', t => {
nameFormat: 'urn:oasis:names:tc:SAML:2.0:attrname-format:basic',
valueXsiType: 'xs:string',
}];
const expectedStatement = '<saml:AttributeStatement><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">{attrUserEmail}</saml:AttributeValue></saml:Attribute><saml:Attribute Name="firstname" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">{attrUserFirstname}</saml:AttributeValue></saml:Attribute></saml:AttributeStatement>';
const expectedStatement = '<saml:AttributeStatement><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">{attrUserEmail}</saml:AttributeValue></saml:Attribute><saml:Attribute Name="firstname" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">{attrUserFirstname}</saml:AttributeValue></saml:Attribute></saml:AttributeStatement>';
t.is(libsaml.attributeStatementBuilder(attributes), expectedStatement);
});
})();
Expand Down

0 comments on commit b1956d3

Please sign in to comment.