Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CaseSensitiveClaimsIdentity to 7x #2715

Merged
merged 10 commits into from
Jul 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private ClaimsIdentity CreateClaimsIdentityWithMapping(JsonWebToken jwtToken, To
{
_ = validationParameters ?? throw LogHelper.LogArgumentNullException(nameof(validationParameters));

ClaimsIdentity identity = validationParameters.CreateClaimsIdentity(jwtToken, issuer);
ClaimsIdentity identity = ClaimsIdentityFactory.Create(jwtToken, validationParameters, issuer);
foreach (Claim jwtClaim in jwtToken.Claims)
{
bool wasMapped = _inboundClaimTypeMap.TryGetValue(jwtClaim.Type, out string claimType);
Expand Down Expand Up @@ -296,7 +296,7 @@ private ClaimsIdentity CreateClaimsIdentityPrivate(JsonWebToken jwtToken, TokenV
{
_ = validationParameters ?? throw LogHelper.LogArgumentNullException(nameof(validationParameters));

ClaimsIdentity identity = validationParameters.CreateClaimsIdentity(jwtToken, issuer);
ClaimsIdentity identity = ClaimsIdentityFactory.Create(jwtToken, validationParameters, issuer);
foreach (Claim jwtClaim in jwtToken.Claims)
{
string claimType = jwtClaim.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public SecurityTokenDescriptor CreateTokenDescriptorWithInstanceOverrides()
{
var securityTokenDescriptor = new SecurityTokenDescriptor()
{
Subject = new ClaimsIdentity(_payloadClaims),
Subject = ClaimsIdentityFactory.Create(_payloadClaims),
};

if (!string.IsNullOrEmpty(Issuer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ protected virtual IEnumerable<ClaimsIdentity> ProcessStatements(SamlSecurityToke

if (!identityDict.TryGetValue(statement.Subject, out ClaimsIdentity identity))
{
identity = validationParameters.CreateClaimsIdentity(samlToken, issuer);
identity = ClaimsIdentityFactory.Create(samlToken, validationParameters, issuer);
ProcessSubject(statement.Subject, identity, issuer);
identityDict.Add(statement.Subject, identity);
}
Expand Down Expand Up @@ -870,7 +870,7 @@ protected virtual void SetDelegateFromAttribute(SamlAttribute attribute, ClaimsI
}
}

subject.Actor = new ClaimsIdentity(claims, "Federation");
subject.Actor = ClaimsIdentityFactory.Create(claims, "Federation");
SetDelegateFromAttribute(actingAsAttribute, subject.Actor, issuer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ protected virtual void SetClaimsIdentityActorFromAttribute(Saml2Attribute attrib
}
}

identity.Actor = new ClaimsIdentity(claims);
identity.Actor = ClaimsIdentityFactory.Create(claims);
SetClaimsIdentityActorFromAttribute(actorAttribute, identity.Actor, issuer);
}

Expand Down Expand Up @@ -1307,7 +1307,8 @@ protected virtual ClaimsIdentity CreateClaimsIdentity(Saml2SecurityToken samlTok
actualIssuer = ClaimsIdentity.DefaultIssuer;
}

var identity = validationParameters.CreateClaimsIdentity(samlToken, actualIssuer);
var identity = ClaimsIdentityFactory.Create(samlToken, validationParameters, issuer);

ProcessSubject(samlToken.Assertion.Subject, identity, actualIssuer);
ProcessStatements(samlToken.Assertion.Statements, identity, actualIssuer);

Expand Down
21 changes: 21 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/AppContextSwitches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Security.Claims;

namespace Microsoft.IdentityModel.Tokens
{
/// <summary>
/// AppContext switches for Microsoft.IdentityModel.Tokens and referencing packages.
/// </summary>
internal static class AppContextSwitches
{
/// <summary>
/// Enables a new behavior of using <see cref="CaseSensitiveClaimsIdentity"/> instead of <see cref="ClaimsIdentity"/> globally.
/// </summary>
internal const string UseCaseSensitiveClaimsIdentityIdentityTypeSwitch = "Microsoft.IdentityModel.Tokens.UseCaseSensitiveClaimsIdentity";
keegan-caruso marked this conversation as resolved.
Show resolved Hide resolved

internal static bool UseCaseSensitiveClaimsIdentityType() => (AppContext.TryGetSwitch(UseCaseSensitiveClaimsIdentityIdentityTypeSwitch, out bool useCaseSensitiveClaimsIdentityType) && useCaseSensitiveClaimsIdentityType);
}
}
122 changes: 122 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/CaseSensitiveClaimsIdentity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Security.Claims;

namespace Microsoft.IdentityModel.Tokens
{
/// <summary>
/// A derived <see cref="ClaimsIdentity"/> where claim retrieval is case-sensitive. The current <see cref="ClaimsIdentity"/> retrieves claims in a case-insensitive manner which is different than querying the underlying <see cref="SecurityToken"/>. The <see cref="CaseSensitiveClaimsIdentity"/> provides consistent retrieval logic between the <see cref="SecurityToken"/> and <see cref="ClaimsIdentity"/>.
/// </summary>
public class CaseSensitiveClaimsIdentity : ClaimsIdentity
{
/// <summary>
/// Gets the <see cref="SecurityToken"/> associated with this claims identity.
/// </summary>
public SecurityToken SecurityToken { get; internal set; }

/// <summary>
/// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>.
/// </summary>
public CaseSensitiveClaimsIdentity() : base()
{
}

/// <summary>
/// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>.
/// </summary>
/// <param name="authenticationType">The authentication method used to establish this identity.</param>
public CaseSensitiveClaimsIdentity(string authenticationType) : base(authenticationType)
{
}

/// <summary>
/// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>.
/// </summary>
/// <param name="claimsIdentity"><see cref="ClaimsIdentity"/> to copy.</param>
public CaseSensitiveClaimsIdentity(ClaimsIdentity claimsIdentity) : base(claimsIdentity)
{
}

/// <summary>
/// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>.
/// </summary>
/// <param name="claims"><see cref="IEnumerable{Claim}"/> associated with this instance.</param>
public CaseSensitiveClaimsIdentity(IEnumerable<Claim> claims) : base(claims)
{
}

/// <summary>
/// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>.
/// </summary>
/// <param name="claims"><see cref="IEnumerable{Claim}"/> associated with this instance.</param>
/// <param name="authenticationType">The authentication method used to establish this identity.</param>
public CaseSensitiveClaimsIdentity(IEnumerable<Claim> claims, string authenticationType) : base(claims, authenticationType)
{
}

/// <summary>
/// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>.
/// </summary>
/// <param name="claims"><see cref="IEnumerable{Claim}"/> associated with this instance.</param>
/// <param name="authenticationType">The authentication method used to establish this identity.</param>
/// <param name="nameType">The <see cref="Claim.Type"/> used when obtaining the value of <see cref="ClaimsIdentity.Name"/>.</param>
/// <param name="roleType">The <see cref="Claim.Type"/> used when performing logic for <see cref="ClaimsPrincipal.IsInRole"/>.</param>
public CaseSensitiveClaimsIdentity(IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType) :
base(claims, authenticationType, nameType, roleType)
{
}

/// <summary>
/// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>.
/// </summary>
/// <param name="authenticationType">The authentication method used to establish this identity.</param>
/// <param name="nameType">The <see cref="Claim.Type"/> used when obtaining the value of <see cref="ClaimsIdentity.Name"/>.</param>
/// <param name="roleType">The <see cref="Claim.Type"/> used when performing logic for <see cref="ClaimsPrincipal.IsInRole"/>.</param>
public CaseSensitiveClaimsIdentity(string authenticationType, string nameType, string roleType) :
base(authenticationType, nameType, roleType)
{
}

/// <summary>
/// Retrieves a <see cref="IEnumerable{Claim}"/> where each <see cref="Claim.Type"/> equals <paramref name="type"/>.
/// </summary>
/// <param name="type">The type of the claim to match.</param>
/// <returns>A <see cref="IEnumerable{Claim}"/> of matched claims.</returns>
/// <remarks>Comparison is <see cref="StringComparison.Ordinal"/>.</remarks>
/// <exception cref="ArgumentNullException">if <paramref name="type"/> is null.</exception>
public override IEnumerable<Claim> FindAll(string type)
{
return base.FindAll(claim => claim?.Type.Equals(type, StringComparison.Ordinal) == true);
}

/// <summary>
/// Retrieves the first <see cref="Claim"/> where <see cref="Claim.Type"/> equals <paramref name="type"/>.
/// </summary>
/// <param name="type">The type of the claim to match.</param>
/// <returns>A <see cref="Claim"/>, <see langword="null"/> if nothing matches.</returns>
/// <remarks>Comparison is <see cref="StringComparison.Ordinal"/>.</remarks>
/// <exception cref="ArgumentNullException">if <paramref name="type"/> is null.</exception>
public override Claim FindFirst(string type)
{
return base.FindFirst(claim => claim?.Type.Equals(type, StringComparison.Ordinal) == true);
}

/// <summary>
/// Determines if a claim with type AND value is contained within this claims identity.
/// </summary>
/// <param name="type">The type of the claim to match.</param>
/// <param name="value">The value of the claim to match.</param>
/// <returns><c>true</c> if a claim is matched, <c>false</c> otherwise.</returns>
/// <remarks>Comparison is <see cref="StringComparison.Ordinal"/> for <see cref="Claim.Type"/> and <see cref="Claim.Value"/>.</remarks>
/// <exception cref="ArgumentNullException">if <paramref name="type"/> is null.</exception>
/// <exception cref="ArgumentNullException">if <paramref name="value"/> is null.</exception>
public override bool HasClaim(string type, string value)
{
return base.HasClaim(claim => claim?.Type.Equals(type, StringComparison.Ordinal) == true
&& claim?.Value.Equals(value, StringComparison.Ordinal) == true);
}
}
}
81 changes: 81 additions & 0 deletions src/Microsoft.IdentityModel.Tokens/ClaimsIdentityFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Security.Claims;

namespace Microsoft.IdentityModel.Tokens
{
/// <summary>
/// Facilitates the creation of <see cref="ClaimsIdentity"/> and <see cref="CaseSensitiveClaimsIdentity"/> instances based on the <see cref="AppContextSwitches.UseCaseSensitiveClaimsIdentityIdentityTypeSwitch"/>.
/// </summary>
internal static class ClaimsIdentityFactory
{
internal static ClaimsIdentity Create(IEnumerable<Claim> claims)
{
if (AppContextSwitches.UseCaseSensitiveClaimsIdentityType())
return new CaseSensitiveClaimsIdentity(claims);

return new ClaimsIdentity(claims);
}

internal static ClaimsIdentity Create(IEnumerable<Claim> claims, string authenticationType)
{
if (AppContextSwitches.UseCaseSensitiveClaimsIdentityType())
return new CaseSensitiveClaimsIdentity(claims, authenticationType);

return new ClaimsIdentity(claims, authenticationType);
}

internal static ClaimsIdentity Create(string authenticationType, string nameType, string roleType, SecurityToken securityToken)
{
if (AppContextSwitches.UseCaseSensitiveClaimsIdentityType())
return new CaseSensitiveClaimsIdentity(authenticationType: authenticationType, nameType: nameType, roleType: roleType)
{
SecurityToken = securityToken,
};

return new ClaimsIdentity(authenticationType: authenticationType, nameType: nameType, roleType: roleType);
}

internal static ClaimsIdentity Create(SecurityToken securityToken, TokenValidationParameters validationParameters, string issuer)
{
ClaimsIdentity claimsIdentity = validationParameters.CreateClaimsIdentity(securityToken, issuer);

// Set the SecurityToken in cases where derived TokenValidationParameters created a CaseSensitiveClaimsIdentity.
if (claimsIdentity is CaseSensitiveClaimsIdentity caseSensitiveClaimsIdentity && caseSensitiveClaimsIdentity.SecurityToken == null)
{
caseSensitiveClaimsIdentity.SecurityToken = securityToken;
}
else if (claimsIdentity is not CaseSensitiveClaimsIdentity && AppContextSwitches.UseCaseSensitiveClaimsIdentityType())
{
claimsIdentity = new CaseSensitiveClaimsIdentity(claimsIdentity)
{
SecurityToken = securityToken,
};
}

return claimsIdentity;
}

internal static ClaimsIdentity Create(TokenHandler tokenHandler, SecurityToken securityToken, TokenValidationParameters validationParameters, string issuer)
{
ClaimsIdentity claimsIdentity = tokenHandler.CreateClaimsIdentityInternal(securityToken, validationParameters, issuer);

// Set the SecurityToken in cases where derived TokenHandler created a CaseSensitiveClaimsIdentity.
if (claimsIdentity is CaseSensitiveClaimsIdentity caseSensitiveClaimsIdentity && caseSensitiveClaimsIdentity.SecurityToken == null)
{
caseSensitiveClaimsIdentity.SecurityToken = securityToken;
}
else if (claimsIdentity is not CaseSensitiveClaimsIdentity && AppContextSwitches.UseCaseSensitiveClaimsIdentityType())
{
claimsIdentity = new CaseSensitiveClaimsIdentity(claimsIdentity)
{
SecurityToken = securityToken,
};
}

return claimsIdentity;
}
}
}
5 changes: 2 additions & 3 deletions src/Microsoft.IdentityModel.Tokens/TokenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public abstract class TokenHandler
/// <exception cref="ArgumentOutOfRangeException">'value' less than 1.</exception>
public virtual int MaximumTokenSizeInBytes
{
get => _maximumTokenSizeInBytes;
set => _maximumTokenSizeInBytes = (value < 1) ? throw LogExceptionMessage(new ArgumentOutOfRangeException(nameof(value), FormatInvariant(LogMessages.IDX10101, LogHelper.MarkAsNonPII(value)))) : value;
get => _maximumTokenSizeInBytes;
set => _maximumTokenSizeInBytes = (value < 1) ? throw LogExceptionMessage(new ArgumentOutOfRangeException(nameof(value), FormatInvariant(LogMessages.IDX10101, MarkAsNonPII(value)))) : value;
}

/// <summary>
Expand All @@ -54,7 +54,6 @@ public int TokenLifetimeInMinutes
}

#region methods

/// <summary>
/// Validates a token.
/// On a validation failure, no exception will be thrown; instead, the exception will be set in the returned TokenValidationResult.Exception property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public virtual ClaimsIdentity CreateClaimsIdentity(SecurityToken securityToken,
if (LogHelper.IsEnabled(EventLogLevel.Informational))
LogHelper.LogInformation(LogMessages.IDX10245, securityToken);

return new ClaimsIdentity(authenticationType: AuthenticationType ?? DefaultAuthenticationType, nameType: nameClaimType ?? ClaimsIdentity.DefaultNameClaimType, roleType: roleClaimType ?? ClaimsIdentity.DefaultRoleClaimType);
return ClaimsIdentityFactory.Create(authenticationType: AuthenticationType ?? DefaultAuthenticationType, nameType: nameClaimType ?? ClaimsIdentity.DefaultNameClaimType, roleType: roleClaimType ?? ClaimsIdentity.DefaultRoleClaimType, securityToken);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal ClaimsIdentity ClaimsIdentityNoLocking

if (_validationParameters != null && SecurityToken != null && _tokenHandler != null && Issuer != null)
{
_claimsIdentity = _tokenHandler.CreateClaimsIdentityInternal(SecurityToken, _validationParameters, Issuer);
_claimsIdentity = ClaimsIdentityFactory.Create(_tokenHandler, SecurityToken, _validationParameters, Issuer);
}

_claimsIdentityInitialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ protected virtual ClaimsIdentity CreateClaimsIdentity(JwtSecurityToken jwtToken,

private ClaimsIdentity CreateClaimsIdentityWithMapping(JwtSecurityToken jwtToken, string actualIssuer, TokenValidationParameters validationParameters)
{
ClaimsIdentity identity = validationParameters.CreateClaimsIdentity(jwtToken, actualIssuer);
ClaimsIdentity identity = ClaimsIdentityFactory.Create(jwtToken, validationParameters, actualIssuer);
foreach (Claim jwtClaim in jwtToken.Claims)
{
if (_inboundClaimFilter.Contains(jwtClaim.Type))
Expand Down Expand Up @@ -1551,7 +1551,7 @@ private ClaimsIdentity CreateClaimsIdentityWithMapping(JwtSecurityToken jwtToken

private ClaimsIdentity CreateClaimsIdentityWithoutMapping(JwtSecurityToken jwtToken, string actualIssuer, TokenValidationParameters validationParameters)
{
ClaimsIdentity identity = validationParameters.CreateClaimsIdentity(jwtToken, actualIssuer);
ClaimsIdentity identity = ClaimsIdentityFactory.Create(jwtToken, validationParameters, actualIssuer);
foreach (Claim jwtClaim in jwtToken.Claims)
{
if (_inboundClaimFilter.Contains(jwtClaim.Type))
Expand Down
Loading