66using System . Security . Claims ;
77using System . Threading . Tasks ;
88using Microsoft . AspNetCore . Shared ;
9- using Microsoft . Extensions . DependencyInjection ;
109using Microsoft . Extensions . Logging ;
1110using Microsoft . Extensions . Options ;
1211
@@ -18,7 +17,6 @@ namespace Microsoft.AspNetCore.Authorization;
1817public class DefaultAuthorizationService : IAuthorizationService
1918{
2019 private readonly AuthorizationOptions _options ;
21- private readonly AuthorizationMetrics ? _metrics ;
2220 private readonly IAuthorizationHandlerContextFactory _contextFactory ;
2321 private readonly IAuthorizationHandlerProvider _handlers ;
2422 private readonly IAuthorizationEvaluator _evaluator ;
@@ -34,35 +32,7 @@ public class DefaultAuthorizationService : IAuthorizationService
3432 /// <param name="contextFactory">The <see cref="IAuthorizationHandlerContextFactory"/> used to create the context to handle the authorization.</param>
3533 /// <param name="evaluator">The <see cref="IAuthorizationEvaluator"/> used to determine if authorization was successful.</param>
3634 /// <param name="options">The <see cref="AuthorizationOptions"/> used.</param>
37- public DefaultAuthorizationService (
38- IAuthorizationPolicyProvider policyProvider ,
39- IAuthorizationHandlerProvider handlers ,
40- ILogger < DefaultAuthorizationService > logger ,
41- IAuthorizationHandlerContextFactory contextFactory ,
42- IAuthorizationEvaluator evaluator ,
43- IOptions < AuthorizationOptions > options )
44- : this ( policyProvider , handlers , logger , contextFactory , evaluator , options , services : null )
45- {
46- }
47-
48- /// <summary>
49- /// Creates a new instance of <see cref="DefaultAuthorizationService"/>.
50- /// </summary>
51- /// <param name="policyProvider">The <see cref="IAuthorizationPolicyProvider"/> used to provide policies.</param>
52- /// <param name="handlers">The handlers used to fulfill <see cref="IAuthorizationRequirement"/>s.</param>
53- /// <param name="logger">The logger used to log messages, warnings and errors.</param>
54- /// <param name="contextFactory">The <see cref="IAuthorizationHandlerContextFactory"/> used to create the context to handle the authorization.</param>
55- /// <param name="evaluator">The <see cref="IAuthorizationEvaluator"/> used to determine if authorization was successful.</param>
56- /// <param name="options">The <see cref="AuthorizationOptions"/> used.</param>
57- /// <param name="services">The <see cref="IServiceProvider"/> used to provide other services.</param>
58- public DefaultAuthorizationService (
59- IAuthorizationPolicyProvider policyProvider ,
60- IAuthorizationHandlerProvider handlers ,
61- ILogger < DefaultAuthorizationService > logger ,
62- IAuthorizationHandlerContextFactory contextFactory ,
63- IAuthorizationEvaluator evaluator ,
64- IOptions < AuthorizationOptions > options ,
65- IServiceProvider ? services )
35+ public DefaultAuthorizationService ( IAuthorizationPolicyProvider policyProvider , IAuthorizationHandlerProvider handlers , ILogger < DefaultAuthorizationService > logger , IAuthorizationHandlerContextFactory contextFactory , IAuthorizationEvaluator evaluator , IOptions < AuthorizationOptions > options )
6636 {
6737 ArgumentNullThrowHelper . ThrowIfNull ( options ) ;
6838 ArgumentNullThrowHelper . ThrowIfNull ( policyProvider ) ;
@@ -77,7 +47,6 @@ public DefaultAuthorizationService(
7747 _logger = logger ;
7848 _evaluator = evaluator ;
7949 _contextFactory = contextFactory ;
80- _metrics = services ? . GetService < AuthorizationMetrics > ( ) ;
8150 }
8251
8352 /// <summary>
@@ -90,33 +59,7 @@ public DefaultAuthorizationService(
9059 /// A flag indicating whether authorization has succeeded.
9160 /// This value is <c>true</c> when the user fulfills the policy, otherwise <c>false</c>.
9261 /// </returns>
93- public virtual Task < AuthorizationResult > AuthorizeAsync ( ClaimsPrincipal user , object ? resource , IEnumerable < IAuthorizationRequirement > requirements )
94- => AuthorizeCoreAsync ( user , resource , requirements , policyName : null ) ;
95-
96- /// <summary>
97- /// Checks if a user meets a specific authorization policy.
98- /// </summary>
99- /// <param name="user">The user to check the policy against.</param>
100- /// <param name="resource">The resource the policy should be checked with.</param>
101- /// <param name="policyName">The name of the policy to check against a specific context.</param>
102- /// <returns>
103- /// A flag indicating whether authorization has succeeded.
104- /// This value is <c>true</c> when the user fulfills the policy otherwise <c>false</c>.
105- /// </returns>
106- public virtual async Task < AuthorizationResult > AuthorizeAsync ( ClaimsPrincipal user , object ? resource , string policyName )
107- {
108- ArgumentNullThrowHelper . ThrowIfNull ( policyName ) ;
109-
110- var policy = await _policyProvider . GetPolicyAsync ( policyName ) . ConfigureAwait ( false ) ;
111- if ( policy == null )
112- {
113- throw new InvalidOperationException ( $ "No policy found: { policyName } .") ;
114- }
115-
116- return await AuthorizeCoreAsync ( user , resource , policy . Requirements , policyName ) . ConfigureAwait ( false ) ;
117- }
118-
119- private async Task < AuthorizationResult > AuthorizeCoreAsync ( ClaimsPrincipal user , object ? resource , IEnumerable < IAuthorizationRequirement > requirements , string ? policyName )
62+ public virtual async Task < AuthorizationResult > AuthorizeAsync ( ClaimsPrincipal user , object ? resource , IEnumerable < IAuthorizationRequirement > requirements )
12063 {
12164 ArgumentNullThrowHelper . ThrowIfNull ( requirements ) ;
12265
@@ -132,9 +75,6 @@ private async Task<AuthorizationResult> AuthorizeCoreAsync(ClaimsPrincipal user,
13275 }
13376
13477 var result = _evaluator . Evaluate ( authContext ) ;
135-
136- _metrics ? . AuthorizedRequest ( policyName , result ) ;
137-
13878 if ( result . Succeeded )
13979 {
14080 _logger . UserAuthorizationSucceeded ( ) ;
@@ -143,7 +83,29 @@ private async Task<AuthorizationResult> AuthorizeCoreAsync(ClaimsPrincipal user,
14383 {
14484 _logger . UserAuthorizationFailed ( result . Failure ) ;
14585 }
146-
14786 return result ;
14887 }
88+
89+ /// <summary>
90+ /// Checks if a user meets a specific authorization policy.
91+ /// </summary>
92+ /// <param name="user">The user to check the policy against.</param>
93+ /// <param name="resource">The resource the policy should be checked with.</param>
94+ /// <param name="policyName">The name of the policy to check against a specific context.</param>
95+ /// <returns>
96+ /// A flag indicating whether authorization has succeeded.
97+ /// This value is <c>true</c> when the user fulfills the policy otherwise <c>false</c>.
98+ /// </returns>
99+ public virtual async Task < AuthorizationResult > AuthorizeAsync ( ClaimsPrincipal user , object ? resource , string policyName )
100+ {
101+ var policy = await GetPolicyAsync ( policyName ) . ConfigureAwait ( false ) ;
102+ return await this . AuthorizeAsync ( user , resource , policy ) . ConfigureAwait ( false ) ;
103+ }
104+
105+ // For use in DefaultAuthorizationServiceImpl.
106+ private protected async Task < AuthorizationPolicy > GetPolicyAsync ( string policyName )
107+ {
108+ ArgumentNullThrowHelper . ThrowIfNull ( policyName ) ;
109+ return await _policyProvider . GetPolicyAsync ( policyName ) . ConfigureAwait ( false ) ?? throw new InvalidOperationException ( $ "No policy found: { policyName } .") ;
110+ }
149111}
0 commit comments