< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.X509SecurityToken
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509SecurityToken.cs
Line coverage
61%
Covered lines: 47
Uncovered lines: 30
Coverable lines: 77
Total lines: 195
Line coverage: 61%
Branch coverage
34%
Covered branches: 15
Total branches: 44
Branch coverage: 34%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)66.66%6688.88%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
CanCreateKeyIdentifierClause()0%880%
CreateKeyIdentifierClause()0%10100%
MatchesKeyIdentifierClause(...)62.5%8878.57%
Dispose()0%440%
ThrowIfDisposed()50%2266.66%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/X509SecurityToken.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.Collections.ObjectModel;
 7using System.Security.Cryptography.X509Certificates;
 8using CoreWCF.Security;
 9
 10namespace CoreWCF.IdentityModel.Tokens
 11{
 12    public class X509SecurityToken : SecurityToken, IDisposable
 13    {
 14        private readonly string _id;
 15        private readonly X509Certificate2 _certificate;
 16        private ReadOnlyCollection<SecurityKey> _securityKeys;
 1517        private DateTime _effectiveTime = SecurityUtils.MaxUtcDateTime;
 1518        private DateTime _expirationTime = SecurityUtils.MinUtcDateTime;
 19        private bool _disposed = false;
 20        private readonly bool _disposable;
 21
 22        public X509SecurityToken(X509Certificate2 certificate)
 523            : this(certificate, SecurityUniqueId.Create().Value)
 24        {
 525        }
 26
 27        public X509SecurityToken(X509Certificate2 certificate, string id)
 528            : this(certificate, id, true)
 29        {
 530        }
 31
 32        public X509SecurityToken(X509Certificate2 certificate, bool clone)
 233            : this(certificate, SecurityUniqueId.Create().Value, clone)
 34        {
 235        }
 36
 37        internal X509SecurityToken(X509Certificate2 certificate, bool clone, bool disposable)
 538            : this(certificate, SecurityUniqueId.Create().Value, clone, disposable)
 39        {
 540        }
 41
 42        internal X509SecurityToken(X509Certificate2 certificate, string id, bool clone)
 1043            : this(certificate, id, clone, true)
 44        {
 1045        }
 46
 1547        internal X509SecurityToken(X509Certificate2 certificate, string id, bool clone, bool disposable)
 48        {
 1549            if (certificate == null)
 50            {
 051                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate));
 52            }
 53
 1554            _id = id ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id));
 1555            _certificate = clone ? new X509Certificate2(certificate) : certificate;
 56            // if the cert needs to be cloned then the token owns the clone and should dispose it
 1557            _disposable = clone || disposable;
 1558        }
 59
 60        public override string Id
 61        {
 662            get { return _id; }
 63        }
 64
 65        public override ReadOnlyCollection<SecurityKey> SecurityKeys
 66        {
 67            get
 68            {
 869                ThrowIfDisposed();
 870                if (_securityKeys == null)
 71                {
 472                    List<SecurityKey> temp = new List<SecurityKey>(1)
 473                    {
 474                        new X509AsymmetricSecurityKey(_certificate)
 475                    };
 476                    _securityKeys = temp.AsReadOnly();
 77                }
 878                return _securityKeys;
 79            }
 80        }
 81
 82        public override DateTime ValidFrom
 83        {
 84            get
 85            {
 086                ThrowIfDisposed();
 087                if (_effectiveTime == SecurityUtils.MaxUtcDateTime)
 88                {
 089                    _effectiveTime = _certificate.NotBefore.ToUniversalTime();
 90                }
 91
 092                return _effectiveTime;
 93            }
 94        }
 95
 96        public override DateTime ValidTo
 97        {
 98            get
 99            {
 5100                ThrowIfDisposed();
 5101                if (_expirationTime == SecurityUtils.MinUtcDateTime)
 102                {
 5103                    _expirationTime = _certificate.NotAfter.ToUniversalTime();
 104                }
 105
 5106                return _expirationTime;
 107            }
 108        }
 109
 110        public X509Certificate2 Certificate
 111        {
 112            get
 113            {
 138114                ThrowIfDisposed();
 138115                return _certificate;
 116            }
 117        }
 118
 119        public override bool CanCreateKeyIdentifierClause<T>()
 120        {
 0121            ThrowIfDisposed();
 0122            if (typeof(T) == typeof(X509SubjectKeyIdentifierClause))
 0123                return X509SubjectKeyIdentifierClause.CanCreateFrom(_certificate);
 124
 0125            return typeof(T) == typeof(X509ThumbprintKeyIdentifierClause) ||
 0126                   typeof(T) == typeof(X509IssuerSerialKeyIdentifierClause) ||
 0127                   typeof(T) == typeof(X509RawDataKeyIdentifierClause) ||
 0128                   base.CanCreateKeyIdentifierClause<T>();
 129        }
 130
 131        public override T CreateKeyIdentifierClause<T>()
 132        {
 0133            ThrowIfDisposed();
 0134            if (typeof(T) == typeof(X509SubjectKeyIdentifierClause))
 135            {
 136                X509SubjectKeyIdentifierClause x509KeyIdentifierClause;
 0137                if (X509SubjectKeyIdentifierClause.TryCreateFrom(_certificate, out x509KeyIdentifierClause))
 0138                    return x509KeyIdentifierClause as T;
 139            }
 0140            else if (typeof(T) == typeof(X509ThumbprintKeyIdentifierClause))
 141            {
 0142                return new X509ThumbprintKeyIdentifierClause(_certificate) as T;
 143            }
 0144            else if (typeof(T) == typeof(X509IssuerSerialKeyIdentifierClause))
 145            {
 0146                return new X509IssuerSerialKeyIdentifierClause(_certificate) as T;
 147            }
 0148            else if (typeof(T) == typeof(X509RawDataKeyIdentifierClause))
 149            {
 0150                return new X509RawDataKeyIdentifierClause(_certificate) as T;
 151            }
 152
 0153            return base.CreateKeyIdentifierClause<T>();
 154        }
 155
 156        public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause)
 157        {
 56158            ThrowIfDisposed();
 56159            X509SubjectKeyIdentifierClause subjectKeyIdentifierClause = keyIdentifierClause as X509SubjectKeyIdentifierC
 56160            if (subjectKeyIdentifierClause != null)
 3161                return subjectKeyIdentifierClause.Matches(_certificate);
 162
 53163            X509ThumbprintKeyIdentifierClause thumbprintKeyIdentifierClause = keyIdentifierClause as X509ThumbprintKeyId
 53164            if (thumbprintKeyIdentifierClause != null)
 0165                return thumbprintKeyIdentifierClause.Matches(_certificate);
 166
 53167            X509IssuerSerialKeyIdentifierClause issuerKeyIdentifierClause = keyIdentifierClause as X509IssuerSerialKeyId
 53168            if (issuerKeyIdentifierClause != null)
 0169                return issuerKeyIdentifierClause.Matches(_certificate);
 170
 53171            X509RawDataKeyIdentifierClause rawCertKeyIdentifierClause = keyIdentifierClause as X509RawDataKeyIdentifierC
 53172            if (rawCertKeyIdentifierClause != null)
 50173                return rawCertKeyIdentifierClause.Matches(_certificate);
 174
 3175            return base.MatchesKeyIdentifierClause(keyIdentifierClause);
 176        }
 177
 178        public virtual void Dispose()
 179        {
 0180            if (_disposable && !_disposed)
 181            {
 0182                _disposed = true;
 0183                _certificate.Reset();
 184            }
 0185        }
 186
 187        protected void ThrowIfDisposed()
 188        {
 207189            if (_disposed)
 190            {
 0191                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().FullName
 192            }
 207193        }
 194    }
 195}