< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Description.ServiceAuthorizationBehavior
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/ServiceAuthorizationBehavior.cs
Line coverage
73%
Covered lines: 65
Uncovered lines: 23
Coverable lines: 88
Total lines: 262
Line coverage: 73.8%
Branch coverage
80%
Covered branches: 29
Total branches: 36
Branch coverage: 80.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/ServiceAuthorizationBehavior.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.ComponentModel;
 7using System.Runtime.CompilerServices;
 8using CoreWCF.Channels;
 9using CoreWCF.Collections.Generic;
 10using CoreWCF.Dispatcher;
 11using CoreWCF.IdentityModel.Policy;
 12using CoreWCF.Runtime;
 13using Microsoft.AspNetCore.Authorization;
 14using Microsoft.Extensions.DependencyInjection;
 15
 16namespace CoreWCF.Description
 17{
 18    public sealed class ServiceAuthorizationBehavior : IServiceBehavior, IDisposable, ICloneable
 19    {
 20        internal const bool DefaultImpersonateCallerForAllOperations = false;
 21        internal const bool DefaultImpersonateOnSerializingReply = false;
 22        internal const PrincipalPermissionMode DefaultPrincipalPermissionMode = PrincipalPermissionMode.UseWindowsGroups
 23        private bool _impersonateCallerForAllOperations;
 24        private readonly bool _impersonateOnSerializingReply;
 25        private ReadOnlyCollection<IAuthorizationPolicy> _externalAuthorizationPolicies;
 26        private ServiceAuthorizationManager _serviceAuthorizationManager;
 27        private IServiceScopeFactory _serviceScopeFactory;
 28        private IServiceScope _scope;
 29        private PrincipalPermissionMode _principalPermissionMode;
 30        private bool _isExternalPoliciesSet;
 31        private bool _isAuthorizationManagerSet;
 32        private bool _isReadOnly;
 33
 57534        public ServiceAuthorizationBehavior()
 35        {
 57536            _impersonateCallerForAllOperations = DefaultImpersonateCallerForAllOperations;
 57537            _impersonateOnSerializingReply = DefaultImpersonateOnSerializingReply;
 57538            _principalPermissionMode = DefaultPrincipalPermissionMode;
 57539        }
 40
 58541        private ServiceAuthorizationBehavior(ServiceAuthorizationBehavior other)
 42        {
 58543            _impersonateCallerForAllOperations = other._impersonateCallerForAllOperations;
 58544            _impersonateOnSerializingReply = other._impersonateOnSerializingReply;
 58545            _externalAuthorizationPolicies = other._externalAuthorizationPolicies;
 58546            _serviceAuthorizationManager = other._serviceAuthorizationManager;
 58547            _serviceScopeFactory = other._serviceScopeFactory;
 58548            _scope = other._scope;
 58549            _principalPermissionMode = other._principalPermissionMode;
 58550            _isExternalPoliciesSet = other._isExternalPoliciesSet;
 58551            _isAuthorizationManagerSet = other._isAuthorizationManagerSet;
 58552            _isReadOnly = other._isReadOnly;
 58553        }
 54
 55        public ReadOnlyCollection<IAuthorizationPolicy> ExternalAuthorizationPolicies
 56        {
 57            get
 58            {
 059                return _externalAuthorizationPolicies;
 60            }
 61            set
 62            {
 1763                ThrowIfImmutable();
 1764                _isExternalPoliciesSet = true;
 1765                _externalAuthorizationPolicies = value;
 1766            }
 67        }
 68
 69        public bool ShouldSerializeExternalAuthorizationPolicies()
 70        {
 071            return _isExternalPoliciesSet;
 72        }
 73
 74        public ServiceAuthorizationManager ServiceAuthorizationManager
 75        {
 76            get
 77            {
 078                return _serviceAuthorizationManager;
 79            }
 80            set
 81            {
 582                ThrowIfImmutable();
 583                _isAuthorizationManagerSet = true;
 584                _serviceAuthorizationManager = value;
 585            }
 86        }
 87
 88        public bool ShouldSerializeServiceAuthorizationManager()
 89        {
 090            return _isAuthorizationManagerSet;
 91        }
 92
 93        [DefaultValue(DefaultPrincipalPermissionMode)]
 94        public PrincipalPermissionMode PrincipalPermissionMode
 95        {
 96            get
 97            {
 098                return _principalPermissionMode;
 99            }
 100            set
 101            {
 7102                if (!PrincipalPermissionModeHelper.IsDefined(value))
 103                {
 0104                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 105                }
 7106                ThrowIfImmutable();
 7107                _principalPermissionMode = value;
 7108            }
 109        }
 110
 111
 112        [DefaultValue(DefaultImpersonateCallerForAllOperations)]
 113        public bool ImpersonateCallerForAllOperations
 114        {
 115            get
 116            {
 0117                return _impersonateCallerForAllOperations;
 118            }
 119            set
 120            {
 0121                ThrowIfImmutable();
 0122                _impersonateCallerForAllOperations = value;
 0123            }
 124        }
 125
 126
 127        [DefaultValue(DefaultImpersonateOnSerializingReply)]
 128        public bool ImpersonateOnSerializingReply
 129        {
 130            get
 131            {
 0132                return _impersonateOnSerializingReply;
 133            }
 134            set
 135            {
 136                // ThrowIfImmutable();
 137                // impersonateOnSerializingReply = value;
 0138                throw new PlatformNotSupportedException();
 139            }
 140        }
 141
 142        [Obsolete("ServiceAuthorizationBehavior.AuthorizationService will be made internal in next major release.")]
 143        public IAuthorizationService AuthorizationService
 144        {
 0145            get => throw new NotSupportedException();
 0146            set => throw new NotSupportedException();
 147        }
 148
 149        private IAuthorizationService GetAuthorizationService()
 150        {
 751151            IServiceScope scope = _scope ??= _serviceScopeFactory?.CreateScope();
 751152            return scope?.ServiceProvider.GetService<IAuthorizationService>();
 153        }
 154
 155        internal IServiceScopeFactory ServiceScopeFactory
 156        {
 157            set
 158            {
 571159                ThrowIfImmutable();
 571160                _serviceScopeFactory = value;
 571161            }
 162        }
 163
 164        [MethodImpl(MethodImplOptions.NoInlining)]
 165        private void ApplyAuthorizationPoliciesAndManager(DispatchRuntime behavior)
 166        {
 27167            if (_externalAuthorizationPolicies != null)
 168            {
 26169                behavior.ExternalAuthorizationPolicies = _externalAuthorizationPolicies;
 170            }
 27171            if (_serviceAuthorizationManager != null)
 172            {
 5173                behavior.ServiceAuthorizationManager = _serviceAuthorizationManager;
 174            }
 27175        }
 176
 177        [MethodImpl(MethodImplOptions.NoInlining)]
 178        private void CopyAuthorizationPoliciesAndManager(ServiceAuthorizationBehavior other)
 179        {
 0180            _externalAuthorizationPolicies = other._externalAuthorizationPolicies;
 0181            _serviceAuthorizationManager = other._serviceAuthorizationManager;
 0182        }
 183
 184        void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
 185        {
 2434186            foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
 187            {
 641188                TransportBindingElement transportBindingElement = endpoint.Binding.CreateBindingElements().Find<Transpor
 189                Fx.Assert(transportBindingElement != null, "TransportBindingElement is null");
 641190                var behaviors = (KeyedByTypeCollection<IEndpointBehavior>)endpoint.EndpointBehaviors;
 641191                behaviors.Add(new EndpointAuthorizationBehavior());
 192            }
 576193        }
 194
 195        void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Coll
 196        {
 664197        }
 198
 199        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
 200        {
 576201            if (description == null)
 202            {
 0203                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(description))
 204            }
 576205            if (serviceHostBase == null)
 206            {
 0207                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(serviceHostBa
 208            }
 209
 2432210            for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
 211            {
 212                // TODO: ServiceMetadataBehavior reference needs to be put back once we have MetadataBehavior
 640213                if (serviceHostBase.ChannelDispatchers[i] is ChannelDispatcher channelDispatcher /*&& !ServiceMetadataBe
 214                {
 2562215                    foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
 216                    {
 641217                        DispatchRuntime behavior = endpointDispatcher.DispatchRuntime;
 641218                        behavior.PrincipalPermissionMode = _principalPermissionMode;
 641219                        if (!endpointDispatcher.IsSystemEndpoint)
 220                        {
 641221                            behavior.ImpersonateCallerForAllOperations = _impersonateCallerForAllOperations;
 641222                            behavior.ImpersonateOnSerializingReply = _impersonateOnSerializingReply;
 223                        }
 641224                        if (_isAuthorizationManagerSet || _isExternalPoliciesSet)
 225                        {
 27226                            ApplyAuthorizationPoliciesAndManager(behavior);
 227                        }
 228
 641229                        IAuthorizationService authorizationService = GetAuthorizationService();
 641230                        if (authorizationService != null)
 231                        {
 110232                            behavior.SetAuthorizationService(GetAuthorizationService());
 233                        }
 234                    }
 235                }
 236            }
 576237        }
 238
 239        internal void MakeReadOnly()
 240        {
 0241            _isReadOnly = true;
 0242        }
 243
 244        public object Clone()
 245        {
 585246            return new ServiceAuthorizationBehavior(this);
 247        }
 248
 249        private void ThrowIfImmutable()
 250        {
 600251            if (_isReadOnly)
 252            {
 0253                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO
 254            }
 600255        }
 256
 257        public void Dispose()
 258        {
 1028259            _scope?.Dispose();
 0260        }
 261    }
 262}

Methods/Properties

.ctor()
.ctor(CoreWCF.Description.ServiceAuthorizationBehavior)
ExternalAuthorizationPolicies()
ExternalAuthorizationPolicies(System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Policy.IAuthorizationPolicy>)
ShouldSerializeExternalAuthorizationPolicies()
ServiceAuthorizationManager()
ServiceAuthorizationManager(CoreWCF.ServiceAuthorizationManager)
ShouldSerializeServiceAuthorizationManager()
PrincipalPermissionMode()
PrincipalPermissionMode(CoreWCF.Description.PrincipalPermissionMode)
ImpersonateCallerForAllOperations()
ImpersonateCallerForAllOperations(System.Boolean)
ImpersonateOnSerializingReply()
ImpersonateOnSerializingReply(System.Boolean)
AuthorizationService()
AuthorizationService(Microsoft.AspNetCore.Authorization.IAuthorizationService)
GetAuthorizationService()
ServiceScopeFactory(Microsoft.Extensions.DependencyInjection.IServiceScopeFactory)
ApplyAuthorizationPoliciesAndManager(CoreWCF.Dispatcher.DispatchRuntime)
CopyAuthorizationPoliciesAndManager(CoreWCF.Description.ServiceAuthorizationBehavior)
CoreWCF.Description.IServiceBehavior.Validate(CoreWCF.Description.ServiceDescription,CoreWCF.ServiceHostBase)
CoreWCF.Description.IServiceBehavior.AddBindingParameters(CoreWCF.Description.ServiceDescription,CoreWCF.ServiceHostBase,System.Collections.ObjectModel.Collection`1<CoreWCF.Description.ServiceEndpoint>,CoreWCF.Channels.BindingParameterCollection)
CoreWCF.Description.IServiceBehavior.ApplyDispatchBehavior(CoreWCF.Description.ServiceDescription,CoreWCF.ServiceHostBase)
MakeReadOnly()
Clone()
ThrowIfImmutable()
Dispose()