|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | + |
| 8 | +namespace Microsoft.AspNetCore.Authorization; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Used to configure authorization |
| 12 | +/// </summary> |
| 13 | +public class AuthorizationBuilder |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of <see cref="AuthorizationBuilder"/>. |
| 17 | + /// </summary> |
| 18 | + /// <param name="services">The services being configured.</param> |
| 19 | + public AuthorizationBuilder(IServiceCollection services) |
| 20 | + => Services = services; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The services being configured. |
| 24 | + /// </summary> |
| 25 | + public virtual IServiceCollection Services { get; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Determines whether authorization handlers should be invoked after <see cref="AuthorizationHandlerContext.HasFailed"/>. |
| 29 | + /// Defaults to true. |
| 30 | + /// </summary> |
| 31 | + /// <returns>The builder.</returns> |
| 32 | + public virtual AuthorizationBuilder SetInvokeHandlersAfterFailure(bool invoke) |
| 33 | + { |
| 34 | + Services.Configure<AuthorizationOptions>(o => o.InvokeHandlersAfterFailure = invoke); |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Sets the default authorization policy. Defaults to require authenticated users. |
| 40 | + /// </summary> |
| 41 | + /// <remarks> |
| 42 | + /// The default policy used when evaluating <see cref="IAuthorizeData"/> with no policy name specified. |
| 43 | + /// </remarks> |
| 44 | + /// <returns>The builder.</returns> |
| 45 | + public virtual AuthorizationBuilder SetDefaultPolicy(AuthorizationPolicy policy) |
| 46 | + { |
| 47 | + Services.Configure<AuthorizationOptions>(o => o.DefaultPolicy = policy); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Sets the fallback authorization policy used by <see cref="AuthorizationPolicy.CombineAsync(IAuthorizationPolicyProvider, IEnumerable{IAuthorizeData})"/> |
| 53 | + /// when no IAuthorizeData have been provided. As a result, the AuthorizationMiddleware uses the fallback policy |
| 54 | + /// if there are no <see cref="IAuthorizeData"/> instances for a resource. If a resource has any <see cref="IAuthorizeData"/> |
| 55 | + /// then they are evaluated instead of the fallback policy. By default the fallback policy is null, and usually will have no |
| 56 | + /// effect unless you have the AuthorizationMiddleware in your pipeline. It is not used in any way by the |
| 57 | + /// default <see cref="IAuthorizationService"/>. |
| 58 | + /// </summary> |
| 59 | + /// <returns>The builder.</returns> |
| 60 | + public virtual AuthorizationBuilder SetFallbackPolicy(AuthorizationPolicy? policy) |
| 61 | + { |
| 62 | + Services.Configure<AuthorizationOptions>(o => o.FallbackPolicy = policy); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Adds a <see cref="AuthorizationPolicy"/> which can be used by <see cref="IAuthorizationService"/>. |
| 68 | + /// </summary> |
| 69 | + /// <param name="name">The name of this policy.</param> |
| 70 | + /// <param name="policy">The <see cref="AuthorizationPolicy"/>.></param> |
| 71 | + /// <returns>The builder.</returns> |
| 72 | + public virtual AuthorizationBuilder AddPolicy(string name, AuthorizationPolicy policy) |
| 73 | + { |
| 74 | + Services.Configure<AuthorizationOptions>(o => o.AddPolicy(name, policy)); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Add a policy that is built from a delegate with the provided name. |
| 80 | + /// </summary> |
| 81 | + /// <param name="name">The name of the policy.</param> |
| 82 | + /// <param name="configurePolicy">The delegate that will be used to build the policy.</param> |
| 83 | + /// <returns>The builder.</returns> |
| 84 | + public virtual AuthorizationBuilder AddPolicy(string name, Action<AuthorizationPolicyBuilder> configurePolicy) |
| 85 | + { |
| 86 | + Services.Configure<AuthorizationOptions>(o => o.AddPolicy(name, configurePolicy)); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Add a policy that is built from a delegate with the provided name and used as the default policy. |
| 92 | + /// </summary> |
| 93 | + /// <param name="name">The name of the default policy.</param> |
| 94 | + /// <param name="policy">The default <see cref="AuthorizationPolicy"/>.></param> |
| 95 | + /// <returns>The builder.</returns> |
| 96 | + public virtual AuthorizationBuilder AddDefaultPolicy(string name, AuthorizationPolicy policy) |
| 97 | + { |
| 98 | + SetDefaultPolicy(policy); |
| 99 | + return AddPolicy(name, policy); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Add a policy that is built from a delegate with the provided name and used as the DefaultPolicy. |
| 104 | + /// </summary> |
| 105 | + /// <param name="name">The name of the DefaultPolicy.</param> |
| 106 | + /// <param name="configurePolicy">The delegate that will be used to build the DefaultPolicy.</param> |
| 107 | + /// <returns>The builder.</returns> |
| 108 | + public virtual AuthorizationBuilder AddDefaultPolicy(string name, Action<AuthorizationPolicyBuilder> configurePolicy) |
| 109 | + { |
| 110 | + if (configurePolicy == null) |
| 111 | + { |
| 112 | + throw new ArgumentNullException(nameof(configurePolicy)); |
| 113 | + } |
| 114 | + |
| 115 | + var policyBuilder = new AuthorizationPolicyBuilder(); |
| 116 | + configurePolicy(policyBuilder); |
| 117 | + return AddDefaultPolicy(name, policyBuilder.Build()); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Add a policy that is built from a delegate with the provided name and used as the FallbackPolicy. |
| 122 | + /// </summary> |
| 123 | + /// <param name="name">The name of the FallbackPolicy.</param> |
| 124 | + /// <param name="policy">The Fallback <see cref="AuthorizationPolicy"/>.></param> |
| 125 | + /// <returns>The builder.</returns> |
| 126 | + public virtual AuthorizationBuilder AddFallbackPolicy(string name, AuthorizationPolicy policy) |
| 127 | + { |
| 128 | + SetFallbackPolicy(policy); |
| 129 | + return AddPolicy(name, policy); |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Add a policy that is built from a delegate with the provided name and used as the FallbackPolicy. |
| 134 | + /// </summary> |
| 135 | + /// <param name="name">The name of the Fallback policy.</param> |
| 136 | + /// <param name="configurePolicy">The delegate that will be used to build the Fallback policy.</param> |
| 137 | + /// <returns>The builder.</returns> |
| 138 | + public virtual AuthorizationBuilder AddFallbackPolicy(string name, Action<AuthorizationPolicyBuilder> configurePolicy) |
| 139 | + { |
| 140 | + if (configurePolicy == null) |
| 141 | + { |
| 142 | + throw new ArgumentNullException(nameof(configurePolicy)); |
| 143 | + } |
| 144 | + |
| 145 | + var policyBuilder = new AuthorizationPolicyBuilder(); |
| 146 | + configurePolicy(policyBuilder); |
| 147 | + return AddFallbackPolicy(name, policyBuilder.Build()); |
| 148 | + } |
| 149 | +} |
0 commit comments