Some 4.0 API tweaks
[mono.git] / mcs / class / System.ServiceModel / old-code / SecurityHandler.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ServiceModel.Channels;
5 using System.ServiceModel.Channels.Security;
6 using System.ServiceModel;
7 using System.ServiceModel.Security.Tokens;
8
9 namespace System.ServiceModel.Dispatcher
10 {
11         internal class SecurityHandler : BaseRequestProcessorHandler
12         {
13                 protected override bool ProcessRequest (MessageProcessingContext mrc)
14                 {
15                         DispatchRuntime dispatch_runtime = mrc.OperationContext.EndpointDispatcher.DispatchRuntime;
16
17                         // FIXME: I doubt this should be done at this "handler" 
18                         // layer, especially considering about non-ServiceHost
19                         // use of SecurityBindingElement + listener.
20                         //
21                         // For example there is no way to handle it in duplex
22                         // dispatch callbacks.
23                         if (dispatch_runtime.ChannelDispatcher == null)
24                                 return false;
25
26                         Message negoResponce = null;
27                         // process WS-Trust based negotiation
28                         MessageSecurityBindingSupport support =
29                                 dispatch_runtime.ChannelDispatcher.Listener.GetProperty<MessageSecurityBindingSupport> ();
30                         if (support != null && mrc.IncomingMessage.Headers.FindHeader ("Security", Constants.WssNamespace) < 0) {
31                                 CommunicationSecurityTokenAuthenticator nego =
32                                         support.TokenAuthenticator as CommunicationSecurityTokenAuthenticator;
33                                 if (nego != null)
34                                         negoResponce = nego.Communication.ProcessNegotiation (mrc.IncomingMessage);
35                         }
36
37                         if (negoResponce == null)
38                                 return false;
39                         
40                         ReplyNegoResponse (mrc, negoResponce);
41                         return true;
42
43                 }
44
45                 void ReplyNegoResponse (MessageProcessingContext mrc, Message negoResponse)
46                 {
47                         negoResponse.Headers.CopyHeadersFrom (mrc.OperationContext.OutgoingMessageHeaders);
48                         negoResponse.Properties.CopyProperties (mrc.OperationContext.OutgoingMessageProperties);                        
49                         mrc.RequestContext.Reply (negoResponse, mrc.Operation.Parent.ChannelDispatcher.timeouts.SendTimeout);
50                         return;
51                 }
52         }
53 }