| | | 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 System.ComponentModel; |
| | | 6 | | using CoreWCF.Channels; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF |
| | | 9 | | { |
| | | 10 | | public sealed class WSHTTPSecurity |
| | | 11 | | { |
| | | 12 | | internal const SecurityMode DefaultMode = SecurityMode.Message; |
| | | 13 | | private SecurityMode _mode; |
| | | 14 | | private HttpTransportSecurity _transportSecurity; |
| | | 15 | | private NonDualMessageSecurityOverHttp _messageSecurity; |
| | | 16 | | |
| | | 17 | | public WSHTTPSecurity() |
| | 45 | 18 | | : this(DefaultMode, GetDefaultHttpTransportSecurity(), new NonDualMessageSecurityOverHttp()) |
| | | 19 | | { |
| | 45 | 20 | | } |
| | | 21 | | |
| | 45 | 22 | | internal WSHTTPSecurity(SecurityMode mode, HttpTransportSecurity transportSecurity, NonDualMessageSecurityOverHt |
| | | 23 | | { |
| | 45 | 24 | | _mode = mode; |
| | 45 | 25 | | _transportSecurity = transportSecurity ?? GetDefaultHttpTransportSecurity(); |
| | 45 | 26 | | _messageSecurity = messageSecurity ?? new NonDualMessageSecurityOverHttp(); |
| | 45 | 27 | | } |
| | | 28 | | |
| | | 29 | | internal static HttpTransportSecurity GetDefaultHttpTransportSecurity() |
| | | 30 | | { |
| | 45 | 31 | | HttpTransportSecurity transportSecurity = new HttpTransportSecurity |
| | 45 | 32 | | { |
| | 45 | 33 | | ClientCredentialType = HttpClientCredentialType.Windows |
| | 45 | 34 | | }; |
| | 45 | 35 | | return transportSecurity; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public SecurityMode Mode |
| | | 39 | | { |
| | 697 | 40 | | get { return _mode; } |
| | | 41 | | set |
| | | 42 | | { |
| | 41 | 43 | | if (!SecurityModeHelper.IsDefined(value)) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 46 | | } |
| | 41 | 47 | | _mode = value; |
| | 41 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public HttpTransportSecurity Transport |
| | | 52 | | { |
| | 610 | 53 | | get { return _transportSecurity; } |
| | | 54 | | set |
| | | 55 | | { |
| | 6 | 56 | | _transportSecurity = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNull |
| | 6 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public NonDualMessageSecurityOverHttp Message |
| | | 61 | | { |
| | 34 | 62 | | get { return _messageSecurity; } |
| | | 63 | | set |
| | | 64 | | { |
| | 0 | 65 | | _messageSecurity = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullEx |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | internal void ApplyTransportSecurity(HttpsTransportBindingElement https) |
| | | 70 | | { |
| | 224 | 71 | | if (_mode == SecurityMode.TransportWithMessageCredential) |
| | | 72 | | { |
| | 130 | 73 | | _transportSecurity.ConfigureTransportProtectionOnly(https); |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | 94 | 77 | | _transportSecurity.ConfigureTransportProtectionAndAuthentication(https); |
| | | 78 | | } |
| | 94 | 79 | | } |
| | | 80 | | |
| | | 81 | | internal static void ApplyTransportSecurity(HttpsTransportBindingElement transport, HttpTransportSecurity transp |
| | | 82 | | { |
| | 0 | 83 | | HttpTransportSecurity.ConfigureTransportProtectionAndAuthentication(transport, transportSecurity); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | internal SecurityBindingElement CreateMessageSecurity(bool isReliableSessionEnabled, MessageSecurityVersion vers |
| | | 87 | | { |
| | 293 | 88 | | if (_mode == SecurityMode.Message || _mode == SecurityMode.TransportWithMessageCredential) |
| | | 89 | | { |
| | 87 | 90 | | return _messageSecurity.CreateSecurityBindingElement(Mode == SecurityMode.TransportWithMessageCredential |
| | | 91 | | } |
| | | 92 | | else |
| | | 93 | | { |
| | 206 | 94 | | return null; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | //internal static bool TryCreate(SecurityBindingElement sbe, UnifiedSecurityMode mode, HttpTransportSecurity tra |
| | | 99 | | //{ |
| | | 100 | | // security = null; |
| | | 101 | | // NonDualMessageSecurityOverHttp messageSecurity = null; |
| | | 102 | | // SecurityMode securityMode = SecurityMode.None; |
| | | 103 | | // if (sbe != null) |
| | | 104 | | // { |
| | | 105 | | // mode &= UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential; |
| | | 106 | | // securityMode = SecurityModeHelper.ToSecurityMode(mode); |
| | | 107 | | // Fx.Assert(SecurityModeHelper.IsDefined(securityMode), string.Format("Invalid SecurityMode value: {0}." |
| | | 108 | | // if (!MessageSecurityOverHttp.TryCreate(sbe, securityMode == SecurityMode.TransportWithMessageCredentia |
| | | 109 | | // { |
| | | 110 | | // return false; |
| | | 111 | | // } |
| | | 112 | | // } |
| | | 113 | | // else |
| | | 114 | | // { |
| | | 115 | | // mode &= ~(UnifiedSecurityMode.Message | UnifiedSecurityMode.TransportWithMessageCredential); |
| | | 116 | | // securityMode = SecurityModeHelper.ToSecurityMode(mode); |
| | | 117 | | // } |
| | | 118 | | // Fx.Assert(SecurityModeHelper.IsDefined(securityMode), string.Format("Invalid SecurityMode value: {0}.", se |
| | | 119 | | // security = new WSHttpSecurity(securityMode, transportSecurity, messageSecurity); |
| | | 120 | | // return true; |
| | | 121 | | //} |
| | | 122 | | |
| | | 123 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 124 | | public bool ShouldSerializeMode() |
| | | 125 | | { |
| | 0 | 126 | | return Mode != DefaultMode; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | internal void ApplyAuthorizationPolicySupport(HttpTransportBindingElement httpTransport) |
| | | 130 | | { |
| | 224 | 131 | | httpTransport.AlwaysUseAuthorizationPolicySupport = |
| | 224 | 132 | | Transport.AlwaysUseAuthorizationPolicySupport |
| | 224 | 133 | | || Transport.ClientCredentialType == HttpClientCredentialType.InheritedFromHost; |
| | 224 | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |