Skip to content

Commit

Permalink
Fix usage of IDX13300 and IDX13107 (#1458)
Browse files Browse the repository at this point in the history
* Fix usage of IDX13300 and IDX13107
  • Loading branch information
keegan-caruso committed Jun 23, 2020
1 parent 63f2585 commit 0567929
Show file tree
Hide file tree
Showing 17 changed files with 498 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Uri Namespace
throw LogArgumentNullException(nameof(value));

if (!value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, value), nameof(value)));
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(Namespace), value), nameof(value)));

_namespace = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Uri NameFormat
{
get => _nameFormat;
set => _nameFormat = (value != null && !value.IsAbsoluteUri)
? throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, value), nameof(value)))
? throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(NameFormat), value), nameof(value)))
: value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class Saml2AuthenticationContext
/// Creates an instance of Saml2AuthenticationContext.
/// </summary>
public Saml2AuthenticationContext()
: this(null, null)
{
}

Expand All @@ -62,8 +61,8 @@ public Saml2AuthenticationContext()
/// </summary>
/// <param name="classReference">The class reference of the authentication context.</param>
public Saml2AuthenticationContext(Uri classReference)
: this(classReference, null)
{
ClassReference = classReference;
}

/// <summary>
Expand All @@ -75,7 +74,6 @@ public Saml2AuthenticationContext(Uri classReference, Uri declarationReference)
{
ClassReference = classReference;
DeclarationReference = declarationReference;
AuthenticatingAuthorities = new List<Uri>();
}

/// <summary>
Expand All @@ -87,7 +85,7 @@ public Saml2AuthenticationContext(Uri classReference, Uri declarationReference)
public ICollection<Uri> AuthenticatingAuthorities
{
get;
}
} = new List<Uri>();

/// <summary>
/// Gets or sets a URI reference identifying an authentication context class that
Expand All @@ -105,7 +103,7 @@ public Uri ClassReference
throw LogArgumentNullException(nameof(value));

if (!value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(value), value)));
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(ClassReference), value)));

_classReference = value;
}
Expand All @@ -121,8 +119,11 @@ public Uri DeclarationReference
get { return _declarationReference; }
set
{
if (value != null && !value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(value), value)));
if (value == null)
throw LogArgumentNullException(nameof(value));

if (!value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(DeclarationReference), value)));

_declarationReference = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Uri Resource
throw LogArgumentNullException(nameof(value));

if (!value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(value), value)));
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(Resource), value)));

_resource = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Uri Format
set
{
if (null != value && !value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, value), nameof(value)));
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(Format), value), nameof(value)));

_format = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ protected virtual Saml2Attribute CreateAttribute(Claim claim)
if (claim.Properties.TryGetValue(ClaimProperties.SamlAttributeNameFormat, out string nameFormat))
{
if (!Saml2Serializer.CanCreateValidUri(nameFormat, UriKind.Absolute))
throw LogExceptionMessage(new Saml2SecurityTokenException(FormatInvariant(LogMessages.IDX13300, ClaimProperties.SamlAttributeNameFormat)));
throw LogExceptionMessage(new Saml2SecurityTokenException(FormatInvariant(LogMessages.IDX13300, ClaimProperties.SamlAttributeNameFormat, nameFormat)));

attribute.NameFormat = new Uri(nameFormat);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,10 @@ protected virtual Saml2AuthenticationContext ReadAuthenticationContext(XmlDictio
reader.ReadStartElement(Saml2Constants.Elements.AuthnContextDeclRef, Saml2Constants.Namespace);

// Now we have enough data to create the object
var authnContext = new Saml2AuthenticationContext(classRef, declRef);
var authnContext = new Saml2AuthenticationContext(classRef);

if (declRef != null)
authnContext.DeclarationReference = declRef;

// <AuthenticatingAuthority> - 0-OO
while (reader.IsStartElement(Saml2Constants.Elements.AuthenticatingAuthority, Saml2Constants.Namespace))
Expand Down Expand Up @@ -2300,7 +2303,7 @@ internal static Uri ReadSimpleUriElement(XmlDictionaryReader reader, string elem
throw LogReadException(LogMessages.IDX13136, element);

if (requireUri && !CanCreateValidUri(value, kind))
throw LogReadException(LogMessages.IDX13107, element, value);
throw LogReadException(LogMessages.IDX13107, element, element, value);

return new Uri(value, kind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Saml2SubjectConfirmation(Uri method, Saml2SubjectConfirmationData subject
throw LogArgumentNullException(nameof(method));

if (!method.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, method), nameof(method)));
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(method), method), nameof(method)));

_method = method;
SubjectConfirmationData = subjectConfirmationData;
Expand All @@ -81,7 +81,7 @@ public Uri Method
throw LogArgumentNullException(nameof(value));

if (!value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, value), nameof(value)));
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(Method), value), nameof(value)));

_method = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Uri Recipient
throw LogArgumentNullException(nameof(value));

if (!value.IsAbsoluteUri)
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(value), value)));
throw LogExceptionMessage(new ArgumentException(FormatInvariant(LogMessages.IDX13300, nameof(Recipient), value)));

_recipient = value;
}
Expand Down
60 changes: 60 additions & 0 deletions test/Microsoft.IdentityModel.Tokens.Saml.Tests/Saml2ActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//------------------------------------------------------------------------------

using System;
using Xunit;
using Microsoft.IdentityModel.Tokens.Saml2;

namespace Microsoft.IdentityModel.Tokens.Saml.Tests
{
public class Saml2ActionTests
{
[Fact]
public void Saml2Action_NullValue_ArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new Saml2Action(null, new Uri("http://localhost", UriKind.Absolute)));
}

[Fact]
public void Saml2Action_NullNamespace_ArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new Saml2Action("resource", null));
}

[Fact]
public void Saml2Action_RelativeNamespace_ArgumentException()
{
Assert.Throws<ArgumentException>(() => new Saml2Action(null, new Uri("api", UriKind.Relative)));
}

[Fact]
public void Saml2Action_CanCreate()
{
new Saml2Action("resource", new Uri("http://localhost", UriKind.Absolute));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//------------------------------------------------------------------------------

using System;
using Xunit;
using Microsoft.IdentityModel.Tokens.Saml2;

namespace Microsoft.IdentityModel.Tokens.Saml.Tests
{
public class Saml2AttributeTests
{
[Fact]
public void Saml2Attribute_RelativeNameFormat_ArgumentException()
{
var attr = new Saml2Attribute("Country");

Assert.Throws<ArgumentException>(() => attr.NameFormat = new Uri("resource", UriKind.Relative));
}

[Fact]
public void Saml2Attribute_NullNameFormat_NoException()
{
new Saml2Attribute("Country")
{
NameFormat = null
};
}

[Fact]
public void Saml2Attribute_AbsoluteNameFormat_NoException()
{
new Saml2Attribute("Country")
{
NameFormat = new Uri("http://resource", UriKind.Absolute)
};
}
}
}
Loading

0 comments on commit 0567929

Please sign in to comment.