< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.WSSecurityXXX2005
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/WSSecurityXXX2005.cs
Line coverage
85%
Covered lines: 59
Uncovered lines: 10
Coverable lines: 69
Total lines: 246
Line coverage: 85.5%
Branch coverage
78%
Covered branches: 22
Total branches: 28
Branch coverage: 78.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
PopulateStrEntries(...)100%11100%
PopulateTokenEntries(...)100%11100%
PopulateKeyIdentifierClauseEntries(...)100%11100%
.ctor(...)100%11100%
ReadTokenType(...)100%11100%
WriteKeyIdentifierClauseCore(...)66.66%6680%
GetTokenTypeUri(...)91.66%121288.88%
EmitTokenType(...)83.33%6693.33%
.ctor(...)100%11100%
CanReadClause(...)50%4466.66%
CreateClause(...)100%11100%
GetTokenTypeUri()100%110%
.ctor(...)100%11100%
CreateClause(...)100%110%
GetTokenTypeUri()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/WSSecurityXXX2005.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.Security.Tokens;
 8using static CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer;
 9
 10namespace CoreWCF.IdentityModel.Tokens
 11{
 12    internal class WSSecurityXXX2005 : WSSecurityJan2004
 13    {
 14        public WSSecurityXXX2005(KeyInfoSerializer securityTokenSerializer)
 12815            : base(securityTokenSerializer)
 16        {
 12817        }
 18
 19        public override void PopulateStrEntries(IList<StrEntry> strEntries)
 20        {
 12821            PopulateJan2004StrEntries(strEntries);
 22            //  strEntries.Add(new SamlDirectStrEntry());
 12823            strEntries.Add(new X509ThumbprintStrEntry(SecurityTokenSerializer.EmitBspRequiredAttributes));
 12824            strEntries.Add(new EncryptedKeyHashStrEntry(SecurityTokenSerializer.EmitBspRequiredAttributes));
 12825        }
 26
 27        public override void PopulateTokenEntries(IList<TokenEntry> tokenEntryList)
 28        {
 12829            PopulateJan2004TokenEntries(tokenEntryList);
 12830            tokenEntryList.Add(new WSSecurityXXX2005.WrappedKeyTokenEntry());
 31            // tokenEntryList.Add(new WSSecurityXXX2005.SamlTokenEntry());
 12832        }
 33
 34        public override void PopulateKeyIdentifierClauseEntries(IList<KeyIdentifierClauseEntry> clauseEntries)
 35        {
 12836            List<StrEntry> strEntries = new List<StrEntry>();
 12837            SecurityTokenSerializer.PopulateStrEntries(strEntries);
 12838            SecurityTokenReferenceXXX2005ClauseEntry strClause = new SecurityTokenReferenceXXX2005ClauseEntry(SecurityTo
 12839            clauseEntries.Add(strClause);
 12840        }
 41
 42        private new class WrappedKeyTokenEntry : WSSecurityJan2004.WrappedKeyTokenEntry
 43        {
 1644            public override string TokenTypeUri { get { return SecurityXXX2005Strings.EncryptedKeyTokenType; } }
 45        }
 46
 47        private class SecurityTokenReferenceXXX2005ClauseEntry : SecurityTokenReferenceJan2004ClauseEntry
 48        {
 49            public SecurityTokenReferenceXXX2005ClauseEntry(bool emitBspRequiredAttributes, IList<StrEntry> strEntries)
 12850                : base(emitBspRequiredAttributes, strEntries)
 51            {
 12852            }
 53
 54            protected override string ReadTokenType(XmlDictionaryReader reader)
 55            {
 3956                return reader.GetAttribute(CoreWCF.XD.SecurityXXX2005Dictionary.TokenTypeAttribute, CoreWCF.XD.SecurityX
 57            }
 58
 59            public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause ke
 60            {
 14061                for (int i = 0; i < StrEntries.Count; ++i)
 62                {
 7063                    if (StrEntries[i].SupportsCore(keyIdentifierClause))
 64                    {
 2065                        writer.WriteStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Prefix.Value, CoreWCF.XD.SecurityJ
 66
 2067                        string tokenTypeUri = GetTokenTypeUri(StrEntries[i], keyIdentifierClause);
 2068                        if (tokenTypeUri != null)
 69                        {
 070                            writer.WriteAttributeString(CoreWCF.XD.SecurityXXX2005Dictionary.Prefix.Value, CoreWCF.XD.Se
 71                        }
 72
 2073                        StrEntries[i].WriteContent(writer, keyIdentifierClause);
 2074                        writer.WriteEndElement();
 2075                        return;
 76                    }
 77                }
 078                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta
 79            }
 80
 81            private string GetTokenTypeUri(StrEntry str, SecurityKeyIdentifierClause keyIdentifierClause)
 82            {
 2083                bool emitTokenType = EmitTokenType(str);
 2084                if (emitTokenType)
 85                {
 86                    string tokenTypeUri;
 887                    if (str is LocalReferenceStrEntry)
 88                    {
 889                        tokenTypeUri = (str as LocalReferenceStrEntry).GetLocalTokenTypeUri(keyIdentifierClause);
 90                        // only emit token type for SAML,Kerberos and Encrypted References
 91                        switch (tokenTypeUri)
 92                        {
 93                            case SecurityXXX2005Strings.Saml20TokenType:
 94                            case SecurityXXX2005Strings.SamlTokenType:
 95                            case SecurityXXX2005Strings.EncryptedKeyTokenType:
 96                            case SecurityJan2004Strings.KerberosTokenTypeGSS: break;
 97
 98                            default:
 899                                tokenTypeUri = null;
 8100                                break;
 101                        }
 102                    }
 103                    else
 104                    {
 0105                        tokenTypeUri = str.GetTokenTypeUri();
 106                    }
 107
 8108                    return tokenTypeUri;
 109                }
 110                else
 111                {
 12112                    return null;
 113                }
 114            }
 115
 116            private bool EmitTokenType(StrEntry str)
 117            {
 20118                bool emitTokenType = false;
 119                // we emit tokentype always for SAML and Encrypted Key Tokens
 20120                if (
 20121                        //(str is SamlJan2004KeyIdentifierStrEntry)
 20122                        //||
 20123                        (str is EncryptedKeyHashStrEntry)
 20124                       // || (str is SamlDirectStrEntry)
 20125                       )
 126                {
 0127                    emitTokenType = true;
 128                }
 20129                else if (EmitBspRequiredAttributes)
 130                {
 16131                    if (
 16132                        //(str is KerberosHashStrEntry)
 16133                        //||
 16134                        (str is LocalReferenceStrEntry))
 135                    {
 8136                        emitTokenType = true;
 137                    }
 138                }
 20139                return emitTokenType;
 140            }
 141        }
 142
 143        private class EncryptedKeyHashStrEntry : WSSecurityJan2004.KeyIdentifierStrEntry
 144        {
 20145            protected override Type ClauseType { get { return typeof(EncryptedKeyHashIdentifierClause); } }
 0146            public override Type TokenType { get { return typeof(WrappedKeySecurityToken); } }
 4147            protected override string ValueTypeUri { get { return SecurityXXX2005Strings.EncryptedKeyHashValueType; } }
 148
 149            public EncryptedKeyHashStrEntry(bool emitBspRequiredAttributes)
 128150                : base(emitBspRequiredAttributes)
 151            {
 128152            }
 153
 154            public override bool CanReadClause(XmlDictionaryReader reader, string tokenType)
 155            {
 156                // Backward compatible with V1. Accept if missing.
 14157                if (tokenType != null && tokenType != SecurityXXX2005Strings.EncryptedKeyTokenType)
 158                {
 0159                    return false;
 160                }
 14161                return base.CanReadClause(reader, tokenType);
 162            }
 163
 164            protected override SecurityKeyIdentifierClause CreateClause(byte[] bytes, byte[] derivationNonce, int deriva
 165            {
 4166                return new EncryptedKeyHashIdentifierClause(bytes, true, derivationNonce, derivationLength);
 167            }
 168
 169            public override string GetTokenTypeUri()
 170            {
 0171                return SecurityXXX2005Strings.EncryptedKeyTokenType;
 172            }
 173        }
 174
 175        private class X509ThumbprintStrEntry : WSSecurityJan2004.KeyIdentifierStrEntry
 176        {
 20177            protected override Type ClauseType { get { return typeof(X509ThumbprintKeyIdentifierClause); } }
 0178            public override Type TokenType { get { return typeof(X509SecurityToken); } }
 4179            protected override string ValueTypeUri { get { return SecurityXXX2005Strings.ThumbprintSha1ValueType; } }
 180
 181            public X509ThumbprintStrEntry(bool emitBspRequiredAttributes)
 128182                : base(emitBspRequiredAttributes)
 183            {
 128184            }
 185
 186            protected override SecurityKeyIdentifierClause CreateClause(byte[] bytes, byte[] derivationNonce, int deriva
 187            {
 0188                return new X509ThumbprintKeyIdentifierClause(bytes);
 189            }
 190            public override string GetTokenTypeUri()
 191            {
 0192                return CoreWCF.XD.SecurityXXX2005Dictionary.ThumbprintSha1ValueType.Value;
 193            }
 194        }
 195
 196        //class SamlDirectStrEntry : StrEntry
 197        //{
 198        //    public override bool CanReadClause(XmlDictionaryReader reader, string tokenType)
 199        //    {
 200        //        if (tokenType != CoreWCF.XD.SecurityXXX2005Dictionary.Saml20TokenType.Value)
 201        //        {
 202        //            return false;
 203        //        }
 204        //        return (reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Reference, CoreWCF.XD.SecurityJan20
 205        //    }
 206
 207        //    public override Type GetTokenType(SecurityKeyIdentifierClause clause)
 208        //    {
 209        //        return null;
 210        //    }
 211
 212        //    public override string GetTokenTypeUri()
 213        //    {
 214        //        return CoreWCF.XD.SecurityXXX2005Dictionary.Saml20TokenType.Value;
 215        //    }
 216
 217        //    public override SecurityKeyIdentifierClause ReadClause(XmlDictionaryReader reader, byte[] derivationNone, 
 218        //    {
 219        //        string samlUri = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.URI, null);
 220        //        if (reader.IsEmptyElement)
 221        //        {
 222        //            reader.Read();
 223        //        }
 224        //        else
 225        //        {
 226        //            reader.ReadStartElement();
 227        //            reader.ReadEndElement();
 228        //        }
 229        //        return new SamlAssertionDirectKeyIdentifierClause(samlUri, derivationNone, derivationLength);
 230        //    }
 231
 232        //    public override bool SupportsCore(SecurityKeyIdentifierClause clause)
 233        //    {
 234        //        return typeof(SamlAssertionDirectKeyIdentifierClause).IsAssignableFrom(clause.GetType());
 235        //    }
 236
 237        //    public override void WriteContent(XmlDictionaryWriter writer, SecurityKeyIdentifierClause clause)
 238        //    {
 239        //        SamlAssertionDirectKeyIdentifierClause samlClause = clause as SamlAssertionDirectKeyIdentifierClause;
 240        //        writer.WriteStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Prefix.Value, CoreWCF.XD.SecurityJan2004
 241        //        writer.WriteAttributeString(CoreWCF.XD.SecurityJan2004Dictionary.URI, null, samlClause.SamlUri);
 242        //        writer.WriteEndElement();
 243        //    }
 244        //}
 245    }
 246}