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
+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();
}