< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.HttpsTransportBindingElement
Assembly: CoreWCF.Http
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/src/CoreWCF/Channels/HttpsTransportBindingElement.cs
Line coverage
58%
Covered lines: 46
Uncovered lines: 33
Coverable lines: 79
Total lines: 200
Line coverage: 58.2%
Branch coverage
36%
Covered branches: 14
Total branches: 38
Branch coverage: 36.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%110%
Clone()100%11100%
GetSupportsClientAuthenticationImpl(...)0%220%
GetSupportsClientWindowsIdentityImpl(...)0%220%
CreateFromHttpBindingElement(...)100%110%
GetProperty(...)50%4427.27%
OnExportPolicy(...)83.33%6693.33%
CreateTransportTokenAssertion(...)22.22%181827.27%
CreateWsspAssertion(...)50%4480%
GetTransportTokenAssertion()100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Http/src/CoreWCF/Channels/HttpsTransportBindingElement.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.Net;
 6using System.Net.Security;
 7using System.Xml;
 8using CoreWCF.Description;
 9
 10namespace CoreWCF.Channels
 11{
 12    public class HttpsTransportBindingElement : HttpTransportBindingElement, ITransportTokenAssertionProvider
 13    {
 14        private MessageSecurityVersion _messageSecurityVersion;
 15        private XmlElement _transportTokenAssertion;
 16
 42717        public HttpsTransportBindingElement() : base()
 18        {
 42719            RequireClientCertificate = TransportDefaults.RequireClientCertificate;
 42720        }
 21
 22        protected HttpsTransportBindingElement(HttpsTransportBindingElement elementToBeCloned)
 145123            : base(elementToBeCloned)
 24        {
 145125            RequireClientCertificate = elementToBeCloned.RequireClientCertificate;
 145126            _messageSecurityVersion = elementToBeCloned.MessageSecurityVersion;
 145127        }
 28
 29        private HttpsTransportBindingElement(HttpTransportBindingElement elementToBeCloned)
 030            : base(elementToBeCloned)
 31        {
 032        }
 33
 413434        public bool RequireClientCertificate { get; set; }
 35
 36        public override string Scheme
 37        {
 49038            get { return "https"; }
 39        }
 40
 41        internal MessageSecurityVersion MessageSecurityVersion
 42        {
 43            get
 44            {
 145145                return _messageSecurityVersion;
 46            }
 47            set
 48            {
 1549                if (value == null)
 50                {
 051                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(value)));
 52                }
 1553                _messageSecurityVersion = value;
 1554            }
 55        }
 56
 57        public override BindingElement Clone()
 58        {
 145159            return new HttpsTransportBindingElement(this);
 60        }
 61
 62        internal override bool GetSupportsClientAuthenticationImpl(AuthenticationSchemes effectiveAuthenticationSchemes)
 63        {
 064            return RequireClientCertificate || base.GetSupportsClientAuthenticationImpl(effectiveAuthenticationSchemes);
 65        }
 66
 67        internal override bool GetSupportsClientWindowsIdentityImpl(AuthenticationSchemes effectiveAuthenticationSchemes
 68        {
 069            return RequireClientCertificate || base.GetSupportsClientWindowsIdentityImpl(effectiveAuthenticationSchemes)
 70        }
 71
 72        internal static HttpsTransportBindingElement CreateFromHttpBindingElement(HttpTransportBindingElement elementToB
 73        {
 074            return new HttpsTransportBindingElement(elementToBeCloned);
 75        }
 76
 77        public override T GetProperty<T>(BindingContext context)
 78        {
 15279            if (context == null)
 80            {
 081                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
 82            }
 15283            if (typeof(T) == typeof(ISecurityCapabilities))
 84            {
 085                AuthenticationSchemes effectiveAuthenticationSchemes = GetEffectiveAuthenticationSchemes(AuthenticationS
 086                    context.BindingParameters);
 87
 088                return (T)(object)new SecurityCapabilities(GetSupportsClientAuthenticationImpl(effectiveAuthenticationSc
 089                    true,
 090                    GetSupportsClientWindowsIdentityImpl(effectiveAuthenticationSchemes),
 091                    ProtectionLevel.EncryptAndSign,
 092                    ProtectionLevel.EncryptAndSign);
 93            }
 94            else
 95            {
 15296                return base.GetProperty<T>(context);
 97            }
 98        }
 99
 100        internal override void OnExportPolicy(MetadataExporter exporter, PolicyConversionContext context)
 101        {
 22102            base.OnExportPolicy(exporter, context);
 22103            var tsbe = context.BindingElements.Find<TransportSecurityBindingElement>();
 22104            if (tsbe != null)
 105            {
 19106                _transportTokenAssertion = CreateTransportTokenAssertion(tsbe.MessageSecurityVersion.SecurityPolicyVersi
 19107                SecurityBindingElement.ExportPolicyForTransportTokenAssertionProviders(exporter, context);
 19108                _transportTokenAssertion = null;
 109            }
 110            else
 111            {
 112                // The below code used to be in ExportPolicyForTransportTokenAssertionProviders but as it can't access t
 113                // it's now been moved inline. This is the code that used to be executed before calling ExportTransportS
 3114                TransportSecurityBindingElement dummyTransportBindingElement = new TransportSecurityBindingElement();
 3115                if (context.BindingElements.Find<SecurityBindingElement>() == null)
 116                {
 3117                    dummyTransportBindingElement.IncludeTimestamp = false;
 118                }
 119
 120                // In order to generate the right sp assertion without SBE.
 121                // scenario: WSxHttpBinding with SecurityMode.Transport.
 3122                if (_messageSecurityVersion != null)
 123                {
 0124                    dummyTransportBindingElement.MessageSecurityVersion = _messageSecurityVersion;
 125                }
 126
 3127                _transportTokenAssertion = CreateTransportTokenAssertion(dummyTransportBindingElement.MessageSecurityVer
 3128                SecurityBindingElement.ExportTransportSecurityBindingElement(dummyTransportBindingElement, this, exporte
 3129                _transportTokenAssertion = null;
 130            }
 3131        }
 132
 133        private XmlElement CreateTransportTokenAssertion(Security.SecurityPolicyVersion securityPolicyVersion, MetadataE
 134        {
 22135            XmlElement assertion = null;
 22136            if (securityPolicyVersion == Security.SecurityPolicyVersion.WSSecurityPolicy11)
 137            {
 22138                assertion = CreateWsspAssertion(securityPolicyVersion, "HttpsToken"); // WSSecurityPolicy.HttpsTokenName
 22139                assertion.SetAttribute("RequireClientCertificate", // WSSecurityPolicy.RequireClientCertificateName
 22140                                    RequireClientCertificate ? "true" : "false");
 141            }
 0142            else if (securityPolicyVersion == Security.SecurityPolicyVersion.WSSecurityPolicy12)
 143            {
 0144                assertion = CreateWsspAssertion(securityPolicyVersion, "HttpsToken"); // WSSecurityPolicy.HttpsTokenName
 0145                if (RequireClientCertificate ||
 0146                    AuthenticationScheme == AuthenticationSchemes.Basic ||
 0147                    AuthenticationScheme == AuthenticationSchemes.Digest)
 148                {
 0149                    var doc = new XmlDocument();
 0150                    XmlElement policy = doc.CreateElement("wsp", // WspPrefix
 0151                                                          "Policy", // PolicyName
 0152                                                          exporter.PolicyVersion.Namespace);
 0153                    if (RequireClientCertificate)
 154                    {
 0155                        policy.AppendChild(CreateWsspAssertion(securityPolicyVersion, "RequireClientCertificate"));
 156                    }
 0157                    if (AuthenticationScheme == AuthenticationSchemes.Basic)
 158                    {
 0159                        policy.AppendChild(CreateWsspAssertion(securityPolicyVersion, "HttpBasicAuthentication"));
 160                    }
 0161                    else if (AuthenticationScheme == AuthenticationSchemes.Digest)
 162                    {
 0163                        policy.AppendChild(CreateWsspAssertion(securityPolicyVersion, "HttpDigestAuthentication"));
 164                    }
 0165                    assertion.AppendChild(policy);
 166                }
 167            }
 168
 22169            return assertion;
 170        }
 171
 172        private XmlElement CreateWsspAssertion(Security.SecurityPolicyVersion policyVersion, string name)
 173        {
 22174            string policyNamespace = string.Empty;
 22175            if (policyVersion == Security.SecurityPolicyVersion.WSSecurityPolicy11)
 176            {
 22177                policyNamespace = @"http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"; //WSSecurityPolicy11.WsspName
 178            }
 0179            else if(policyVersion == Security.SecurityPolicyVersion.WSSecurityPolicy12)
 180            {
 0181                policyNamespace = @"http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702"; //WSSecurityPolicy12.Wss
 182            }
 183
 22184            var doc = new XmlDocument();
 22185            XmlElement result = doc.CreateElement("sp", // WSSecurityPolicy.WsspPrefix
 22186                                                  name,
 22187                                                  policyNamespace);
 22188            return result;
 189        }
 190
 191        #region ITransportTokenAssertionProvider Members
 192
 193        public XmlElement GetTransportTokenAssertion()
 194        {
 22195            return _transportTokenAssertion;
 196        }
 197
 198        #endregion
 199    }
 200}