< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.WrappedX509SecurityTokenAuthenticator
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WrappedX509SecurityTokenAuthenticator.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 103
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
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%
ValidateTokenCoreAsync(...)0%10100%
GetMapToWindowsSetting(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WrappedX509SecurityTokenAuthenticator.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.Collections.Generic;
 5using System.Collections.ObjectModel;
 6using CoreWCF.IdentityModel.Policy;
 7using CoreWCF.IdentityModel.Selectors;
 8using CoreWCF.IdentityModel.Tokens;
 9using System.Security.Claims;
 10using System;
 11using System.Threading.Tasks;
 12
 13namespace CoreWCF.Security
 14{
 15    /// <summary>
 16    /// Wraps a X509SecurityTokenHandler. Delegates the token authentication call the inner tokenAuthenticator.
 17    /// Wraps the returned ClaimsIdentities into an AuthorizationPolicy that supports IAuthorizationPolicy
 18    /// </summary>
 19    internal class WrappedX509SecurityTokenAuthenticator : X509SecurityTokenAuthenticator
 20    {
 21        private readonly X509SecurityTokenHandler _wrappedX509SecurityTokenHandler;
 22        private readonly ExceptionMapper _exceptionMapper;
 23
 24        /// <summary>
 25        /// Initializes an instance of <see cref="WrappedX509SecurityTokenAuthenticator"/>
 26        /// </summary>
 27        /// <param name="wrappedX509SecurityTokenHandler">X509SecurityTokenHandler to wrap.</param>
 28        /// <param name="exceptionMapper">Converts token validation exceptions to SOAP faults.</param>
 29        public WrappedX509SecurityTokenAuthenticator(
 30            X509SecurityTokenHandler wrappedX509SecurityTokenHandler,
 31            ExceptionMapper exceptionMapper)
 032            : base(X509CertificateValidator.None, GetMapToWindowsSetting(wrappedX509SecurityTokenHandler), true)
 33        {
 034            _wrappedX509SecurityTokenHandler = wrappedX509SecurityTokenHandler ?? throw DiagnosticUtility.ExceptionUtili
 035            _exceptionMapper = exceptionMapper ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameo
 036        }
 37
 38        /// <summary>
 39        /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy
 40        /// wrapping the returned ClaimsIdentities.
 41        /// </summary>
 42        /// <param name="token">Token to be validated.</param>
 43        /// <returns>Read-only collection of IAuthorizationPolicy</returns>
 44        protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke
 45        {
 046            ReadOnlyCollection<ClaimsIdentity> identities = null;
 47            try
 48            {
 049                identities = _wrappedX509SecurityTokenHandler.ValidateToken(token);
 050            }
 051            catch (Exception ex)
 52            {
 053                if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex))
 54                {
 055                    throw;
 56                }
 057            }
 58
 59            // tlsnego will dispose of the x509, when we write out the bootstrap we will get a dispose error.
 60
 061            bool shouldSaveBootstrapContext = SecurityTokenHandlerConfiguration.DefaultSaveBootstrapContext;
 062            if (_wrappedX509SecurityTokenHandler.Configuration != null)
 63            {
 064                shouldSaveBootstrapContext = _wrappedX509SecurityTokenHandler.Configuration.SaveBootstrapContext;
 65            }
 66
 067            if (shouldSaveBootstrapContext)
 68            {
 069                X509SecurityToken x509Token = token as X509SecurityToken;
 70                SecurityToken tokenToCache;
 071                if (x509Token != null)
 72                {
 073                    tokenToCache = new X509SecurityToken(x509Token.Certificate);
 74                }
 75                else
 76                {
 077                    tokenToCache = token;
 78                }
 79
 080                BootstrapContext bootstrapContext = new BootstrapContext(tokenToCache, _wrappedX509SecurityTokenHandler)
 081                foreach (ClaimsIdentity identity in identities)
 82                {
 083                    identity.BootstrapContext = bootstrapContext;
 84                }
 85            }
 86
 087            List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1);
 088            policies.Add(new AuthorizationPolicy(identities));
 89
 090            return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(policies.AsReadOnly());
 91        }
 92
 93        private static bool GetMapToWindowsSetting(X509SecurityTokenHandler securityTokenHandler)
 94        {
 095            if (securityTokenHandler == null)
 96            {
 097                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenHandler));
 98            }
 99
 0100            return securityTokenHandler.MapToWindows;
 101        }
 102    }
 103}