summaryrefslogtreecommitdiff
path: root/dotnet/Qpid.Client/Client/Handler
diff options
context:
space:
mode:
authorRobert Greig <rgreig@apache.org>2007-01-29 10:46:27 +0000
committerRobert Greig <rgreig@apache.org>2007-01-29 10:46:27 +0000
commit2bcc371558ce0659f53b86046cdf3d5de3b20910 (patch)
treed0c987cfa076eb90edb80620661d69a6e7354d3a /dotnet/Qpid.Client/Client/Handler
parentfe736211136b60bec61c1a22d3765be9142c6b39 (diff)
downloadqpid-python-2bcc371558ce0659f53b86046cdf3d5de3b20910.tar.gz
(Patch supplied by Tomas Restrepo) QPID-291-2.diff applied. Adds SASL capability to the .Net client.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@501001 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'dotnet/Qpid.Client/Client/Handler')
-rw-r--r--dotnet/Qpid.Client/Client/Handler/ChannelCloseMethodHandler.cs5
-rw-r--r--dotnet/Qpid.Client/Client/Handler/ConnectionCloseMethodHandler.cs25
-rw-r--r--dotnet/Qpid.Client/Client/Handler/ConnectionSecureMethodHandler.cs27
-rw-r--r--dotnet/Qpid.Client/Client/Handler/ConnectionStartMethodHandler.cs81
4 files changed, 107 insertions, 31 deletions
diff --git a/dotnet/Qpid.Client/Client/Handler/ChannelCloseMethodHandler.cs b/dotnet/Qpid.Client/Client/Handler/ChannelCloseMethodHandler.cs
index 1031f804a6..0ce8a393c9 100644
--- a/dotnet/Qpid.Client/Client/Handler/ChannelCloseMethodHandler.cs
+++ b/dotnet/Qpid.Client/Client/Handler/ChannelCloseMethodHandler.cs
@@ -21,6 +21,7 @@
using log4net;
using Qpid.Client.Protocol;
using Qpid.Client.State;
+using Qpid.Protocol;
using Qpid.Framing;
namespace Qpid.Client.Handler
@@ -43,9 +44,8 @@ namespace Qpid.Client.Handler
AMQFrame frame = ChannelCloseOkBody.CreateAMQFrame(evt.ChannelId);
evt.ProtocolSession.WriteFrame(frame);
- //if (errorCode != AMQConstant.REPLY_SUCCESS.getCode())
// HACK
- if (errorCode != 200)
+ if ( errorCode != AMQConstant.REPLY_SUCCESS.Code )
{
_logger.Debug("Channel close received with errorCode " + errorCode + ", throwing exception");
evt.ProtocolSession.AMQConnection.ExceptionReceived(new AMQChannelClosedException(errorCode, "Error: " + reason));
@@ -55,3 +55,4 @@ namespace Qpid.Client.Handler
}
}
+
diff --git a/dotnet/Qpid.Client/Client/Handler/ConnectionCloseMethodHandler.cs b/dotnet/Qpid.Client/Client/Handler/ConnectionCloseMethodHandler.cs
index c3acc0b098..dea5316d25 100644
--- a/dotnet/Qpid.Client/Client/Handler/ConnectionCloseMethodHandler.cs
+++ b/dotnet/Qpid.Client/Client/Handler/ConnectionCloseMethodHandler.cs
@@ -19,10 +19,12 @@
*
*/
using System;
+using System.Threading;
using log4net;
using Qpid.Client.Protocol;
using Qpid.Client.State;
using Qpid.Framing;
+using Qpid.Protocol;
namespace Qpid.Client.Handler
{
@@ -38,16 +40,29 @@ namespace Qpid.Client.Handler
int errorCode = method.ReplyCode;
String reason = method.ReplyText;
+ // send CloseOK
evt.ProtocolSession.WriteFrame(ConnectionCloseOkBody.CreateAMQFrame(evt.ChannelId));
- stateManager.ChangeState(AMQState.CONNECTION_CLOSED);
- if (errorCode != 200)
+
+ if ( errorCode != AMQConstant.REPLY_SUCCESS.Code )
{
- _logger.Debug("Connection close received with error code " + errorCode);
- throw new AMQConnectionClosedException(errorCode, "Error: " + reason);
- }
+ if ( errorCode == AMQConstant.NOT_ALLOWED.Code )
+ {
+ _logger.Info("Authentication Error: " + Thread.CurrentThread.Name);
+ evt.ProtocolSession.CloseProtocolSession();
+
+ //todo this is a bit of a fudge (could be conssidered such as each new connection needs a new state manager or at least a fresh state.
+ stateManager.ChangeState(AMQState.CONNECTION_NOT_STARTED);
+ throw new AMQAuthenticationException(errorCode, reason);
+ } else
+ {
+ _logger.Info("Connection close received with error code " + errorCode);
+ throw new AMQConnectionClosedException(errorCode, "Error: " + reason);
+ }
+ }
// this actually closes the connection in the case where it is not an error.
evt.ProtocolSession.CloseProtocolSession();
+ stateManager.ChangeState(AMQState.CONNECTION_CLOSED);
}
}
}
diff --git a/dotnet/Qpid.Client/Client/Handler/ConnectionSecureMethodHandler.cs b/dotnet/Qpid.Client/Client/Handler/ConnectionSecureMethodHandler.cs
index 7c0fbd8f40..fe123e6745 100644
--- a/dotnet/Qpid.Client/Client/Handler/ConnectionSecureMethodHandler.cs
+++ b/dotnet/Qpid.Client/Client/Handler/ConnectionSecureMethodHandler.cs
@@ -21,6 +21,7 @@
using Qpid.Client.Protocol;
using Qpid.Client.State;
using Qpid.Framing;
+using Qpid.Sasl;
namespace Qpid.Client.Handler
{
@@ -28,9 +29,31 @@ namespace Qpid.Client.Handler
{
public void MethodReceived(AMQStateManager stateManager, AMQMethodEvent evt)
{
- AMQFrame response = ConnectionSecureOkBody.CreateAMQFrame(evt.ChannelId, null);
- evt.ProtocolSession.WriteFrame(response);
+ ISaslClient saslClient = evt.ProtocolSession.SaslClient;
+ if ( saslClient == null )
+ {
+ throw new AMQException("No SASL client set up - cannot proceed with authentication");
+ }
+
+
+ ConnectionSecureBody body = (ConnectionSecureBody)evt.Method;
+
+ try
+ {
+ // Evaluate server challenge
+ byte[] response = saslClient.EvaluateChallenge(body.Challenge);
+ // AMQP version change: Hardwire the version to 0-8 (major=8, minor=0)
+ // TODO: Connect this to the session version obtained from ProtocolInitiation for this session.
+ // Be aware of possible changes to parameter order as versions change.
+ AMQFrame responseFrame = ConnectionSecureOkBody.CreateAMQFrame(
+ evt.ChannelId, response);
+ evt.ProtocolSession.WriteFrame(responseFrame);
+ } catch ( SaslException e )
+ {
+ throw new AMQException("Error processing SASL challenge: " + e, e);
+ }
}
}
}
+
diff --git a/dotnet/Qpid.Client/Client/Handler/ConnectionStartMethodHandler.cs b/dotnet/Qpid.Client/Client/Handler/ConnectionStartMethodHandler.cs
index e88ff3ddbd..1815bea152 100644
--- a/dotnet/Qpid.Client/Client/Handler/ConnectionStartMethodHandler.cs
+++ b/dotnet/Qpid.Client/Client/Handler/ConnectionStartMethodHandler.cs
@@ -19,11 +19,15 @@
*
*/
using System;
+using System.Collections;
using System.Text;
using log4net;
using Qpid.Client.Protocol;
+using Qpid.Client.Security;
using Qpid.Client.State;
using Qpid.Framing;
+using Qpid.Sasl;
+
namespace Qpid.Client.Handler
{
@@ -35,36 +39,22 @@ namespace Qpid.Client.Handler
{
ConnectionStartBody body = (ConnectionStartBody) evt.Method;
AMQProtocolSession ps = evt.ProtocolSession;
- string username = ps.Username;
- string password = ps.Password;
try
{
- if (body.Mechanisms == null)
+ if ( body.Mechanisms == null )
{
throw new AMQException("mechanism not specified in ConnectionStart method frame");
}
- string allMechanisms = Encoding.ASCII.GetString(body.Mechanisms);
- string[] mechanisms = allMechanisms.Split(' ');
- string selectedMechanism = null;
- foreach (string mechanism in mechanisms)
- {
- if (mechanism.Equals("PLAIN"))
- {
- selectedMechanism = mechanism;
- break;
- }
- }
-
- if (selectedMechanism == null)
+ string mechanisms = Encoding.UTF8.GetString(body.Mechanisms);
+ string selectedMechanism = ChooseMechanism(mechanisms);
+ if ( selectedMechanism == null )
{
throw new AMQException("No supported security mechanism found, passed: " + mechanisms);
}
+
+ byte[] saslResponse = DoAuthentication(selectedMechanism, ps);
- // we always write out a null authzid which we don't currently use
- byte[] plainData = new byte[1 + ps.Username.Length + 1 + ps.Password.Length];
- Encoding.UTF8.GetBytes(username, 0, username.Length, plainData, 1);
- Encoding.UTF8.GetBytes(password, 0, password.Length, plainData, username.Length + 2);
if (body.Locales == null)
{
throw new AMQException("Locales is not defined in Connection Start method");
@@ -86,8 +76,9 @@ namespace Qpid.Client.Handler
clientProperties["product"] = "Qpid.NET";
clientProperties["version"] = "1.0";
clientProperties["platform"] = GetFullSystemInfo();
- AMQFrame frame = ConnectionStartOkBody.CreateAMQFrame(evt.ChannelId, clientProperties, selectedMechanism,
- plainData, selectedLocale);
+ AMQFrame frame = ConnectionStartOkBody.CreateAMQFrame(
+ evt.ChannelId, clientProperties, selectedMechanism,
+ saslResponse, selectedLocale);
ps.WriteFrame(frame);
}
catch (Exception e)
@@ -109,5 +100,51 @@ namespace Qpid.Client.Handler
// TODO: add in details here
return ".NET 1.1 Client";
}
+
+ private string ChooseMechanism(string mechanisms)
+ {
+ foreach ( string mech in mechanisms.Split(' ') )
+ {
+ if ( CallbackHandlerRegistry.Instance.IsSupportedMechanism(mech) )
+ {
+ return mech;
+ }
+ }
+ return null;
+ }
+
+ private byte[] DoAuthentication(string selectedMechanism, AMQProtocolSession ps)
+ {
+ ISaslClient saslClient = Sasl.Sasl.CreateClient(
+ new string[] { selectedMechanism }, null, "AMQP", "localhost",
+ new Hashtable(), CreateCallbackHandler(selectedMechanism, ps)
+ );
+ if ( saslClient == null )
+ {
+ throw new AMQException("Client SASL configuration error: no SaslClient could be created for mechanism " +
+ selectedMechanism);
+ }
+ ps.SaslClient = saslClient;
+ try
+ {
+ return saslClient.HasInitialResponse ?
+ saslClient.EvaluateChallenge(new byte[0]) : null;
+ } catch ( Exception ex )
+ {
+ ps.SaslClient = null;
+ throw new AMQException("Unable to create SASL client", ex);
+ }
+ }
+
+ private IAMQCallbackHandler CreateCallbackHandler(string mechanism, AMQProtocolSession session)
+ {
+ Type type = CallbackHandlerRegistry.Instance.GetCallbackHandler(mechanism);
+ IAMQCallbackHandler handler =
+ (IAMQCallbackHandler)Activator.CreateInstance(type);
+ if ( handler == null )
+ throw new AMQException("Unable to create callback handler: " + mechanism);
+ handler.Initialize(session);
+ return handler;
+ }
}
}