< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.SpnegoTokenAuthenticator
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SpnegoTokenAuthenticator.cs
Line coverage
27%
Covered lines: 15
Uncovered lines: 39
Coverable lines: 54
Total lines: 166
Line coverage: 27.7%
Branch coverage
16%
Covered branches: 2
Total branches: 12
Branch coverage: 16.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
OpenAsync(...)100%22100%
CloseAsync(...)100%110%
OnAbort()100%110%
FreeCredentialsHandle()0%220%
CreateSspiState(...)100%110%
ValidateSspiNegotiationAsync(...)0%660%
GetAuthorizationPoliciesAsync(...)0%220%
GetNegotiateState()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SpnegoTokenAuthenticator.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 System.Net;
 7using System.Security.Claims;
 8using System.Security.Principal;
 9using System.Threading;
 10using System.Threading.Tasks;
 11using System.Xml;
 12using CoreWCF.IdentityModel;
 13using CoreWCF.IdentityModel.Policy;
 14using CoreWCF.IdentityModel.Selectors;
 15using CoreWCF.IdentityModel.Tokens;
 16using CoreWCF.Security.NegotiateInternal;
 17
 18namespace CoreWCF.Security
 19{
 20    internal sealed class SpnegoTokenAuthenticator : SspiNegotiationTokenAuthenticator
 21    {
 22        private bool _extractGroupsForWindowsAccounts;
 23        private NetworkCredential _serverCredential;
 24        private bool _allowUnauthenticatedCallers;
 25        private LdapSettings _ldapSettings;
 26
 27        // SafeFreeCredentials credentialsHandle;
 28        private INegotiateInternalState _negotiateHandler;
 29        public SpnegoTokenAuthenticator()
 130            : base()
 31        {
 32            // empty
 133        }
 34
 35        // settings
 36        public bool ExtractGroupsForWindowsAccounts
 37        {
 038            get => _extractGroupsForWindowsAccounts;
 39            set
 40            {
 141                CommunicationObject.ThrowIfDisposedOrImmutable();
 142                _extractGroupsForWindowsAccounts = value;
 143            }
 44        }
 45
 46        public NetworkCredential ServerCredential
 47        {
 048            get => _serverCredential;
 49            set
 50            {
 051                CommunicationObject.ThrowIfDisposedOrImmutable();
 052                _serverCredential = value;
 053            }
 54        }
 55
 56        public LdapSettings LdapSettings
 57        {
 058            get => _ldapSettings;
 59            set
 60            {
 161                CommunicationObject.ThrowIfDisposedOrImmutable();
 162                _ldapSettings = value;
 163            }
 64        }
 65
 66        public bool AllowUnauthenticatedCallers
 67        {
 068            get => _allowUnauthenticatedCallers;
 69            set
 70            {
 171                CommunicationObject.ThrowIfDisposedOrImmutable();
 172                _allowUnauthenticatedCallers = value;
 173            }
 74        }
 75
 76        // overrides
 077        public override XmlDictionaryString NegotiationValueType => XD.TrustApr2004Dictionary.SpnegoValueTypeUri;
 78
 79        public override Task OpenAsync(CancellationToken token)
 80        {
 181            base.OpenAsync(token);
 182            if (_negotiateHandler == null)
 83            {
 184                _negotiateHandler = new NegotiateInternalStateFactory().CreateInstance();
 85            }
 86
 187            return Task.CompletedTask;
 88        }
 89
 90        public override Task CloseAsync(CancellationToken token)
 91        {
 092            base.CloseAsync(token);
 093            FreeCredentialsHandle();
 094            return Task.CompletedTask;
 95        }
 96
 97        public override void OnAbort()
 98        {
 99            try
 100            {
 0101                base.OnAbort();
 0102            }
 103            finally
 104            {
 0105                FreeCredentialsHandle();
 0106            }
 0107        }
 108
 109        private void FreeCredentialsHandle()
 110        {
 0111            if (_negotiateHandler != null)
 112            {
 0113                _negotiateHandler.Dispose();
 114            }
 0115        }
 116
 117        protected override SspiNegotiationTokenAuthenticatorState CreateSspiState(byte[] incomingBlob, string incomingVa
 118        {
 0119            ISspiNegotiation windowsNegotiation = new WindowsSspiNegotiation(GetNegotiateState());
 0120            return new SspiNegotiationTokenAuthenticatorState(windowsNegotiation);
 121        }
 122
 123        protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateSspiNegotiationAsync(ISspiNegotia
 124        {
 0125            WindowsSspiNegotiation windowsNegotiation = (WindowsSspiNegotiation)sspiNegotiation;
 0126            if (windowsNegotiation.IsValidContext == false)
 127            {
 0128                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationException(SR.Format(S
 129            }
 130            // SecurityTraceRecordHelper.TraceServiceSpnego(windowsNegotiation);
 0131            if (IsClientAnonymous)
 132            {
 0133                return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(EmptyReadOnlyCollection<IAuthorizationPol
 134            }
 0135            IIdentity identity = windowsNegotiation.GetIdentity();
 0136            if (identity != null)
 137            {
 0138                return GetAuthorizationPoliciesAsync(identity);
 139            }
 140            else
 141            {
 0142                throw new Exception("Identity can't be determined.");
 143            }
 144        }
 145
 146        private ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> GetAuthorizationPoliciesAsync(IIdentity identity)
 147        {
 0148            IIdentity remoteIdentity = identity;
 149            SecurityToken token;
 0150            WindowsSecurityTokenAuthenticator authenticator = new WindowsSecurityTokenAuthenticator(_extractGroupsForWin
 0151            if (remoteIdentity is WindowsIdentity)
 152            {
 0153                WindowsIdentity windowIdentity = (WindowsIdentity)remoteIdentity;
 0154                SecurityUtils.ValidateAnonymityConstraint(windowIdentity, false);
 0155                token = new WindowsSecurityToken(windowIdentity, SecurityUniqueId.Create().Value, windowIdentity.Authent
 156            }
 157            else
 158            {
 0159                token = new GenericIdentitySecurityToken((GenericIdentity)remoteIdentity, SecurityUniqueId.Create().Valu
 160            }
 0161            return authenticator.ValidateTokenAsync(token);
 162        }
 163
 0164        private INegotiateInternalState GetNegotiateState() => new NegotiateInternalStateFactory().CreateInstance();
 165    }
 166}