< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.GenericXmlSecurityToken
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/GenericXMLSecurityToken.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 67
Coverable lines: 67
Total lines: 175
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 44
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%
ToString()0%440%
GetId(...)0%10100%
CanCreateKeyIdentifierClause()0%880%
CreateKeyIdentifierClause()0%880%
MatchesKeyIdentifierClause(...)0%880%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/GenericXMLSecurityToken.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.Globalization;
 7using System.IO;
 8using System.Xml;
 9using CoreWCF.IdentityModel.Policy;
 10using CoreWCF.Security;
 11
 12namespace CoreWCF.IdentityModel.Tokens
 13{
 14    public class GenericXmlSecurityToken : SecurityToken
 15    {
 16        private const int SupportedPersistanceVersion = 1;
 17        private readonly string _id;
 18        private readonly DateTime _effectiveTime;
 19        private readonly DateTime _expirationTime;
 20
 021        public GenericXmlSecurityToken(
 022            XmlElement tokenXml,
 023            SecurityToken proofToken,
 024            DateTime effectiveTime,
 025            DateTime expirationTime,
 026            SecurityKeyIdentifierClause internalTokenReference,
 027            SecurityKeyIdentifierClause externalTokenReference,
 028            ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies
 029            )
 30        {
 031            if (tokenXml == null)
 32            {
 033                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenXml));
 34            }
 35
 036            _id = GetId(tokenXml);
 037            TokenXml = tokenXml;
 038            ProofToken = proofToken;
 039            _effectiveTime = effectiveTime.ToUniversalTime();
 040            _expirationTime = expirationTime.ToUniversalTime();
 41
 042            InternalTokenReference = internalTokenReference;
 043            ExternalTokenReference = externalTokenReference;
 044            AuthorizationPolicies = authorizationPolicies ?? EmptyReadOnlyCollection<IAuthorizationPolicy>.Instance;
 045        }
 46
 047        public override string Id => _id;
 48
 049        public override DateTime ValidFrom => _effectiveTime;
 50
 051        public override DateTime ValidTo => _expirationTime;
 52
 053        public SecurityKeyIdentifierClause InternalTokenReference { get; }
 54
 055        public SecurityKeyIdentifierClause ExternalTokenReference { get; }
 56
 057        public XmlElement TokenXml { get; }
 58
 059        public SecurityToken ProofToken { get; }
 60
 061        public ReadOnlyCollection<IAuthorizationPolicy> AuthorizationPolicies { get; }
 62
 63        public override ReadOnlyCollection<SecurityKey> SecurityKeys
 64        {
 65            get
 66            {
 067                if (ProofToken != null)
 68                {
 069                    return ProofToken.SecurityKeys;
 70                }
 71                else
 72                {
 073                    return EmptyReadOnlyCollection<SecurityKey>.Instance;
 74                }
 75            }
 76        }
 77
 78        public override string ToString()
 79        {
 080            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
 081            writer.WriteLine("Generic XML token:");
 082            writer.WriteLine("   validFrom: {0}", ValidFrom);
 083            writer.WriteLine("   validTo: {0}", ValidTo);
 084            if (InternalTokenReference != null)
 85            {
 086                writer.WriteLine("   InternalTokenReference: {0}", InternalTokenReference);
 87            }
 88
 089            if (ExternalTokenReference != null)
 90            {
 091                writer.WriteLine("   ExternalTokenReference: {0}", ExternalTokenReference);
 92            }
 93
 094            writer.WriteLine("   Token Element: ({0}, {1})", TokenXml.LocalName, TokenXml.NamespaceURI);
 095            return writer.ToString();
 96        }
 97
 98        private static string GetId(XmlElement tokenXml)
 99        {
 0100            if (tokenXml != null)
 101            {
 0102                string id = tokenXml.GetAttribute(UtilityStrings.IdAttribute, UtilityStrings.Namespace);
 0103                if (string.IsNullOrEmpty(id))
 104                {
 105                    // special case SAML 1.1 as this is the only possible ID as
 106                    // spec is closed.  SAML 2.0 is xs:ID
 0107                    id = tokenXml.GetAttribute("AssertionID");
 108
 109                    // if we are still null, "Id"
 0110                    if (string.IsNullOrEmpty(id))
 111                    {
 0112                        id = tokenXml.GetAttribute("Id");
 113                    }
 114
 115                    //This fixes the unecnrypted SAML 2.0 case. Eg: <Assertion ID="_05955298-214f-41e7-b4c3-84dbff7f01b9
 0116                    if (string.IsNullOrEmpty(id))
 117                    {
 0118                        id = tokenXml.GetAttribute("ID");
 119                    }
 120                }
 121
 0122                if (!string.IsNullOrEmpty(id))
 123                {
 0124                    return id;
 125                }
 126            }
 127
 0128            return null;
 129        }
 130
 131        public override bool CanCreateKeyIdentifierClause<T>()
 132        {
 0133            if (InternalTokenReference != null && typeof(T) == InternalTokenReference.GetType())
 134            {
 0135                return true;
 136            }
 137
 0138            if (ExternalTokenReference != null && typeof(T) == ExternalTokenReference.GetType())
 139            {
 0140                return true;
 141            }
 142
 0143            return false;
 144        }
 145
 146        public override T CreateKeyIdentifierClause<T>()
 147        {
 0148            if (InternalTokenReference != null && typeof(T) == InternalTokenReference.GetType())
 149            {
 0150                return (T)InternalTokenReference;
 151            }
 152
 0153            if (ExternalTokenReference != null && typeof(T) == ExternalTokenReference.GetType())
 154            {
 0155                return (T)ExternalTokenReference;
 156            }
 157
 0158            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.UnableToCr
 159        }
 160
 161        public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause)
 162        {
 0163            if (InternalTokenReference != null && InternalTokenReference.Matches(keyIdentifierClause))
 164            {
 0165                return true;
 166            }
 0167            else if (ExternalTokenReference != null && ExternalTokenReference.Matches(keyIdentifierClause))
 168            {
 0169                return true;
 170            }
 171
 0172            return false;
 173        }
 174    }
 175}