| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using ProjectTemplate.Web.Authentication.Claims; |
| | | 4 | | using ProjectTemplate.Web.Authentication.Options; |
| | | 5 | | |
| | | 6 | | namespace ProjectTemplate.Web.Authentication.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides extension methods for registering application authorization services. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class AuthorizationServiceExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Adds application authorization services and baseline policies. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="services">The service collection to add authorization services to.</param> |
| | | 17 | | /// <param name="configuration">The application configuration source.</param> |
| | | 18 | | /// <returns>The same <see cref="IServiceCollection"/> instance for chaining.</returns> |
| | | 19 | | public static IServiceCollection AddApplicationAuthorization( |
| | | 20 | | this IServiceCollection services, |
| | | 21 | | IConfiguration configuration) |
| | | 22 | | { |
| | 160 | 23 | | ArgumentNullException.ThrowIfNull(services); |
| | 160 | 24 | | ArgumentNullException.ThrowIfNull(configuration); |
| | | 25 | | |
| | 160 | 26 | | ApplicationAuthorizationOptions options = configuration |
| | 160 | 27 | | .GetSection(ApplicationAuthorizationOptions.SectionName) |
| | 160 | 28 | | .Get<ApplicationAuthorizationOptions>() ?? new ApplicationAuthorizationOptions(); |
| | | 29 | | |
| | 160 | 30 | | string roleClaimType = string.IsNullOrWhiteSpace(options.RoleClaimType) |
| | 160 | 31 | | ? ApplicationClaimTypes.Role |
| | 160 | 32 | | : options.RoleClaimType; |
| | | 33 | | |
| | 160 | 34 | | string permissionClaimType = string.IsNullOrWhiteSpace(options.PermissionClaimType) |
| | 160 | 35 | | ? ApplicationClaimTypes.Permission |
| | 160 | 36 | | : options.PermissionClaimType; |
| | | 37 | | |
| | 160 | 38 | | services |
| | 160 | 39 | | .AddOptions<ApplicationAuthorizationOptions>() |
| | 160 | 40 | | .Bind(configuration.GetSection(ApplicationAuthorizationOptions.SectionName)) |
| | 160 | 41 | | .Validate( |
| | 160 | 42 | | authorizationOptions => |
| | 276 | 43 | | !authorizationOptions.RequireAuthenticatedUserByDefault || |
| | 276 | 44 | | configuration.GetValue<bool>($"{ApplicationAuthenticationOptions.SectionName}:Enabled"), |
| | 160 | 45 | | "ProjectTemplate:Authorization:RequireAuthenticatedUserByDefault cannot be true when ProjectTemplate:Aut |
| | 160 | 46 | | .Validate( |
| | 276 | 47 | | authorizationOptions => !string.IsNullOrWhiteSpace(authorizationOptions.RoleClaimType), |
| | 160 | 48 | | "ProjectTemplate:Authorization:RoleClaimType is required.") |
| | 160 | 49 | | .Validate( |
| | 276 | 50 | | authorizationOptions => !string.IsNullOrWhiteSpace(authorizationOptions.PermissionClaimType), |
| | 160 | 51 | | "ProjectTemplate:Authorization:PermissionClaimType is required.") |
| | 160 | 52 | | .Validate( |
| | 552 | 53 | | authorizationOptions => authorizationOptions.AdministratorRoles.Any(role => !string.IsNullOrWhiteSpace(r |
| | 160 | 54 | | "ProjectTemplate:Authorization:AdministratorRoles must contain at least one non-empty value.") |
| | 160 | 55 | | .Validate( |
| | 552 | 56 | | authorizationOptions => authorizationOptions.ManageApplicationPermissions.Any(permission => !string.IsNu |
| | 160 | 57 | | "ProjectTemplate:Authorization:ManageApplicationPermissions must contain at least one non-empty value.") |
| | 160 | 58 | | .ValidateOnStart(); |
| | | 59 | | |
| | 160 | 60 | | services.AddAuthorizationBuilder() |
| | 160 | 61 | | .AddPolicy( |
| | 160 | 62 | | ApplicationAuthorizationPolicyNames.AuthenticatedUser, |
| | 140 | 63 | | policy => policy.RequireAuthenticatedUser()) |
| | 160 | 64 | | .AddPolicy( |
| | 160 | 65 | | ApplicationAuthorizationPolicyNames.AdministratorRole, |
| | 160 | 66 | | policy => |
| | 160 | 67 | | { |
| | 140 | 68 | | policy.RequireAuthenticatedUser(); |
| | 140 | 69 | | policy.RequireClaim(roleClaimType, options.AdministratorRoles); |
| | 140 | 70 | | }) |
| | 160 | 71 | | .AddPolicy( |
| | 160 | 72 | | ApplicationAuthorizationPolicyNames.ManageApplicationPermission, |
| | 160 | 73 | | policy => |
| | 160 | 74 | | { |
| | 140 | 75 | | policy.RequireAuthenticatedUser(); |
| | 140 | 76 | | policy.RequireClaim(permissionClaimType, options.ManageApplicationPermissions); |
| | 300 | 77 | | }); |
| | | 78 | | |
| | 160 | 79 | | services |
| | 160 | 80 | | .AddOptions<AuthorizationOptions>() |
| | 300 | 81 | | .Configure<IOptions<ApplicationAuthorizationOptions>>((authorizationOptions, applicationAuthorizationOptions |
| | 300 | 82 | | ? new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build() |
| | 300 | 83 | | : null); |
| | | 84 | | |
| | 160 | 85 | | return services; |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Provides application authorization policy names. |
| | | 91 | | /// </summary> |
| | | 92 | | public static class ApplicationAuthorizationPolicyNames |
| | | 93 | | { |
| | | 94 | | /// <summary> |
| | | 95 | | /// Policy requiring the current user to be authenticated. |
| | | 96 | | /// </summary> |
| | | 97 | | public const string AuthenticatedUser = "application.AuthenticatedUser"; |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Policy defining the administrator role. |
| | | 101 | | /// </summary> |
| | | 102 | | public const string AdministratorRole = "application.Role.Administrator"; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Policy defining the manage application permission. |
| | | 106 | | /// </summary> |
| | | 107 | | public const string ManageApplicationPermission = "application.Permission.ManageApplication"; |
| | | 108 | | } |