Skip to content

Commit

Permalink
Removed Saml2REquesterId class - It's just an EntityId
Browse files Browse the repository at this point in the history
- Use Uri and EntityId types instead of strings.
- Add nullcheck for required ProviderId property of IdpEntry.
  • Loading branch information
AndersAbel committed Apr 27, 2016
1 parent c9b4bbd commit 0a501f6
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 76 deletions.
1 change: 1 addition & 0 deletions Kentor.AuthServices.Tests/Kentor.AuthServices.Tests.csproj
Expand Up @@ -129,6 +129,7 @@
<Compile Include="Saml2NameIdExtensionsTests.cs" />
<Compile Include="Saml2P\Saml2ArtifactResolveTests.cs" />
<Compile Include="Saml2P\Saml2ArtifactResponseTests.cs" />
<Compile Include="Saml2P\Saml2IdpEntryTests.cs" />
<Compile Include="Saml2P\Saml2LogoutRequestTests.cs" />
<Compile Include="Saml2P\Saml2LogoutResponseTests.cs" />
<Compile Include="Saml2P\Saml2ScopingTests.cs" />
Expand Down
32 changes: 17 additions & 15 deletions Kentor.AuthServices.Tests/Saml2P/Saml2AuthenticationRequestTests.cs
Expand Up @@ -7,6 +7,7 @@
using System.Xml;
using Kentor.AuthServices.Saml2P;
using System.Linq;
using System.IdentityModel.Metadata;

namespace Kentor.AuthServices.Tests.Saml2P
{
Expand Down Expand Up @@ -237,10 +238,10 @@ public void Saml2AuthenticationRequest_ToXElement_AddsElementSaml2NameIdPolicy_F
[TestMethod]
public void Saml2AuthenticationRequest_ToXElement_AddsScoping()
{
var requesterId = new Uri("urn://requesterid/");
var location = "location";
var requesterId = "urn://requesterid/";
var location = "http://location";
var name = "name";
var providerId = "providerId";
var providerId = "urn:providerId";

var subject = new Saml2AuthenticationRequest()
{
Expand All @@ -249,23 +250,24 @@ public void Saml2AuthenticationRequest_ToXElement_AddsScoping()
{
ProxyCount = 5
}
.With(new Saml2IdpEntry(location, name, providerId))
.With(new Saml2RequesterId(requesterId))
.With(new Saml2IdpEntry(new EntityId(providerId))
{
Name = name,
Location = new Uri(location)
})
.WithRequesterId(new EntityId(requesterId))
};

var actual = subject.ToXElement().Element(Saml2Namespaces.Saml2P + "Scoping");

var expected = new XElement(Saml2Namespaces.Saml2P + "root",
new XAttribute(XNamespace.Xmlns + "saml2p", Saml2Namespaces.Saml2P),
new XElement(Saml2Namespaces.Saml2P + "Scoping",
new XAttribute("ProxyCount", "5"),
new XElement(Saml2Namespaces.Saml2P + "IDPList",
new XElement(Saml2Namespaces.Saml2P + "IDPEntry",
new XAttribute("ProviderID", providerId),
new XAttribute("Name", name),
var expected = new XElement(Saml2Namespaces.Saml2P + "Scoping",
new XAttribute("ProxyCount", "5"),
new XElement(Saml2Namespaces.Saml2P + "IDPList",
new XElement(Saml2Namespaces.Saml2P + "IDPEntry",
new XAttribute("ProviderID", providerId),
new XAttribute("Name", name),
new XAttribute("Loc", location))),
new XElement(Saml2Namespaces.Saml2P + "RequesterID", requesterId.ToString())))
.Elements().Single();
new XElement(Saml2Namespaces.Saml2P + "RequesterID", requesterId.ToString()));

actual.Should().BeEquivalentTo(expected);
}
Expand Down
21 changes: 21 additions & 0 deletions Kentor.AuthServices.Tests/Saml2P/Saml2IdpEntryTests.cs
@@ -0,0 +1,21 @@
using FluentAssertions;
using Kentor.AuthServices.Saml2P;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IdentityModel.Metadata;

namespace Kentor.AuthServices.Tests.Saml2P
{
[TestClass]
public class Saml2IdpEntryTests
{
[TestMethod]
public void Saml2IdpEntry_NullCheckProviderId()
{
var subject = new Saml2IdpEntry(new EntityId("urn:foo"));

subject.Invoking(s => s.ProviderId = null)
.ShouldThrow<ArgumentNullException>();
}
}
}
1 change: 0 additions & 1 deletion Kentor.AuthServices/Kentor.AuthServices.csproj
Expand Up @@ -80,7 +80,6 @@
<Compile Include="Saml2P\Saml2ArtifactResolve.cs" />
<Compile Include="Saml2P\Saml2ArtifactResponse.cs" />
<Compile Include="Saml2P\Saml2NameIdPolicy.cs" />
<Compile Include="Saml2P\Saml2RequesterId.cs" />
<Compile Include="Saml2P\Saml2Scoping.cs" />
<Compile Include="Saml2P\Saml2SoapBinding.cs" />
<Compile Include="Saml2P\Saml2StatusResponseType.cs" />
Expand Down
10 changes: 5 additions & 5 deletions Kentor.AuthServices/SAML2P/Saml2AuthenticationRequest.cs
Expand Up @@ -71,7 +71,7 @@ private void AddNameIdPolicy(XElement xElement)
if (NameIdPolicy != null &&
(NameIdPolicy.AllowCreate.HasValue || NameIdPolicy.Format != NameIdFormat.NotConfigured))
{
if(NameIdPolicy.AllowCreate.HasValue && NameIdPolicy.Format == NameIdFormat.Transient)
if (NameIdPolicy.AllowCreate.HasValue && NameIdPolicy.Format == NameIdFormat.Transient)
{
throw new InvalidOperationException("When NameIdPolicy/Format is set to Transient, it is not permitted to specify AllowCreate. Change Format or leave AllowCreate as null.");
}
Expand Down Expand Up @@ -167,7 +167,7 @@ public Saml2AuthenticationRequest(XmlElement xml, string relayState)
allowCreate = bool.Parse(allowCreateStr);
}

NameIdPolicy = new Saml2NameIdPolicy(allowCreate, nameIdFormat);
NameIdPolicy = new Saml2NameIdPolicy(allowCreate, nameIdFormat);
}
}

Expand All @@ -182,9 +182,9 @@ public Saml2AuthenticationRequest(XmlElement xml, string relayState)
public int? AttributeConsumingServiceIndex { get; set; }

/// <summary>
/// Scoping for request
/// </summary>
public Saml2Scoping Scoping { get; set; }
/// Scoping for request
/// </summary>
public Saml2Scoping Scoping { get; set; }

/// <summary>
/// NameId policy.
Expand Down
51 changes: 33 additions & 18 deletions Kentor.AuthServices/SAML2P/Saml2IdPEntry.cs
@@ -1,40 +1,55 @@
using System.Xml.Linq;
using System;
using System.IdentityModel.Metadata;
using System.Xml.Linq;

namespace Kentor.AuthServices.Saml2P
{
/// <summary>
/// The Saml2IdPEntry specifies a single identity provider trusted by the requester to authenticate the presenter
/// The Saml2IdPEntry specifies a single identity provider trusted by the
/// requester to authenticate the presenter
/// </summary>
public class Saml2IdpEntry
{
/// <summary>
/// Initializes a new instance of the <see cref="Saml2IdpEntry"/> class.
/// </summary>
/// <param name="location">The location.</param>
/// <param name="name">The name.</param>
/// <param name="providerId">The provider identifier.</param>
public Saml2IdpEntry(string location, string name, string providerId)
public Saml2IdpEntry(EntityId providerId)
{
Location = location;
Name = name;
ProviderId = providerId;
}

/// <summary>
/// Gets or sets the a URI reference representing the location of a profile-specific endpoint supporting
/// the authentication request protocol.The binding to be used must be understood from the profile of use.
/// A URI reference representing the location of a profile-specific
/// endpoint supporting the authentication request protocol. The
/// binding to be used must be understood from the profile of use.
/// </summary>
/// <value>The location.</value>
public string Location { get; set; }
public Uri Location { get; set; }

/// <summary>
/// Gets or sets a human-readable name for the identity provider
/// A human-readable name for the identity provider.
/// </summary>
/// <value>The name.</value>
public string Name { get; set; }

EntityId providerId;
/// <summary>
/// Gets or sets the unique identifier of the identity provider.
/// The Entity Id of the Identity Provider. Cannot be null.
/// </summary>
/// <value>The provider identifier.</value>
public string ProviderId { get; set; }
public EntityId ProviderId
{
get
{
return providerId;
}
set
{
if(value == null)
{
throw new ArgumentNullException(nameof(value));
}
providerId = value;
}
}

/// <summary>
/// Create XElement for the Saml2IdPEntry.
Expand All @@ -43,9 +58,9 @@ public XElement ToXElement()
{
var idpEntryElement = new XElement(Saml2Namespaces.Saml2P + "IDPEntry");

idpEntryElement.AddAttributeIfNotNullOrEmpty("ProviderID", ProviderId);
idpEntryElement.AddAttributeIfNotNullOrEmpty("ProviderID", ProviderId.Id);
idpEntryElement.AddAttributeIfNotNullOrEmpty("Name", Name);
idpEntryElement.AddAttributeIfNotNullOrEmpty("Loc", Location);
idpEntryElement.AddAttributeIfNotNullOrEmpty("Loc", Location.OriginalString);

return idpEntryElement;
}
Expand Down
33 changes: 0 additions & 33 deletions Kentor.AuthServices/SAML2P/Saml2RequesterId.cs

This file was deleted.

10 changes: 6 additions & 4 deletions Kentor.AuthServices/SAML2P/Saml2Scoping.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IdentityModel.Metadata;
using System.Linq;
using System.Xml.Linq;

Expand Down Expand Up @@ -62,15 +63,15 @@ public Saml2Scoping With(Saml2IdpEntry idpEntry)
/// requester is acting. Used to communicate the chain of requesters
/// when proxying occurs.
/// </summary>
public IList<Saml2RequesterId> RequesterIds { get; } = new List<Saml2RequesterId>();
public IList<EntityId> RequesterIds { get; } = new List<EntityId>();

/// <summary>
/// Fluent config helper that adds a <see cref="Saml2RequesterId"/> to the
/// Fluent config helper that adds a requester id to the
/// <see cref="Saml2Scoping"/>
/// </summary>
/// <param name="requesterId">Requester Id to add</param>
/// <returns>this</returns>
public Saml2Scoping With(Saml2RequesterId requesterId)
public Saml2Scoping WithRequesterId(EntityId requesterId)
{
RequesterIds.Add(requesterId);
return this;
Expand All @@ -97,7 +98,8 @@ public XElement ToXElement()

if (RequesterIds != null && RequesterIds.Count > 0)
{
scopingElement.Add(RequesterIds.Select(x => x.ToXElement()));
scopingElement.Add(RequesterIds.Select(x =>
new XElement(Saml2Namespaces.Saml2P + "RequesterID", x.Id)));
}

return scopingElement;
Expand Down

0 comments on commit 0a501f6

Please sign in to comment.