< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Description.ServiceCredentials
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/ServiceCredentials.cs
Line coverage
76%
Covered lines: 64
Uncovered lines: 20
Coverable lines: 84
Total lines: 201
Line coverage: 76.1%
Branch coverage
57%
Covered branches: 15
Total branches: 26
Branch coverage: 57.6%
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(...)50%2293.33%
.ctor(...)100%11100%
CreateDefaultCredentials()100%11100%
CreateSecurityTokenManager()100%22100%
CloneCore()100%11100%
Clone()33.33%6675%
CoreWCF.Description.IServiceBehavior.Validate(...)100%11100%
CoreWCF.Description.IServiceBehavior.AddBindingParameters(...)50%4471.42%
CoreWCF.Description.IServiceBehavior.ApplyDispatchBehavior(...)100%66100%
MakeReadOnly()100%110%
ThrowIfImmutable()0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/ServiceCredentials.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.Generic;
 6using System.Collections.ObjectModel;
 7using CoreWCF.Channels;
 8using CoreWCF.Dispatcher;
 9using CoreWCF.IdentityModel;
 10using CoreWCF.IdentityModel.Configuration;
 11using CoreWCF.IdentityModel.Selectors;
 12using CoreWCF.Security;
 13using Microsoft.Extensions.DependencyInjection;
 14
 15namespace CoreWCF.Description
 16{
 17    public class ServiceCredentials : SecurityCredentialsManager, IServiceBehavior
 18    {
 19        private bool _isReadOnly = false;
 11820        private readonly bool _saveBootstrapTokenInSession = true;
 21        private ExceptionMapper _exceptionMapper;
 22        private IServiceProvider _provider;
 23        private IdentityConfiguration _identityConfiguration;
 24
 5225        public ServiceCredentials()
 26        {
 5227            UserNameAuthentication = new UserNamePasswordServiceCredential();
 5228            ClientCertificate = new X509CertificateInitiatorServiceCredential();
 5229            ServiceCertificate = new X509CertificateRecipientServiceCredential();
 5230            WindowsAuthentication = new WindowsServiceCredential();
 5231            IssuedTokenAuthentication = new IssuedTokenServiceCredential();
 5232            SecureConversationAuthentication = new SecureConversationServiceCredential();
 5233            _exceptionMapper = new ExceptionMapper();
 5234            UseIdentityConfiguration = false;
 5235        }
 36
 6637        protected ServiceCredentials(ServiceCredentials other)
 38        {
 6639            if (other == null)
 40            {
 041                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(other));
 42            }
 6643            _provider = other._provider;
 6644            UserNameAuthentication = new UserNamePasswordServiceCredential(other.UserNameAuthentication);
 6645            ClientCertificate = new X509CertificateInitiatorServiceCredential(other.ClientCertificate);
 6646            ServiceCertificate = new X509CertificateRecipientServiceCredential(other.ServiceCertificate);
 6647            WindowsAuthentication = new WindowsServiceCredential(other.WindowsAuthentication);
 6648            IssuedTokenAuthentication = new IssuedTokenServiceCredential(other.IssuedTokenAuthentication);
 6649            SecureConversationAuthentication = new SecureConversationServiceCredential(other.SecureConversationAuthentic
 6650            _saveBootstrapTokenInSession = other._saveBootstrapTokenInSession;
 6651            _exceptionMapper = other._exceptionMapper;
 6652            _identityConfiguration = other._identityConfiguration;
 6653            UseIdentityConfiguration = other.UseIdentityConfiguration;
 6654        }
 55
 756        internal ServiceCredentials(IServiceProvider provider):this()
 57        {
 758            _provider = provider;
 759        }
 60
 13861        public UserNamePasswordServiceCredential UserNameAuthentication { get; }
 62
 9363        public X509CertificateInitiatorServiceCredential ClientCertificate { get; }
 64
 8965        public X509CertificateRecipientServiceCredential ServiceCertificate { get; }
 66
 11467        public WindowsServiceCredential WindowsAuthentication { get; }
 68
 7269        public IssuedTokenServiceCredential IssuedTokenAuthentication { get; }
 70
 22471        public SecureConversationServiceCredential SecureConversationAuthentication { get; }
 72
 73        public IdentityConfiguration IdentityConfiguration
 74        {
 75            get
 76            {
 677                if(_identityConfiguration == null)
 78                {
 279                    _identityConfiguration = _provider.GetRequiredService<IdentityConfiguration>();
 80                }
 681                return _identityConfiguration;
 82            }
 83            set
 84            {
 085                _identityConfiguration = value;
 086            }
 87        }
 88
 25289        public bool UseIdentityConfiguration { get; set; }
 90
 91        /// <summary>
 92        /// Gets or sets the ExceptionMapper to be used when throwing exceptions.
 93        /// </summary>
 94        public ExceptionMapper ExceptionMapper
 95        {
 96            get
 97            {
 298                return _exceptionMapper;
 99            }
 100            set
 101            {
 0102                ThrowIfImmutable();
 0103                _exceptionMapper = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(valu
 0104            }
 105        }
 106
 107        internal static ServiceCredentials CreateDefaultCredentials()
 108        {
 1109            return new ServiceCredentials();
 110        }
 111
 112        public override SecurityTokenManager CreateSecurityTokenManager()
 113        {
 66114            if (UseIdentityConfiguration)
 115            {
 2116                var list = new List<CookieTransform>
 2117                {
 2118                    new DeflateCookieTransform(),
 2119                    _provider.GetRequiredService<ProtectedDataCookieTransform>()
 2120                };
 121                //
 122                // Note: the token manager we create here is always a wrapper over the default collection of token handl
 123                //
 2124                return new FederatedSecurityTokenManager(Clone(), list.AsReadOnly());
 125            }
 126            else
 127            {
 64128                return new ServiceCredentialsSecurityTokenManager(Clone());
 129            }
 130        }
 131
 132        protected virtual ServiceCredentials CloneCore()
 133        {
 66134            return new ServiceCredentials(this);
 135        }
 136
 137        public ServiceCredentials Clone()
 138        {
 66139            ServiceCredentials result = CloneCore();
 66140            if (result == null || result.GetType() != GetType())
 141            {
 0142                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.Format(SR.Clone
 143            }
 66144            return result;
 145        }
 146
 147        void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
 148        {
 37149        }
 150
 151        void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Coll
 152        {
 57153            if (parameters == null)
 154            {
 0155                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters));
 156            }
 157            // throw if bindingParameters already has a SecurityCredentialsManager
 57158            SecurityCredentialsManager otherCredentialsManager = parameters.Find<SecurityCredentialsManager>();
 57159            if (otherCredentialsManager != null)
 160            {
 0161                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Mul
 162            }
 57163            parameters.Add(this);
 57164        }
 165
 166        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
 167        {
 182168            for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
 169            {
 170                // TODO: ServiceMetadataBehavior
 54171                if (serviceHostBase.ChannelDispatchers[i] is ChannelDispatcher channelDispatcher /*&& !ServiceMetadataBe
 172                {
 216173                    foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
 174                    {
 54175                        DispatchRuntime behavior = endpointDispatcher.DispatchRuntime;
 54176                        behavior.RequireClaimsPrincipalOnOperationContext = false; // _useIdentityConfiguration;
 177                    }
 178                }
 179            }
 37180        }
 181
 182        internal void MakeReadOnly()
 183        {
 0184            _isReadOnly = true;
 0185            ClientCertificate.MakeReadOnly();
 0186            IssuedTokenAuthentication.MakeReadOnly();
 0187            SecureConversationAuthentication.MakeReadOnly();
 0188            ServiceCertificate.MakeReadOnly();
 0189            UserNameAuthentication.MakeReadOnly();
 0190            WindowsAuthentication.MakeReadOnly();
 0191        }
 192
 193        private void ThrowIfImmutable()
 194        {
 0195            if (_isReadOnly)
 196            {
 0197                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO
 198            }
 0199        }
 200    }
 201}