< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.WrappedSaml2SecurityTokenAuthenticator
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WrappedSaml2SecurityTokenAuthenticator.cs
Line coverage
38%
Covered lines: 8
Uncovered lines: 13
Coverable lines: 21
Total lines: 86
Line coverage: 38%
Branch coverage
25%
Covered branches: 2
Total branches: 8
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%4480%
CanValidateTokenCore(...)0%220%
ValidateTokenCoreAsync(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WrappedSaml2SecurityTokenAuthenticator.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 Saml2SecurityTokenHandler. Delegates the token authentication call to
 17    /// this wrapped tokenAuthenticator. Wraps the returned ClaimsIdentities into
 18    /// an IAuthorizationPolicy.
 19    /// </summary>
 20    internal class WrappedSaml2SecurityTokenAuthenticator : SecurityTokenAuthenticator
 21    {
 22        private Saml2SecurityTokenHandler _wrappedSaml2SecurityTokenHandler;
 23        private ExceptionMapper _exceptionMapper;
 24
 25        /// <summary>
 26        /// Initializes an instance of <see cref="WrappedSaml2SecurityTokenAuthenticator"/>
 27        /// </summary>
 28        /// <param name="saml2SecurityTokenHandler">The Saml2SecurityTokenHandler to wrap.</param>
 29        /// <param name="exceptionMapper">Converts token validation exceptions to SOAP faults.</param>
 230        public WrappedSaml2SecurityTokenAuthenticator(
 231            Saml2SecurityTokenHandler saml2SecurityTokenHandler,
 232            ExceptionMapper exceptionMapper)
 33        {
 234            if (saml2SecurityTokenHandler == null)
 35            {
 036                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(saml2SecurityTokenHandler));
 37            }
 38
 239            if (exceptionMapper == null)
 40            {
 041                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exceptionMapper));
 42            }
 43
 244            _wrappedSaml2SecurityTokenHandler = saml2SecurityTokenHandler;
 245            _exceptionMapper = exceptionMapper;
 246        }
 47
 48        /// <summary>
 49        /// Checks if the given token can be validated. Returns true if the token is of type
 50        /// Saml2SecurityToken and if the wrapped SecurityTokenHandler can validate tokens.
 51        /// </summary>
 52        /// <param name="token">The token to be checked.</param>
 53        /// <returns>True if the token is of type Saml2SecurityToken and if the wrapped
 54        /// SecurityTokenHandler can validate tokens.</returns>
 55        protected override bool CanValidateTokenCore(SecurityToken token)
 56        {
 057            return (token is Saml2SecurityToken) && _wrappedSaml2SecurityTokenHandler.CanValidateToken;
 58        }
 59
 60        /// <summary>
 61        /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy
 62        /// wrapping the returned ClaimsIdentities.
 63        /// </summary>
 64        /// <param name="token">Token to be validated.</param>
 65        /// <returns>Read-only collection of IAuthorizationPolicy</returns>
 66        protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke
 67        {
 068            IEnumerable<ClaimsIdentity> identities = null;
 69            try
 70            {
 071                identities = _wrappedSaml2SecurityTokenHandler.ValidateToken(token);
 072            }
 073            catch (Exception ex)
 74            {
 075                if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex))
 76                {
 077                    throw;
 78                }
 079            }
 80
 081            List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1);
 082            policies.Add(new AuthorizationPolicy(identities));
 083            return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(policies.AsReadOnly());
 84        }
 85    }
 86}