< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.SecurityTokenSerializerAdapter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityTokenSerializerAdapter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 162
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
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/Tokens/SecurityTokenSerializerAdapter.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.Xml;
 6using CoreWCF.IdentityModel.Selectors;
 7
 8namespace CoreWCF.IdentityModel.Tokens
 9{
 10    /// <summary>
 11    /// This class derives from System.IdentityModel.Selectors.SecurityTokenSerializer and wraps a collection of Securit
 12    /// Any call to this serilaizer is delegated to the token handler and delegated to the base class if no token handle
 13    /// is registered to handle this particular token or KeyIdentifier.
 14    /// </summary>
 15    internal class SecurityTokenSerializerAdapter : SecurityTokenSerializer
 16    {
 17        /// <summary>
 18        /// Initializes an instance of <see cref="SecurityTokenSerializerAdapter"/>
 19        /// </summary>
 20        /// <param name="securityTokenHandlerCollection">
 21        /// The <see cref="SecurityTokenHandlerCollection" /> containing the set of <see cref="SecurityTokenHandler" />
 22        /// </param>
 023        public SecurityTokenSerializerAdapter(SecurityTokenHandlerCollection securityTokenHandlerCollection)
 24        {
 025            SecurityTokenHandlers = securityTokenHandlerCollection ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelp
 026            if (securityTokenHandlerCollection.KeyInfoSerializer is KeyInfoSerializer serializer)
 27            {
 028                serializer.InnerSecurityTokenSerializer = this;
 29            }
 030        }
 31
 32        /// <summary>
 33        /// Gets the SecurityTokenHandlerCollection.
 34        /// </summary>
 035        public SecurityTokenHandlerCollection SecurityTokenHandlers { get; }
 36
 37        /// <summary>
 38        /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
 39        /// can read the security token.
 40        /// </summary>
 41        /// <param name="reader">Reader to a Security token.</param>
 42        /// <returns>'True' if the serializer can read the given Security Token.</returns>
 043        protected override bool CanReadTokenCore(XmlReader reader) => SecurityTokenHandlers.CanReadToken(reader);
 44
 45        /// <summary>
 46        /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
 47        /// can write the given security token.
 48        /// </summary>
 49        /// <param name="token">SecurityToken instance.</param>
 50        /// <returns>'True' if the serializer can write the given security token.</returns>
 051        protected override bool CanWriteTokenCore(SecurityToken token) => SecurityTokenHandlers.CanWriteToken(token);
 52
 53        /// <summary>
 54        /// Deserializes the SecurityToken from the given XmlReader.
 55        /// </summary>
 56        /// <param name="reader">Reader to a Security token.</param>
 57        /// <param name="tokenResolver">Instance of SecurityTokenResolver.</param>
 58        /// <returns>'True' if the serializer can read the given Security Token.</returns>
 059        protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver) => Securit
 60
 61        /// <summary>
 62        /// Serializes the SecurityToken to the XmlWriter.
 63        /// </summary>
 64        /// <param name="writer">XmlWriter to write to.</param>
 65        /// <param name="token">The SecurityToken to serializer.</param>
 66        /// <exception cref="ArgumentNullException">The input parameter 'writer' or 'token' is null.</exception>
 067        protected override void WriteTokenCore(XmlWriter writer, SecurityToken token) => SecurityTokenHandlers.WriteToke
 68
 69        /// <summary>
 70        /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
 71        /// can read the security key identifier.
 72        /// </summary>
 73        /// <param name="reader">Reader pointing at a Security Key Identifier {ds:Keyinfo}.</param>
 74        /// <returns>'True' if the serializer can read the given Security Key Identifier.</returns>
 75        /// <exception cref="ArgumentNullException">The <paramref name="reader"/> is null.</exception>
 076        protected override bool CanReadKeyIdentifierCore(XmlReader reader) => (reader.IsStartElement(XmlSignatureConstan
 77
 78        /// <summary>
 79        /// Reads an SecurityKeyIdentifier from a XML stream.
 80        /// </summary>
 81        /// <param name="reader">An XML reader positioned at an SecurityKeyIdentifier (ds: KeyInfo) as defined in 'http:
 82        /// <returns>SecurityKeyIdentifier.</returns>
 83        /// <exception cref="ArgumentNullException">The <paramref name="reader"/> is null.</exception>
 84        /// <exception cref="InvalidOperationException">If the <paramref name="reader"/> is not positioned at KeyInfo el
 85        protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader)
 86        {
 087            if (reader == null)
 88            {
 089                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 90            }
 91
 092            if (reader.IsStartElement(XmlSignatureConstants.Elements.KeyInfo, XmlSignatureConstants.Namespace))
 93            {
 094                KeyInfo keyInfo = new KeyInfo(this);
 095                keyInfo.ReadXml(XmlDictionaryReader.CreateDictionaryReader(reader));
 096                return keyInfo.KeyIdentifier;
 97            }
 98            else
 99            {
 0100                throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml(reader, SR.Format(SR.ID4192));
 101            }
 102        }
 103
 0104        protected override bool CanWriteKeyIdentifierCore(SecurityKeyIdentifier keyIdentifier) => SecurityTokenHandlers.
 105
 0106        protected override void WriteKeyIdentifierCore(XmlWriter writer, SecurityKeyIdentifier keyIdentifier) => Securit
 107
 108        /// <summary>
 109        /// Checks if the wrapped SecurityTokenHandler can read the
 110        /// SecurityKeyIdentifierClause.
 111        /// </summary>
 112        /// <param name="reader">Reader to a SecurityKeyIdentifierClause.</param>
 113        /// <returns>'True' if the SecurityKeyIdentifierCause can be read.</returns>
 114        /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception>
 115        protected override bool CanReadKeyIdentifierClauseCore(XmlReader reader)
 116        {
 0117            foreach (SecurityTokenHandler securityTokenHandler in SecurityTokenHandlers)
 118            {
 0119                if (securityTokenHandler.CanReadKeyIdentifierClause(reader))
 120                {
 0121                    return true;
 122                }
 123            }
 124
 0125            return SecurityTokenHandlers.KeyInfoSerializer != null && SecurityTokenHandlers.KeyInfoSerializer.CanReadKey
 0126        }
 127
 128        /// <summary>
 129        /// Checks if the wrapped SecurityTokenHandler or the base WSSecurityTokenSerializer can write the
 130        /// given SecurityKeyIdentifierClause.
 131        /// </summary>
 132        /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be checked.</param>
 133        /// <returns>'True' if the SecurityTokenKeyIdentifierClause can be written.</returns>
 134        /// <exception cref="ArgumentNullException">The input parameter 'keyIdentifierClause' is null.</exception>
 135        protected override bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause)
 136        {
 0137            foreach (SecurityTokenHandler securityTokenHandler in SecurityTokenHandlers)
 138            {
 0139                if (securityTokenHandler.CanWriteKeyIdentifierClause(keyIdentifierClause))
 140                {
 0141                    return true;
 142                }
 143            }
 144
 0145            return SecurityTokenHandlers.KeyInfoSerializer != null && SecurityTokenHandlers.KeyInfoSerializer.CanWriteKe
 0146        }
 147
 148        /// <summary>
 149        /// Deserializes a SecurityKeyIdentifierClause from the given reader.
 150        /// </summary>
 151        /// <param name="reader">XmlReader to a SecurityKeyIdentifierClause.</param>
 152        /// <returns>The deserialized SecurityKeyIdentifierClause.</returns>
 0153        protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader) => SecurityTokenHan
 154
 155        /// <summary>
 156        /// Serializes the given SecurityKeyIdentifierClause in a XmlWriter.
 157        /// </summary>
 158        /// <param name="writer">XmlWriter to write into.</param>
 159        /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be written.</param>
 0160        protected override void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifier
 161    }
 162}