| | | 1 | | using System; |
| | | 2 | | using System.ServiceModel.Channels; |
| | | 3 | | using System.ServiceModel.Security; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using CoreWCF.Runtime; |
| | | 6 | | using System.IdentityModel.Selectors; |
| | | 7 | | using System.IdentityModel.Tokens; |
| | | 8 | | using System.Net.Http; |
| | | 9 | | using ClientCredentials = System.ServiceModel.Description.ClientCredentials; |
| | | 10 | | using System.Net.Security; |
| | | 11 | | using System.Security.Cryptography.X509Certificates; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.ServiceModel.Channels |
| | | 14 | | { |
| | | 15 | | internal class RabbitMqChannelFactory : ChannelFactoryBase<IOutputChannel> |
| | | 16 | | { |
| | | 17 | | private SecurityCredentialsManager _channelCredentials; |
| | | 18 | | private SecurityTokenManager _securityTokenManager; |
| | | 19 | | private X509CertificateValidator _sslCertificateValidator; |
| | | 20 | | private Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> _remoteCertificateValidatio |
| | | 21 | | |
| | | 22 | | internal RabbitMqChannelFactory(RabbitMqTransportBindingElement transport, BindingContext context) |
| | 3 | 23 | | : base(context.Binding) |
| | | 24 | | { |
| | 3 | 25 | | Transport = transport; |
| | 3 | 26 | | BufferManager = BufferManager.CreateBufferManager(transport.MaxBufferPoolSize, int.MaxValue); |
| | 3 | 27 | | InitializeSecurityTokenManager(context); |
| | | 28 | | |
| | | 29 | | // TODO: Uncomment when .GetCertificateValidator() becomes available (currently internal to System.ServiceMo |
| | | 30 | | //ClientCredentials credentials = context.BindingParameters.Find<ClientCredentials>(); |
| | | 31 | | //if (credentials != null && credentials.ServiceCertificate.SslCertificateAuthentication != null) |
| | | 32 | | //{ |
| | | 33 | | // _sslCertificateValidator = credentials.ServiceCertificate.SslCertificateAuthentication.GetCertificateV |
| | | 34 | | // _remoteCertificateValidationCallback = RemoteCertificateValidationCallback; |
| | | 35 | | //} |
| | | 36 | | |
| | 3 | 37 | | var messageEncoderBindingElement = context.BindingParameters.Find<MessageEncodingBindingElement>(); |
| | 3 | 38 | | MessageEncoderFactory = messageEncoderBindingElement == null |
| | 3 | 39 | | ? RabbitMqDefaults.DefaultMessageEncoderFactory |
| | 3 | 40 | | : messageEncoderBindingElement.CreateMessageEncoderFactory(); |
| | 3 | 41 | | } |
| | | 42 | | |
| | | 43 | | private void InitializeSecurityTokenManager(BindingContext context) |
| | | 44 | | { |
| | 3 | 45 | | _channelCredentials = context.BindingParameters.Find<SecurityCredentialsManager>(); |
| | 3 | 46 | | _channelCredentials ??= new ClientCredentials(); |
| | 3 | 47 | | _securityTokenManager = _channelCredentials.CreateSecurityTokenManager(); |
| | 3 | 48 | | } |
| | | 49 | | |
| | 6 | 50 | | public RabbitMqTransportBindingElement Transport { get; } |
| | | 51 | | |
| | 6 | 52 | | public BufferManager BufferManager { get; } |
| | | 53 | | |
| | 6 | 54 | | public MessageEncoderFactory MessageEncoderFactory { get; } |
| | | 55 | | |
| | | 56 | | public override T GetProperty<T>() |
| | | 57 | | { |
| | 3 | 58 | | T messageEncoderProperty = MessageEncoderFactory.Encoder.GetProperty<T>(); |
| | 3 | 59 | | if (messageEncoderProperty != null) |
| | | 60 | | { |
| | 0 | 61 | | return messageEncoderProperty; |
| | | 62 | | } |
| | | 63 | | |
| | 3 | 64 | | if (typeof(T) == typeof(MessageVersion)) |
| | | 65 | | { |
| | 0 | 66 | | return (T)(object)MessageEncoderFactory.Encoder.MessageVersion; |
| | | 67 | | } |
| | | 68 | | |
| | 3 | 69 | | return base.GetProperty<T>(); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | protected override void OnOpen(TimeSpan timeout) |
| | | 73 | | { |
| | 3 | 74 | | } |
| | | 75 | | |
| | | 76 | | protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) |
| | | 77 | | { |
| | 0 | 78 | | return Task.CompletedTask.ToApm(callback, state); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | protected override void OnEndOpen(IAsyncResult result) |
| | | 82 | | { |
| | 0 | 83 | | result.ToApmEnd(); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | protected override IOutputChannel OnCreateChannel(System.ServiceModel.EndpointAddress endpointAddress, Uri via) |
| | | 87 | | { |
| | 3 | 88 | | return new RabbitMqOutputChannel( |
| | 3 | 89 | | this, |
| | 3 | 90 | | endpointAddress, |
| | 3 | 91 | | via, |
| | 3 | 92 | | Transport, |
| | 3 | 93 | | _securityTokenManager, |
| | 3 | 94 | | MessageEncoderFactory.Encoder); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | protected override void OnClosed() |
| | | 98 | | { |
| | 0 | 99 | | base.OnClosed(); |
| | 0 | 100 | | BufferManager.Clear(); |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | private bool RemoteCertificateValidationCallback(HttpRequestMessage sender, X509Certificate2 certificate, X509Ch |
| | | 104 | | { |
| | 0 | 105 | | if (_sslCertificateValidator == null) |
| | | 106 | | { |
| | 0 | 107 | | throw new Exception(SR.SslCertificateValidatorIsNull); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | try |
| | | 111 | | { |
| | 0 | 112 | | _sslCertificateValidator.Validate(certificate); |
| | 0 | 113 | | return true; |
| | | 114 | | } |
| | 0 | 115 | | catch (SecurityTokenValidationException ex) |
| | | 116 | | { |
| | 0 | 117 | | return false; |
| | | 118 | | } |
| | 0 | 119 | | } |
| | | 120 | | } |
| | | 121 | | } |