< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.MessageSecurityOverHttp
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/MessageSecurityOverHttp.cs
Line coverage
77%
Covered lines: 42
Uncovered lines: 12
Coverable lines: 54
Total lines: 208
Line coverage: 77.7%
Branch coverage
62%
Covered branches: 20
Total branches: 32
Branch coverage: 62.5%
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%
IsSecureConversationEnabled()100%110%
CreateSecurityBindingElement(...)62.5%242471.87%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/MessageSecurityOverHttp.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.Runtime.InteropServices;
 6using CoreWCF.Channels;
 7using CoreWCF.Runtime;
 8using CoreWCF.Security;
 9using CoreWCF.Security.Tokens;
 10
 11namespace CoreWCF
 12{
 13    public class MessageSecurityOverHttp
 14    {
 15        internal const MessageCredentialType DefaultClientCredentialType = MessageCredentialType.Windows;
 16        internal const bool DefaultNegotiateServiceCredential = true;
 17        private MessageCredentialType _clientCredentialType;
 18        private SecurityAlgorithmSuite _algorithmSuite;
 219        private static readonly TimeSpan s_defaultServerIssuedTransitionTokenLifetime = TimeSpan.FromMinutes(15);
 20        private const string NetFrameworkFrameworkName = ".NET Framework";
 4521        public MessageSecurityOverHttp()
 22        {
 4523            _clientCredentialType = DefaultClientCredentialType;
 4524            NegotiateServiceCredential = DefaultNegotiateServiceCredential;
 4525            _algorithmSuite = SecurityAlgorithmSuite.Default;
 4526        }
 27
 28        public MessageCredentialType ClientCredentialType
 29        {
 130            get { return _clientCredentialType; }
 31            set
 32            {
 2533                if (!MessageCredentialTypeHelper.IsDefined(value))
 34                {
 035                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 36                }
 37
 2538                if (value is MessageCredentialType.Windows)
 39                {
 40                    //TODO Remove this after .net 5+
 241                    string frameworkDescription = RuntimeInformation.FrameworkDescription;
 242                    if (frameworkDescription.IndexOf(NetFrameworkFrameworkName, StringComparison.Ordinal) >= 0)
 43                    {
 044                        throw new PlatformNotSupportedException("Windows auth only supported on .NET Core");
 45                    }
 46                }
 47
 2548                _clientCredentialType = value;
 2549            }
 50        }
 51
 4752        public bool NegotiateServiceCredential { get; set; }
 53
 54        public SecurityAlgorithmSuite AlgorithmSuite
 55        {
 8756            get { return _algorithmSuite; }
 57            set
 58            {
 659                _algorithmSuite = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value
 660                WasAlgorithmSuiteSet = true;
 661            }
 62        }
 63
 9364        internal bool WasAlgorithmSuiteSet { get; private set; }
 65
 66        protected virtual bool IsSecureConversationEnabled()
 67        {
 068            return true;
 69        }
 70
 71        public SecurityBindingElement CreateSecurityBindingElement(bool isSecureTransportMode, bool isReliableSession, M
 72        {
 8773            if (isReliableSession && !IsSecureConversationEnabled())
 74            {
 075                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec
 76            }
 77
 78            SecurityBindingElement result;
 8779            bool isKerberosSelected = false;
 80            SecurityBindingElement oneShotSecurity;
 8781            if (isSecureTransportMode)
 82            {
 8783                switch (_clientCredentialType)
 84                {
 85                    case MessageCredentialType.None:
 086                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 87                    case MessageCredentialType.UserName:
 5388                        oneShotSecurity = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
 5389                        break;
 90                    case MessageCredentialType.Certificate:
 3391                        oneShotSecurity = SecurityBindingElement.CreateCertificateOverTransportBindingElement();
 3392                        break;
 93                    case MessageCredentialType.Windows:
 194                        oneShotSecurity = SecurityBindingElement.CreateSspiNegotiationOverTransportBindingElement(true);
 195                        break;
 96                    case MessageCredentialType.IssuedToken:
 097                        oneShotSecurity = SecurityBindingElement.CreateIssuedTokenOverTransportBindingElement(IssuedSecu
 098                        break;
 99                    default:
 100                        Fx.Assert("unknown ClientCredentialType");
 0101                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
 102                }
 87103                if (IsSecureConversationEnabled())
 104                {
 85105                    result = SecurityBindingElement.CreateSecureConversationBindingElement(oneShotSecurity, true);
 106                }
 107                else
 108                {
 2109                    result = oneShotSecurity;
 110                }
 111            }
 112            else
 113            {
 0114                throw new PlatformNotSupportedException();
 115                //TODO
 116                //if (negotiateServiceCredential)
 117                //{
 118                //    switch (this.clientCredentialType)
 119                //    {
 120                //        case MessageCredentialType.None:
 121                //            oneShotSecurity = SecurityBindingElement.CreateSslNegotiationBindingElement(false, true);
 122                //            break;
 123                //        case MessageCredentialType.UserName:
 124                //            oneShotSecurity = SecurityBindingElement.CreateUserNameForSslBindingElement(true);
 125                //            break;
 126                //        case MessageCredentialType.Certificate:
 127                //            oneShotSecurity = SecurityBindingElement.CreateSslNegotiationBindingElement(true, true);
 128                //            break;
 129                //        case MessageCredentialType.Windows:
 130                //            oneShotSecurity = SecurityBindingElement.CreateSspiNegotiationBindingElement(true);
 131                //            break;
 132                //        case MessageCredentialType.IssuedToken:
 133                //            oneShotSecurity = SecurityBindingElement.CreateIssuedTokenForSslBindingElement(IssuedSecur
 134                //            break;
 135                //        default:
 136                //            Fx.Assert("unknown ClientCredentialType");
 137                //            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
 138                //    }
 139                //}
 140                //else
 141                //{
 142                //    switch (this.clientCredentialType)
 143                //    {
 144                //        case MessageCredentialType.None:
 145                //            oneShotSecurity = SecurityBindingElement.CreateAnonymousForCertificateBindingElement();
 146                //            break;
 147                //        case MessageCredentialType.UserName:
 148                //            oneShotSecurity = SecurityBindingElement.CreateUserNameForCertificateBindingElement();
 149                //            break;
 150                //        case MessageCredentialType.Certificate:
 151                //            oneShotSecurity = SecurityBindingElement.CreateMutualCertificateBindingElement();
 152                //            break;
 153                //        case MessageCredentialType.Windows:
 154                //            oneShotSecurity = SecurityBindingElement.CreateKerberosBindingElement();
 155                //            isKerberosSelected = true;
 156                //            break;
 157                //        case MessageCredentialType.IssuedToken:
 158                //            oneShotSecurity = SecurityBindingElement.CreateIssuedTokenForCertificateBindingElement(Iss
 159                //            break;
 160                //        default:
 161                //            Fx.Assert("unknown ClientCredentialType");
 162                //            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
 163                //    }
 164                //}
 165                //if (IsSecureConversationEnabled())
 166                //{
 167                //    result = SecurityBindingElement.CreateSecureConversationBindingElement(oneShotSecurity, true);
 168                //}
 169                //else
 170                //{
 171                //    result = oneShotSecurity;
 172                //}
 173            }
 174
 175            // set the algorithm suite and issued token params if required
 87176            if (WasAlgorithmSuiteSet || (!isKerberosSelected))
 177            {
 87178                result.DefaultAlgorithmSuite = oneShotSecurity.DefaultAlgorithmSuite = AlgorithmSuite;
 179            }
 0180            else if (isKerberosSelected)
 181            {
 0182                result.DefaultAlgorithmSuite = oneShotSecurity.DefaultAlgorithmSuite = SecurityAlgorithmSuite.KerberosDe
 183            }
 184
 87185            result.IncludeTimestamp = true;
 87186            oneShotSecurity.MessageSecurityVersion = version;
 87187            result.MessageSecurityVersion = version;
 87188            if (!isReliableSession)
 189            {
 87190                result.LocalServiceSettings.ReconnectTransportOnFailure = false;
 191            }
 192            else
 193            {
 0194                result.LocalServiceSettings.ReconnectTransportOnFailure = true;
 195            }
 196
 87197            if (IsSecureConversationEnabled())
 198            {
 85199                oneShotSecurity.LocalServiceSettings.IssuedCookieLifetime = s_defaultServerIssuedTransitionTokenLifetime
 200                //TODO SpNego when port, remove above and enable below.
 201                // issue the transition SCT for a short duration only
 202                // oneShotSecurity.LocalServiceSettings.IssuedCookieLifetime = SpnegoTokenAuthenticator.defaultServerIss
 203            }
 204
 87205            return result;
 206        }
 207    }
 208}