< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Configuration.IssuedTokenParametersElement
Assembly: CoreWCF.ConfigurationManager
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/IssuedTokenParametersElement.cs
Line coverage
22%
Covered lines: 24
Uncovered lines: 84
Coverable lines: 108
Total lines: 328
Line coverage: 22.2%
Branch coverage
20%
Covered branches: 12
Total branches: 60
Branch coverage: 20%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ApplyConfiguration(...)68.75%161673.68%
Copy(...)0%12120%
Create(...)50%2260%
InitializeFrom(...)0%16160%
SerializeToXmlElement(...)0%660%
Unmerge(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/IssuedTokenParametersElement.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.ComponentModel;
 7using System.Configuration;
 8using System.IO;
 9using System.Text;
 10using System.Xml;
 11using CoreWCF.IdentityModel.Tokens;
 12using CoreWCF.Runtime;
 13using CoreWCF.Security.Tokens;
 14
 15namespace CoreWCF.Configuration
 16{
 17    public sealed class IssuedTokenParametersElement : ServiceModelConfigurationElement
 18    {
 19        private Collection<IssuedTokenParametersElement> _optionalIssuedTokenParameters = null;
 20
 21        [ConfigurationProperty(ConfigurationStrings.DefaultMessageSecurityVersion)]
 22        [TypeConverter(typeof(MessageSecurityVersionConverter))]
 23        public MessageSecurityVersion DefaultMessageSecurityVersion
 24        {
 225            get { return (MessageSecurityVersion)base[ConfigurationStrings.DefaultMessageSecurityVersion]; }
 026            set { base[ConfigurationStrings.DefaultMessageSecurityVersion] = value; }
 27        }
 28
 29        [ConfigurationProperty(ConfigurationStrings.AdditionalRequestParameters)]
 30        public XmlElementElementCollection AdditionalRequestParameters
 31        {
 432            get { return (XmlElementElementCollection)base[ConfigurationStrings.AdditionalRequestParameters]; }
 33        }
 34
 35        [ConfigurationProperty(ConfigurationStrings.ClaimTypeRequirements)]
 36        public ClaimTypeElementCollection ClaimTypeRequirements
 37        {
 438            get { return (ClaimTypeElementCollection)base[ConfigurationStrings.ClaimTypeRequirements]; }
 39        }
 40
 41        [ConfigurationProperty(ConfigurationStrings.Issuer)]
 42        public IssuedTokenParametersEndpointAddressElement Issuer
 43        {
 044            get { return (IssuedTokenParametersEndpointAddressElement)base[ConfigurationStrings.Issuer]; }
 45        }
 46
 47        [ConfigurationProperty(ConfigurationStrings.IssuerMetadata)]
 48        public EndpointAddressElementBase IssuerMetadata
 49        {
 050            get { return (EndpointAddressElementBase)base[ConfigurationStrings.IssuerMetadata]; }
 51        }
 52
 53        [ConfigurationProperty(ConfigurationStrings.KeySize, DefaultValue = 0)]
 54        [IntegerValidator(MinValue = 0)]
 55        public int KeySize
 56        {
 257            get { return (int)base[ConfigurationStrings.KeySize]; }
 058            set { base[ConfigurationStrings.KeySize] = value; }
 59        }
 60
 61        [ConfigurationProperty(ConfigurationStrings.KeyType, DefaultValue = SecurityBindingDefaults.DefaultKeyType)]
 62        public SecurityKeyType KeyType
 63        {
 264            get { return (SecurityKeyType)base[ConfigurationStrings.KeyType]; }
 065            set { base[ConfigurationStrings.KeyType] = value; }
 66        }
 67
 68        internal Collection<IssuedTokenParametersElement> OptionalIssuedTokenParameters
 69        {
 70            get
 71            {
 72                // OptionalIssuedTokenParameters built on assumption that configuration is writable.
 73                // This should be protected at the callers site.  If assumption is invalid, then
 74                // configuration system is in an indeterminate state.  Need to stop in a manner that
 75                // user code can not capture.
 076                if (IsReadOnly())
 77                {
 78                    Fx.Assert("IssuedTokenParametersElement.OptionalIssuedTokenParameters should only be called by Admin
 079                    DiagnosticUtility.FailFast("IssuedTokenParametersElement.OptionalIssuedTokenParameters should only b
 80                }
 81
 82                // No need to worry about a race condition here-- this method is not meant to be called by multi-threade
 83                // apps. It is only supposed to be called by svcutil and single threaded equivalents.
 084                if (_optionalIssuedTokenParameters == null)
 85                {
 086                    _optionalIssuedTokenParameters = new Collection<IssuedTokenParametersElement>();
 87                }
 088                return _optionalIssuedTokenParameters;
 89            }
 90        }
 91
 92
 93        [ConfigurationProperty(ConfigurationStrings.TokenType, DefaultValue = "")]
 94        [StringValidator(MinLength = 0)]
 95        public string TokenType
 96        {
 297            get { return (string)base[ConfigurationStrings.TokenType]; }
 98            set
 99            {
 0100                if (string.IsNullOrEmpty(value))
 101                {
 0102                    value = string.Empty;
 103                }
 104
 0105                base[ConfigurationStrings.TokenType] = value;
 0106            }
 107        }
 108
 109        [ConfigurationProperty(ConfigurationStrings.UseStrTransform, DefaultValue = false)]
 110        public bool UseStrTransform
 111        {
 2112            get { return (bool)base[ConfigurationStrings.UseStrTransform]; }
 0113            set { base[ConfigurationStrings.UseStrTransform] = value; }
 114        }
 115
 116        internal void ApplyConfiguration(IssuedSecurityTokenParameters parameters)
 117        {
 2118            if (parameters == null)
 0119                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters)))
 120
 2121            if (AdditionalRequestParameters != null)
 122            {
 4123                foreach (XmlElementElement e in AdditionalRequestParameters)
 124                {
 0125                    parameters.AdditionalRequestParameters.Add(e.XmlElement);
 126                }
 127            }
 128
 2129            if (ClaimTypeRequirements != null)
 130            {
 4131                foreach (ClaimTypeElement c in ClaimTypeRequirements)
 132                {
 0133                    parameters.ClaimTypeRequirements.Add(new ClaimTypeRequirement(c.ClaimType, c.IsOptional));
 134                }
 135            }
 136
 2137            parameters.KeySize = KeySize;
 2138            parameters.KeyType = this.KeyType;
 2139            parameters.DefaultMessageSecurityVersion = DefaultMessageSecurityVersion;
 2140            parameters.UseStrTransform = UseStrTransform;
 141
 2142            if (!string.IsNullOrEmpty(TokenType))
 143            {
 0144                parameters.TokenType = TokenType;
 145            }
 2146            if (PropertyValueOrigin.Default != ElementInformation.Properties[ConfigurationStrings.Issuer].ValueOrigin)
 147            {
 148                //TODO: ? I am not sure of the best way of loading the Issuer EndPoint and Binding
 149
 1150                throw new PlatformNotSupportedException(nameof(Issuer));
 151                //TODO: Implement IssuedTokenParameters
 152                //this.Issuer.Validate();
 153
 154                //TODO: Implement IssuedTokenParameters
 155                //parameters.IssuerAddress = ConfigLoader.LoadEndpointAddress(this.Issuer);
 156
 157                // if (!string.IsNullOrEmpty(Issuer.Binding))
 158                //{
 159                    //TODO: Implement IssuedTokenParameters
 160                    //parameters.IssuerBinding = ConfigLoader.LookupBinding(this.Issuer.Binding, this.Issuer.BindingConf
 161                //}
 162            }
 163
 1164            if (PropertyValueOrigin.Default != ElementInformation.Properties[ConfigurationStrings.IssuerMetadata].ValueO
 165            {
 1166                throw new PlatformNotSupportedException(nameof(IssuerMetadata));
 167                //TODO: Implement IssuedTokenParameters
 168                //parameters.IssuerMetadataAddress = ConfigLoader.LoadEndpointAddress(this.IssuerMetadata);
 169            }
 0170        }
 171
 172        internal void Copy(IssuedTokenParametersElement source)
 173        {
 0174            if (IsReadOnly())
 175            {
 0176                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR.
 177            }
 0178            if (null == source)
 179            {
 0180                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(source));
 181            }
 182
 0183            foreach (XmlElementElement xmlElement in source.AdditionalRequestParameters)
 184            {
 0185                XmlElementElement newElement = new XmlElementElement();
 0186                newElement.Copy(xmlElement);
 0187                this.AdditionalRequestParameters.Add(newElement);
 188            }
 189
 0190            foreach (ClaimTypeElement c in source.ClaimTypeRequirements)
 191            {
 0192                this.ClaimTypeRequirements.Add(new ClaimTypeElement(c.ClaimType, c.IsOptional));
 193            }
 194
 0195            KeySize = source.KeySize;
 0196            KeyType = source.KeyType;
 0197            TokenType = source.TokenType;
 0198            DefaultMessageSecurityVersion = source.DefaultMessageSecurityVersion;
 0199            UseStrTransform = source.UseStrTransform;
 200
 0201            if (PropertyValueOrigin.Default != source.ElementInformation.Properties[ConfigurationStrings.Issuer].ValueOr
 202            {
 0203                Issuer.Copy(source.Issuer);
 204            }
 0205            if (PropertyValueOrigin.Default != source.ElementInformation.Properties[ConfigurationStrings.IssuerMetadata]
 206            {
 0207                IssuerMetadata.Copy(source.IssuerMetadata);
 208            }
 0209        }
 210
 211        internal IssuedSecurityTokenParameters Create(bool createTemplateOnly, SecurityKeyType templateKeyType)
 212        {
 2213            IssuedSecurityTokenParameters result = new IssuedSecurityTokenParameters();
 2214            if (!createTemplateOnly)
 215            {
 2216                ApplyConfiguration(result);
 217            }
 218            else
 219            {
 0220                result.KeyType = templateKeyType;
 221            }
 0222            return result;
 223        }
 224
 225        internal void InitializeFrom(IssuedSecurityTokenParameters source, bool initializeNestedBindings)
 226        {
 0227            if (null == source)
 0228                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(source));
 229
 0230            SetPropertyValueIfNotDefaultValue(ConfigurationStrings.KeyType, source.KeyType);
 0231            if (source.KeySize > 0)
 232            {
 0233                SetPropertyValueIfNotDefaultValue(ConfigurationStrings.KeySize, source.KeySize);
 234            }
 0235            SetPropertyValueIfNotDefaultValue(ConfigurationStrings.TokenType, source.TokenType);
 0236            SetPropertyValueIfNotDefaultValue(ConfigurationStrings.UseStrTransform, source.UseStrTransform);
 237
 0238            if (source.IssuerAddress != null)
 0239                Issuer.InitializeFrom(source.IssuerAddress);
 240
 0241            if (source.DefaultMessageSecurityVersion != null)
 0242                SetPropertyValueIfNotDefaultValue(ConfigurationStrings.DefaultMessageSecurityVersion, source.DefaultMess
 243
 0244            if (source.IssuerBinding != null && initializeNestedBindings)
 245            {
 0246                Issuer.BindingConfiguration = Issuer.Address.ToString();
 247
 248
 249                //TODO: Implement IssuedTokenParameters
 0250                throw new PlatformNotSupportedException(nameof(IssuedTokenParametersEndpointAddressElement));
 251                //string bindingSectionName;
 252
 253                //BindingsSection.TryAdd(this.Issuer.BindingConfiguration,
 254                //    source.IssuerBinding,
 255                //    out bindingSectionName);
 256                //this.Issuer.Binding = bindingSectionName;
 257            }
 258
 0259            if (source.IssuerMetadataAddress != null)
 260            {
 0261                IssuerMetadata.InitializeFrom(source.IssuerMetadataAddress);
 262            }
 263
 0264            foreach (XmlElement element in source.AdditionalRequestParameters)
 265            {
 0266                this.AdditionalRequestParameters.Add(new XmlElementElement(element));
 267            }
 268
 0269            foreach (ClaimTypeRequirement c in source.ClaimTypeRequirements)
 270            {
 0271                this.ClaimTypeRequirements.Add(new ClaimTypeElement(c.ClaimType, c.IsOptional));
 272            }
 273
 274            //TODO: Implement IssuedTokenParameters
 275            //foreach (IssuedSecurityTokenParameters.AlternativeIssuerEndpoint alternativeIssuer in source.AlternativeIs
 276            //{
 277            //    IssuedTokenParametersElement element = new IssuedTokenParametersElement();
 278            //    element.Issuer.InitializeFrom(alternativeIssuer.IssuerAddress);
 279            //    if (initializeNestedBindings)
 280            //    {
 281            //        element.Issuer.BindingConfiguration = element.Issuer.Address.ToString();
 282            //        string bindingSectionName;
 283            //        BindingsSection.TryAdd(element.Issuer.BindingConfiguration,
 284            //            alternativeIssuer.IssuerBinding,
 285            //            out bindingSectionName);
 286            //        element.Issuer.Binding = bindingSectionName;
 287            //    }
 288            //    this.OptionalIssuedTokenParameters.Add(element);
 289            //}
 0290        }
 291
 292        protected override bool SerializeToXmlElement(XmlWriter writer, string elementName)
 293        {
 0294            bool writeMe = base.SerializeToXmlElement(writer, elementName);
 0295            bool writeComment = OptionalIssuedTokenParameters.Count > 0;
 0296            if (writeComment && writer != null)
 297            {
 0298                MemoryStream memoryStream = new MemoryStream();
 0299                using (XmlTextWriter commentWriter = new XmlTextWriter(memoryStream, Encoding.UTF8))
 300                {
 0301                    commentWriter.Formatting = Formatting.Indented;
 0302                    commentWriter.WriteStartElement(ConfigurationStrings.AlternativeIssuedTokenParameters);
 0303                    foreach (IssuedTokenParametersElement element in OptionalIssuedTokenParameters)
 304                    {
 0305                        element.SerializeToXmlElement(commentWriter, ConfigurationStrings.IssuedTokenParameters);
 306                    }
 0307                    commentWriter.WriteEndElement();
 0308                    commentWriter.Flush();
 0309                    string commentString = new UTF8Encoding().GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.L
 0310                    writer.WriteComment(commentString.Substring(1, commentString.Length - 1));
 0311                    commentWriter.Close();
 0312                }
 313            }
 0314            return writeMe || writeComment;
 315        }
 316
 317        protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, Configur
 318        {
 0319            if (sourceElement is IssuedTokenParametersElement)
 320            {
 0321                IssuedTokenParametersElement source = (IssuedTokenParametersElement)sourceElement;
 0322                _optionalIssuedTokenParameters = source._optionalIssuedTokenParameters;
 323            }
 324
 0325            base.Unmerge(sourceElement, parentElement, saveMode);
 0326        }
 327    }
 328}