< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.WsSecurityTokenSerializerAdapter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WsSecurityTokenSerializerAdapter.cs
Line coverage
19%
Covered lines: 23
Uncovered lines: 98
Coverable lines: 121
Total lines: 481
Line coverage: 19%
Branch coverage
12%
Covered branches: 10
Total branches: 82
Branch coverage: 12.1%
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/Security/WsSecurityTokenSerializerAdapter.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;
 8using CoreWCF.IdentityModel.Selectors;
 9using CoreWCF.IdentityModel.Tokens;
 10using CoreWCF.Runtime;
 11using CoreWCF.Security.Tokens;
 12
 13namespace CoreWCF.Security
 14{
 15    /// <summary>
 16    /// This class derives from CoreWCF.Security.WSSecurityTokenSerializer and wraps a collection of SecurityTokenHandle
 17    /// Any call to this serilaizer is delegated to the token handler and delegated to the base class if no token handle
 18    /// is registered to handle this particular token or KeyIdentifier.
 19    /// </summary>
 20    internal class WsSecurityTokenSerializerAdapter : WSSecurityTokenSerializer
 21    {
 22        private SecureConversationVersion _scVersion;
 23        private SecurityTokenHandlerCollection _securityTokenHandlers;
 24        private bool _mapExceptionsToSoapFaults;
 225        private ExceptionMapper _exceptionMapper = new ExceptionMapper();
 26
 27        /// <summary>
 28        /// Initializes an instance of <see cref="WsSecurityTokenSerializerAdapter"/>
 29        /// </summary>
 30        /// <param name="securityTokenHandlerCollection">
 31        /// The <see cref="SecurityTokenHandlerCollection" /> containing the set of <see cref="SecurityTokenHandler" />
 32        /// objects used for serializing and validating tokens found in WS-Trust messages.
 33        /// </param>
 34        public WsSecurityTokenSerializerAdapter(SecurityTokenHandlerCollection securityTokenHandlerCollection)
 035            : this(securityTokenHandlerCollection, MessageSecurityVersion.Default.SecurityVersion)
 36        {
 037        }
 38
 39        /// <summary>
 40        /// Initializes an instance of <see cref="WsSecurityTokenSerializerAdapter"/>
 41        /// </summary>
 42        /// <param name="securityTokenHandlerCollection">
 43        /// The <see cref="SecurityTokenHandlerCollection" /> containing the set of <see cref="SecurityTokenHandler" />
 44        /// objects used for serializing and validating tokens found in WS-Trust messages.
 45        /// </param>
 46        /// <param name="securityVersion">The SecurityTokenVersion of the base WSSecurityTokenSerializer.</param>
 47        public WsSecurityTokenSerializerAdapter(SecurityTokenHandlerCollection securityTokenHandlerCollection, SecurityV
 048            : this(securityTokenHandlerCollection, securityVersion, true, new SamlSerializer(), null, null)
 49        {
 050        }
 51
 52        /// <summary>
 53        /// Initializes an instance of <see cref="WsSecurityTokenSerializerAdapter"/>
 54        /// </summary>
 55        /// <param name="securityTokenHandlerCollection">
 56        /// The <see cref="SecurityTokenHandlerCollection" /> containing the set of <see cref="SecurityTokenHandler" />
 57        /// objects used for serializing and validating tokens found in WS-Trust messages.
 58        /// </param>
 59        /// <param name="securityVersion">The SecurityVersion of the base WSSecurityTokenSerializer.</param>
 60        /// <param name="emitBspAttributes">Flag that determines if the serailization shoudl be BSP compliant.</param>
 61        /// <param name="samlSerializer">Serializer for SAML 1.1 tokens.</param>
 62        /// <param name="stateEncoder">SecurityStateEncoder used for resolving SCT.</param>
 63        /// <param name="knownTypes">The collection of known claim types.</param>
 64        public WsSecurityTokenSerializerAdapter(SecurityTokenHandlerCollection securityTokenHandlerCollection, SecurityV
 065            : this(securityTokenHandlerCollection, securityVersion, TrustVersion.WSTrust13, SecureConversationVersion.WS
 66        {
 067        }
 68
 69        /// <summary>
 70        /// Initializes an instance of <see cref="WsSecurityTokenSerializerAdapter"/>
 71        /// </summary>
 72        /// <param name="securityTokenHandlerCollection">
 73        /// The <see cref="SecurityTokenHandlerCollection" /> containing the set of <see cref="SecurityTokenHandler" />
 74        /// objects used for serializing and validating tokens found in WS-Trust messages.
 75        /// </param>
 76        /// <param name="securityVersion">The SecurityVersion of the base WSSecurityTokenSerializer.</param>
 77        /// <param name="trustVersion">The TrustVersion of the serializer uses.</param>
 78        /// <param name="secureConversationVersion">The SecureConversationVersion of the serializer.</param>
 79        /// <param name="emitBspAttributes">Flag that determines if the serailization shoudl be BSP compliant.</param>
 80        /// <param name="samlSerializer">Serializer for SAML 1.1 tokens.</param>
 81        /// <param name="stateEncoder">SecurityStateEncoder used for resolving SCT.</param>
 82        /// <param name="knownTypes">The collection of known claim types.</param>
 83        public WsSecurityTokenSerializerAdapter(SecurityTokenHandlerCollection securityTokenHandlerCollection, SecurityV
 284            : base(securityVersion, trustVersion, secureConversationVersion, emitBspAttributes, samlSerializer, stateEnc
 85        {
 286            if (securityTokenHandlerCollection == null)
 87            {
 088                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenHandlerCollection))
 89            }
 90
 291            _scVersion = secureConversationVersion;
 292            _securityTokenHandlers = securityTokenHandlerCollection;
 293        }
 94
 95        /// <summary>
 96        /// Gets and Sets the property that describes if exceptions
 97        /// should be mapped to SOAP Fault exceptions. Default is false.
 98        /// </summary>
 99        public bool MapExceptionsToSoapFaults
 100        {
 101            get
 102            {
 6103                return _mapExceptionsToSoapFaults;
 104            }
 105            set
 106            {
 2107                _mapExceptionsToSoapFaults = value;
 2108            }
 109        }
 110
 111        /// <summary>
 112        /// Gets the SecurityTokenHandlerCollection.
 113        /// </summary>
 114        public SecurityTokenHandlerCollection SecurityTokenHandlers
 115        {
 116            get
 117            {
 0118                return _securityTokenHandlers;
 119            }
 120        }
 121
 122        /// <summary>
 123        /// Gets or sets the ExceptionMapper to be used when throwing exceptions.
 124        /// </summary>
 125        public ExceptionMapper ExceptionMapper
 126        {
 127            get
 128            {
 0129                return _exceptionMapper;
 130            }
 131            set
 132            {
 2133                if (value == null)
 134                {
 0135                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 136                }
 2137                _exceptionMapper = value;
 2138            }
 139        }
 140
 141        /// <summary>
 142        /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
 143        /// can read the security token.
 144        /// </summary>
 145        /// <param name=nameof(reader)>Reader to a Security token.</param>
 146        /// <returns>'True' if the serializer can read the given Security Token.</returns>
 147        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 148        protected override bool CanReadTokenCore(XmlReader reader)
 149        {
 0150            if (reader == null)
 151            {
 0152                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 153            }
 154
 0155            if (_securityTokenHandlers.CanReadToken(reader))
 156            {
 0157                return true;
 158            }
 159
 0160            return base.CanReadTokenCore(reader);
 161        }
 162
 163        /// <summary>
 164        /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
 165        /// can write the given security token.
 166        /// </summary>
 167        /// <param name=(nameof(token)>SecurityToken instance.</param>
 168        /// <returns>'True' if the serializer can write the given security token.</returns>
 169        /// <exception cref="ArgumentNullException">The input parameter 'token' is null.</exception>
 170        protected override bool CanWriteTokenCore(SecurityToken token)
 171        {
 0172            if (token == null)
 173            {
 0174                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull((nameof(token)));
 175            }
 176
 0177            if (_securityTokenHandlers.CanWriteToken(token))
 178            {
 0179                return true;
 180            }
 181
 0182            return base.CanWriteTokenCore(token);
 183        }
 184
 185        /// <summary>
 186        /// Deserializes the SecurityToken from the given XmlReader.
 187        /// </summary>
 188        /// <param name=nameof(reader)>Reader to a Security token.</param>
 189        /// <param name="tokenResolver">Instance of SecurityTokenResolver.</param>
 190        /// <returns>'True' if the serializer can read the given Security Token.</returns>
 191        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 192        protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver)
 193        {
 53194            if (reader == null)
 195            {
 0196                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 197            }
 198
 199            try
 200            {
 161201                foreach (SecurityTokenHandler securityTokenHandler in _securityTokenHandlers)
 202                {
 54203                    if (securityTokenHandler.CanReadToken(reader))
 204                    {
 53205                        SecurityToken token = securityTokenHandler.ReadToken(reader, tokenResolver);
 47206                        SessionSecurityToken sessionToken = token as SessionSecurityToken;
 207
 47208                        if (sessionToken != null)
 209                        {
 0210                            if (sessionToken.SecureConversationVersion.AbsoluteUri != _scVersion.Namespace.Value)
 211                            {
 0212                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4053
 213                            }
 214
 0215                            return SecurityContextSecurityTokenHelper.ConvertSessionTokenToSecurityContextSecurityToken(
 216                        }
 217                        else
 218                        {
 47219                            return token;
 220                        }
 221                    }
 222                }
 223
 0224                return base.ReadTokenCore(reader, tokenResolver);
 225            }
 6226            catch (Exception ex)
 227            {
 6228                if (!(MapExceptionsToSoapFaults && _exceptionMapper.HandleSecurityTokenProcessingException(ex)))
 229                {
 6230                    throw;
 231                }
 232                Fx.Assert(false, "ExceptionMapper did not handle an exception correctly.");
 233                // This should never happen. ExceptionMapper will handle the exception, in which case,
 234                // a fault exception is thrown or the original exception gets thrown.
 0235            }
 236
 0237            return null;
 47238        }
 239
 240        /// <summary>
 241        /// Serializes the SecurityToken to the XmlWriter.
 242        /// </summary>
 243        /// <param name=nameof(writer)>XmlWriter to write to.</param>
 244        /// <param name=(nameof(token)>The SecurityToken to serializer.</param>
 245        /// <exception cref="ArgumentNullException">The input parameter 'writer' or 'token' is null.</exception>
 246        protected override void WriteTokenCore(XmlWriter writer, SecurityToken token)
 247        {
 0248            if (writer == null)
 249            {
 0250                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 251            }
 252
 0253            if (token == null)
 254            {
 0255                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull((nameof(token)));
 256            }
 257
 258            try
 259            {
 260                //
 261                // Wire the session handler for SCT
 262                //
 0263                SecurityContextSecurityToken sct = token as SecurityContextSecurityToken;
 0264                if (sct != null)
 265                {
 266                    //
 267                    // Bare SCT tokens are wrapped with a SessionSecurityToken.
 268                    // The property SessionSecurityToken.IsSecurityContextSecurityTokenWrapper will be true.
 269                    //
 0270                    token = SecurityContextSecurityTokenHelper.ConvertSctToSessionToken(sct, _scVersion);
 271                }
 272
 0273                SecurityTokenHandler securityTokenHandler = _securityTokenHandlers[token];
 274
 0275                if ((securityTokenHandler != null) && (securityTokenHandler.CanWriteToken))
 276                {
 0277                    securityTokenHandler.WriteToken(writer, token);
 278
 0279                    return;
 280                }
 281
 0282                base.WriteTokenCore(writer, token);
 0283            }
 0284            catch (Exception ex)
 285            {
 0286                if (!(MapExceptionsToSoapFaults && _exceptionMapper.HandleSecurityTokenProcessingException(ex)))
 287                {
 0288                    throw;
 289                }
 290                Fx.Assert(false, "ExceptionMapper did not handle an exception correctly.");
 291                // This should never happen. ExceptionMapper will handle the exception, in which case,
 292                // a fault exception is thrown or the original exception gets thrown.
 0293            }
 0294        }
 295
 296        /// <summary>
 297        /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
 298        /// can read the security key identifier.
 299        /// </summary>
 300        /// <param name=nameof(reader)>Reader pointing at a Security Key Identifier {ds:Keyinfo}.</param>
 301        /// <returns>'True' if the serializer can read the given Security Key Identifier.</returns>
 302        /// <exception cref="ArgumentNullException">The <paramref name=nameof(reader)/> is null.</exception>
 303        protected override bool CanReadKeyIdentifierCore(XmlReader reader)
 304        {
 0305            if (reader == null)
 306            {
 0307                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 308            }
 309
 0310            if (reader.IsStartElement(XmlSignatureConstants.Elements.KeyInfo, XmlSignatureConstants.Namespace))
 311            {
 0312                return true;
 313            }
 314            else
 315            {
 0316                return base.CanReadKeyIdentifierCore(reader);
 317            }
 318        }
 319
 320        /// <summary>
 321        /// Reads an SecurityKeyIdentifier from a XML stream.
 322        /// </summary>
 323        /// <param name=nameof(reader)>An XML reader positioned at an SecurityKeyIdentifier (ds: KeyInfo) as defined in 
 324        /// <returns>SecurityKeyIdentifier.</returns>
 325        /// <exception cref="ArgumentNullException">The <paramref name=nameof(reader)/> is null.</exception>
 326        /// <exception cref="InvalidOperationException">If the <paramref name=nameof(reader)/> is not positioned at KeyI
 327        protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader)
 328        {
 0329            if (reader == null)
 330            {
 0331                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 332            }
 333
 0334            if (reader.IsStartElement(XmlSignatureConstants.Elements.KeyInfo, XmlSignatureConstants.Namespace))
 335            {
 0336                KeyInfo keyInfo = new KeyInfo(this);
 0337                keyInfo.ReadXml(XmlDictionaryReader.CreateDictionaryReader(reader));
 0338                return keyInfo.KeyIdentifier;
 339            }
 340            else
 341            {
 0342                throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml(reader, SR.Format(SR.ID4192));
 343            }
 344        }
 345
 346        /// <summary>
 347        /// Checks if the wrapped SecurityTokenHandler or the base WSSecurityTokenSerializer can read the
 348        /// SecurityKeyIdentifierClause.
 349        /// </summary>
 350        /// <param name=nameof(reader)>Reader to a SecurityKeyIdentifierClause.</param>
 351        /// <returns>'True' if the SecurityKeyIdentifierCause can be read.</returns>
 352        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 353        protected override bool CanReadKeyIdentifierClauseCore(XmlReader reader)
 354        {
 0355            if (reader == null)
 356            {
 0357                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 358            }
 359
 0360            foreach (SecurityTokenHandler securityTokenHandler in _securityTokenHandlers)
 361            {
 0362                if (securityTokenHandler.CanReadKeyIdentifierClause(reader))
 363                {
 0364                    return true;
 365                }
 366            }
 367
 0368            return base.CanReadKeyIdentifierClauseCore(reader);
 0369        }
 370
 371        /// <summary>
 372        /// Checks if the wrapped SecurityTokenHandler or the base WSSecurityTokenSerializer can write the
 373        /// given SecurityKeyIdentifierClause.
 374        /// </summary>
 375        /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be checked.</param>
 376        /// <returns>'True' if the SecurityTokenKeyIdentifierClause can be written.</returns>
 377        /// <exception cref="ArgumentNullException">The input parameter 'keyIdentifierClause' is null.</exception>
 378        protected override bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause)
 379        {
 0380            if (keyIdentifierClause == null)
 381            {
 0382                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 383            }
 384
 0385            foreach (SecurityTokenHandler securityTokenHandler in _securityTokenHandlers)
 386            {
 0387                if (securityTokenHandler.CanWriteKeyIdentifierClause(keyIdentifierClause))
 388                {
 0389                    return true;
 390                }
 391            }
 392
 0393            return base.CanWriteKeyIdentifierClauseCore(keyIdentifierClause);
 0394        }
 395
 396        /// <summary>
 397        /// Deserializes a SecurityKeyIdentifierClause from the given reader.
 398        /// </summary>
 399        /// <param name=nameof(reader)>XmlReader to a SecurityKeyIdentifierClause.</param>
 400        /// <returns>The deserialized SecurityKeyIdentifierClause.</returns>
 401        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 402        protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader)
 403        {
 0404            if (reader == null)
 405            {
 0406                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 407            }
 408
 409            try
 410            {
 0411                foreach (SecurityTokenHandler securityTokenHandler in _securityTokenHandlers)
 412                {
 0413                    if (securityTokenHandler.CanReadKeyIdentifierClause(reader))
 414                    {
 0415                        return securityTokenHandler.ReadKeyIdentifierClause(reader);
 416                    }
 417                }
 418
 0419                return base.ReadKeyIdentifierClauseCore(reader);
 420            }
 0421            catch (Exception ex)
 422            {
 0423                if (!(MapExceptionsToSoapFaults && _exceptionMapper.HandleSecurityTokenProcessingException(ex)))
 424                {
 0425                    throw;
 426                }
 427                Fx.Assert(false, "ExceptionMapper did not handle an exception correctly.");
 428                // This should never happen. ExceptionMapper will handle the exception, in which case,
 429                // a fault exception is thrown or the original exception gets thrown.
 0430            }
 431
 0432            return null;
 0433        }
 434
 435        /// <summary>
 436        /// Serializes the given SecurityKeyIdentifierClause in a XmlWriter.
 437        /// </summary>
 438        /// <param name=nameof(writer)>XmlWriter to write into.</param>
 439        /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be written.</param>
 440        /// <exception cref="ArgumentNullException">The input parameter 'writer' or 'keyIdentifierClause' is null.</exce
 441        protected override void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifier
 442        {
 0443            if (writer == null)
 444            {
 0445                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 446            }
 447
 0448            if (keyIdentifierClause == null)
 449            {
 0450                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 451            }
 452
 453            try
 454            {
 0455                foreach (SecurityTokenHandler securityTokenHandler in _securityTokenHandlers)
 456                {
 0457                    if (securityTokenHandler.CanWriteKeyIdentifierClause(keyIdentifierClause))
 458                    {
 0459                        securityTokenHandler.WriteKeyIdentifierClause(writer, keyIdentifierClause);
 0460                        return;
 461                    }
 462                }
 463
 0464                base.WriteKeyIdentifierClauseCore(writer, keyIdentifierClause);
 0465            }
 0466            catch (Exception ex)
 467            {
 0468                if (!(MapExceptionsToSoapFaults && _exceptionMapper.HandleSecurityTokenProcessingException(ex)))
 469                {
 0470                    throw;
 471                }
 472                Fx.Assert(false, "ExceptionMapper did not handle an exception correctly.");
 473                // This should never happen. ExceptionMapper will handle the exception, in which case,
 474                // a fault exception is thrown or the original exception gets thrown.
 0475            }
 476
 0477        }
 478
 479
 480    }
 481}

Methods/Properties

.ctor(CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerCollection,CoreWCF.Security.SecurityVersion,CoreWCF.Security.TrustVersion,CoreWCF.Security.SecureConversationVersion,System.Boolean,CoreWCF.Security.SamlSerializer,CoreWCF.Security.SecurityStateEncoder,System.Collections.Generic.IEnumerable`1<System.Type>)
.ctor(CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerCollection)
.ctor(CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerCollection,CoreWCF.Security.SecurityVersion)
.ctor(CoreWCF.IdentityModel.Tokens.SecurityTokenHandlerCollection,CoreWCF.Security.SecurityVersion,System.Boolean,CoreWCF.Security.SamlSerializer,CoreWCF.Security.SecurityStateEncoder,System.Collections.Generic.IEnumerable`1<System.Type>)
MapExceptionsToSoapFaults()
MapExceptionsToSoapFaults(System.Boolean)
SecurityTokenHandlers()
ExceptionMapper()
ExceptionMapper(CoreWCF.ExceptionMapper)
CanReadTokenCore(System.Xml.XmlReader)
CanWriteTokenCore(CoreWCF.IdentityModel.Tokens.SecurityToken)
ReadTokenCore(System.Xml.XmlReader,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver)
WriteTokenCore(System.Xml.XmlWriter,CoreWCF.IdentityModel.Tokens.SecurityToken)
CanReadKeyIdentifierCore(System.Xml.XmlReader)
ReadKeyIdentifierCore(System.Xml.XmlReader)
CanReadKeyIdentifierClauseCore(System.Xml.XmlReader)
CanWriteKeyIdentifierClauseCore(CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
ReadKeyIdentifierClauseCore(System.Xml.XmlReader)
WriteKeyIdentifierClauseCore(System.Xml.XmlWriter,CoreWCF.IdentityModel.SecurityKeyIdentifierClause)