| | | 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.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF |
| | | 9 | | { |
| | | 10 | | public sealed class BasicHttpSecurity |
| | | 11 | | { |
| | | 12 | | internal const BasicHttpSecurityMode DefaultMode = BasicHttpSecurityMode.None; |
| | | 13 | | private BasicHttpSecurityMode _mode; |
| | | 14 | | private HttpTransportSecurity _transportSecurity; |
| | | 15 | | private BasicHttpMessageSecurity _messageSecurity; |
| | | 16 | | |
| | | 17 | | public BasicHttpSecurity() |
| | 427 | 18 | | : this(DefaultMode, new HttpTransportSecurity(), new BasicHttpMessageSecurity()) |
| | | 19 | | { |
| | 427 | 20 | | } |
| | | 21 | | |
| | 427 | 22 | | private BasicHttpSecurity(BasicHttpSecurityMode mode, HttpTransportSecurity transportSecurity, BasicHttpMessageS |
| | | 23 | | { |
| | | 24 | | Fx.Assert(BasicHttpSecurityModeHelper.IsDefined(mode), string.Format("Invalid BasicHttpSecurityMode value: { |
| | 427 | 25 | | if (mode == BasicHttpSecurityMode.Message ) // || mode == BasicHttpSecurityMode.TransportWithMessageCredenti |
| | | 26 | | { |
| | 0 | 27 | | throw new PlatformNotSupportedException($"{nameof(BasicHttpSecurityMode.Message)}"); |
| | | 28 | | } |
| | | 29 | | |
| | 427 | 30 | | Mode = mode; |
| | 427 | 31 | | _transportSecurity = transportSecurity ?? new HttpTransportSecurity(); |
| | 427 | 32 | | _messageSecurity = messageSecurity == null ? new BasicHttpMessageSecurity() : messageSecurity; |
| | 427 | 33 | | } |
| | | 34 | | |
| | | 35 | | public BasicHttpSecurityMode Mode |
| | | 36 | | { |
| | 5054 | 37 | | get => _mode; |
| | | 38 | | set |
| | | 39 | | { |
| | 860 | 40 | | if (!BasicHttpSecurityModeHelper.IsDefined(value)) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 43 | | } |
| | | 44 | | |
| | 860 | 45 | | _mode = value; |
| | 860 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public HttpTransportSecurity Transport |
| | | 50 | | { |
| | 9892 | 51 | | get => _transportSecurity; |
| | 84 | 52 | | set => _transportSecurity = value ?? new HttpTransportSecurity(); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public BasicHttpMessageSecurity Message |
| | | 56 | | { |
| | 13 | 57 | | get => _messageSecurity; |
| | 0 | 58 | | set => _messageSecurity = value ?? new BasicHttpMessageSecurity(); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | internal SecurityBindingElement CreateMessageSecurity() |
| | | 62 | | { |
| | 3511 | 63 | | if (_mode == BasicHttpSecurityMode.TransportWithMessageCredential) |
| | | 64 | | { |
| | 70 | 65 | | return _messageSecurity.CreateMessageSecurity(Mode == BasicHttpSecurityMode.TransportWithMessageCredenti |
| | | 66 | | } |
| | 3441 | 67 | | else if(_mode == BasicHttpSecurityMode.Message) |
| | | 68 | | { |
| | 0 | 69 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Unsuppo |
| | | 70 | | } |
| | | 71 | | else |
| | | 72 | | { |
| | 3441 | 73 | | return null; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | internal void EnableTransportSecurity(HttpsTransportBindingElement https) |
| | | 78 | | { |
| | 533 | 79 | | if (_mode == BasicHttpSecurityMode.TransportWithMessageCredential) |
| | | 80 | | { |
| | 88 | 81 | | _transportSecurity.ConfigureTransportProtectionOnly(https); |
| | | 82 | | } |
| | | 83 | | else |
| | | 84 | | { |
| | 445 | 85 | | _transportSecurity.ConfigureTransportProtectionAndAuthentication(https); |
| | | 86 | | } |
| | 445 | 87 | | } |
| | | 88 | | |
| | | 89 | | internal void EnableTransportAuthentication(HttpTransportBindingElement http) |
| | | 90 | | { |
| | 974 | 91 | | _transportSecurity.ConfigureTransportAuthentication(http); |
| | 974 | 92 | | } |
| | | 93 | | |
| | | 94 | | internal void DisableTransportAuthentication(HttpTransportBindingElement http) |
| | | 95 | | { |
| | 3472 | 96 | | _transportSecurity.DisableTransportAuthentication(http); |
| | 3472 | 97 | | } |
| | | 98 | | |
| | | 99 | | internal void ApplyAuthorizationPolicySupport(HttpTransportBindingElement httpTransport) |
| | | 100 | | { |
| | 4979 | 101 | | httpTransport.AlwaysUseAuthorizationPolicySupport = |
| | 4979 | 102 | | Transport.AlwaysUseAuthorizationPolicySupport |
| | 4979 | 103 | | || Transport.ClientCredentialType == HttpClientCredentialType.InheritedFromHost; |
| | 4979 | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |