Skip to content

Commit

Permalink
Added support for "RequestAbstractType.Extensions" through notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Veselý committed Jan 9, 2019
1 parent 6a155ef commit 30f8a5f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
30 changes: 24 additions & 6 deletions Sustainsys.Saml2/SAML2P/Saml2RequestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ public string MessageName
/// </summary>
public EntityId Issuer { get; set; }

/// <summary>
/// The SAML2 request name
/// </summary>
protected abstract string LocalName { get; }
/// <summary>
/// The additional content to append within an Extensions element.
/// </summary>
public List<XElement> ExtensionContents { get; } = new List<XElement>();

/// <summary>
/// The SAML2 request name
/// </summary>
protected abstract string LocalName { get; }

/// <summary>
/// Creates XNodes for the fields of the Saml2RequestBase class. These
Expand All @@ -109,7 +114,12 @@ protected IEnumerable<XObject> ToXNodes()
{
yield return new XElement(Saml2Namespaces.Saml2 + "Issuer", Issuer.Id);
}
}

if (ExtensionContents != null && ExtensionContents.Count > 0)
{
yield return new XElement(Saml2Namespaces.Saml2P + "Extensions", ExtensionContents);
}
}

/// <summary>
/// Reads the request properties present in Saml2RequestBase
Expand All @@ -136,7 +146,15 @@ protected void ReadBaseProperties(XmlElement xml)
{
Issuer = new EntityId(issuerNode.InnerXml);
}
}

var extensionsNode = xml["Extensions", Saml2Namespaces.Saml2PName];
if (extensionsNode != null && extensionsNode.HasChildNodes)
{
XElement converted = XElement.Parse(extensionsNode.OuterXml);
ExtensionContents.Clear();
ExtensionContents.AddRange(converted.Elements());
}
}

private void ValidateCorrectDocument(XmlElement xml)
{
Expand Down
42 changes: 40 additions & 2 deletions Tests/Tests.Shared/Saml2P/Saml2AuthenticationRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public void Saml2AuthenticationRequest_ForceAuthentication()
.Should().NotBeNull().And.Subject.Value.Should().Be("true");
}

[TestMethod]
[TestMethod]
public void Saml2AuthenticationRequest_Extensions()
{
var request = new Saml2AuthenticationRequest();
request.ExtensionContents.Add(new XElement(XNamespace.Get("test") + "aditional"));
var subject = request.ToXElement();

subject.Should().NotBeNull().And.Subject
.Element(Saml2Namespaces.Saml2P + "Extensions").Should().NotBeNull().And.Subject
.Elements().Should().HaveCount(1).And.Subject
.First().Name.LocalName.Should().Be("aditional");
}

[TestMethod]
public void Saml2AuthenticationRequest_Read()
{
var xmlData = @"<?xml version=""1.0"" encoding=""UTF-8""?>
Expand Down Expand Up @@ -218,7 +231,32 @@ public void Saml2AuthenticationRequest_Read_NoFormat()
subject.NameIdPolicy.Format.Should().Be(NameIdFormat.NotConfigured);
}

[TestMethod]
[TestMethod]
public void Saml2AuthenticationRequest_Read_Extensions()
{
var xmlData = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<samlp:AuthnRequest
xmlns:samlp=""urn:oasis:names:tc:SAML:2.0:protocol""
xmlns:saml=""urn:oasis:names:tc:SAML:2.0:assertion""
ID=""Saml2AuthenticationRequest_AssertionConsumerServiceUrl""
Version=""2.0""
Destination=""http://destination.example.com""
AssertionConsumerServiceURL=""https://sp.example.com/SAML2/Acs""
IssueInstant=""2004-12-05T09:21:59Z""
ForceAuthn=""true"">
<saml:Issuer>https://sp.example.com/SAML2</saml:Issuer>
<samlp:Extensions>
<additional xmlns=""testurn:test"" />
</samlp:Extensions>
</samlp:AuthnRequest>
";

var subject = Saml2AuthenticationRequest.Read(xmlData, null);
subject.ExtensionContents.Should().HaveCount(1);
subject.ExtensionContents[0].ToString().Should().BeEquivalentTo(@"<additional xmlns=""testurn:test"" />");
}

[TestMethod]
public void Saml2AuthenticationRequest_ToXElement_AddsElementSaml2NameIdPolicy_ForAllowCreate()
{
var subject = new Saml2AuthenticationRequest()
Expand Down

0 comments on commit 30f8a5f

Please sign in to comment.