< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.SecurityTokenElement
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityTokenElement.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 152
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
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(...)0%440%
.ctor(...)0%440%
GetSecurityToken()0%220%
GetIdentities()0%220%
ValidateToken(...)0%440%
ReadSecurityToken(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityTokenElement.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.Security.Claims;
 7using System.Xml;
 8
 9namespace CoreWCF.IdentityModel.Tokens
 10{
 11    /// <summary>
 12    /// This class represents a number elements found in a <see cref="System.IdentityModel.Protocols.WSTrust.RequestSecu
 13    /// </summary>
 14    /// <remarks>
 15    /// This class is not thread-safe.
 16    /// </remarks>
 17    public class SecurityTokenElement
 18    {
 19        private SecurityToken _securityToken;
 20        private readonly SecurityTokenHandlerCollection _securityTokenHandlers;
 21        private ReadOnlyCollection<ClaimsIdentity> _subject;
 22
 23        /// <summary>
 24        /// Creates an instance of this object using a <see cref="SecurityToken"/> object.
 25        /// </summary>
 26        /// <param name="securityToken">The security token this object represents.</param>
 27        /// <remarks>
 28        /// <see cref="GetIdentities"/> is not supported by this object if this constructor is used unless
 29        /// <see cref="ValidateToken"/> is overriden.
 30        /// If the securityToken passed in is a <see cref="GenericXmlSecurityToken"/> then SecurityTokenXml will
 31        /// be set to the value found in <see cref="GenericXmlSecurityToken"/>
 32        /// </remarks>
 033        public SecurityTokenElement(SecurityToken securityToken)
 34        {
 035            if (securityToken == null)
 36            {
 037                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityToken));
 38            }
 39
 040            if (securityToken is GenericXmlSecurityToken xmlToken)
 41            {
 042                SecurityTokenXml = xmlToken.TokenXml;
 43            }
 44
 045            _securityToken = securityToken;
 046        }
 47
 48        /// <summary>
 49        /// Creates an instance of this object using XML representation of the security token.
 50        /// </summary>
 51        /// <param name="securityTokenXml">The <see cref="XmlElement"/> representation of the security token.</param>
 52        /// <param name="securityTokenHandlers">The collection of <see cref="SecurityTokenHandler"/> objects that may
 53        /// be used to read and validate the security token this object represents.</param>
 054        public SecurityTokenElement(XmlElement securityTokenXml, SecurityTokenHandlerCollection securityTokenHandlers)
 55        {
 056            if (securityTokenXml == null)
 57            {
 058                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenXml));
 59            }
 60
 061            if (securityTokenHandlers == null)
 62            {
 063                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenHandlers));
 64            }
 65
 066            SecurityTokenXml = securityTokenXml;
 067            _securityTokenHandlers = securityTokenHandlers;
 068        }
 69
 70        /// <summary>
 71        /// Gets the XML representation of the token.
 72        /// </summary>
 73        /// <remarks>This property will be null unless this object was constructed using
 74        /// <see cref="SecurityTokenElement(XmlElement, SecurityTokenHandlerCollection)"/>.
 75        /// </remarks>
 076        public XmlElement SecurityTokenXml { get; }
 77
 78        /// <summary>
 79        /// Gets the security token this object represents.
 80        /// </summary>
 81        /// <remarks>
 82        /// If this object was not constructed directly with a <see cref="SecurityToken"/> using
 83        /// <see cref="SecurityTokenElement(SecurityToken)"/>, <see cref="ReadSecurityToken"/>
 84        /// will be called for this value.
 85        /// </remarks>
 86        /// <returns>The <see cref="SecurityToken"/> this object represents</returns>
 87        public SecurityToken GetSecurityToken()
 88        {
 089            if (_securityToken == null)
 90            {
 091                _securityToken = ReadSecurityToken(SecurityTokenXml, _securityTokenHandlers);
 92            }
 93
 094            return _securityToken;
 95        }
 96
 97        /// <summary>
 98        /// Gets the collection of <see cref="ClaimsIdentity"/> contained in the token.
 99        /// <seealso cref="ValidateToken"/>
 100        /// </summary>
 101        /// <returns>A <see cref="ReadOnlyCollection{T}"/> of <see cref="ClaimsIdentity"/> representing the identities c
 102        public ReadOnlyCollection<ClaimsIdentity> GetIdentities()
 103        {
 0104            if (_subject == null)
 105            {
 0106                _subject = ValidateToken(SecurityTokenXml, _securityTokenHandlers);
 107            }
 108
 0109            return _subject;
 110        }
 111
 112        /// <summary>
 113        /// Creates the identities for the represented by the <see cref="SecurityToken"/>.
 114        /// </summary>
 115        /// <param name="securityTokenXml">The <see cref="XmlElement"/> representation of the security token.</param>
 116        /// <param name="securityTokenHandlers">The collection of <see cref="SecurityTokenHandler"/> objects that may
 117        /// be used to read and validate the security token this object represents.</param>
 118        /// <returns>A <see cref="ReadOnlyCollection{T}"/> of <see cref="ClaimsIdentity"/> representing the identities c
 119        /// <exception cref="InvalidOperationException">If either parameter 'securityTokenXml' or 'securityTokenHandlers
 120        protected virtual ReadOnlyCollection<ClaimsIdentity> ValidateToken(XmlElement securityTokenXml, SecurityTokenHan
 121        {
 0122            if (securityTokenXml == null || securityTokenHandlers == null)
 123            {
 0124                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4
 125            }
 126
 0127            SecurityToken securityToken = GetSecurityToken();
 0128            return securityTokenHandlers.ValidateToken(securityToken);
 129        }
 130
 131        /// <summary>
 132        /// Reads a <see cref="SecurityToken"/> from the provided XML representation.
 133        /// </summary>
 134        /// <param name="securityTokenXml">The XML representation of the security token.</param>
 135        /// <param name="securityTokenHandlers">The <see cref="SecurityTokenHandlerCollection"/> used to
 136        /// read the token.</param>
 137        /// <returns>A <see cref="SecurityToken"/>.</returns>
 138        protected virtual SecurityToken ReadSecurityToken(XmlElement securityTokenXml,
 139                                                           SecurityTokenHandlerCollection securityTokenHandlers)
 140        {
 0141            XmlReader reader = new XmlNodeReader(securityTokenXml);
 0142            reader.MoveToContent();
 0143            SecurityToken securityToken = securityTokenHandlers.ReadToken(reader);
 0144            if (securityToken == null)
 145            {
 0146                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4
 147            }
 148
 0149            return securityToken;
 150        }
 151    }
 152}