2009-05-22 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / SecurityChannelListener.cs
1 //
2 // SecurityChannelListener.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System.Collections.Generic;
29 using System.Collections.ObjectModel;
30 using System.Net.Security;
31 using System.IdentityModel.Selectors;
32 using System.IdentityModel.Tokens;
33 using System.Security.Cryptography.X509Certificates;
34 using System.ServiceModel;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Security;
37 using System.ServiceModel.Security.Tokens;
38
39 namespace System.ServiceModel.Channels
40 {
41         internal class SecurityChannelListener<TChannel> : ChannelListenerBase<TChannel>
42                   where TChannel : class, IChannel
43         {
44                 IChannelListener<TChannel> inner;
45                 RecipientMessageSecurityBindingSupport security;
46
47                 public SecurityChannelListener (
48                         IChannelListener<TChannel> innerListener, 
49                         RecipientMessageSecurityBindingSupport security)
50                 {
51                         inner = innerListener;
52                         this.security = security;
53                 }
54
55                 public RecipientMessageSecurityBindingSupport SecuritySupport {
56                         get { return security; }
57                 }
58
59                 public override T GetProperty<T> ()
60                 {
61                         if (typeof (T) == typeof (MessageSecurityBindingSupport))
62                                 return (T) (object) security;
63                         return base.GetProperty<T> ();
64                 }
65
66                 TChannel CreateSecurityWrapper (TChannel src)
67                 {
68                         if (typeof (TChannel) == typeof (IReplyChannel))
69                                 return (TChannel) (object) new SecurityReplyChannel ((SecurityChannelListener<IReplyChannel>) (object) this, (IReplyChannel) (object) src);
70                         throw new NotImplementedException ();
71                 }
72
73                 void AcquireTokens ()
74                 {
75                         security.Prepare (this);
76                 }
77
78                 void ReleaseTokens ()
79                 {
80                         security.Release ();
81                 }
82
83                 // ChannelListenerBase
84
85                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
86                 {
87                         return CreateSecurityWrapper (inner.AcceptChannel (timeout));
88                 }
89
90                 protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
91                 {
92                         return inner.BeginAcceptChannel (timeout, callback, state);
93                 }
94
95                 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
96                 {
97                         return CreateSecurityWrapper (inner.EndAcceptChannel (result));
98                 }
99
100                 protected override bool OnWaitForChannel (TimeSpan timeout)
101                 {
102                         return inner.WaitForChannel (timeout);
103                 }
104
105                 protected override IAsyncResult OnBeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
106                 {
107                         return inner.BeginWaitForChannel (timeout, callback, state);
108                 }
109
110                 protected override bool OnEndWaitForChannel (IAsyncResult result)
111                 {
112                         return inner.EndWaitForChannel (result);
113                 }
114
115                 public override Uri Uri {
116                         get { return inner.Uri; }
117                 }
118
119                 // CommunicationObject
120                 protected override void OnAbort ()
121                 {
122                         inner.Abort ();
123                 }
124
125                 protected override void OnClose (TimeSpan timeout)
126                 {
127                         ReleaseTokens ();
128                         inner.Close (timeout);
129                 }
130
131                 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
132                 {
133                         ReleaseTokens ();
134                         return inner.BeginClose (timeout, callback, state);
135                 }
136
137                 protected override void OnEndClose (IAsyncResult result)
138                 {
139                         inner.EndClose (result);
140                 }
141
142                 protected override void OnOpen (TimeSpan timeout)
143                 {
144                         AcquireTokens ();
145                         inner.Open (timeout);
146                 }
147
148                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
149                 {
150                         AcquireTokens ();
151                         return inner.BeginOpen (timeout, callback, state);
152                 }
153
154                 protected override void OnEndOpen (IAsyncResult result)
155                 {
156                         inner.EndOpen (result);
157                 }
158         }
159
160         internal class SecurityReplyChannel : LayeredReplyChannel
161         {
162                 SecurityChannelListener<IReplyChannel> source;
163
164                 public SecurityReplyChannel (
165                         SecurityChannelListener<IReplyChannel> source,
166                         IReplyChannel innerChannel)
167                         : base (innerChannel)
168                 {
169                         this.source = source;
170                 }
171
172                 public override ChannelListenerBase Listener {
173                         get { return source; }
174                 }
175
176                 public SecurityChannelListener<IReplyChannel> Source {
177                         get { return source; }
178                 }
179
180                 // IReplyChannel
181
182                 public override IAsyncResult BeginReceiveRequest (
183                         TimeSpan timeout, AsyncCallback callback, object state)
184                 {
185                         throw new NotImplementedException ();
186                 }
187
188                 public override IAsyncResult BeginTryReceiveRequest (
189                         TimeSpan timeout, AsyncCallback callback, object state)
190                 {
191                         throw new NotImplementedException ();
192                 }
193
194                 public override RequestContext EndReceiveRequest (IAsyncResult result)
195                 {
196                         throw new NotImplementedException ();
197                 }
198
199                 public override bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
200                 {
201                         throw new NotImplementedException ();
202                 }
203
204                 public override RequestContext  ReceiveRequest (TimeSpan timeout)
205                 {
206                         return new SecurityRequestContext (this, base.ReceiveRequest (timeout));
207                 }
208
209                 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
210                 {
211                         if (!base.TryReceiveRequest (timeout, out context))
212                                 return false;
213                         context = new SecurityRequestContext (this, context);
214                         return true;
215                 }
216
217                 // IChannel
218
219                 public override T GetProperty<T> ()
220                 {
221                         // FIXME: implement
222                         return base.GetProperty<T> ();
223                 }
224         }
225 }