diff options
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/AuthBackend.h | 11 | ||||
-rw-r--r-- | src/Common/AuthManager.cpp | 50 | ||||
-rw-r--r-- | src/Common/AuthManager.h | 44 | ||||
-rw-r--r-- | src/Common/AuthProvider.h | 52 | ||||
-rw-r--r-- | src/Common/CMakeLists.txt | 1 |
5 files changed, 114 insertions, 44 deletions
diff --git a/src/Common/AuthBackend.h b/src/Common/AuthBackend.h index d916d1d..3fe3840 100644 --- a/src/Common/AuthBackend.h +++ b/src/Common/AuthBackend.h @@ -32,17 +32,18 @@ namespace Common { class AuthContext; class AuthManager; +class AuthProvider; class AuthBackend { protected: friend class AuthManager; - virtual const std::vector<std::string>& getMethods() const = 0; - virtual const std::vector<std::string>& getSubMethods(const std::string &method) const throw(Core::Exception) = 0; + virtual const std::string& getMethodName() const = 0; + virtual std::vector<std::string> getSubMethods(boost::shared_ptr<AuthProvider> provider) const = 0; - virtual boost::shared_ptr<AuthContext> authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector<boost::uint8_t>& /*data*/, std::vector<boost::uint8_t>& /*response*/, - boost::shared_ptr<AuthContext> /*context*/) throw(Core::Exception) = 0; + virtual boost::shared_ptr<AuthContext> authenticate(boost::shared_ptr<AuthProvider> provider, const std::string &subMethod, + const std::string &user, const std::vector<boost::uint8_t> &data, std::vector<boost::uint8_t> &response, + boost::shared_ptr<AuthContext> context) throw(Core::Exception) = 0; public: virtual ~AuthBackend() {} diff --git a/src/Common/AuthManager.cpp b/src/Common/AuthManager.cpp index d21909d..c4cace7 100644 --- a/src/Common/AuthManager.cpp +++ b/src/Common/AuthManager.cpp @@ -25,31 +25,54 @@ namespace Mad { namespace Common { -const std::vector<std::string> AuthManager::DenyBackend::methods; +void AuthManager::setProvider(boost::shared_ptr<AuthProvider> newProvider) { + boost::lock_guard<boost::shared_mutex> lock(mutex); + + provider = newProvider; +} + +void AuthManager::unsetProvider(boost::shared_ptr<AuthProvider> oldProvider) { + boost::lock_guard<boost::shared_mutex> lock(mutex); + + if(oldProvider == provider) + provider.reset(); +} -void AuthManager::registerBackend(boost::shared_ptr<AuthBackend> newBackend) { +void AuthManager::registerBackend(boost::shared_ptr<AuthBackend> backend) { boost::lock_guard<boost::shared_mutex> lock(mutex); - backend = newBackend; + methods.insert(backend->getMethodName()); + backends.insert(std::make_pair(backend->getMethodName(), backend)); } -void AuthManager::unregisterBackend(boost::shared_ptr<AuthBackend> oldBackend) { +void AuthManager::unregisterBackend(boost::shared_ptr<AuthBackend> backend) { boost::lock_guard<boost::shared_mutex> lock(mutex); - if(oldBackend == backend) - backend = denyBackend; + std::map<std::string, boost::shared_ptr<AuthBackend> >::iterator backendIt = backends.find(backend->getMethodName()); + if(backendIt == backends.end() || backendIt->second != backend) + return; + + methods.erase(backend->getMethodName()); + backends.erase(backendIt); } -std::vector<std::string> AuthManager::getMethods() { +std::set<std::string> AuthManager::getMethods() { boost::shared_lock<boost::shared_mutex> lock(mutex); - return backend->getMethods(); + return methods; } std::vector<std::string> AuthManager::getSubMethods(const std::string &method) throw(Core::Exception) { boost::shared_lock<boost::shared_mutex> lock(mutex); - return backend->getSubMethods(method); + std::map<std::string, boost::shared_ptr<AuthBackend> >::iterator backend = backends.find(method); + if(backend == backends.end()) + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + + if(!provider) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + return backend->second->getSubMethods(provider); } boost::shared_ptr<AuthContext> AuthManager::authenticate(const std::string &method, const std::string &subMethod, const std::string &user, @@ -58,7 +81,14 @@ boost::shared_ptr<AuthContext> AuthManager::authenticate(const std::string &meth response.clear(); - return backend->authenticate(method, subMethod, user, data, response, context); + std::map<std::string, boost::shared_ptr<AuthBackend> >::iterator backend = backends.find(method); + if(backend == backends.end()) + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + + if(!provider) + throw Core::Exception(Core::Exception::NOT_AVAILABLE); + + return backend->second->authenticate(provider, subMethod, user, data, response, context); } } diff --git a/src/Common/AuthManager.h b/src/Common/AuthManager.h index 65e1fd3..e45762a 100644 --- a/src/Common/AuthManager.h +++ b/src/Common/AuthManager.h @@ -22,14 +22,13 @@ #include "export.h" -#include "AuthBackend.h" #include "AuthContext.h" #include <Core/Exception.h> -#include <vector> +#include <map> +#include <set> -#include <boost/cstdint.hpp> #include <boost/noncopyable.hpp> #include <boost/shared_ptr.hpp> @@ -40,43 +39,30 @@ namespace Common { class Application; +class AuthBackend; +class AuthProvider; + class MAD_COMMON_EXPORT AuthManager : private boost::noncopyable { private: friend class Application; - class DenyBackend : public AuthBackend { - private: - const static std::vector<std::string> methods; - - protected: - virtual const std::vector<std::string>& getMethods() const { - return methods; - } - - virtual const std::vector<std::string>& getSubMethods(const std::string& /*method*/) const throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } + boost::shared_ptr<AuthProvider> provider; - virtual boost::shared_ptr<AuthContext> authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector<boost::uint8_t>& /*data*/, std::vector<boost::uint8_t>& /*response*/, - boost::shared_ptr<AuthContext> /*context*/) throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } - }; - - boost::shared_ptr<DenyBackend> denyBackend; - - boost::shared_ptr<AuthBackend> backend; + std::set<std::string> methods; + std::map<std::string, boost::shared_ptr<AuthBackend> > backends; boost::shared_mutex mutex; - AuthManager() : denyBackend(new DenyBackend), backend(denyBackend) {} + AuthManager() {} public: - void registerBackend(boost::shared_ptr<AuthBackend> newBackend); - void unregisterBackend(boost::shared_ptr<AuthBackend> oldBackend); + void setProvider(boost::shared_ptr<AuthProvider> newProvider); + void unsetProvider(boost::shared_ptr<AuthProvider> oldProvider); + + void registerBackend(boost::shared_ptr<AuthBackend> backend); + void unregisterBackend(boost::shared_ptr<AuthBackend> backend); - std::vector<std::string> getMethods(); + std::set<std::string> getMethods(); std::vector<std::string> getSubMethods(const std::string &method) throw(Core::Exception); boost::shared_ptr<AuthContext> authenticate(const std::string &method, const std::string &subMethod, diff --git a/src/Common/AuthProvider.h b/src/Common/AuthProvider.h new file mode 100644 index 0000000..aaf8f10 --- /dev/null +++ b/src/Common/AuthProvider.h @@ -0,0 +1,52 @@ +/* + * AuthProvider.h + * + * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_COMMON_AUTHPROVIDER_H_ +#define MAD_COMMON_AUTHPROVIDER_H_ + +#include <Core/Exception.h> + +#include <vector> + +#include <boost/cstdint.hpp> + +namespace Mad { +namespace Common { + +class AuthProvider { + public: + virtual const std::vector<std::string>& getHashes() const = 0; + + virtual bool checkPassword(const std::string &user, const std::vector<boost::uint8_t> &data, const std::string &hash) throw(Core::Exception) { + std::vector<boost::uint8_t> password = getPassword(user, hash); + + return (!password.empty() && data.size() == password.size() && std::equal(data.begin(), data.end(), password.begin())); + } + + virtual std::vector<boost::uint8_t> getPassword(const std::string& /*user*/, const std::string& /*hash*/) throw(Core::Exception) { + throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); + } + + virtual ~AuthProvider() {} +}; + +} +} + +#endif /* MAD_COMMON_AUTHPROVIDER_H_ */ diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index e370c54..a92648b 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -22,6 +22,7 @@ mad_library(Common AuthBackend.h AuthContext.h AuthManager.cpp AuthManager.h + AuthProvider.h Base64Encoder.cpp Base64Encoder.h ClientConnection.cpp ClientConnection.h Connection.cpp Connection.h |