< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Configuration.IdentityConfiguration
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Configuration/IdentityConfiguration.cs
Line coverage
35%
Covered lines: 30
Uncovered lines: 54
Coverable lines: 84
Total lines: 318
Line coverage: 35.7%
Branch coverage
13%
Covered branches: 4
Total branches: 30
Branch coverage: 13.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor(...)100%11100%
Initialize()0%22220%
LoadHandlersNoConfig(...)100%22100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Configuration/IdentityConfiguration.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 System.Security.Cryptography.X509Certificates;
 8using CoreWCF.IdentityModel.Selectors;
 9using CoreWCF.IdentityModel.Tokens;
 10using CoreWCF.Security;
 11
 12namespace CoreWCF.IdentityModel.Configuration
 13{
 14    /// <summary>
 15    /// Defines the collection of configurable properties controlling the behavior of the Windows Identity Foundation.
 16    /// </summary>
 17    public class IdentityConfiguration
 18    {
 19        public const string DefaultServiceName = ConfigurationStrings.DefaultServiceName;
 220        public static readonly TimeSpan DefaultMaxClockSkew = new TimeSpan(0, 5, 0);
 21        internal const string DefaultMaxClockSkewString = "00:05:00";
 222        public static readonly X509CertificateValidationMode DefaultCertificateValidationMode = X509CertificateValidatio
 223        public static readonly Type DefaultIssuerNameRegistryType = typeof(ConfigurationBasedIssuerNameRegistry);
 224        public static readonly X509RevocationMode DefaultRevocationMode = X509RevocationMode.Online;
 225        public static readonly StoreLocation DefaultTrustedStoreLocation = StoreLocation.LocalMachine;
 226        private TimeSpan _serviceMaxClockSkew = DefaultMaxClockSkew;
 27        private SecurityTokenHandlerConfiguration _serviceHandlerConfiguration;
 28
 229        public IdentityConfiguration(IEnumerable<SecurityTokenHandler> securityTokenHandlers)
 30        {
 231            LoadHandlersNoConfig(securityTokenHandlers);
 232        }
 33
 34
 35        /// <summary>
 36        /// Gets or sets the AudienceRestriction.
 37        /// </summary>
 38        public AudienceRestriction AudienceRestriction
 39        {
 240            get { return _serviceHandlerConfiguration.AudienceRestriction; }
 041            set { _serviceHandlerConfiguration.AudienceRestriction = value; }
 42        }
 43
 44        /// <summary>
 45        /// Gets the Caches configured.
 46        /// </summary>
 47        public IdentityModelCaches Caches
 48        {
 049            get { return _serviceHandlerConfiguration.Caches; }
 050            set { _serviceHandlerConfiguration.Caches = value; }
 51        }
 52
 53        /// <summary>
 54        /// Gets or sets the certificate validation mode used by handlers to validate issuer certificates
 55        /// </summary>
 56        public X509CertificateValidationMode CertificateValidationMode
 57        {
 058            get { return _serviceHandlerConfiguration.CertificateValidationMode; }
 459            set { _serviceHandlerConfiguration.CertificateValidationMode = value; }
 60        }
 61
 62        /// <summary>
 63        /// Gets or sets the certificate validator used by handlers to validate issuer certificates
 64        /// </summary>
 65        public X509CertificateValidator CertificateValidator
 66        {
 067            get { return _serviceHandlerConfiguration.CertificateValidator; }
 468            set { _serviceHandlerConfiguration.CertificateValidator = value; }
 69        }
 70
 71        /// <summary>
 72        /// Gets or Sets detection of replaying of tokens by handlers in the default handler configuration.
 73        /// </summary>
 74        public bool DetectReplayedTokens
 75        {
 076            get { return _serviceHandlerConfiguration.DetectReplayedTokens; }
 077            set { _serviceHandlerConfiguration.DetectReplayedTokens = value; }
 78        }
 79
 80        /// <summary>
 81        /// Determines if <see cref="IdentityConfiguration.Initialize"/> has been called.
 82        /// </summary>
 083        public virtual bool IsInitialized { get; set; }
 84
 85        /// <summary>
 86        /// Updates properties in the <see cref="SecurityTokenHandlerConfiguration"/> objects for the
 87        /// <see cref="SecurityTokenHandlerCollection"/> objects contained in
 88        /// <see cref="IdentityConfiguration.SecurityTokenHandlerCollectionManager"/> to be consistent with the property
 89        /// values on this <see cref="IdentityConfiguration"/> instance.
 90        /// </summary>
 91        /// <remarks>
 92        /// This method should be invoked prior to using these token handlers
 93        /// for token processing.
 94        /// </remarks>
 95        /// <exception cref="InvalidOperationException">If this method is invoked more than once.</exception>
 96        public virtual void Initialize()
 97        {
 098            if (IsInitialized)
 99            {
 0100                throw new InvalidOperationException(SR.Format(SR.ID7009));
 101            }
 102
 0103            SecurityTokenHandlerCollection defaultCollection = SecurityTokenHandlers;
 104
 0105            if (!object.ReferenceEquals(_serviceHandlerConfiguration, defaultCollection.Configuration))
 106            {
 107                //
 108                // If someone has created their own new STHConfig and set it as default, leave that config alone.
 109                //
 110               // TraceUtility.TraceString(TraceEventType.Information, SR.Format(SR.ID4283));
 0111                IsInitialized = true;
 0112                return;
 113            }
 114
 115            // Update the ServiceTokenResolver of the default TokenHandlerCollection's configuration, if serviceCertific
 0116            if (ServiceCertificate != null)
 117            {
 0118                SecurityTokenResolver serviceCertificateResolver = SecurityTokenResolver.CreateDefaultSecurityTokenResol
 0119                                                      new SecurityToken[] { new X509SecurityToken(ServiceCertificate) })
 120
 0121                SecurityTokenResolver tokenResolver = SecurityTokenHandlers.Configuration.ServiceTokenResolver;
 122
 0123                if ((tokenResolver != null) && (tokenResolver != EmptySecurityTokenResolver.Instance))
 124                {
 0125                    SecurityTokenHandlers.Configuration.ServiceTokenResolver = new AggregateTokenResolver(new SecurityTo
 126                }
 127                else
 128                {
 0129                    SecurityTokenHandlers.Configuration.ServiceTokenResolver = serviceCertificateResolver;
 130                }
 131            }
 132
 0133            SecurityTokenResolver configuredIssuerTokenResolver = IssuerTokenResolver;
 134
 0135            if (IssuerTokenResolver == SecurityTokenHandlerConfiguration.DefaultIssuerTokenResolver)
 136            {
 137                //
 138                // Add the known certificates from WCF's ServiceCredentials in front of
 139                // the default issuer token resolver.
 140                //
 0141                if (KnownIssuerCertificates != null)
 142                {
 0143                    int count = KnownIssuerCertificates.Count;
 0144                    if (count > 0)
 145                    {
 0146                        SecurityToken[] tokens = new SecurityToken[count];
 0147                        for (int i = 0; i < count; i++)
 148                        {
 0149                            tokens[i] = new X509SecurityToken(KnownIssuerCertificates[i]);
 150                        }
 151
 0152                        SecurityTokenResolver knownCertificateTokenResolver = SecurityTokenResolver.CreateDefaultSecurit
 153
 0154                        IssuerTokenResolver = new AggregateTokenResolver(new SecurityTokenResolver[] { knownCertificateT
 155                    }
 156                }
 157            }
 158
 0159            if (CertificateValidationMode != X509CertificateValidationMode.Custom)
 160            {
 0161                defaultCollection.Configuration.CertificateValidator = X509Util.CreateCertificateValidator(defaultCollec
 0162                                                                                                            defaultColle
 0163                                                                                                            defaultColle
 164            }
 0165            else if (object.ReferenceEquals(defaultCollection.Configuration.CertificateValidator, SecurityTokenHandlerCo
 166            {
 167                //
 168                // If the mode is custom but the validator or still default, something has gone wrong.
 169                //
 0170                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4
 171            }
 172
 0173            IsInitialized = true;
 0174        }
 175
 176        /// <summary>
 177        /// TODO : Discuss better way to separate the configurations from Primitives
 178        /// </summary>
 179        private void LoadHandlersNoConfig(IEnumerable<SecurityTokenHandler> serviceTokenHandlers)
 180        {
 2181            SecurityTokenHandlerCollectionManager manager = SecurityTokenHandlerCollectionManager.CreateEmptySecurityTok
 2182            _serviceHandlerConfiguration = new SecurityTokenHandlerConfiguration
 2183            {
 2184                MaxClockSkew = _serviceMaxClockSkew
 2185            };
 186
 2187            if (!manager.ContainsKey(SecurityTokenHandlerCollectionManager.Usage.Default))
 188            {
 2189                manager[SecurityTokenHandlerCollectionManager.Usage.Default] = SecurityTokenHandlerCollection.CreateDefa
 190            }
 2191            SecurityTokenHandlerCollectionManager = manager;
 2192        }
 193
 194        /// <summary>
 195        /// Gets or sets the maximum allowable time difference between the
 196        /// system clocks of the two parties that are communicating.
 197        /// </summary>
 198        public TimeSpan MaxClockSkew
 199        {
 0200            get { return _serviceHandlerConfiguration.MaxClockSkew; }
 0201            set { _serviceHandlerConfiguration.MaxClockSkew = value; }
 202        }
 203
 204        /// <summary>
 205        /// Gets or sets the service name of this configuration.
 206        /// </summary>
 2207        public string Name { get; } = DefaultServiceName;
 208
 209        /// <summary>
 210        /// Gets or sets the IssuerNameRegistry used to resolve issuer names.
 211        /// </summary>
 212        public IssuerNameRegistry IssuerNameRegistry
 213        {
 214            get
 215            {
 0216                return _serviceHandlerConfiguration.IssuerNameRegistry;
 217            }
 218            set
 219            {
 2220                _serviceHandlerConfiguration.IssuerNameRegistry = value ?? throw DiagnosticUtility.ExceptionUtility.Thro
 2221            }
 222        }
 223
 224        /// <summary>
 225        /// The service certificate to initialize the ServiceTokenResolver and the SessionSecurityTokenHandler.
 226        /// </summary>
 0227        public X509Certificate2 ServiceCertificate { get; set; }
 228
 0229        internal List<X509Certificate2> KnownIssuerCertificates { get; set; }
 230
 231
 232        /// <summary>
 233        /// Gets or Sets the Issuer token resolver.
 234        /// </summary>
 235        public SecurityTokenResolver IssuerTokenResolver
 236        {
 237            get
 238            {
 0239                return _serviceHandlerConfiguration.IssuerTokenResolver;
 240            }
 241            set
 242            {
 2243                _serviceHandlerConfiguration.IssuerTokenResolver = value ?? throw DiagnosticUtility.ExceptionUtility.Thr
 2244            }
 245        }
 246
 247        /// <summary>
 248        /// Gets or sets the revocation mode used by handlers to validate issuer certificates
 249        /// </summary>
 250        public X509RevocationMode RevocationMode
 251        {
 0252            get { return _serviceHandlerConfiguration.RevocationMode; }
 4253            set { _serviceHandlerConfiguration.RevocationMode = value; }
 254        }
 255
 256        /// <summary>
 257        /// Gets or Sets the Service token resolver.
 258        /// </summary>
 259        public SecurityTokenResolver ServiceTokenResolver
 260        {
 261            get
 262            {
 0263                return _serviceHandlerConfiguration.ServiceTokenResolver;
 264            }
 265            set
 266            {
 0267                _serviceHandlerConfiguration.ServiceTokenResolver = value ?? throw DiagnosticUtility.ExceptionUtility.Th
 0268            }
 269        }
 270
 271        /// <summary>
 272        /// Gets or sets if BootstrapContext is saved in the ClaimsIdentity and Sessions after token validation.
 273        /// </summary>
 274        public bool SaveBootstrapContext
 275        {
 0276            get { return _serviceHandlerConfiguration.SaveBootstrapContext; }
 4277            set { _serviceHandlerConfiguration.SaveBootstrapContext = value; }
 278        }
 279
 280        /// <summary>
 281        /// The <see cref="SecurityTokenHandlerCollectionManager" /> containing the set of <see cref="SecurityTokenHandl
 282        /// objects used for serializing and validating tokens found in WS-Trust messages.
 283        /// </summary>
 4284        public SecurityTokenHandlerCollectionManager SecurityTokenHandlerCollectionManager { get; private set; }
 285
 286        /// <summary>
 287        /// The <see cref="SecurityTokenHandlerCollection" /> collection of <see cref="SecurityTokenHandler" />
 288        /// objects used for serializing and validating tokens found in WS-Trust messages.
 289        /// If user wants to register their own token handler, they
 290        /// can simply add their own handler to this collection.
 291        /// </summary>
 292        public SecurityTokenHandlerCollection SecurityTokenHandlers
 293        {
 294            get
 295            {
 2296                return SecurityTokenHandlerCollectionManager[SecurityTokenHandlerCollectionManager.Usage.Default];
 297            }
 298        }
 299
 300        /// <summary>
 301        /// Gets or Sets the expiration period for items placed in the TokenReplayCache.
 302        /// </summary>
 303        public TimeSpan TokenReplayCacheExpirationPeriod
 304        {
 0305            get { return _serviceHandlerConfiguration.TokenReplayCacheExpirationPeriod; }
 0306            set { _serviceHandlerConfiguration.TokenReplayCacheExpirationPeriod = value; }
 307        }
 308
 309        /// <summary>
 310        /// Gets or sets the trusted store location used by handlers to validate issuer certificates
 311        /// </summary>
 312        public StoreLocation TrustedStoreLocation
 313        {
 0314            get { return _serviceHandlerConfiguration.TrustedStoreLocation; }
 0315            set { _serviceHandlerConfiguration.TrustedStoreLocation = value; }
 316        }
 317    }
 318}