2
0

SecurityReplyChannel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // SecurityReplyChannel.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006,2010 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.Channels;
  36. using System.ServiceModel.Description;
  37. using System.ServiceModel.Security;
  38. using System.ServiceModel.Security.Tokens;
  39. namespace System.ServiceModel.Channels.Security
  40. {
  41. internal class SecurityReplyChannel : InternalReplyChannelBase
  42. {
  43. IReplyChannel inner;
  44. public SecurityReplyChannel (
  45. SecurityChannelListener<IReplyChannel> source,
  46. IReplyChannel innerChannel)
  47. : base (source)
  48. {
  49. this.inner = innerChannel;
  50. }
  51. public SecurityChannelListener<IReplyChannel> Source {
  52. get { return (SecurityChannelListener<IReplyChannel>) Listener; }
  53. }
  54. // IReplyChannel
  55. protected override void OnOpen (TimeSpan timeout)
  56. {
  57. inner.Open (timeout);
  58. }
  59. protected override void OnClose (TimeSpan timeout)
  60. {
  61. inner.Close (timeout);
  62. }
  63. public override RequestContext ReceiveRequest (TimeSpan timeout)
  64. {
  65. RequestContext ctx;
  66. if (TryReceiveRequest (timeout, out ctx))
  67. return ctx;
  68. throw new TimeoutException ("Failed to receive request context");
  69. }
  70. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  71. {
  72. DateTime start = DateTime.Now;
  73. if (!inner.TryReceiveRequest (timeout, out context))
  74. return false;
  75. if (context == null)
  76. return true;
  77. Message req, res;
  78. req = context.RequestMessage;
  79. switch (req.Headers.Action) {
  80. case Constants.WstIssueAction:
  81. case Constants.WstIssueReplyAction:
  82. case Constants.WstRenewAction:
  83. case Constants.WstCancelAction:
  84. case Constants.WstValidateAction:
  85. var support = Source.SecuritySupport;
  86. var commAuth = support.TokenAuthenticator as CommunicationSecurityTokenAuthenticator;
  87. if (commAuth != null)
  88. res = commAuth.Communication.ProcessNegotiation (req, timeout - (DateTime.Now - start));
  89. else
  90. throw new MessageSecurityException ("This reply channel does not expect incoming WS-Trust requests");
  91. context.Reply (res, timeout - (DateTime.Now - start));
  92. context.Close (timeout - (DateTime.Now - start));
  93. // wait for another incoming message
  94. return TryReceiveRequest (timeout - (DateTime.Now - start), out context);
  95. break;
  96. }
  97. context = new SecurityRequestContext (this, context);
  98. return true;
  99. }
  100. [MonoTODO]
  101. public override bool WaitForRequest (TimeSpan timeout)
  102. {
  103. throw new NotImplementedException ();
  104. }
  105. // IChannel
  106. public override T GetProperty<T> ()
  107. {
  108. // FIXME: implement
  109. return inner.GetProperty<T> ();
  110. }
  111. }
  112. }