From 415cd36477e152c12f91a10ad61bb719373cd9d1 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 23 Aug 2009 20:57:00 +0200 Subject: =?UTF-8?q?Authentifikation=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/AuthBackendFile/AuthBackendFile.cpp | 100 ++++++++++++++++++++++++ src/modules/AuthBackendFile/AuthBackendFile.h | 80 +++++++++++++++++++ src/modules/AuthBackendFile/CMakeLists.txt | 8 ++ src/modules/AuthBackendFile/Module.cpp | 30 +++++++ src/modules/AuthBackendFile/Module.h | 52 ++++++++++++ src/modules/CMakeLists.txt | 1 + 6 files changed, 271 insertions(+) create mode 100644 src/modules/AuthBackendFile/AuthBackendFile.cpp create mode 100644 src/modules/AuthBackendFile/AuthBackendFile.h create mode 100644 src/modules/AuthBackendFile/CMakeLists.txt create mode 100644 src/modules/AuthBackendFile/Module.cpp create mode 100644 src/modules/AuthBackendFile/Module.h (limited to 'src/modules') diff --git a/src/modules/AuthBackendFile/AuthBackendFile.cpp b/src/modules/AuthBackendFile/AuthBackendFile.cpp new file mode 100644 index 0000000..8374101 --- /dev/null +++ b/src/modules/AuthBackendFile/AuthBackendFile.cpp @@ -0,0 +1,100 @@ +/* + * AuthBackendFile.cpp + * + * 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 . + */ + +#include "AuthBackendFile.h" +#include +#include + +#include + +#include + +namespace Mad { +namespace Modules { +namespace AuthBackendFile { + +void AuthBackendFile::readFile(const std::string &name) { + std::ifstream stream(name.c_str()); + if(!stream.good()) { + application->logf(Core::LoggerBase::LOG_WARNING, "AuthBackendFile: Can't read file '%s'.", name.c_str()); + return; + } + + while(stream.good() && !stream.eof()) { + std::string line; + std::getline(stream, line); + + if(line.empty()) + continue; + + static const boost::regex regex("([^:]+):(.+)", boost::regex_constants::perl); + boost::smatch match; + + if(!boost::regex_match(line, match, regex)) { + application->logf(Core::LoggerBase::LOG_WARNING, "AuthBackendFile: Malformed line in file '%s'.", name.c_str()); + continue; + } + + + userMap.insert(std::make_pair(match[1].str(), match[2].str())); + } +} + +bool AuthBackendFile::handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/) { + if(!entry[0].getKey().matches("AuthBackendFile")) + return false; + + if(entry[1].empty()) + return true; + + if(entry[1].getKey().matches("File")) { + if(entry[2].empty()) { + readFile(entry[1][0]); + } + } + else if(!entry[2].empty()) + return false; + + return true; +} + +boost::shared_ptr AuthBackendFile::authenticate(const std::string &method, const std::string &user, + const std::vector &challenge, std::vector& /*response*/, + boost::shared_ptr context) throw(Core::Exception) { + if(method != "Password") + throw(Core::Exception(Core::Exception::INVALID_INPUT)); + + if(context.get() != 0 && dynamic_cast(context.get()) == 0) + throw(Core::Exception(Core::Exception::INVALID_INPUT)); + + if(context.get() == 0) + context.reset(new AuthContextFile); + + std::string password(challenge.begin(), challenge.end()); + + std::map::iterator userIt = userMap.find(user); + if(userIt == userMap.end() || password != userIt->second) + throw(Core::Exception(Core::Exception::AUTHENTICATION)); + + return context; +} + +} +} +} diff --git a/src/modules/AuthBackendFile/AuthBackendFile.h b/src/modules/AuthBackendFile/AuthBackendFile.h new file mode 100644 index 0000000..81e7d12 --- /dev/null +++ b/src/modules/AuthBackendFile/AuthBackendFile.h @@ -0,0 +1,80 @@ +/* + * AuthBackendFile.h + * + * Copyright (C) 2008 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_MODULES_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ +#define MAD_MODULES_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ + +#include "../export.h" + +#include +#include +#include + +#include +#include + +namespace Mad { +namespace Modules { +namespace AuthBackendFile { + +class MAD_MODULE_EXPORT AuthBackendFile : public Common::AuthBackend, private Core::Configurable, private boost::noncopyable { + private: + class AuthContextFile : public Common::AuthContext { + protected: + virtual bool isAuthenticated() const { + return true; + } + }; + + void readFile(const std::string &name); + + std::vector methods; + + Common::Application *application; + + std::map userMap; + + protected: + virtual bool handleConfigEntry(const Core::ConfigEntry &entry, bool /*handled*/); + + virtual const std::vector& getMethods() const { + return methods; + } + + virtual boost::shared_ptr authenticate(const std::string &method, const std::string &user, + const std::vector &challenge, std::vector &response, + boost::shared_ptr context) throw(Core::Exception); + + public: + AuthBackendFile(Common::Application *application0) : application(application0) { + methods.push_back("Password"); + + application->getConfigManager()->registerConfigurable(this); + } + + virtual ~AuthBackendFile() { + application->getConfigManager()->unregisterConfigurable(this); + } +}; + +} +} +} + +#endif /* MAD_MODULES_AUTHBACKENDFILE_AUTHBACKENDFILE_H_ */ diff --git a/src/modules/AuthBackendFile/CMakeLists.txt b/src/modules/AuthBackendFile/CMakeLists.txt new file mode 100644 index 0000000..3afd71f --- /dev/null +++ b/src/modules/AuthBackendFile/CMakeLists.txt @@ -0,0 +1,8 @@ +include_directories(${INCLUDES}) + +mad_module(AuthBackendFile + Module.cpp Module.h + AuthBackendFile.cpp AuthBackendFile.h +) + +mad_module_libraries(AuthBackendFile) diff --git a/src/modules/AuthBackendFile/Module.cpp b/src/modules/AuthBackendFile/Module.cpp new file mode 100644 index 0000000..e5a9a18 --- /dev/null +++ b/src/modules/AuthBackendFile/Module.cpp @@ -0,0 +1,30 @@ +/* + * Module.cpp + * + * 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 . + */ + +#include "../export.h" + +#include "Module.h" + +extern "C" { + +MAD_MODULE_EXPORT Mad::Common::Module* AuthBackendFile_create(Mad::Common::Application *application) { + return new Mad::Modules::AuthBackendFile::Module(application); +} + +} diff --git a/src/modules/AuthBackendFile/Module.h b/src/modules/AuthBackendFile/Module.h new file mode 100644 index 0000000..b0d14aa --- /dev/null +++ b/src/modules/AuthBackendFile/Module.h @@ -0,0 +1,52 @@ +/* + * Module.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_MODULES_AUTHBACKENDFILE_MODULE_H_ +#define MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ + +#include "AuthBackendFile.h" + +#include +#include + +namespace Mad { +namespace Modules { +namespace AuthBackendFile { + +class Module : public Common::Module { + private: + Common::Application *application; + + boost::shared_ptr backend; + + public: + Module(Common::Application *application0) : application(application0), backend(new AuthBackendFile(application)) { + application->getAuthManager()->registerBackend(backend); + } + + virtual ~Module() { + application->getAuthManager()->unregisterBackend(backend); + } +}; + +} +} +} + +#endif /* MAD_MODULES_AUTHBACKENDFILE_MODULE_H_ */ diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt index 7e11214..e5890f4 100644 --- a/src/modules/CMakeLists.txt +++ b/src/modules/CMakeLists.txt @@ -33,6 +33,7 @@ macro(mad_module_libraries name) endif(WITH_${upper_name}) endmacro(mad_module_libraries) +add_subdirectory(AuthBackendFile) add_subdirectory(FileLogger) if(UNIX) -- cgit v1.2.3