< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.AuthenticationBehavior
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/AuthenticationBehavior.cs
Line coverage
11%
Covered lines: 3
Uncovered lines: 23
Coverable lines: 26
Total lines: 82
Line coverage: 11.5%
Branch coverage
25%
Covered branches: 2
Total branches: 8
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
AuthenticateAsync()0%440%
CreateAuthenticationBehavior(...)100%110%
TryCreate(...)50%4460%
CreateFailedAuthenticationFaultException()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/AuthenticationBehavior.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.Runtime.CompilerServices;
 7using System.Threading.Tasks;
 8using CoreWCF.IdentityModel.Policy;
 9using CoreWCF.Runtime;
 10using CoreWCF.Security;
 11
 12namespace CoreWCF.Dispatcher
 13{
 14    internal sealed class AuthenticationBehavior
 15    {
 16        private readonly ServiceAuthenticationManager _serviceAuthenticationManager;
 17
 018        private AuthenticationBehavior(ServiceAuthenticationManager authenticationManager)
 19        {
 020            _serviceAuthenticationManager = authenticationManager;
 021        }
 22
 23        public async ValueTask<MessageRpc> AuthenticateAsync(MessageRpc rpc)
 24        {
 025            SecurityMessageProperty security = SecurityMessageProperty.GetOrCreate(rpc.Request);
 026            ReadOnlyCollection<IAuthorizationPolicy> authPolicy = security.ServiceSecurityContext.AuthorizationPolicies;
 27            try
 28            {
 029                (authPolicy, rpc.Request) = await _serviceAuthenticationManager.AuthenticateAsync(security.ServiceSecuri
 030                if (authPolicy == null)
 31                {
 032                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Authentic
 33                }
 034            }
 35            catch (Exception ex)
 36            {
 037                if (Fx.IsFatal(ex))
 38                {
 039                    throw;
 40                }
 41
 42                // TODO: PerformanceCounters
 43                // TODO: Decide if we want Auditing and add back
 044                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateFailedAuthenticationFaultException());
 45            }
 46
 047            rpc.Request.Properties.Security.ServiceSecurityContext.AuthorizationPolicies = authPolicy;
 48
 049            return rpc;
 050        }
 51
 52        [MethodImpl(MethodImplOptions.NoInlining)]
 53        private static AuthenticationBehavior CreateAuthenticationBehavior(DispatchRuntime dispatch)
 54        {
 055            AuthenticationBehavior authenticationBehavior = new AuthenticationBehavior(dispatch.ServiceAuthenticationMan
 056            return authenticationBehavior;
 57        }
 58
 59        public static AuthenticationBehavior TryCreate(DispatchRuntime dispatch)
 60        {
 66461            if (dispatch == null)
 62            {
 063                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dispatch));
 64            }
 65
 66466            if (!dispatch.RequiresAuthentication)
 67            {
 66468                return null;
 69            }
 70
 071            return CreateAuthenticationBehavior(dispatch);
 72        }
 73
 74        internal static Exception CreateFailedAuthenticationFaultException()
 75        {
 076            SecurityVersion wss = SecurityVersion.Default;
 077            FaultCode faultCode = FaultCode.CreateSenderFaultCode(wss.InvalidSecurityFaultCode.Value, wss.HeaderNamespac
 078            FaultReason faultReason = new FaultReason(SR.AuthenticationOfClientFailed);
 079            return new FaultException(faultReason, faultCode);
 80        }
 81    }
 82}