< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/ConfigurationBasedIssuerNameRegistry.cs
Line coverage
79%
Covered lines: 19
Uncovered lines: 5
Coverable lines: 24
Total lines: 116
Line coverage: 79.1%
Branch coverage
57%
Covered branches: 8
Total branches: 14
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
GetIssuerName(...)62.5%8888.88%
AddTrustedIssuer(...)50%6666.66%
Equals(...)100%11100%
GetHashCode(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/ConfigurationBasedIssuerNameRegistry.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.Collections.Generic;
 5using System.Globalization;
 6using System;
 7
 8namespace CoreWCF.IdentityModel.Tokens
 9{
 10    public class ConfigurationBasedIssuerNameRegistry : IssuerNameRegistry
 11    {
 612        private readonly Dictionary<string, string> _configuredTrustedIssuers = new Dictionary<string, string>(new Thumb
 13
 14        /// <summary>
 15        /// Creates an instance of <see cref="ConfigurationBasedIssuerNameRegistry"/>
 16        /// </summary>
 617        public ConfigurationBasedIssuerNameRegistry()
 18        {
 619        }
 20
 21        /// <summary>
 22        /// Returns the issuer name of the given X509SecurityToken mapping the Certificate Thumbprint to
 23        /// a name in the configured map.
 24        /// </summary>
 25        /// <param name="securityToken">SecurityToken for which the issuer name is requested.</param>
 26        /// <returns>Issuer name if the token was registered, null otherwise.</returns>
 27        /// <exception cref="ArgumentNullException">The input parameter 'securityToken' is null.</exception>
 28        public override string GetIssuerName(SecurityToken securityToken)
 29        {
 3230            if (securityToken == null)
 31            {
 232                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityToken));
 33            }
 34
 3035            if (securityToken is X509SecurityToken x509SecurityToken)
 36            {
 3037                string thumbprint = x509SecurityToken.Certificate.Thumbprint;
 3038                if (_configuredTrustedIssuers.ContainsKey(thumbprint))
 39                {
 3040                    string issuerName = _configuredTrustedIssuers[thumbprint];
 3041                    issuerName = string.IsNullOrEmpty(issuerName) ? x509SecurityToken.Certificate.Subject : issuerName;
 42
 43                    //if (TD.GetIssuerNameSuccessIsEnabled())
 44                    //{
 45                    //    TD.GetIssuerNameSuccess(EventTraceActivity.GetFromThreadOrCreate(), issuerName, securityToken.
 46                    //}
 47
 3048                    return issuerName;
 49                }
 50            }
 51
 52            //if (TD.GetIssuerNameFailureIsEnabled())
 53            //{
 54            //    TD.GetIssuerNameFailure(EventTraceActivity.GetFromThreadOrCreate(), securityToken.Id);
 55            //}
 56
 057            return null;
 58        }
 59
 60        /// <summary>
 61        /// Gets the Dictionary of Configured Trusted Issuers. The key
 62        /// to the dictionary is the ASN.1 encoded form of the Thumbprint
 63        /// of the trusted issuer's certificate and the value is the issuer name.
 64        /// </summary>
 65        public IDictionary<string, string> ConfiguredTrustedIssuers
 66        {
 067            get { return _configuredTrustedIssuers; }
 68        }
 69
 70        /// <summary>
 71        /// Adds a trusted issuer to the collection.
 72        /// </summary>
 73        /// <param name="certificateThumbprint">ASN.1 encoded form of the trusted issuer's certificate Thumbprint.</para
 74        /// <param name="name">Name of the trusted issuer.</param>
 75        /// <exception cref="ArgumentException">The argument 'certificateThumbprint' or 'name' is either null or Empty.<
 76        /// <exception cref="InvalidOperationException">The issuer specified by 'certificateThumbprint' argument has alr
 77        public void AddTrustedIssuer(string certificateThumbprint, string name)
 78        {
 479            if (string.IsNullOrEmpty(certificateThumbprint))
 80            {
 081                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificateThumbprint));
 82            }
 83
 484            if (string.IsNullOrEmpty(name))
 85            {
 086                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name));
 87            }
 88
 489            if (_configuredTrustedIssuers.ContainsKey(certificateThumbprint))
 90            {
 091                throw new InvalidOperationException(SR.Format(SR.ID4265, certificateThumbprint));
 92            }
 93
 494            certificateThumbprint = certificateThumbprint.Replace(" ", "");
 95
 496            _configuredTrustedIssuers.Add(certificateThumbprint, name);
 497        }
 98
 99        private class ThumbprintKeyComparer : IEqualityComparer<string>
 100        {
 101            #region IEqualityComparer<string> Members
 102
 103            public bool Equals(string x, string y)
 104            {
 60105                return StringComparer.OrdinalIgnoreCase.Equals(x, y);
 106            }
 107
 108            public int GetHashCode(string obj)
 109            {
 64110                return obj.ToUpper(CultureInfo.InvariantCulture).GetHashCode();
 111            }
 112
 113            #endregion
 114        }
 115    }
 116}