< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.WindowsStreamSecurityUpgradeProvider
Assembly: CoreWCF.NetFramingBase
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetFramingBase/src/CoreWCF/Channels/WindowsStreamSecurityUpgradeProvider.cs
Line coverage
43%
Covered lines: 34
Uncovered lines: 44
Coverable lines: 78
Total lines: 208
Line coverage: 43.5%
Branch coverage
72%
Covered branches: 13
Total branches: 18
Branch coverage: 72.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%44100%
CreateUpgradeAcceptor()100%110%
OnAbort()100%110%
OnCloseAsync(...)100%110%
OnOpenAsync()100%11100%
OnOpened()75%4483.33%
.ctor(...)100%110%
OnAcceptUpgradeAsync()100%110%
CreateClientSecurityAsync()0%220%
GetRemoteSecurity()0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetFramingBase/src/CoreWCF/Channels/WindowsStreamSecurityUpgradeProvider.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.IO;
 7using System.Net;
 8using System.Net.Security;
 9using System.Security.Authentication;
 10using System.Security.Claims;
 11using System.Security.Principal;
 12using System.Threading;
 13using System.Threading.Tasks;
 14using CoreWCF.Channels.Framing;
 15using CoreWCF.Description;
 16using CoreWCF.IdentityModel;
 17using CoreWCF.IdentityModel.Policy;
 18using CoreWCF.IdentityModel.Selectors;
 19using CoreWCF.IdentityModel.Tokens;
 20using CoreWCF.Security;
 21
 22namespace CoreWCF.Channels
 23{
 24    internal class WindowsStreamSecurityUpgradeProvider : StreamSecurityUpgradeProvider
 25    {
 26        private EndpointIdentity _identity;
 27        private readonly SecurityTokenManager _securityTokenManager;
 28        private readonly Uri _listenUri;
 29
 30        public WindowsStreamSecurityUpgradeProvider(WindowsStreamSecurityBindingElement bindingElement, BindingContext c
 1231            : base(context.Binding)
 32        {
 1233            ExtractGroupsForWindowsAccounts = TransportDefaults.ExtractGroupsForWindowsAccounts;
 1234            ProtectionLevel = bindingElement.ProtectionLevel;
 1235            Scheme = context.Binding.Scheme;
 1236            _listenUri = TransportSecurityHelpers.GetListenUri(context.ListenUriBaseAddress, context.ListenUriRelativeAd
 37
 1238            SecurityCredentialsManager credentialProvider = context.BindingParameters.Find<SecurityCredentialsManager>()
 1239            if (credentialProvider == null)
 40            {
 41                //if (isClient)
 42                //{
 43                //    credentialProvider = ClientCredentials.CreateDefaultCredentials();
 44                //}
 45                //else
 46                //{
 1247                credentialProvider = new ServiceCredentials(); //ServiceCredentials.CreateDefaultCredentials();
 48                //}
 49            }
 50
 1251           if(credentialProvider is ServiceCredentials)
 52            {
 1253                ServiceCredentials serviceCred = (ServiceCredentials)credentialProvider;
 1254                LdapSettings = serviceCred.WindowsAuthentication.LdapSetting;
 55            }
 1256           _securityTokenManager = credentialProvider.CreateSecurityTokenManager();
 1257        }
 58
 1259        public string Scheme { get; }
 60
 2461        internal bool ExtractGroupsForWindowsAccounts { get; private set; }
 62
 63        public override EndpointIdentity Identity
 64        {
 65            get
 66            {
 67                // If the server credential is null, then we have not been opened yet and have no identity to expose.
 1268                if (ServerCredential != null)
 69                {
 1270                    if (_identity == null)
 71                    {
 1272                        lock (ThisLock)
 73                        {
 1274                            if (_identity == null)
 75                            {
 1276                                _identity = SecurityUtils.CreateWindowsIdentity(ServerCredential);
 77                            }
 1278                        }
 79                    }
 80                }
 1281                return _identity;
 82            }
 83        }
 84
 2485        internal IdentityVerifier IdentityVerifier { get; private set; }
 86
 087        public ProtectionLevel ProtectionLevel { get; }
 88
 4889        private NetworkCredential ServerCredential { get; set; }
 90
 1291        protected LdapSettings LdapSettings { get; private set; }
 92
 93        public override StreamUpgradeAcceptor CreateUpgradeAcceptor()
 94        {
 095            ThrowIfDisposedOrNotOpen();
 096            return new WindowsStreamSecurityUpgradeAcceptor(this);
 97        }
 98
 99        protected override void OnAbort()
 100        {
 0101        }
 102
 103        protected override Task OnCloseAsync(CancellationToken token)
 104        {
 0105            return Task.CompletedTask;
 106        }
 107
 108        protected override async Task OnOpenAsync(CancellationToken token)
 109        {
 12110            SecurityTokenRequirement sspiTokenRequirement = TransportSecurityHelpers.CreateSspiTokenRequirement(Scheme, 
 12111            (ServerCredential, ExtractGroupsForWindowsAccounts) = await
 12112                TransportSecurityHelpers.GetSspiCredentialAsync(_securityTokenManager, sspiTokenRequirement, token);
 12113        }
 114
 115        protected override void OnOpened()
 116        {
 12117            base.OnOpened();
 118
 12119            if (IdentityVerifier == null)
 120            {
 12121                IdentityVerifier = IdentityVerifier.CreateDefault();
 122            }
 123
 12124            if (ServerCredential == null)
 125            {
 0126                ServerCredential = CredentialCache.DefaultNetworkCredentials;
 127            }
 12128        }
 129
 130        private class WindowsStreamSecurityUpgradeAcceptor : StreamSecurityUpgradeAcceptorBase
 131        {
 132            private readonly WindowsStreamSecurityUpgradeProvider _parent;
 133            private readonly SecurityMessageProperty _clientSecurity;
 134            private readonly LdapSettings _ldapSettings;
 135
 136            public WindowsStreamSecurityUpgradeAcceptor(WindowsStreamSecurityUpgradeProvider parent)
 0137                : base(FramingUpgradeString.Negotiate)
 138            {
 0139                _parent = parent;
 0140                _clientSecurity = new SecurityMessageProperty();
 0141                _ldapSettings = parent.LdapSettings;
 0142            }
 143
 144            protected override async Task<(Stream, SecurityMessageProperty)> OnAcceptUpgradeAsync(Stream stream)
 145            {
 146                // wrap stream
 0147                NegotiateStream negotiateStream = new NegotiateStream(stream, true);
 148
 149                // authenticate
 150                try
 151                {
 0152                    await negotiateStream.AuthenticateAsServerAsync(_parent.ServerCredential, _parent.ProtectionLevel,
 0153                        TokenImpersonationLevel.Identification);
 0154                }
 0155                catch (AuthenticationException exception)
 156                {
 0157                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception
 0158                        exception));
 159                }
 0160                catch (IOException ioException)
 161                {
 0162                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
 0163                        SR.Format(SR.NegotiationFailedIO, ioException.Message), ioException));
 164                }
 165
 0166                SecurityMessageProperty remoteSecurity = await CreateClientSecurityAsync(negotiateStream, _parent.Extrac
 0167                return (negotiateStream, remoteSecurity);
 0168            }
 169
 170            private async Task<SecurityMessageProperty> CreateClientSecurityAsync(NegotiateStream negotiateStream,
 171                          bool extractGroupsForWindowsAccounts)
 172            {
 0173                IIdentity remoteIdentity = negotiateStream.RemoteIdentity;
 174                SecurityToken token;
 175                ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies;
 0176                WindowsSecurityTokenAuthenticator authenticator = new WindowsSecurityTokenAuthenticator(extractGroupsFor
 0177                if (remoteIdentity is WindowsIdentity)
 178                {
 0179                    WindowsIdentity windowIdentity = (WindowsIdentity)remoteIdentity;
 0180                    SecurityUtils.ValidateAnonymityConstraint(windowIdentity, false);
 0181                    token = new WindowsSecurityToken(windowIdentity, SecurityUniqueId.Create().Value, windowIdentity.Aut
 182                }
 183                else
 184                {
 0185                    GenericIdentity genericIdentity = (GenericIdentity)remoteIdentity;
 0186                    ClaimsIdentity claimsIdentity = new ClaimsIdentity(remoteIdentity);
 0187                    token = new GenericIdentitySecurityToken(genericIdentity, SecurityUniqueId.Create().Value);
 188                }
 0189                authorizationPolicies = await authenticator.ValidateTokenAsync(token);
 0190                SecurityMessageProperty clientSecurity = new SecurityMessageProperty
 0191                {
 0192                    TransportToken = new SecurityTokenSpecification(token, authorizationPolicies),
 0193                    ServiceSecurityContext = new ServiceSecurityContext(authorizationPolicies)
 0194                };
 0195                return clientSecurity;
 0196            }
 197
 198            public override SecurityMessageProperty GetRemoteSecurity()
 199            {
 0200                if (_clientSecurity.TransportToken != null)
 201                {
 0202                    return _clientSecurity;
 203                }
 0204                return base.GetRemoteSecurity();
 205            }
 206        }
 207    }
 208}