From 6139c256a5a98fd92074e518f07b86714987564b Mon Sep 17 00:00:00 2001 From: Gordon Sim Date: Fri, 12 Jul 2013 19:27:43 +0000 Subject: QPID-4407: verify mechanism used when no external SASL library is available git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1502664 13f79535-47bb-0310-9956-ffa450edef68 --- qpid/cpp/src/qpid/client/ConnectionHandler.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'qpid/cpp/src') diff --git a/qpid/cpp/src/qpid/client/ConnectionHandler.cpp b/qpid/cpp/src/qpid/client/ConnectionHandler.cpp index 4f88cb97ee..3ee3f1cd40 100644 --- a/qpid/cpp/src/qpid/client/ConnectionHandler.cpp +++ b/qpid/cpp/src/qpid/client/ConnectionHandler.cpp @@ -50,6 +50,7 @@ using qpid::sys::Mutex; namespace { const std::string OK("OK"); const std::string PLAIN("PLAIN"); +const std::string ANONYMOUS("ANONYMOUS"); const std::string en_US("en_US"); const std::string INVALID_STATE_START("start received in invalid state"); @@ -244,6 +245,7 @@ void ConnectionHandler::start(const FieldTable& /*serverProps*/, const Array& me std::vector mechlist; mechlist.reserve(mechanisms.size()); + if (mechanism.empty()) { //mechlist is simply what the server offers std::transform(mechanisms.begin(), mechanisms.end(), std::back_inserter(mechlist), Array::get); @@ -273,9 +275,25 @@ void ConnectionHandler::start(const FieldTable& /*serverProps*/, const Array& me proxy.send(body); } } else { - //TODO: verify that desired mechanism and locale are supported - std::string response = ((char)0) + username + ((char)0) + password; - proxy.startOk(properties, mechanism, response, locale); + bool haveAnonymous(false); + bool havePlain(false); + for (std::vector::const_iterator i = mechlist.begin(); i != mechlist.end(); ++i) { + if (*i == ANONYMOUS) { + haveAnonymous = true; + break; + } else if (*i == PLAIN) { + havePlain = true; + } + } + if (haveAnonymous && (mechanism.empty() || mechanism.find(ANONYMOUS) != std::string::npos)) { + proxy.startOk(properties, ANONYMOUS, username, locale); + } else if (havePlain && (mechanism.empty() || mechanism.find(PLAIN) !=std::string::npos)) { + std::string response = ((char)0) + username + ((char)0) + password; + proxy.startOk(properties, PLAIN, response, locale); + } else { + if (!mechanism.empty()) throw Exception(QPID_MSG("Desired mechanism(s) not valid: " << mechanism << "; client supports PLAIN or ANONYMOUS, broker supports: " << join(mechlist))); + throw Exception(QPID_MSG("No valid mechanism; client supports PLAIN or ANONYMOUS, broker supports: " << join(mechlist))); + } } } -- cgit v1.2.1