< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.AuthorizationBehavior
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/AuthorizationBehavior.cs
Line coverage
97%
Covered lines: 44
Uncovered lines: 1
Coverable lines: 45
Total lines: 117
Line coverage: 97.7%
Branch coverage
92%
Covered branches: 24
Total branches: 26
Branch coverage: 92.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor()100%11100%
AuthorizeAsync()100%44100%
AuthorizePolicyAsync()87.5%88100%
CreateAuthorizationBehavior(...)100%11100%
TryCreate(...)92.85%141491.66%
CreateAccessDeniedFaultException()100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/AuthorizationBehavior.cs

#LineLine coverage
 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
 4using System;
 5using System.Collections.ObjectModel;
 6using System.Globalization;
 7using System.Runtime.CompilerServices;
 8using System.Security.Claims;
 9using System.Threading.Tasks;
 10using CoreWCF.IdentityModel.Policy;
 11using CoreWCF.Runtime;
 12using CoreWCF.Security;
 13using Microsoft.AspNetCore.Authorization;
 14
 15namespace CoreWCF.Dispatcher
 16{
 17    internal sealed class AuthorizationBehavior
 18    {
 219        private static readonly ServiceAuthorizationManager s_defaultServiceAuthorizationManager = new();
 20        private ReadOnlyCollection<IAuthorizationPolicy> _externalAuthorizationPolicies;
 21        private ServiceAuthorizationManager _serviceAuthorizationManager;
 22        private IAuthorizationService _authorizationService;
 23
 26624        private AuthorizationBehavior() { }
 25
 26        public async ValueTask<MessageRpc> AuthorizeAsync(MessageRpc rpc)
 27        {
 28            // TODO: Events
 1729            SecurityMessageProperty security = SecurityMessageProperty.GetOrCreate(rpc.Request);
 1730            security.ExternalAuthorizationPolicies = _externalAuthorizationPolicies;
 31
 1732            ServiceAuthorizationManager serviceAuthorizationManager = _serviceAuthorizationManager ?? s_defaultServiceAu
 33            try
 34            {
 1735                var checkAccessResult = await serviceAuthorizationManager.CheckAccessAsync(rpc.OperationContext, rpc.Req
 1736                rpc.Request = checkAccessResult.message;
 1737                if(!checkAccessResult.isAuthorized)
 38                {
 239                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateAccessDeniedFaultException());
 40                }
 1541            }
 42            catch (Exception ex)
 43            {
 44                // I know this code looks weird and it looks like the try/catch block should be removed, but I'm maintai
 45                // this code structure in preparation for Perf counters and auditing to be put back in.
 246                if (Fx.IsFatal(ex))
 47                {
 248                    throw;
 49                }
 50                // TODO: PerformanceCounters
 51                // TODO: Auditing
 52                throw;
 53            }
 54
 1555            return rpc;
 1556        }
 57
 58        internal async ValueTask<MessageRpc> AuthorizePolicyAsync(MessageRpc rpc)
 59        {
 5560            ClaimsPrincipal principal = rpc.OperationContext.ClaimsPrincipal;
 5561            AuthorizationPolicy authorizationPolicy = rpc.Operation.AuthorizationPolicy?.Value;
 5562            if (principal != null && authorizationPolicy != null)
 63            {
 5464                var result = await _authorizationService.AuthorizeAsync(principal, authorizationPolicy);
 5465                if (!result.Succeeded)
 66                {
 3067                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateAccessDeniedFaultException());
 68                }
 69            }
 70
 2571            return rpc;
 2572        }
 73
 74        [MethodImpl(MethodImplOptions.NoInlining)]
 75        private static AuthorizationBehavior CreateAuthorizationBehavior(DispatchRuntime dispatch)
 76        {
 13377            AuthorizationBehavior behavior = new()
 13378            {
 13379                _externalAuthorizationPolicies = dispatch.ExternalAuthorizationPolicies,
 13380                _serviceAuthorizationManager = dispatch.ServiceAuthorizationManager,
 13381                _authorizationService = dispatch.GetAuthorizationService()
 13382            };
 83
 13384            return behavior;
 85        }
 86
 87        public static AuthorizationBehavior TryCreate(DispatchRuntime dispatch)
 88        {
 66489            if (dispatch == null)
 90            {
 091                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(dispatch)));
 92            }
 93
 66494            if (dispatch.SupportsAuthorizationData && !dispatch.IsAuthorizationInfrastructureRegistered())
 95            {
 196                throw new NotSupportedException(SR.AuthorizationFeaturesAuthorizationServiceIsNotRegistered);
 97            }
 98
 66399            return dispatch switch
 663100            {
 663101                { RequiresAuthorization: true, SupportsAuthorizationData: true } =>
 2102                    throw new NotSupportedException(SR.AuthorizationFeaturesAreMutuallyExclusive),
 663103                { RequiresAuthorization: true } or { SupportsAuthorizationData: true } =>
 133104                    CreateAuthorizationBehavior(dispatch),
 528105                _ => null
 663106            };
 107        }
 108
 109        internal static Exception CreateAccessDeniedFaultException()
 110        {
 38111            SecurityVersion wss = SecurityVersion.Default;
 38112            FaultCode faultCode = FaultCode.CreateSenderFaultCode(wss.FailedAuthenticationFaultCode.Value, wss.HeaderNam
 38113            FaultReason faultReason = new FaultReason(SR.AccessDenied, CultureInfo.CurrentCulture);
 38114            return new FaultException(faultReason, faultCode);
 115        }
 116    }
 117}