< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.WrappedSessionSecurityTokenAuthenticator
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WrappedSessionSecurityTokenAuthenticator.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 48
Coverable lines: 48
Total lines: 221
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
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%12120%
ValidateTokenCoreAsync(...)0%220%
CanValidateTokenCore(...)100%110%
SetSecureServiceDispatcher(...)0%440%
Abort()100%110%
add_Closing(...)100%110%
remove_Closing(...)100%110%
CloseAsync()100%110%
CloseAsync(...)100%110%
OpenAsync()100%110%
OpenAsync(...)100%110%
add_Opened(...)100%110%
remove_Opened(...)100%110%
add_Opening(...)100%110%
remove_Opening(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WrappedSessionSecurityTokenAuthenticator.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.Claims;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using CoreWCF.Dispatcher;
 11using CoreWCF.IdentityModel.Policy;
 12using CoreWCF.IdentityModel.Selectors;
 13using CoreWCF.IdentityModel.Tokens;
 14using CoreWCF.Security.Tokens;
 15
 16namespace CoreWCF.Security
 17{
 18    /// <summary>
 19    /// Wraps a SessionSecurityTokenHandler. Delegates the token authentication call to
 20    /// this wrapped tokenAuthenticator. Wraps the returned ClaimsIdentities into
 21    /// an IAuthorizationPolicy. This class is wired into WCF and actually receives
 22    /// SecurityContextSecurityTokens which are then wrapped into SessionSecurityTokens for
 23    /// validation.
 24    /// </summary>
 25    internal class WrappedSessionSecurityTokenAuthenticator : SecurityTokenAuthenticator, IIssuanceSecurityTokenAuthenti
 26    {
 27        private readonly SessionSecurityTokenHandler _sessionTokenHandler;
 28        private readonly IIssuanceSecurityTokenAuthenticator _issuanceSecurityTokenAuthenticator;
 29        private readonly ICommunicationObject _communicationObject;
 30        private readonly SctClaimsHandler _sctClaimsHandler;
 31        private readonly ExceptionMapper _exceptionMapper;
 32
 33#pragma warning disable CS0067 // The event is never used
 34        public event EventHandler Closed;
 35        public event EventHandler Faulted;
 36#pragma warning restore CS0067 // The event is never used
 37
 38        /// <summary>
 39        /// Initializes an instance of <see cref="WrappedRsaSecurityTokenAuthenticator"/>
 40        /// </summary>
 41        /// <param name="sessionTokenHandler">The sessionTokenHandler to wrap</param>
 42        /// <param name="wcfSessionAuthenticator">The wcf SessionTokenAuthenticator.</param>
 43        /// <param name="sctClaimsHandler">Handler that converts WCF generated IAuthorizationPolicy to <see cref="Author
 44        /// <param name="exceptionMapper">Converts token validation exception to SOAP faults.</param>
 45        public WrappedSessionSecurityTokenAuthenticator(SessionSecurityTokenHandler sessionTokenHandler,
 46                                                         SecurityTokenAuthenticator wcfSessionAuthenticator,
 47                                                         SctClaimsHandler sctClaimsHandler,
 48                                                         ExceptionMapper exceptionMapper)
 049            : base()
 50        {
 051            if (wcfSessionAuthenticator == null)
 52            {
 053                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(wcfSessionAuthenticator));
 54            }
 55
 056            _issuanceSecurityTokenAuthenticator = wcfSessionAuthenticator as IIssuanceSecurityTokenAuthenticator;
 057            if (_issuanceSecurityTokenAuthenticator == null)
 58            {
 059                throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4244));
 60            }
 61
 062            _communicationObject = wcfSessionAuthenticator as ICommunicationObject;
 063            if (_communicationObject == null)
 64            {
 065                throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4245));
 66            }
 67
 068            _sessionTokenHandler = sessionTokenHandler ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNu
 069            _sctClaimsHandler = sctClaimsHandler ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nam
 70
 071            _exceptionMapper = exceptionMapper ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameo
 072        }
 73
 74        /// <summary>
 75        /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy
 76        /// wrapping the returned ClaimsIdentities.
 77        /// </summary>
 78        /// <param name="token">Token to be validated. This is always a SecurityContextSecurityToken.</param>
 79        /// <returns>Read-only collection of IAuthorizationPolicy</returns>
 80        protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke
 81        {
 082            SecurityContextSecurityToken sct = token as SecurityContextSecurityToken;
 083            SessionSecurityToken sessionToken = SecurityContextSecurityTokenHelper.ConvertSctToSessionToken(sct);
 084            IEnumerable<ClaimsIdentity> identities = null;
 85
 86            try
 87            {
 088                identities = _sessionTokenHandler.ValidateToken(sessionToken, _sctClaimsHandler.EndpointId);
 089            }
 090            catch (Exception ex)
 91            {
 092                if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex))
 93                {
 094                    throw;
 95                }
 096            }
 97
 098            return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(new List<IAuthorizationPolicy>(new Authorizat
 99        }
 100
 101        protected override bool CanValidateTokenCore(SecurityToken token)
 102        {
 0103            return (token is SecurityContextSecurityToken);
 104        }
 105
 106        /// <summary>
 107        /// The SecurityServiceDispatcher is updated to communication obj which is SecuritySessionSecurityTokenAuthentic
 108        /// </summary>
 109        /// <param name="securitySeviceDispatcher"></param>
 110        public void SetSecureServiceDispatcher(SecurityServiceDispatcher securitySeviceDispatcher)
 111        {
 0112            if (_communicationObject == null || !(_communicationObject is SecuritySessionSecurityTokenAuthenticator))
 113            {
 0114                throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4245));
 115            }
 0116            var authenticator = (SecuritySessionSecurityTokenAuthenticator)_communicationObject;
 0117            authenticator.SecurityServiceDispatcher = securitySeviceDispatcher;
 0118        }
 119
 120        #region IIssuanceSecurityTokenAuthenticator Members
 121
 122        public IssuedSecurityTokenHandler IssuedSecurityTokenHandler
 123        {
 124            get
 125            {
 0126                return _issuanceSecurityTokenAuthenticator.IssuedSecurityTokenHandler;
 127            }
 128            set
 129            {
 0130                _issuanceSecurityTokenAuthenticator.IssuedSecurityTokenHandler = value;
 0131            }
 132        }
 133
 134        public RenewedSecurityTokenHandler RenewedSecurityTokenHandler
 135        {
 136            get
 137            {
 0138                return _issuanceSecurityTokenAuthenticator.RenewedSecurityTokenHandler;
 139            }
 140            set
 141            {
 0142                _issuanceSecurityTokenAuthenticator.RenewedSecurityTokenHandler = value;
 0143            }
 144        }
 145
 146        #endregion
 147
 148
 149        #region ICommunicationObject Members
 150
 151        // all these methods are passthroughs
 152
 153        public void Abort()
 154        {
 0155            _communicationObject.Abort();
 0156        }
 157
 158        public event System.EventHandler Closing
 159        {
 0160            add { _communicationObject.Closing += value; }
 0161            remove { _communicationObject.Closing -= value; }
 162        }
 163
 164        public Task CloseAsync()
 165        {
 0166           return _communicationObject.CloseAsync();
 167        }
 168        public Task CloseAsync(CancellationToken token)
 169        {
 0170            return _communicationObject.CloseAsync(token);
 171        }
 172        public Task OpenAsync()
 173        {
 0174            return _communicationObject.OpenAsync();
 175        }
 176        public Task OpenAsync(CancellationToken token)
 177        {
 0178            return _communicationObject.OpenAsync(token);
 179        }
 180
 181        public event System.EventHandler Opened
 182        {
 0183            add { _communicationObject.Opened += value; }
 0184            remove { _communicationObject.Opened -= value; }
 185        }
 186
 187        public event System.EventHandler Opening
 188        {
 0189            add { _communicationObject.Opening += value; }
 0190            remove { _communicationObject.Opening -= value; }
 191        }
 192
 193        public CommunicationState State
 194        {
 195
 0196            get { return _communicationObject.State; }
 197        }
 198
 199        #endregion
 200    }
 201
 202    /// <summary>
 203    /// Defines a SecurityStateEncoder whose Encode and Decode operations are
 204    /// a no-op. This class is used to null WCF SecurityContextToken creation
 205    /// code to skip any encryption and decryption cost. When SessionSecurityTokenHandler
 206    /// is being used we will use our own EncryptionTransform and ignore the WCF
 207    /// generated cookie.
 208    /// </summary>
 209    internal class NoOpSecurityStateEncoder : SecurityStateEncoder
 210    {
 211        protected internal override byte[] EncodeSecurityState(byte[] data)
 212        {
 213            return data;
 214        }
 215
 216        protected internal override byte[] DecodeSecurityState(byte[] data)
 217        {
 218            return data;
 219        }
 220    }
 221}