< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.ImpersonateOnSerializingReplyMessageProperty
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/ImpersonateOnSerializingReplyMessageProperty.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 105
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(...)100%110%
TryGet(...)0%220%
TryGet(...)0%440%
CreateCopy()100%110%
RunImpersonated(...)0%10100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/ImpersonateOnSerializingReplyMessageProperty.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 CoreWCF.Channels;
 6using CoreWCF.Dispatcher;
 7
 8namespace CoreWCF.Security
 9{
 10    /// <summary>
 11    /// The helper class to enable impersonation while serializing the body of the reply message.
 12    /// </summary>
 13    public class ImpersonateOnSerializingReplyMessageProperty : IMessageProperty
 14    {
 15        private const string PropertyName = "ImpersonateOnSerializingReplyMessageProperty";
 16        private readonly MessageRpc _rpc;
 17
 018        internal ImpersonateOnSerializingReplyMessageProperty(MessageRpc rpc)
 19        {
 020            _rpc = rpc;
 021        }
 22
 23        /// <summary>
 24        /// Gets the name of the message property.
 25        /// </summary>
 26        public static string Name
 27        {
 028            get { return PropertyName; }
 29        }
 30
 31        /// <summary>
 32        /// Gets the ImpersonateOnSerializingReplyMessageProperty property from a message.
 33        /// </summary>
 34        /// <param name="message">The message to extract the property from.</param>
 35        /// <param name="property">An output paramter to hold the ImpersonateOnSerializingReplyMessageProperty property.
 36        /// <returns>True if the ImpersonateOnSerializingReplyMessageProperty property was found.</returns>
 37        public static bool TryGet(Message message, out ImpersonateOnSerializingReplyMessageProperty property)
 38        {
 039            if (message == null)
 40            {
 041                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
 42            }
 43
 044            return TryGet(message.Properties, out property);
 45        }
 46
 47        /// <summary>
 48        /// Gets the ImpersonateOnSerializingReplyMessageProperty property from MessageProperties.
 49        /// </summary>
 50        /// <param name="properties">The MessagePropeties object.</param>
 51        /// <param name="property">An output paramter to hold the ImpersonateOnSerializingReplyMessageProperty property.
 52        /// <returns>True if the ImpersonateOnSerializingReplyMessageProperty property was found.</returns>
 53        public static bool TryGet(MessageProperties properties, out ImpersonateOnSerializingReplyMessageProperty propert
 54        {
 055            if (properties == null)
 56            {
 057                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties));
 58            }
 59
 060            if (properties.TryGetValue(PropertyName, out object value))
 61            {
 062                property = value as ImpersonateOnSerializingReplyMessageProperty;
 63            }
 64            else
 65            {
 066                property = null;
 67            }
 68
 069            return property != null;
 70        }
 71
 72        /// <summary>
 73        /// Creates a copy of the message property.
 74        /// </summary>
 75        /// <returns>Returns a copy of the message property.</returns>
 76        public IMessageProperty CreateCopy()
 77        {
 078            return new ImpersonateOnSerializingReplyMessageProperty(_rpc);
 79        }
 80
 81        /// <summary>
 82        /// Executes a Func<typeparamref name="T"/> with the caller's context if impersonation is enabled on the service
 83        /// </summary>
 84        /// <param name="func">The function to execute under caller's impersonated context</param>
 85        /// <returns>The return value from executing the func</returns>
 86        public T RunImpersonated<T>(Func<T> func)
 87        {
 088            if (OperationContext.Current != null)
 89            {
 090                EndpointDispatcher endpointDispatcher = OperationContext.Current.EndpointDispatcher;
 091                if (endpointDispatcher != null)
 92                {
 093                    DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
 094                    ImmutableDispatchRuntime runtime = dispatchRuntime.GetRuntime();
 095                    if (runtime?.SecurityImpersonation?.IsSecurityContextImpersonationRequired(_rpc) ?? false)
 96                    {
 097                        return runtime.SecurityImpersonation.RunImpersonated(_rpc, func);
 98                    }
 99                }
 100            }
 101
 0102            return func();
 103        }
 104    }
 105}