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

small clean up in claims principal #1092

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/Microsoft.Identity.Web/ClaimsPrincipalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static class ClaimsPrincipalExtensions
}

// Finally falling back to name
return claimsPrincipal.FindFirstValue(ClaimConstants.Name);
return GetClaimValue(claimsPrincipal, ClaimConstants.Name);
}

/// <summary>
Expand All @@ -151,15 +151,10 @@ public static class ClaimsPrincipalExtensions
/// <returns>User flow ID of the identity, or <c>null</c> if it cannot be found.</returns>
public static string? GetUserFlowId(this ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}

string? userFlowId = claimsPrincipal.FindFirstValue(ClaimConstants.Tfp);
if (string.IsNullOrEmpty(userFlowId))
{
return claimsPrincipal.FindFirstValue(ClaimConstants.UserFlow);
return GetClaimValue(claimsPrincipal, ClaimConstants.UserFlow);
}

return userFlowId;
Expand All @@ -172,12 +167,7 @@ public static class ClaimsPrincipalExtensions
/// <returns>Home Object ID (sub) of the identity, or <c>null</c> if it cannot be found.</returns>
public static string? GetHomeObjectId(this ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}

return claimsPrincipal.FindFirstValue(ClaimConstants.UniqueObjectIdentifier);
return GetClaimValue(claimsPrincipal, ClaimConstants.UniqueObjectIdentifier);
}

/// <summary>
Expand All @@ -187,12 +177,7 @@ public static class ClaimsPrincipalExtensions
/// <returns>Home Tenant ID (sub) of the identity, or <c>null</c> if it cannot be found.</returns>
public static string? GetHomeTenantId(this ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}

return claimsPrincipal.FindFirstValue(ClaimConstants.UniqueTenantIdentifier);
return GetClaimValue(claimsPrincipal, ClaimConstants.UniqueTenantIdentifier);
}

/// <summary>
Expand All @@ -201,13 +186,27 @@ public static class ClaimsPrincipalExtensions
/// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> from which to retrieve the <c>NameIdentifierId</c> claim.</param>
/// <returns>Name identifier ID of the identity, or <c>null</c> if it cannot be found.</returns>
public static string? GetNameIdentifierId(this ClaimsPrincipal claimsPrincipal)
{
return GetClaimValue(claimsPrincipal, ClaimConstants.NameIdentifierId);
}

private static string? GetClaimValue(ClaimsPrincipal? claimsPrincipal, params string[] claimNames)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}

return claimsPrincipal.FindFirstValue(ClaimConstants.NameIdentifierId);
for (var i = 0; i < claimNames.Length; i++)
{
var currentValue = claimsPrincipal.FindFirstValue(claimNames[i]);
if (!string.IsNullOrEmpty(currentValue))
{
return currentValue;
}
}

return null;
}

#if NET472
Expand Down