From 82ef58fb3d0bdf6ce7d13f42cca30d03b24973c6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 27 Aug 2009 17:27:58 +0200 Subject: =?UTF-8?q?AuthProvider=20hinzugef=C3=BCgt=20AuthBackendFile=20in?= =?UTF-8?q?=20AuthProviderFile=20und=20AuthBackendPassword=20aufgeteilt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/AuthBackend.h | 11 +++++----- src/Common/AuthManager.cpp | 50 +++++++++++++++++++++++++++++++++++--------- src/Common/AuthManager.h | 44 +++++++++++++-------------------------- src/Common/AuthProvider.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++ src/Common/CMakeLists.txt | 1 + 5 files changed, 114 insertions(+), 44 deletions(-) create mode 100644 src/Common/AuthProvider.h (limited to 'src/Common') 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& getMethods() const = 0; - virtual const std::vector& getSubMethods(const std::string &method) const throw(Core::Exception) = 0; + virtual const std::string& getMethodName() const = 0; + virtual std::vector getSubMethods(boost::shared_ptr provider) const = 0; - virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector& /*data*/, std::vector& /*response*/, - boost::shared_ptr /*context*/) throw(Core::Exception) = 0; + virtual boost::shared_ptr authenticate(boost::shared_ptr provider, const std::string &subMethod, + const std::string &user, const std::vector &data, std::vector &response, + boost::shared_ptr 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 AuthManager::DenyBackend::methods; +void AuthManager::setProvider(boost::shared_ptr newProvider) { + boost::lock_guard lock(mutex); + + provider = newProvider; +} + +void AuthManager::unsetProvider(boost::shared_ptr oldProvider) { + boost::lock_guard lock(mutex); + + if(oldProvider == provider) + provider.reset(); +} -void AuthManager::registerBackend(boost::shared_ptr newBackend) { +void AuthManager::registerBackend(boost::shared_ptr backend) { boost::lock_guard lock(mutex); - backend = newBackend; + methods.insert(backend->getMethodName()); + backends.insert(std::make_pair(backend->getMethodName(), backend)); } -void AuthManager::unregisterBackend(boost::shared_ptr oldBackend) { +void AuthManager::unregisterBackend(boost::shared_ptr backend) { boost::lock_guard lock(mutex); - if(oldBackend == backend) - backend = denyBackend; + std::map >::iterator backendIt = backends.find(backend->getMethodName()); + if(backendIt == backends.end() || backendIt->second != backend) + return; + + methods.erase(backend->getMethodName()); + backends.erase(backendIt); } -std::vector AuthManager::getMethods() { +std::set AuthManager::getMethods() { boost::shared_lock lock(mutex); - return backend->getMethods(); + return methods; } std::vector AuthManager::getSubMethods(const std::string &method) throw(Core::Exception) { boost::shared_lock lock(mutex); - return backend->getSubMethods(method); + std::map >::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 AuthManager::authenticate(const std::string &method, const std::string &subMethod, const std::string &user, @@ -58,7 +81,14 @@ boost::shared_ptr AuthManager::authenticate(const std::string &meth response.clear(); - return backend->authenticate(method, subMethod, user, data, response, context); + std::map >::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 -#include +#include +#include -#include #include #include @@ -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 methods; - - protected: - virtual const std::vector& getMethods() const { - return methods; - } - - virtual const std::vector& getSubMethods(const std::string& /*method*/) const throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } + boost::shared_ptr provider; - virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*subMethod*/, - const std::string& /*user*/, const std::vector& /*data*/, std::vector& /*response*/, - boost::shared_ptr /*context*/) throw(Core::Exception) { - throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); - } - }; - - boost::shared_ptr denyBackend; - - boost::shared_ptr backend; + std::set methods; + std::map > backends; boost::shared_mutex mutex; - AuthManager() : denyBackend(new DenyBackend), backend(denyBackend) {} + AuthManager() {} public: - void registerBackend(boost::shared_ptr newBackend); - void unregisterBackend(boost::shared_ptr oldBackend); + void setProvider(boost::shared_ptr newProvider); + void unsetProvider(boost::shared_ptr oldProvider); + + void registerBackend(boost::shared_ptr backend); + void unregisterBackend(boost::shared_ptr backend); - std::vector getMethods(); + std::set getMethods(); std::vector getSubMethods(const std::string &method) throw(Core::Exception); boost::shared_ptr 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 + * + * 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 . + */ + +#ifndef MAD_COMMON_AUTHPROVIDER_H_ +#define MAD_COMMON_AUTHPROVIDER_H_ + +#include + +#include + +#include + +namespace Mad { +namespace Common { + +class AuthProvider { + public: + virtual const std::vector& getHashes() const = 0; + + virtual bool checkPassword(const std::string &user, const std::vector &data, const std::string &hash) throw(Core::Exception) { + std::vector password = getPassword(user, hash); + + return (!password.empty() && data.size() == password.size() && std::equal(data.begin(), data.end(), password.begin())); + } + + virtual std::vector 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 -- cgit v1.2.3