< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.KeyInfoSerializer
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/KeyInfoSerializer.cs
Line coverage
58%
Covered lines: 86
Uncovered lines: 62
Coverable lines: 148
Total lines: 369
Line coverage: 58.1%
Branch coverage
47%
Covered branches: 40
Total branches: 84
Branch coverage: 47.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/KeyInfoSerializer.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.Xml;
 7using CoreWCF.IdentityModel.Selectors;
 8using CoreWCF.Runtime;
 9
 10namespace CoreWCF.IdentityModel.Tokens
 11{
 12    /// <summary>
 13    /// Abstract class for SecurityKeyIdentifierClause Serializer.
 14    /// </summary>
 15    internal class KeyInfoSerializer : SecurityTokenSerializer
 16    {
 17        private readonly List<KeyIdentifierEntry> _keyIdentifierEntries;
 18        private readonly List<KeyIdentifierClauseEntry> _keyIdentifierClauseEntries;
 19        private readonly List<SerializerEntries> _serializerEntries;
 20        private readonly List<TokenEntry> _tokenEntries;
 21        private SecurityTokenSerializer _innerSecurityTokenSerializer;
 22
 23        /// <summary>
 24        /// Creates an instance of <see cref="SecurityKeyIdentifierClauseSerializer"/>
 25        /// </summary>
 26        public KeyInfoSerializer(bool emitBspRequiredAttributes)
 227            : this(emitBspRequiredAttributes, new DictionaryManager(), DXD.TrustDec2005Dictionary, null)
 28        {
 229        }
 30
 31        public KeyInfoSerializer(
 32            bool emitBspRequiredAttributes,
 33            DictionaryManager dictionaryManager,
 34            TrustDictionary trustDictionary,
 35            SecurityTokenSerializer innerSecurityTokenSerializer) :
 236            this(emitBspRequiredAttributes, dictionaryManager, trustDictionary, innerSecurityTokenSerializer, null)
 37        {
 238        }
 39
 13440        public KeyInfoSerializer(
 13441            bool emitBspRequiredAttributes,
 13442            DictionaryManager dictionaryManager,
 13443            TrustDictionary trustDictionary,
 13444            SecurityTokenSerializer innerSecurityTokenSerializer,
 13445            Func<KeyInfoSerializer, IEnumerable<SerializerEntries>> additionalEntries)
 46        {
 13447            DictionaryManager = dictionaryManager;
 13448            EmitBspRequiredAttributes = emitBspRequiredAttributes;
 13449            _innerSecurityTokenSerializer = innerSecurityTokenSerializer;
 50
 13451            _serializerEntries = new List<SerializerEntries>
 13452            {
 13453                new XmlDsigSep2000(this),
 13454                new Security.WSTrust(this, trustDictionary)
 13455            };
 13456            if (additionalEntries != null)
 57            {
 79258                foreach (SerializerEntries entries in additionalEntries(this))
 59                {
 26460                    _serializerEntries.Add(entries);
 61                }
 62            }
 63
 13464            bool wsSecuritySerializerFound = false;
 93665            foreach (SerializerEntries entry in _serializerEntries)
 66            {
 40067                if ((entry is WSSecurityXXX2005) || (entry is WSSecurityJan2004))
 68                {
 13269                    wsSecuritySerializerFound = true;
 13270                    break;
 71                }
 72            }
 73
 13474            if (!wsSecuritySerializerFound)
 75            {
 276                _serializerEntries.Add(new WSSecurityXXX2005(this));
 77            }
 78
 13479            _tokenEntries = new List<TokenEntry>();
 13480            _keyIdentifierEntries = new List<KeyIdentifierEntry>();
 13481            _keyIdentifierClauseEntries = new List<KeyIdentifierClauseEntry>();
 82
 133683            for (int i = 0; i < _serializerEntries.Count; ++i)
 84            {
 53485                SerializerEntries serializerEntry = _serializerEntries[i];
 53486                serializerEntry.PopulateTokenEntries(_tokenEntries);
 53487                serializerEntry.PopulateKeyIdentifierEntries(_keyIdentifierEntries);
 53488                serializerEntry.PopulateKeyIdentifierClauseEntries(_keyIdentifierClauseEntries);
 89            }
 13490        }
 91
 26292        public DictionaryManager DictionaryManager { get; }
 93
 94        /// <summary>
 95        /// Gets or sets a value indicating if BSP required attributes should be written out.
 96        /// </summary>
 65897        public bool EmitBspRequiredAttributes { get; }
 98
 99        public SecurityTokenSerializer InnerSecurityTokenSerializer
 100        {
 101            get
 102            {
 23103                return _innerSecurityTokenSerializer ?? this;
 104            }
 105            set
 106            {
 0107                _innerSecurityTokenSerializer = value;
 0108            }
 109        }
 110
 111        protected override bool CanReadTokenCore(XmlReader reader)
 112        {
 0113            return false;
 114        }
 115
 116        protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver)
 117        {
 0118            XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
 0119            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.CannotReadToken, rea
 120        }
 121
 122        protected override bool CanWriteTokenCore(SecurityToken token)
 123        {
 0124            return false;
 125        }
 126
 127        protected override void WriteTokenCore(XmlWriter writer, SecurityToken token)
 128        {
 0129            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Standar
 130        }
 131
 132        protected override bool CanReadKeyIdentifierCore(XmlReader reader)
 133        {
 0134            XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
 0135            for (int i = 0; i < _keyIdentifierEntries.Count; i++)
 136            {
 0137                KeyIdentifierEntry keyIdentifierEntry = _keyIdentifierEntries[i];
 0138                if (keyIdentifierEntry.CanReadKeyIdentifierCore(localReader))
 139                {
 0140                    return true;
 141                }
 142            }
 0143            return false;
 144        }
 145
 146        protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader)
 147        {
 23148            XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
 23149            localReader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.KeyInfo, CoreWCF.XD.XmlSignatureDictionary.Na
 23150            SecurityKeyIdentifier keyIdentifier = new SecurityKeyIdentifier();
 46151            while (localReader.IsStartElement())
 152            {
 23153                SecurityKeyIdentifierClause clause = InnerSecurityTokenSerializer.ReadKeyIdentifierClause(localReader);
 23154                if (clause == null)
 155                {
 0156                    localReader.Skip();
 157                }
 158                else
 159                {
 23160                    keyIdentifier.Add(clause);
 161                }
 162            }
 23163            if (keyIdentifier.Count == 0)
 164            {
 0165                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("ErrorDeserializing
 166            }
 23167            localReader.ReadEndElement();
 168
 23169            return keyIdentifier;
 170        }
 171
 172        protected override bool CanWriteKeyIdentifierCore(SecurityKeyIdentifier keyIdentifier)
 173        {
 0174            for (int i = 0; i < _keyIdentifierEntries.Count; ++i)
 175            {
 0176                KeyIdentifierEntry keyIdentifierEntry = _keyIdentifierEntries[i];
 0177                if (keyIdentifierEntry.SupportsCore(keyIdentifier))
 178                {
 0179                    return true;
 180                }
 181            }
 0182            return false;
 183        }
 184
 185        protected override void WriteKeyIdentifierCore(XmlWriter writer, SecurityKeyIdentifier keyIdentifier)
 186        {
 0187            bool wroteKeyIdentifier = false;
 0188            XmlDictionaryWriter localWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
 0189            for (int i = 0; i < _keyIdentifierEntries.Count; ++i)
 190            {
 0191                KeyIdentifierEntry keyIdentifierEntry = _keyIdentifierEntries[i];
 0192                if (keyIdentifierEntry.SupportsCore(keyIdentifier))
 193                {
 194                    try
 195                    {
 0196                        keyIdentifierEntry.WriteKeyIdentifierCore(localWriter, keyIdentifier);
 0197                    }
 0198                    catch (Exception e)
 199                    {
 0200                        if (Fx.IsFatal(e))
 201                        {
 0202                            throw;
 203                        }
 204
 0205                        if (!ShouldWrapException(e))
 206                        {
 0207                            throw;
 208                        }
 209
 0210                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("ErrorSeria
 211                    }
 0212                    wroteKeyIdentifier = true;
 0213                    break;
 214                }
 215            }
 216
 0217            if (!wroteKeyIdentifier)
 218            {
 0219                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta
 220            }
 221
 0222            localWriter.Flush();
 0223        }
 224
 225        protected override bool CanReadKeyIdentifierClauseCore(XmlReader reader)
 226        {
 6227            XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
 48228            for (int i = 0; i < _keyIdentifierClauseEntries.Count; i++)
 229            {
 24230                KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i];
 24231                if (keyIdentifierClauseEntry.CanReadKeyIdentifierClauseCore(localReader))
 232                {
 6233                    return true;
 234                }
 235            }
 0236            return false;
 237        }
 238
 239        protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader)
 240        {
 39241            XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
 312242            for (int i = 0; i < _keyIdentifierClauseEntries.Count; i++)
 243            {
 156244                KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i];
 156245                if (keyIdentifierClauseEntry.CanReadKeyIdentifierClauseCore(localReader))
 246                {
 247                    try
 248                    {
 39249                        return keyIdentifierClauseEntry.ReadKeyIdentifierClauseCore(localReader);
 250                    }
 0251                    catch (Exception e)
 252                    {
 0253                        if (Fx.IsFatal(e))
 254                        {
 0255                            throw;
 256                        }
 257
 0258                        if (!ShouldWrapException(e))
 259                        {
 0260                            throw;
 261                        }
 0262                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("ErrorDeser
 263                    }
 264                }
 265            }
 0266            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("CannotReadKeyIdentifie
 39267        }
 268
 269        protected override bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause)
 270        {
 0271            for (int i = 0; i < _keyIdentifierClauseEntries.Count; ++i)
 272            {
 0273                KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i];
 0274                if (keyIdentifierClauseEntry.SupportsCore(keyIdentifierClause))
 275                {
 0276                    return true;
 277                }
 278            }
 0279            return false;
 280        }
 281
 282        protected override void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifier
 283        {
 20284            bool wroteKeyIdentifierClause = false;
 20285            XmlDictionaryWriter localWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
 160286            for (int i = 0; i < _keyIdentifierClauseEntries.Count; ++i)
 287            {
 80288                KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i];
 80289                if (keyIdentifierClauseEntry.SupportsCore(keyIdentifierClause))
 290                {
 291                    try
 292                    {
 20293                        keyIdentifierClauseEntry.WriteKeyIdentifierClauseCore(localWriter, keyIdentifierClause);
 20294                    }
 0295                    catch (Exception e)
 296                    {
 0297                        if (Fx.IsFatal(e))
 298                        {
 0299                            throw;
 300                        }
 301
 0302                        if (!ShouldWrapException(e))
 303                        {
 0304                            throw;
 305                        }
 306
 0307                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException("ErrorSerializingKeyI
 308                    }
 20309                    wroteKeyIdentifierClause = true;
 20310                    break;
 311                }
 312            }
 313
 20314            if (!wroteKeyIdentifierClause)
 315            {
 0316                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta
 317            }
 318
 20319            localWriter.Flush();
 20320        }
 321
 322        internal void PopulateStrEntries(IList<StrEntry> strEntries)
 323        {
 1336324            foreach (SerializerEntries serializerEntry in _serializerEntries)
 325            {
 534326                serializerEntry.PopulateStrEntries(strEntries);
 327            }
 134328        }
 329
 330        private bool ShouldWrapException(Exception e)
 331        {
 0332            return ((e is ArgumentException) || (e is FormatException) || (e is InvalidOperationException));
 333        }
 334
 335        internal Type[] GetTokenTypes(string tokenTypeUri)
 336        {
 19337            if (tokenTypeUri != null)
 338            {
 248339                for (int i = 0; i < _tokenEntries.Count; i++)
 340                {
 124341                    TokenEntry tokenEntry = _tokenEntries[i];
 342
 124343                    if (tokenEntry.SupportsTokenTypeUri(tokenTypeUri))
 344                    {
 19345                        return tokenEntry.GetTokenTypes();
 346                    }
 347                }
 348            }
 0349            return null;
 350        }
 351
 352        protected internal virtual string GetTokenTypeUri(Type tokenType)
 353        {
 16354            if (tokenType != null)
 355            {
 224356                for (int i = 0; i < _tokenEntries.Count; i++)
 357                {
 112358                    TokenEntry tokenEntry = _tokenEntries[i];
 359
 112360                    if (tokenEntry.SupportsCore(tokenType))
 361                    {
 16362                        return tokenEntry.TokenTypeUri;
 363                    }
 364                }
 365            }
 0366            return null;
 367        }
 368    }
 369}

Methods/Properties

.ctor(System.Boolean)
.ctor(System.Boolean,CoreWCF.IdentityModel.DictionaryManager,CoreWCF.TrustDictionary,CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer)
.ctor(System.Boolean,CoreWCF.IdentityModel.DictionaryManager,CoreWCF.TrustDictionary,CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer,System.Func`2<CoreWCF.IdentityModel.Tokens.KeyInfoSerializer,System.Collections.Generic.IEnumerable`1<CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer/SerializerEntries>>)
DictionaryManager()
EmitBspRequiredAttributes()
InnerSecurityTokenSerializer()
InnerSecurityTokenSerializer(CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer)
CanReadTokenCore(System.Xml.XmlReader)
ReadTokenCore(System.Xml.XmlReader,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver)
CanWriteTokenCore(CoreWCF.IdentityModel.Tokens.SecurityToken)
WriteTokenCore(System.Xml.XmlWriter,CoreWCF.IdentityModel.Tokens.SecurityToken)
CanReadKeyIdentifierCore(System.Xml.XmlReader)
ReadKeyIdentifierCore(System.Xml.XmlReader)
CanWriteKeyIdentifierCore(CoreWCF.IdentityModel.Tokens.SecurityKeyIdentifier)
WriteKeyIdentifierCore(System.Xml.XmlWriter,CoreWCF.IdentityModel.Tokens.SecurityKeyIdentifier)
CanReadKeyIdentifierClauseCore(System.Xml.XmlReader)
ReadKeyIdentifierClauseCore(System.Xml.XmlReader)
CanWriteKeyIdentifierClauseCore(CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
WriteKeyIdentifierClauseCore(System.Xml.XmlWriter,CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
PopulateStrEntries(System.Collections.Generic.IList`1<CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer/StrEntry>)
ShouldWrapException(System.Exception)
GetTokenTypes(System.String)
GetTokenTypeUri(System.Type)