Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 34 additions & 0 deletions src/Microsoft.Identity.Web.OWIN/AppBuilderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ public static IAppBuilder AddMicrosoftIdentityWebApp(

if (clientInfoFromServer != null && clientInfoFromServer.UniqueTenantIdentifier != null && clientInfoFromServer.UniqueObjectIdentifier != null)
{
var uniqueTenantIdentifierClaim = context.AuthenticationTicket.Identity.FindFirst(c => c.Type == ClaimConstants.UniqueTenantIdentifier);
var uniqueObjectIdentifierClaim = context.AuthenticationTicket.Identity.FindFirst(c => c.Type == ClaimConstants.UniqueObjectIdentifier);
if (uniqueTenantIdentifierClaim != null && !string.Equals(clientInfoFromServer.UniqueTenantIdentifier, uniqueTenantIdentifierClaim.Value, StringComparison.OrdinalIgnoreCase))
{
throw new InternalClaimDetectedException($"The claim \"{ClaimConstants.UniqueTenantIdentifier}\" is reserved for internal use by this library. To ensure proper functionality and avoid conflicts, please remove or rename this claim in your ID Token.")
{
Claim = uniqueTenantIdentifierClaim,
};
}
if (uniqueObjectIdentifierClaim != null && !string.Equals(clientInfoFromServer.UniqueObjectIdentifier, uniqueObjectIdentifierClaim.Value, StringComparison.OrdinalIgnoreCase))
{
throw new InternalClaimDetectedException($"The claim \"{ClaimConstants.UniqueObjectIdentifier}\" is reserved for internal use by this library. To ensure proper functionality and avoid conflicts, please remove or rename this claim in your ID Token.")
{
Claim = uniqueObjectIdentifierClaim
};
}

context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimConstants.UniqueTenantIdentifier, clientInfoFromServer.UniqueTenantIdentifier));
context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimConstants.UniqueObjectIdentifier, clientInfoFromServer.UniqueObjectIdentifier));
}
Expand All @@ -187,6 +204,23 @@ public static IAppBuilder AddMicrosoftIdentityWebApp(

if (clientInfoFromServer != null && clientInfoFromServer.UniqueTenantIdentifier != null && clientInfoFromServer.UniqueObjectIdentifier != null)
{
var uniqueTenantIdentifierClaim = context.AuthenticationTicket.Identity.FindFirst(c => c.Type == ClaimConstants.UniqueTenantIdentifier);
var uniqueObjectIdentifierClaim = context.AuthenticationTicket.Identity.FindFirst(c => c.Type == ClaimConstants.UniqueObjectIdentifier);
if (uniqueTenantIdentifierClaim != null && !string.Equals(clientInfoFromServer.UniqueTenantIdentifier, uniqueTenantIdentifierClaim.Value, StringComparison.OrdinalIgnoreCase))
{
throw new InternalClaimDetectedException($"The claim \"{ClaimConstants.UniqueTenantIdentifier}\" is reserved for internal use by this library. To ensure proper functionality and avoid conflicts, please remove or rename this claim in your ID Token.")
{
Claim = uniqueTenantIdentifierClaim
};
}
if (uniqueObjectIdentifierClaim != null && !string.Equals(clientInfoFromServer.UniqueObjectIdentifier, uniqueObjectIdentifierClaim.Value, StringComparison.OrdinalIgnoreCase))
{
throw new InternalClaimDetectedException($"The claim \"{ClaimConstants.UniqueObjectIdentifier}\" is reserved for internal use by this library. To ensure proper functionality and avoid conflicts, please remove or rename this claim in your ID Token.")
{
Claim = uniqueObjectIdentifierClaim
};
}

context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimConstants.UniqueTenantIdentifier, clientInfoFromServer.UniqueTenantIdentifier));
context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimConstants.UniqueObjectIdentifier, clientInfoFromServer.UniqueObjectIdentifier));
}
Expand Down
31 changes: 31 additions & 0 deletions src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Security.Claims;

namespace Microsoft.Identity.Web.OWIN
{
/// <summary>
/// The exception that is thrown when an internal ID Token claim used by Microsoft.Identity.Web internally is detected in the user's ID Token.
/// </summary>
public class InternalClaimDetectedException : Exception

Check warning on line 12 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Symbol 'InternalClaimDetectedException' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
{
/// <summary>
/// Gets or sets the invalid claim.
/// </summary>
public Claim Claim { get; set; }

Check warning on line 17 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Check warning on line 17 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests


public InternalClaimDetectedException()

Check warning on line 19 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Non-nullable property 'Claim' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 19 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Missing XML comment for publicly visible type or member 'InternalClaimDetectedException.InternalClaimDetectedException()'

Check warning on line 19 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Symbol 'InternalClaimDetectedException' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
{
}

public InternalClaimDetectedException(string message) : base(message)

Check warning on line 23 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Non-nullable property 'Claim' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Missing XML comment for publicly visible type or member 'InternalClaimDetectedException.InternalClaimDetectedException(string)'
{
}

public InternalClaimDetectedException(string message, Exception innerException) : base(message, innerException)

Check warning on line 27 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Non-nullable property 'Claim' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 27 in src/Microsoft.Identity.Web.OWIN/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Missing XML comment for publicly visible type or member 'InternalClaimDetectedException.InternalClaimDetectedException(string, Exception)'
{
}
}
}
30 changes: 20 additions & 10 deletions src/Microsoft.Identity.Web.OWIN/Microsoft.Identity.Web.OWIN.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/Microsoft.Identity.Web/InternalClaimDetectedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Security.Claims;

namespace Microsoft.Identity.Web
{
/// <summary>
/// The exception that is thrown when an internal ID Token claim used by Microsoft.Identity.Web internally is detected in the user's ID Token.
/// </summary>
public class InternalClaimDetectedException : Exception

Check warning on line 12 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Symbol 'InternalClaimDetectedException' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
{
/// <summary>
/// Gets or sets the invalid claim.
/// </summary>
public Claim Claim { get; set; }

Check warning on line 17 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Check warning on line 17 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse


public InternalClaimDetectedException()

Check warning on line 19 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Non-nullable property 'Claim' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 19 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Missing XML comment for publicly visible type or member 'InternalClaimDetectedException.InternalClaimDetectedException()'

Check warning on line 19 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Symbol 'InternalClaimDetectedException' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
{
}

public InternalClaimDetectedException(string message) : base(message)

Check warning on line 23 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Non-nullable property 'Claim' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 23 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Missing XML comment for publicly visible type or member 'InternalClaimDetectedException.InternalClaimDetectedException(string)'
{
}

public InternalClaimDetectedException(string message, Exception innerException) : base(message, innerException)

Check warning on line 27 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Non-nullable property 'Claim' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 27 in src/Microsoft.Identity.Web/InternalClaimDetectedException.cs

View workflow job for this annotation

GitHub Actions / Analyse

Missing XML comment for publicly visible type or member 'InternalClaimDetectedException.InternalClaimDetectedException(string, Exception)'
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,31 @@ internal static void WebAppCallsWebApiImplementation(

if (clientInfoFromServer != null && clientInfoFromServer.UniqueTenantIdentifier != null && clientInfoFromServer.UniqueObjectIdentifier != null)
{
context!.Principal!.Identities.FirstOrDefault()?.AddClaim(new Claim(ClaimConstants.UniqueTenantIdentifier, clientInfoFromServer.UniqueTenantIdentifier));
context!.Principal!.Identities.FirstOrDefault()?.AddClaim(new Claim(ClaimConstants.UniqueObjectIdentifier, clientInfoFromServer.UniqueObjectIdentifier));
var identity = context!.Principal!.Identities.FirstOrDefault();
if (identity != null)
{
var uniqueTenantIdentifierClaim = identity.FindFirst(c => c.Type == ClaimConstants.UniqueTenantIdentifier);
var uniqueObjectIdentifierClaim = identity.FindFirst(c => c.Type == ClaimConstants.UniqueObjectIdentifier);
if (uniqueTenantIdentifierClaim != null && !string.Equals(clientInfoFromServer.UniqueTenantIdentifier, uniqueTenantIdentifierClaim.Value, StringComparison.OrdinalIgnoreCase))
{
context.Fail(new InternalClaimDetectedException($"The claim \"{ClaimConstants.UniqueTenantIdentifier}\" is reserved for internal use by this library. To ensure proper functionality and avoid conflicts, please remove or rename this claim in your ID Token.")
{
Claim = uniqueTenantIdentifierClaim
});
return;
}
if (uniqueObjectIdentifierClaim != null && !string.Equals(clientInfoFromServer.UniqueObjectIdentifier, uniqueObjectIdentifierClaim.Value, StringComparison.OrdinalIgnoreCase))
{
context.Fail(new InternalClaimDetectedException($"The claim \"{ClaimConstants.UniqueObjectIdentifier}\" is reserved for internal use by this library. To ensure proper functionality and avoid conflicts, please remove or rename this claim in your ID Token.")
{
Claim = uniqueObjectIdentifierClaim
});
return;
}

identity.AddClaim(new Claim(ClaimConstants.UniqueTenantIdentifier, clientInfoFromServer.UniqueTenantIdentifier));
identity.AddClaim(new Claim(ClaimConstants.UniqueObjectIdentifier, clientInfoFromServer.UniqueObjectIdentifier));
}
}
}
await onTokenValidatedHandler(context).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.IO;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -44,5 +45,14 @@ public static HttpContext CreateHttpContext(

return httpContext;
}

public static HttpContext CreateHttpContext(IEnumerable<Claim> claims)
{
var httpContext = CreateHttpContext();

httpContext.User = new ClaimsPrincipal(new CaseSensitiveClaimsIdentity(claims));

return httpContext;
}
}
}
Loading
Loading