From 662c9243216dc11d84f1c16ad05dd87b04e4fd73 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 29 Jul 2022 22:06:40 +0200 Subject: [PATCH] Use `flagenumclass.h` for auth flags --- librepomgr/CMakeLists.txt | 2 +- librepomgr/authentication.h | 12 +++++++----- librepomgr/webapi/session.cpp | 9 +++------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/librepomgr/CMakeLists.txt b/librepomgr/CMakeLists.txt index 5534311..efa0fd4 100644 --- a/librepomgr/CMakeLists.txt +++ b/librepomgr/CMakeLists.txt @@ -80,7 +80,7 @@ set(LINK_TESTS_AGAINST_APP_TARGET ON) set(CONFIGURATION_PACKAGE_SUFFIX "" CACHE STRING "sets the suffix for find_package() calls to packages configured via c++utilities") -find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.11.0 REQUIRED) +find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.18.0 REQUIRED) use_cpp_utilities(VISIBILITY PUBLIC) # find passwordfile diff --git a/librepomgr/authentication.h b/librepomgr/authentication.h index 5beec57..e289493 100644 --- a/librepomgr/authentication.h +++ b/librepomgr/authentication.h @@ -1,6 +1,8 @@ #ifndef LIBREPOMGR_AUTHENTICATION_H #define LIBREPOMGR_AUTHENTICATION_H +#include + #include #include @@ -23,11 +25,11 @@ struct UserAuth { UserPermissions permissions = UserPermissions::DefaultPermissions; }; -constexpr UserPermissions operator|(UserPermissions lhs, UserPermissions rhs) -{ - return static_cast( - static_cast>(lhs) | static_cast>(rhs)); -} +} // namespace LibRepoMgr + +CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(LibPkg, LibRepoMgr::UserPermissions) + +namespace LibRepoMgr { struct UserInfo { std::string passwordSha512; diff --git a/librepomgr/webapi/session.cpp b/librepomgr/webapi/session.cpp index e9880e2..235234f 100644 --- a/librepomgr/webapi/session.cpp +++ b/librepomgr/webapi/session.cpp @@ -88,22 +88,19 @@ void Session::received(boost::system::error_code ec, size_t bytesTransferred) return; } const auto userAuth = m_setup.auth.authenticate(std::string_view(authInfo->value().data(), authInfo->value().size())); - using PermissionFlags = std::underlying_type_t; - if (static_cast(userAuth.permissions) & static_cast(UserPermissions::TryAgain)) { + if (userAuth.permissions & UserPermissions::TryAgain) { // send the 401 response again if credentials are 'try again' to show the password prompt for the XMLHttpRequest again // note: This is kind of a hack. Maybe there's a better solution to make XMLHttpRequest forget wrongly entered credentials // and instead show the login prompt again? respond(Render::makeAuthRequired(request)); return; } - if ((static_cast(requiredPermissions) & static_cast(userAuth.permissions)) - != static_cast(requiredPermissions)) { + if (!checkFlagEnum(userAuth.permissions, requiredPermissions)) { respond(Render::makeForbidden(request)); return; } // prepare file with secrets for user - if (!userAuth.name.empty() && !userAuth.password.empty() - && (static_cast(requiredPermissions) & static_cast(UserPermissions::AccessSecrets))) { + if (!userAuth.name.empty() && !userAuth.password.empty() && (requiredPermissions & UserPermissions::AccessSecrets)) { try { if (m_secrets) { m_secrets->clear();