< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.UserNameSecurityTokenHandler
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/UserNameSecurityTokenHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 81
Coverable lines: 81
Total lines: 212
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
CanReadToken(...)0%220%
GetTokenTypeIdentifiers()100%110%
ReadToken(...)0%22220%
WriteToken(...)0%10100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/UserNameSecurityTokenHandler.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;
 6
 7namespace CoreWCF.IdentityModel.Tokens
 8{
 9    /// <summary>
 10    /// Defines a SecurityTokenHandler for Username Password Tokens.
 11    /// </summary>
 12    public abstract class UserNameSecurityTokenHandler : SecurityTokenHandler
 13    {
 14
 15        /// <summary>
 16        /// Initializes an instance of <see cref="UserNameSecurityTokenHandler"/>
 17        /// </summary>
 018        protected UserNameSecurityTokenHandler()
 19        {
 020        }
 21
 22        /// <summary>
 23        /// Controls if the password will be retained in the bootstrap token that is
 24        /// attached to the ClaimsIdentity in ValidateToken.  The default is false.
 25        /// </summary>
 026        public virtual bool RetainPassword { get; set; }
 27
 28        /// <summary>
 29        /// Checks the given XmlReader to verify that it is pointing to a Username
 30        /// token.
 31        /// </summary>
 32        /// <param name=nameof(reader)>XmlReader pointing to SecurityToken.</param>
 33        /// <returns>True if the reader is pointing to a Username SecurityToken.</returns>
 34        /// <exception cref="ArgumentNullException">The given reader is null.</exception>
 35        public override bool CanReadToken(XmlReader reader)
 36        {
 037            if (reader == null)
 38            {
 039                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 40            }
 41
 042            return reader.IsStartElement(WSSecurity10Constants.Elements.UsernameToken, WSSecurity10Constants.Namespace);
 43        }
 44
 45        /// <summary>
 46        /// Returns true to indicate that the handler can write UsernameSecurityToken.
 47        /// </summary>
 048        public override bool CanWriteToken => true;
 49
 50        /// <summary>
 51        /// Get the System.Type of the SecurityToken that this handler can handle.
 52        /// </summary>
 053        public override Type TokenType => typeof(UserNameSecurityToken);
 54
 55        /// <summary>
 56        /// Get the TokenTypeIdentifier of the token that this handler can work with.
 57        /// </summary>
 58        public override string[] GetTokenTypeIdentifiers()
 59        {
 060            return new string[] { SecurityTokenTypes.UserName };
 61        }
 62
 63        /// <summary>
 64        /// Reads the UsernameSecurityToken from the given XmlReader.
 65        /// </summary>
 66        /// <param name="reader">XmlReader pointing to the SecurityToken.</param>
 67        /// <returns>An instance of <see cref="UserNameSecurityToken"/>.</returns>
 68        /// <exception cref="ArgumentNullException">The parameter 'reader' is null.</exception>
 69        /// <exception cref="XmlException">The token cannot be read.</exception>
 70        /// <exception cref="NotSupportedException">The Password was not in plain text format.</exception>
 71        /// <exception cref="InvalidOperationException">An unknown element was found in the SecurityToken or
 72        /// the username was not specified.</exception>
 73        public override SecurityToken ReadToken(XmlReader reader)
 74        {
 075            if (reader == null)
 76            {
 077                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 78            }
 79
 080            if (!CanReadToken(reader))
 81            {
 082                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 083                    new XmlException(
 084                        SR.Format(
 085                        SR.ID4065,
 086                        WSSecurity10Constants.Elements.Username,
 087                        WSSecurity10Constants.Namespace,
 088                        reader.LocalName,
 089                        reader.NamespaceURI)));
 90            }
 91
 092            string id = null;
 093            string userName = null;
 094            string password = null;
 95
 096            reader.MoveToContent();
 097            id = reader.GetAttribute(WSUtilityConstants.Attributes.IdAttribute, WSUtilityConstants.NamespaceURI);
 98
 099            reader.ReadStartElement(WSSecurity10Constants.Elements.UsernameToken, WSSecurity10Constants.Namespace);
 0100            while (reader.IsStartElement())
 101            {
 0102                if (reader.IsStartElement(WSSecurity10Constants.Elements.Username, WSSecurity10Constants.Namespace))
 103                {
 0104                    userName = reader.ReadElementString();
 105                }
 0106                else if (reader.IsStartElement(WSSecurity10Constants.Elements.Password, WSSecurity10Constants.Namespace)
 107                {
 0108                    string type = reader.GetAttribute(WSSecurity10Constants.Attributes.Type, null);
 0109                    if (!string.IsNullOrEmpty(type) && !StringComparer.Ordinal.Equals(type, WSSecurity10Constants.UPToke
 110                    {
 0111                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR
 112                    }
 113
 0114                    password = reader.ReadElementString();
 115                }
 0116                else if (reader.IsStartElement(WSSecurity10Constants.Elements.Nonce, WSSecurity10Constants.Namespace))
 117                {
 118                    // Nonce can be safely ignored
 0119                    reader.Skip();
 120                }
 0121                else if (reader.IsStartElement(WSUtilityConstants.ElementNames.Created, WSUtilityConstants.NamespaceURI)
 122                {
 123                    // wsu:Created can be safely ignored
 0124                    reader.Skip();
 125                }
 126                else
 127                {
 0128                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.ID4060, read
 129                }
 130            }
 0131            reader.ReadEndElement();
 132
 0133            if (string.IsNullOrEmpty(userName))
 134            {
 0135                throw new InvalidOperationException(SR.Format(SR.ID4061));
 136            }
 137
 0138            return string.IsNullOrEmpty(id) ?
 0139                new UserNameSecurityToken(userName, password) :
 0140                new UserNameSecurityToken(userName, password, id);
 141        }
 142
 143
 144        /// <summary>
 145        /// Writes the given UsernameSecurityToken to the XmlWriter.
 146        /// </summary>
 147        /// <param name=nameof(writer)>XmlWriter to write the token to.</param>
 148        /// <param name=nameof(token)>SecurityToken to be written.</param>
 149        /// <exception cref="InvalidOperationException">The given token is not a UsernameSecurityToken.</exception>
 150        /// <exception cref="ArgumentNullException">The parameter 'writer' or 'token' is null.</exception>
 151        public override void WriteToken(XmlWriter writer, SecurityToken token)
 152        {
 0153            if (writer == null)
 154            {
 0155                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 156            }
 157
 0158            if (token == null)
 159            {
 0160                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
 161            }
 162
 163
 0164            if (!(token is UserNameSecurityToken usernameSecurityToken))
 165            {
 0166                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(token), SR.Format(SR.ID0018, typeof(
 167            }
 168
 169            // <wsse:UsernameToken
 0170            writer.WriteStartElement(
 0171               WSSecurity10Constants.Elements.UsernameToken,
 0172               WSSecurity10Constants.Namespace
 0173               );
 0174            if (!string.IsNullOrEmpty(token.Id))
 175            {
 176                // wsu:Id="..."
 0177                writer.WriteAttributeString(
 0178                       WSUtilityConstants.Attributes.IdAttribute,
 0179                       WSUtilityConstants.NamespaceURI,
 0180                       token.Id
 0181                       );
 182            }
 183            // <wsse:Username>...</wsse:Username>
 0184            writer.WriteElementString(
 0185                WSSecurity10Constants.Elements.Username,
 0186                WSSecurity10Constants.Namespace,
 0187                usernameSecurityToken.UserName
 0188                );
 189
 190            // <wsse:Password>...</wsse:Password>
 0191            if (usernameSecurityToken.Password != null)
 192            {
 0193                writer.WriteStartElement(
 0194                    WSSecurity10Constants.Elements.Password,
 0195                    WSSecurity10Constants.Namespace
 0196                    );
 197
 0198                writer.WriteAttributeString(
 0199                    WSSecurity10Constants.Attributes.Type,
 0200                    null,
 0201                    WSSecurity10Constants.UPTokenPasswordTextValue
 0202                    );
 203
 0204                writer.WriteString(usernameSecurityToken.Password);
 0205                writer.WriteEndElement();
 206            }
 207
 0208            writer.WriteEndElement();
 0209            writer.Flush();
 0210        }
 211    }
 212}