-
Notifications
You must be signed in to change notification settings - Fork 22
/
PermissionAuthorizationExtensions.cs
104 lines (92 loc) · 5.8 KB
/
PermissionAuthorizationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SimpleAuthentication.Permissions;
namespace SimpleAuthentication;
/// <summary>
/// Provides extension methods for adding permission-based authorization support in ASP.NET Core.
/// </summary>
public static class PermissionAuthorizationExtensions
{
/// <summary>
/// Registers services required by permission-based authorization, using the specified <typeparamref name="T"/> implementation to validate permissions.
/// </summary>
/// <typeparam name="T">The type implementing <see cref="IPermissionHandler"/> to register.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <exception cref="ArgumentNullException">One or more required configuration settings are missing.</exception>
/// <seealso cref="IPermissionHandler"/>
public static IServiceCollection AddPermissions<T>(this IServiceCollection services) where T : class, IPermissionHandler
{
ArgumentNullException.ThrowIfNull(services);
services.AddSingleton<IAuthorizationPolicyProvider, PermissionAuthorizationPolicyProvider>();
services.AddTransient<IAuthorizationHandler, PermissionAuthorizationHandler>();
services.AddTransient<IPermissionHandler, T>();
return services;
}
/// <summary>
/// Registers services required by permission-based authorization, using the default <see cref="ScopeClaimPermissionHandler"/> implementation that uses scopes to validate permissions.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <exception cref="ArgumentNullException">One or more required configuration settings are missing.</exception>
/// <seealso cref="ScopeClaimPermissionHandler"/>
public static IServiceCollection AddScopePermissions(this IServiceCollection services)
=> services.AddPermissions<ScopeClaimPermissionHandler>();
/// <summary>
/// Registers services required by permission-based authorization, using the specified <typeparamref name="T"/> implementation to validates permissions.
/// </summary>
/// <typeparam name="T">The type implementing <see cref="IPermissionHandler"/> to register.</typeparam>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to add services to.</param>
/// <returns>An <see cref="AuthenticationBuilder"/> that can be used to further customize authentication.</returns>
/// <exception cref="ArgumentNullException">One or more required configuration settings are missing.</exception>
/// <seealso cref="IPermissionHandler"/>
/// <seealso cref="AuthenticationBuilder"/>
public static AuthenticationBuilder AddPermissions<T>(this AuthenticationBuilder builder) where T : class, IPermissionHandler
{
builder.Services.AddPermissions<T>();
return builder;
}
/// <summary>
/// Registers services required by permission-based authorization, using the default <see cref="ScopeClaimPermissionHandler"/> implementation that uses scopes to validate permissions.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/> to add services to.</param>
/// <returns>An <see cref="AuthenticationBuilder"/> that can be used to further customize authentication.</returns>
/// <exception cref="ArgumentNullException">One or more required configuration settings are missing.</exception>
/// <seealso cref="IPermissionHandler"/>
/// <seealso cref="ScopeClaimPermissionHandler"/>
/// <seealso cref="AuthenticationBuilder"/>
public static AuthenticationBuilder AddScopePermissions(this AuthenticationBuilder builder)
{
builder.Services.AddPermissions<ScopeClaimPermissionHandler>();
return builder;
}
/// <summary>
/// Adds a <see cref="PermissionRequirement"/> to the <see cref="AuthorizationPolicyBuilder.Requirements"/> for this instance.
/// </summary>
/// <param name="builder">The <see cref="AuthorizationPolicyBuilder"/> to add policy to.</param>
/// <param name="permissions">The list of permissions to add.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <exception cref="ArgumentNullException">One or more required configuration settings are missing.</exception>
/// <seealso cref="AuthorizationPolicyBuilder"/>
/// <seealso cref="PermissionRequirement"/>
public static AuthorizationPolicyBuilder RequirePermission(this AuthorizationPolicyBuilder builder, params string[] permissions)
{
ArgumentNullException.ThrowIfNull(builder);
return builder.AddRequirements(new PermissionRequirement(permissions));
}
/// <summary>
/// Adds permission-based authorization policy to the endpoint(s).
/// </summary>
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/> to add policy to.</param>
/// <param name="permissions">The permission list to require for authorization.</param>
/// <returns>The original <see cref="IEndpointConventionBuilder"/> parameter.</returns>
/// <exception cref="ArgumentNullException">One or more required configuration settings are missing.</exception>
/// <seealso cref="IEndpointConventionBuilder"/>
public static TBuilder RequirePermission<TBuilder>(this TBuilder builder, params string[] permissions) where TBuilder : IEndpointConventionBuilder
{
ArgumentNullException.ThrowIfNull(builder);
return builder.RequireAuthorization(new PermissionAttribute(permissions));
}
}