< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.TransportSecurityHelpers
Assembly: CoreWCF.NetFramingBase
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetFramingBase/src/CoreWCF/Channels/TransportSecurityHelpers.cs
Line coverage
78%
Covered lines: 54
Uncovered lines: 15
Coverable lines: 69
Total lines: 157
Line coverage: 78.2%
Branch coverage
66%
Covered branches: 16
Total branches: 24
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetTokenAsync()75%4466.66%
GetSspiCredentialAsync()83.33%6693.75%
GetSspiCredentialAsync()87.5%8888.23%
CreateSspiTokenRequirement(...)100%11100%
GetCertificateTokenAuthenticator(...)100%11100%
GetListenUri(...)25%4433.33%
FixIpv6Hostname(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.NetFramingBase/src/CoreWCF/Channels/TransportSecurityHelpers.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.Security.Principal;
 7using System.Threading;
 8using System.Threading.Tasks;
 9using CoreWCF.IdentityModel.Selectors;
 10using CoreWCF.IdentityModel.Tokens;
 11using CoreWCF.Security;
 12using CoreWCF.Security.Tokens;
 13
 14namespace CoreWCF.Channels
 15{
 16    internal static class TransportSecurityHelpers
 17    {
 18        private static async Task<T> GetTokenAsync<T>(SecurityTokenProvider tokenProvider, CancellationToken token)
 19            where T : SecurityToken
 20        {
 1221            SecurityToken result = await tokenProvider.GetTokenAsync(token);
 1222            if ((result != null) && !(result is T))
 23            {
 024                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(
 025                    SR.InvalidTokenProvided, tokenProvider.GetType(), typeof(T))));
 26            }
 1227            return result as T;
 1228        }
 29
 30        // used by server WindowsStream security (from Open)
 31        public static async Task<(NetworkCredential, bool)> GetSspiCredentialAsync(SecurityTokenManager credentialProvid
 32            SecurityTokenRequirement sspiTokenRequirement, CancellationToken token)
 33        {
 1234            bool extractGroupsForWindowsAccounts = TransportDefaults.ExtractGroupsForWindowsAccounts;
 1235            NetworkCredential result = null;
 36
 1237            if (credentialProvider != null)
 38            {
 1239                SecurityTokenProvider tokenProvider = credentialProvider.CreateSecurityTokenProvider(sspiTokenRequiremen
 1240                if (tokenProvider != null)
 41                {
 1242                    await SecurityUtils.OpenTokenProviderIfRequiredAsync(tokenProvider, token);
 1243                    bool success = false;
 44                    try
 45                    {
 46                        TokenImpersonationLevel dummyImpersonationLevel;
 47                        bool dummyAllowNtlm;
 1248                        (result, extractGroupsForWindowsAccounts, dummyImpersonationLevel, dummyAllowNtlm) = await GetSs
 49
 1250                        success = true;
 1251                    }
 52                    finally
 53                    {
 1254                        if (!success)
 55                        {
 056                            SecurityUtils.AbortTokenProviderIfRequired(tokenProvider);
 57                        }
 58                    }
 1259                    await SecurityUtils.CloseTokenProviderIfRequiredAsync(tokenProvider, token);
 60                }
 1261            }
 62
 1263            return (result, extractGroupsForWindowsAccounts);
 1264        }
 65
 66        // core Cred lookup code
 67        private static async Task<(NetworkCredential, bool, TokenImpersonationLevel, bool)> GetSspiCredentialAsync(SspiS
 68        {
 1269            NetworkCredential credential = null;
 1270            bool extractGroupsForWindowsAccounts = TransportDefaults.ExtractGroupsForWindowsAccounts;
 1271            TokenImpersonationLevel impersonationLevel = TokenImpersonationLevel.Identification;
 1272            bool allowNtlm = ConnectionOrientedTransportDefaults.AllowNtlm;
 73
 1274            if (tokenProvider != null)
 75            {
 1276                SspiSecurityToken token = await GetTokenAsync<SspiSecurityToken>(tokenProvider, cancellationToken);
 1277                if (token != null)
 78                {
 1279                    extractGroupsForWindowsAccounts = token.ExtractGroupsForWindowsAccounts;
 1280                    impersonationLevel = token.ImpersonationLevel;
 1281                    allowNtlm = token.AllowNtlm;
 1282                    if (token.NetworkCredential != null)
 83                    {
 084                        credential = token.NetworkCredential;
 085                        SecurityUtils.FixNetworkCredential(ref credential);
 86                    }
 87                }
 88            }
 89
 90            // Initialize to the default value if no token provided. A partial trust app should not have access to the
 91            // default network credentials but should be able to provide credentials. The DefaultNetworkCredentials
 92            // getter will throw under partial trust.
 1293            if (credential == null)
 94            {
 1295                credential = CredentialCache.DefaultNetworkCredentials;
 96            }
 97
 1298            return (credential, extractGroupsForWindowsAccounts, impersonationLevel, allowNtlm);
 1299        }
 100
 101        public static SecurityTokenRequirement CreateSspiTokenRequirement(string transportScheme, Uri listenUri)
 102        {
 12103            RecipientServiceModelSecurityTokenRequirement tokenRequirement = new RecipientServiceModelSecurityTokenRequi
 12104            {
 12105                TransportScheme = transportScheme,
 12106                RequireCryptographicToken = false,
 12107                ListenUri = listenUri,
 12108                TokenType = ServiceModelSecurityTokenTypes.SspiCredential
 12109            };
 12110            return tokenRequirement;
 111        }
 112
 113        public static SecurityTokenAuthenticator GetCertificateTokenAuthenticator(SecurityTokenManager tokenManager, str
 114        {
 5115            RecipientServiceModelSecurityTokenRequirement clientAuthRequirement = new RecipientServiceModelSecurityToken
 5116            {
 5117                TokenType = SecurityTokenTypes.X509Certificate,
 5118                RequireCryptographicToken = true,
 5119                KeyUsage = SecurityKeyUsage.Signature,
 5120                TransportScheme = transportScheme,
 5121                ListenUri = listenUri
 5122            };
 5123            return tokenManager.CreateSecurityTokenAuthenticator(clientAuthRequirement, out SecurityTokenResolver dummy)
 124        }
 125
 126        public static Uri GetListenUri(Uri baseAddress, string relativeAddress)
 127        {
 17128            Uri fullUri = baseAddress;
 129
 130            // Ensure that baseAddress Path does end with a slash if we have a relative address
 17131            if (!string.IsNullOrEmpty(relativeAddress))
 132            {
 0133                if (!baseAddress.AbsolutePath.EndsWith("/", StringComparison.Ordinal))
 134                {
 0135                    UriBuilder uriBuilder = new UriBuilder(baseAddress);
 0136                    FixIpv6Hostname(uriBuilder, baseAddress);
 0137                    uriBuilder.Path = uriBuilder.Path + "/";
 0138                    baseAddress = uriBuilder.Uri;
 139                }
 140
 0141                fullUri = new Uri(baseAddress, relativeAddress);
 142            }
 143
 17144            return fullUri;
 145        }
 146
 147        // Moved from TcpChannelListener
 148        internal static void FixIpv6Hostname(UriBuilder uriBuilder, Uri originalUri)
 149        {
 0150            if (originalUri.HostNameType == UriHostNameType.IPv6)
 151            {
 0152                string ipv6Host = originalUri.DnsSafeHost;
 0153                uriBuilder.Host = string.Concat("[", ipv6Host, "]");
 154            }
 0155        }
 156    }
 157}