-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Strawman for new authorization packages that can be shared between server and client #9997
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
14432b0
Add Microsoft.AspNetCore.Authorization.Common
SteveSandersonMS fc5aeb6
Also add Microsoft.AspNetCore.Authorization.Common.Abstractions
SteveSandersonMS 3fc9167
Move IAuthorizeData and AuthorizeAttribute
SteveSandersonMS c6385b1
Move all the policy stuff
SteveSandersonMS 6c46464
Move AllowAnonymousAttribute
SteveSandersonMS 4cde3c3
Move DefaultAuthorizationPolicyProvider
SteveSandersonMS 9979389
Move everything else that can be moved. Just not the stuff coupled to…
SteveSandersonMS 07c6ba2
Refactor PolicyEvaluator so it's possible to move the authorization p…
SteveSandersonMS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
...ion/Common.Abstractions/src/Microsoft.AspNetCore.Authorization.Common.Abstractions.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IsAspNetCoreApp>true</IsAspNetCoreApp> | ||
<IsShippingPackage>true</IsShippingPackage> | ||
</PropertyGroup> | ||
|
||
</Project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions
64
src/Security/Authorization/Common/src/AuthorizationServiceCollectionCommonExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Authorization.Infrastructure; | ||
using Microsoft.AspNetCore.Authorization.Policy; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
/// <summary> | ||
/// Extension methods for setting up authorization services in an <see cref="IServiceCollection" />. | ||
/// </summary> | ||
public static class AuthorizationServiceCollectionCommonExtensions | ||
{ | ||
/// <summary> | ||
/// Adds authorization services to the specified <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||
public static IServiceCollection AddAuthorizationCommon(this IServiceCollection services) | ||
{ | ||
if (services == null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
services.TryAdd(ServiceDescriptor.Transient<IAuthorizationService, DefaultAuthorizationService>()); | ||
services.TryAdd(ServiceDescriptor.Transient<IAuthorizationPolicyProvider, DefaultAuthorizationPolicyProvider>()); | ||
services.TryAdd(ServiceDescriptor.Transient<IAuthorizationHandlerProvider, DefaultAuthorizationHandlerProvider>()); | ||
services.TryAdd(ServiceDescriptor.Transient<IAuthorizationEvaluator, DefaultAuthorizationEvaluator>()); | ||
services.TryAdd(ServiceDescriptor.Transient<IAuthorizationHandlerContextFactory, DefaultAuthorizationHandlerContextFactory>()); | ||
services.TryAddEnumerable(ServiceDescriptor.Transient<IAuthorizationHandler, PassThroughAuthorizationHandler>()); | ||
|
||
// Policy | ||
services.TryAdd(ServiceDescriptor.Transient<ICommonPolicyEvaluator, CommonPolicyEvaluator>()); | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Adds authorization services to the specified <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||
/// <param name="configure">An action delegate to configure the provided <see cref="AuthorizationOptions"/>.</param> | ||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||
public static IServiceCollection AddAuthorizationCommon(this IServiceCollection services, Action<AuthorizationOptions> configure) | ||
{ | ||
if (services == null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
if (configure == null) | ||
{ | ||
throw new ArgumentNullException(nameof(configure)); | ||
} | ||
|
||
services.Configure(configure); | ||
return services.AddAuthorizationCommon(); | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
src/Security/Authorization/Common/src/Microsoft.AspNetCore.Authorization.Common.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IsAspNetCoreApp>true</IsAspNetCoreApp> | ||
<IsShippingPackage>true</IsShippingPackage> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.AspNetCore.Authorization.Common.Abstractions" /> | ||
<Reference Include="Microsoft.Extensions.Logging.Abstractions" /> | ||
<Reference Include="Microsoft.Extensions.Options" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Update="Resources.Designer.cs"> | ||
<DesignTime>True</DesignTime> | ||
<AutoGen>True</AutoGen> | ||
<DependentUpon>Resources.resx</DependentUpon> | ||
</Compile> | ||
|
||
<EmbeddedResource Update="Resources.resx"> | ||
<Generator>ResXFileCodeGenerator</Generator> | ||
<LastGenOutput>Resources.Designer.cs</LastGenOutput> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
</Project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
60 changes: 60 additions & 0 deletions
60
src/Security/Authorization/Common/src/Policy/CommonPolicyEvaluator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Authorization.Policy | ||
{ | ||
public class CommonPolicyEvaluator : ICommonPolicyEvaluator | ||
{ | ||
private readonly IAuthorizationService _authorization; | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="authorization">The authorization service.</param> | ||
public CommonPolicyEvaluator(IAuthorizationService authorization) | ||
{ | ||
_authorization = authorization; | ||
} | ||
|
||
/// <summary> | ||
/// Attempts authorization for a policy using <see cref="IAuthorizationService"/>. | ||
/// </summary> | ||
/// <param name="policy">The <see cref="AuthorizationPolicy"/>.</param> | ||
/// <param name="authenticationSucceeded">True if authentication succeeded, otherwise false.</param> | ||
/// <param name="user">The <see cref="ClaimsPrincipal"/>.</param> | ||
/// <param name="resource"> | ||
/// An optional resource the policy should be checked with. | ||
/// If a resource is not required for policy evaluation you may pass null as the value. | ||
/// </param> | ||
/// <returns>Returns <see cref="PolicyAuthorizationResult.Success"/> if authorization succeeds. | ||
/// Otherwise returns <see cref="PolicyAuthorizationResult.Forbid"/> if <see cref="AuthenticateResult.Succeeded"/>, otherwise | ||
/// returns <see cref="PolicyAuthorizationResult.Challenge"/></returns> | ||
public virtual async Task<PolicyAuthorizationResult> AuthorizeAsync(AuthorizationPolicy policy, bool authenticationSucceeded, ClaimsPrincipal user, object resource) | ||
{ | ||
if (policy == null) | ||
{ | ||
throw new ArgumentNullException(nameof(policy)); | ||
} | ||
|
||
if (user == null) | ||
{ | ||
throw new ArgumentNullException(nameof(user)); | ||
} | ||
|
||
var result = await _authorization.AuthorizeAsync(user, resource, policy); | ||
if (result.Succeeded) | ||
{ | ||
return PolicyAuthorizationResult.Success(); | ||
} | ||
|
||
// If authentication was successful, return forbidden, otherwise challenge | ||
return authenticationSucceeded | ||
? PolicyAuthorizationResult.Forbid() | ||
: PolicyAuthorizationResult.Challenge(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Security/Authorization/Common/src/Policy/ICommonPolicyEvaluator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Authorization.Policy | ||
{ | ||
public interface ICommonPolicyEvaluator | ||
{ | ||
/// <summary> | ||
/// Attempts authorization for a policy using <see cref="IAuthorizationService"/>. | ||
/// </summary> | ||
/// <param name="policy">The <see cref="AuthorizationPolicy"/>.</param> | ||
/// <param name="authenticationSucceeded">True if authentication succeeded, otherwise false.</param> | ||
/// <param name="user">The <see cref="ClaimsPrincipal"/>.</param> | ||
/// <param name="resource"> | ||
/// An optional resource the policy should be checked with. | ||
/// If a resource is not required for policy evaluation you may pass null as the value. | ||
/// </param> | ||
/// <returns>Returns <see cref="PolicyAuthorizationResult.Success"/> if authorization succeeds. | ||
/// Otherwise returns <see cref="PolicyAuthorizationResult.Forbid"/> if <see cref="AuthenticateResult.Succeeded"/>, otherwise | ||
/// returns <see cref="PolicyAuthorizationResult.Challenge"/></returns> | ||
Task<PolicyAuthorizationResult> AuthorizeAsync(AuthorizationPolicy policy, bool authenticationSucceeded, ClaimsPrincipal user, object resource); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,4 @@ public static PolicyAuthorizationResult Success() | |
=> new PolicyAuthorizationResult { Succeeded = true }; | ||
|
||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/Security/Authorization/Common/src/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need a PolicyEvaluator in the common layer? It was only introduced to tie authN + authZ together, I thought on the client side there's no authentication needed, so wouldn't the base
IAuthorizationService
be sufficient?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we do want to evaluate policies in client-side code.
That's not correct. Although authentication isn't enforced there (all true enforcement is server-side), we still want policies to exist as a way of informing the client about what the user is going to be allowed to do.