| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using CoreWCF.Channels; |
| | | 6 | | using CoreWCF.Dispatcher; |
| | | 7 | | |
| | | 8 | | namespace 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 | | |
| | 0 | 18 | | internal ImpersonateOnSerializingReplyMessageProperty(MessageRpc rpc) |
| | | 19 | | { |
| | 0 | 20 | | _rpc = rpc; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the name of the message property. |
| | | 25 | | /// </summary> |
| | | 26 | | public static string Name |
| | | 27 | | { |
| | 0 | 28 | | 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 | | { |
| | 0 | 39 | | if (message == null) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | 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 | | { |
| | 0 | 55 | | if (properties == null) |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties)); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | if (properties.TryGetValue(PropertyName, out object value)) |
| | | 61 | | { |
| | 0 | 62 | | property = value as ImpersonateOnSerializingReplyMessageProperty; |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | 0 | 66 | | property = null; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | 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 | | { |
| | 0 | 78 | | 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 | | { |
| | 0 | 88 | | if (OperationContext.Current != null) |
| | | 89 | | { |
| | 0 | 90 | | EndpointDispatcher endpointDispatcher = OperationContext.Current.EndpointDispatcher; |
| | 0 | 91 | | if (endpointDispatcher != null) |
| | | 92 | | { |
| | 0 | 93 | | DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime; |
| | 0 | 94 | | ImmutableDispatchRuntime runtime = dispatchRuntime.GetRuntime(); |
| | 0 | 95 | | if (runtime?.SecurityImpersonation?.IsSecurityContextImpersonationRequired(_rpc) ?? false) |
| | | 96 | | { |
| | 0 | 97 | | return runtime.SecurityImpersonation.RunImpersonated(_rpc, func); |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | return func(); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |