Added authentication backends: internal, pam and http-headers. fixes #119

This commit is contained in:
emeric
2021-03-04 19:32:09 +01:00
parent b94fe3e852
commit cc28d893f5
73 changed files with 2208 additions and 1254 deletions
+1
View File
@@ -8,6 +8,7 @@ add_library(lmsutils SHARED
impl/NetAddress.cpp
impl/Path.cpp
impl/Random.cpp
impl/RecursiveSharedMutex.cpp
impl/StreamLogger.cpp
impl/String.cpp
impl/UUID.cpp
+16 -24
View File
@@ -47,70 +47,62 @@ Config::Config(const std::filesystem::path& p)
}
}
std::string
Config::getString(const std::string& setting, const std::string& def, const std::unordered_set<std::string>& allowedValues)
std::string_view
Config::getString(std::string_view setting, std::string_view def)
{
try {
std::string res {(const char*)_config.lookup(setting)};
if (!allowedValues.empty() && allowedValues.find(res) == std::cend(allowedValues))
{
LMS_LOG(MAIN, ERROR) << "Invalid setting for '" << setting << "', using default value '" << def << "'";
return def;
}
return res;
return static_cast<const char*>(_config.lookup(std::string {setting}));
}
catch (std::exception &e)
catch (libconfig::ConfigException&)
{
return def;
}
}
std::filesystem::path
Config::getPath(const std::string& setting, const std::filesystem::path& path)
Config::getPath(std::string_view setting, const std::filesystem::path& path)
{
try {
const char* res = _config.lookup(setting);
const char* res {_config.lookup(std::string {setting})};
return std::filesystem::path {std::string(res)};
}
catch (std::exception &e)
catch (libconfig::ConfigException&)
{
return path;
}
}
unsigned long
Config::getULong(const std::string& setting, unsigned long def)
Config::getULong(std::string_view setting, unsigned long def)
{
try {
return static_cast<unsigned int>(_config.lookup(setting));
return static_cast<unsigned int>(_config.lookup(std::string {setting}));
}
catch (...)
catch (libconfig::ConfigException&)
{
return def;
}
}
long
Config::getLong(const std::string& setting, long def)
Config::getLong(std::string_view setting, long def)
{
try {
return _config.lookup(setting);
return _config.lookup(std::string {setting});
}
catch (...)
catch (libconfig::ConfigException&)
{
return def;
}
}
bool
Config::getBool(const std::string& setting, bool def)
Config::getBool(std::string_view setting, bool def)
{
try {
return _config.lookup(setting);
return _config.lookup(std::string {setting});
}
catch (...)
catch (libconfig::ConfigException&)
{
return def;
}
+5 -5
View File
@@ -35,11 +35,11 @@ class Config final : public IConfig
Config& operator=(Config&&) = delete;
// Default values are returned in case of setting not found
std::string getString(const std::string& setting, const std::string& def = "", const std::unordered_set<std::string>& allowedValues = {}) override;
std::filesystem::path getPath(const std::string& setting, const std::filesystem::path& def = std::filesystem::path()) override;
unsigned long getULong(const std::string& setting, unsigned long def = 0) override;
long getLong(const std::string& setting, long def = 0) override;
bool getBool(const std::string& setting, bool def = false) override;
std::string_view getString(std::string_view setting, std::string_view def = "") override;
std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def = std::filesystem::path()) override;
unsigned long getULong(std::string_view setting, unsigned long def = 0) override;
long getLong(std::string_view setting, long def = 0) override;
bool getBool(std::string_view setting, bool def = false) override;
private:
@@ -0,0 +1,112 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils/RecursiveSharedMutex.hpp"
#include <cassert>
void
RecursiveSharedMutex::lock()
{
if (_uniqueOwner == std::this_thread::get_id())
{
// already locked
_uniqueCount++;
}
else
{
_mutex.lock();
_uniqueOwner = std::this_thread::get_id();
assert(_uniqueCount == 0);
_uniqueCount = 1;
}
}
void
RecursiveSharedMutex::unlock()
{
assert(_uniqueCount > 0);
if (--_uniqueCount == 0)
{
_uniqueOwner = {};
_mutex.unlock();
}
}
void
RecursiveSharedMutex::lock_shared()
{
if (_uniqueOwner == std::this_thread::get_id())
{
// alone here, no need to lock
_sharedCounts[std::this_thread::get_id()]++;
return;
}
bool needLock {};
{
std::scoped_lock lock {_sharedCountMutex};
auto& sharedCount {_sharedCounts[std::this_thread::get_id()]};
if (sharedCount == 0)
needLock = true;
else
++sharedCount;
}
if (needLock)
{
_mutex.lock_shared();
assert(_uniqueOwner == std::thread::id {});
std::scoped_lock lock {_sharedCountMutex};
_sharedCounts[std::this_thread::get_id()]++;
}
}
void
RecursiveSharedMutex::unlock_shared()
{
if (_uniqueOwner == std::this_thread::get_id())
{
// alone here, no need to lock
auto& sharedCount {_sharedCounts[std::this_thread::get_id()]};
assert(sharedCount > 0);
--sharedCount;
return;
}
bool needUnlock {};
{
std::scoped_lock lock {_sharedCountMutex};
auto& sharedCount {_sharedCounts[std::this_thread::get_id()]};
assert(sharedCount > 0);
needUnlock = (--sharedCount == 0);
}
if (needUnlock)
_mutex.unlock_shared();
}
+6 -7
View File
@@ -18,9 +18,8 @@
*/
#pragma once
#include <string_view>
#include <filesystem>
#include <memory>
#include <unordered_set>
// Used to get config values from configuration files
class IConfig
@@ -30,11 +29,11 @@ class IConfig
virtual ~IConfig() = default;
// Default values are returned in case of setting not found
virtual std::string getString(const std::string& setting, const std::string& def = "", const std::unordered_set<std::string>& allowedValues = {}) = 0;
virtual std::filesystem::path getPath(const std::string& setting, const std::filesystem::path& def = std::filesystem::path()) = 0;
virtual unsigned long getULong(const std::string& setting, unsigned long def = 0) = 0;
virtual long getLong(const std::string& setting, long def = 0) = 0;
virtual bool getBool(const std::string& setting, bool def = false) = 0;
virtual std::string_view getString(std::string_view setting, std::string_view def = "") = 0;
virtual std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def = std::filesystem::path()) = 0;
virtual unsigned long getULong(std::string_view setting, unsigned long def = 0) = 0;
virtual long getLong(std::string_view setting, long def = 0) = 0;
virtual bool getBool(std::string_view setting, bool def = false) = 0;
};
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <unordered_map>
// API compatible with shared_mutex
class RecursiveSharedMutex
{
public:
void lock();
void unlock();
void lock_shared();
void unlock_shared();
private:
std::shared_mutex _mutex;
std::thread::id _uniqueOwner;
std::size_t _uniqueCount{};
std::mutex _sharedCountMutex;
std::unordered_map<std::thread::id, std::size_t> _sharedCounts;
};
+6 -2
View File
@@ -26,6 +26,7 @@ template <typename Class>
class Service
{
public:
Service() = default;
Service(std::unique_ptr<Class> service)
{
assign(std::move(service));
@@ -52,14 +53,17 @@ class Service
}
static Class* get() { return _service.get(); }
static bool exists() { return _service.get(); }
private:
static Class& assign(std::unique_ptr<Class> service)
template <typename SubClass>
static Class& assign(std::unique_ptr<SubClass> service)
{
assert(!_service);
_service = std::move(service);
return *get();
}
private:
static void clear() { _service.reset(); }
static inline std::unique_ptr<Class> _service;