< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Selectors.SecurityTokenResolver
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Selectors/SecurityTokenResolver.cs
Line coverage
41%
Covered lines: 30
Uncovered lines: 43
Coverable lines: 73
Total lines: 204
Line coverage: 41%
Branch coverage
33%
Covered branches: 19
Total branches: 56
Branch coverage: 33.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ResolveToken(...)0%440%
TryResolveToken(...)50%2266.66%
ResolveToken(...)0%440%
TryResolveToken(...)0%220%
ResolveSecurityKey(...)0%440%
TryResolveSecurityKey(...)50%2266.66%
CreateDefaultSecurityTokenResolver(...)100%11100%
.ctor(...)50%22100%
TryResolveSecurityKeyCore(...)18.75%161635%
TryResolveTokenCore(...)83.33%6688.88%
TryResolveTokenCore(...)0%440%
ResolveSecurityToken(...)80%101075%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Selectors/SecurityTokenResolver.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 CoreWCF.IdentityModel.Tokens;
 7
 8namespace CoreWCF.IdentityModel.Selectors
 9{
 10    // TODO: Evaluate removing this class and all usages as the full framework implementation has a lot more functionali
 11    public abstract class SecurityTokenResolver
 12    {
 13        public SecurityToken ResolveToken(SecurityKeyIdentifier keyIdentifier)
 14        {
 015            if (keyIdentifier == null)
 16            {
 017                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier));
 18            }
 019            if (!TryResolveTokenCore(keyIdentifier, out SecurityToken token))
 20            {
 021                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format("Una
 22            }
 023            return token;
 24        }
 25
 26        public bool TryResolveToken(SecurityKeyIdentifier keyIdentifier, out SecurityToken token)
 27        {
 7728            if (keyIdentifier == null)
 29            {
 030                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier));
 31            }
 7732            return TryResolveTokenCore(keyIdentifier, out token);
 33        }
 34
 35        public SecurityToken ResolveToken(SecurityKeyIdentifierClause keyIdentifierClause)
 36        {
 037            if (keyIdentifierClause == null)
 38            {
 039                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 40            }
 041            if (!TryResolveTokenCore(keyIdentifierClause, out SecurityToken token))
 42            {
 043                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format("Una
 44            }
 045            return token;
 46        }
 47
 48        public bool TryResolveToken(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token)
 49        {
 050            if (keyIdentifierClause == null)
 51            {
 052                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 53            }
 054            return TryResolveTokenCore(keyIdentifierClause, out token);
 55        }
 56
 57        public SecurityKey ResolveSecurityKey(SecurityKeyIdentifierClause keyIdentifierClause)
 58        {
 059            if (keyIdentifierClause == null)
 60            {
 061                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 62            }
 063            if (!TryResolveSecurityKeyCore(keyIdentifierClause, out SecurityKey key))
 64            {
 065                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format("Una
 66            }
 067            return key;
 68        }
 69
 70        public bool TryResolveSecurityKey(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key)
 71        {
 372            if (keyIdentifierClause == null)
 73            {
 074                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 75            }
 376            return TryResolveSecurityKeyCore(keyIdentifierClause, out key);
 77        }
 78
 79        // protected methods
 80        protected abstract bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token);
 81        protected abstract bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken t
 82        protected abstract bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityK
 83
 84        public static SecurityTokenResolver CreateDefaultSecurityTokenResolver(ReadOnlyCollection<SecurityToken> tokens,
 85        {
 786            return new SimpleTokenResolver(tokens, canMatchLocalId);
 87        }
 88
 89        private class SimpleTokenResolver : SecurityTokenResolver
 90        {
 91            private readonly ReadOnlyCollection<SecurityToken> _tokens;
 92            private readonly bool _canMatchLocalId;
 93
 794            public SimpleTokenResolver(ReadOnlyCollection<SecurityToken> tokens, bool canMatchLocalId)
 95            {
 796                _tokens = tokens ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokens));
 797                _canMatchLocalId = canMatchLocalId;
 798            }
 99
 100            protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out Secur
 101            {
 1102                if (keyIdentifierClause == null)
 103                {
 0104                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 105                }
 106
 1107                key = null;
 2108                for (int i = 0; i < _tokens.Count; ++i)
 109                {
 1110                    SecurityKey securityKey = _tokens[i].ResolveKeyIdentifierClause(keyIdentifierClause);
 1111                    if (securityKey != null)
 112                    {
 1113                        key = securityKey;
 1114                        return true;
 115                    }
 116                }
 117
 0118                if (keyIdentifierClause is EncryptedKeyIdentifierClause)
 119                {
 0120                    EncryptedKeyIdentifierClause keyClause = (EncryptedKeyIdentifierClause)keyIdentifierClause;
 0121                    SecurityKeyIdentifier keyIdentifier = keyClause.EncryptingKeyIdentifier;
 0122                    if (keyIdentifier != null && keyIdentifier.Count > 0)
 123                    {
 0124                        for (int i = 0; i < keyIdentifier.Count; i++)
 125                        {
 0126                            if (TryResolveSecurityKey(keyIdentifier[i], out SecurityKey unwrappingSecurityKey))
 127                            {
 0128                                byte[] wrappedKey = keyClause.GetEncryptedKey();
 0129                                string wrappingAlgorithm = keyClause.EncryptionMethod;
 0130                                byte[] unwrappedKey = unwrappingSecurityKey.DecryptKey(wrappingAlgorithm, wrappedKey);
 0131                                key = new InMemorySymmetricSecurityKey(unwrappedKey, false);
 0132                                return true;
 133                            }
 134                        }
 135                    }
 136                }
 137
 0138                return key != null;
 139            }
 140
 141            protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token)
 142            {
 52143                if (keyIdentifier == null)
 144                {
 0145                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier));
 146                }
 147
 52148                token = null;
 108149                for (int i = 0; i < keyIdentifier.Count; ++i)
 150                {
 52151                    SecurityToken securityToken = ResolveSecurityToken(keyIdentifier[i]);
 52152                    if (securityToken != null)
 153                    {
 50154                        token = securityToken;
 50155                        break;
 156                    }
 157                }
 158
 52159                return (token != null);
 160            }
 161
 162            protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityTok
 163            {
 0164                if (keyIdentifierClause == null)
 165                {
 0166                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 167                }
 168
 0169                token = null;
 170
 0171                SecurityToken securityToken = ResolveSecurityToken(keyIdentifierClause);
 0172                if (securityToken != null)
 173                {
 0174                    token = securityToken;
 175                }
 176
 0177                return (token != null);
 178            }
 179
 180            private SecurityToken ResolveSecurityToken(SecurityKeyIdentifierClause keyIdentifierClause)
 181            {
 52182                if (keyIdentifierClause == null)
 183                {
 0184                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause));
 185                }
 186
 52187                if (!_canMatchLocalId && keyIdentifierClause is LocalIdKeyIdentifierClause)
 188                {
 0189                    return null;
 190                }
 191
 108192                for (int i = 0; i < _tokens.Count; ++i)
 193                {
 52194                    if (_tokens[i].MatchesKeyIdentifierClause(keyIdentifierClause))
 195                    {
 50196                        return _tokens[i];
 197                    }
 198                }
 199
 2200                return null;
 201            }
 202        }
 203    }
 204}