mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-26 12:06:00 +01:00
managers: Add a TokenManager
This commit is contained in:
parent
da839f20f1
commit
7778f01194
4 changed files with 84 additions and 0 deletions
|
@ -247,6 +247,7 @@ target_link_libraries(Hyprland
|
||||||
OpenGL::GL
|
OpenGL::GL
|
||||||
Threads::Threads
|
Threads::Threads
|
||||||
libudis86
|
libudis86
|
||||||
|
uuid
|
||||||
)
|
)
|
||||||
|
|
||||||
protocol("protocols/idle.xml" "idle" true)
|
protocol("protocols/idle.xml" "idle" true)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include "helpers/Splashes.hpp"
|
#include "helpers/Splashes.hpp"
|
||||||
#include "config/ConfigValue.hpp"
|
#include "config/ConfigValue.hpp"
|
||||||
#include "managers/CursorManager.hpp"
|
#include "managers/CursorManager.hpp"
|
||||||
|
#include "managers/TokenManager.hpp"
|
||||||
#include "managers/eventLoop/EventLoopManager.hpp"
|
#include "managers/eventLoop/EventLoopManager.hpp"
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
@ -456,6 +457,9 @@ void CCompositor::initManagers(eManagersInitStage stage) {
|
||||||
Debug::log(LOG, "Creating the LayoutManager!");
|
Debug::log(LOG, "Creating the LayoutManager!");
|
||||||
g_pLayoutManager = std::make_unique<CLayoutManager>();
|
g_pLayoutManager = std::make_unique<CLayoutManager>();
|
||||||
|
|
||||||
|
Debug::log(LOG, "Creating the TokenManager!");
|
||||||
|
g_pTokenManager = std::make_unique<CTokenManager>();
|
||||||
|
|
||||||
g_pConfigManager->init();
|
g_pConfigManager->init();
|
||||||
g_pWatchdog = std::make_unique<CWatchdog>(); // requires config
|
g_pWatchdog = std::make_unique<CWatchdog>(); // requires config
|
||||||
} break;
|
} break;
|
||||||
|
|
43
src/managers/TokenManager.cpp
Normal file
43
src/managers/TokenManager.cpp
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
#include "TokenManager.hpp"
|
||||||
|
#include <uuid/uuid.h>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
CUUIDToken::CUUIDToken(const std::string& uuid_, std::any data_, std::chrono::system_clock::duration expires) : data(data_), uuid(uuid_) {
|
||||||
|
expiresAt = std::chrono::system_clock::now() + expires;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CUUIDToken::getUUID() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CTokenManager::registerNewToken(std::any data, std::chrono::system_clock::duration expires) {
|
||||||
|
std::string uuid;
|
||||||
|
do {
|
||||||
|
uuid_t uuid_;
|
||||||
|
uuid_generate_random(uuid_);
|
||||||
|
uuid = std::format("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", (uint16_t)uuid_[0], (uint16_t)uuid_[1], (uint16_t)uuid_[2],
|
||||||
|
(uint16_t)uuid_[3], (uint16_t)uuid_[4], (uint16_t)uuid_[5], (uint16_t)uuid_[6], (uint16_t)uuid_[7], (uint16_t)uuid_[8], (uint16_t)uuid_[9],
|
||||||
|
(uint16_t)uuid_[10], (uint16_t)uuid_[11], (uint16_t)uuid_[12], (uint16_t)uuid_[13], (uint16_t)uuid_[14], (uint16_t)uuid_[15]);
|
||||||
|
} while (m_mTokens.contains(uuid));
|
||||||
|
|
||||||
|
m_mTokens[uuid] = std::make_shared<CUUIDToken>(uuid, data, expires);
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<CUUIDToken> CTokenManager::getToken(const std::string& uuid) {
|
||||||
|
|
||||||
|
// cleanup expired tokens
|
||||||
|
const auto NOW = std::chrono::system_clock::now();
|
||||||
|
std::erase_if(m_mTokens, [this, &NOW](const auto& el) { return el.second->expiresAt < NOW; });
|
||||||
|
|
||||||
|
if (!m_mTokens.contains(uuid))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return m_mTokens.at(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CTokenManager::removeToken(std::shared_ptr<CUUIDToken> token) {
|
||||||
|
if (!token)
|
||||||
|
return;
|
||||||
|
m_mTokens.erase(token->uuid);
|
||||||
|
}
|
36
src/managers/TokenManager.hpp
Normal file
36
src/managers/TokenManager.hpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <chrono>
|
||||||
|
#include <any>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class CUUIDToken {
|
||||||
|
public:
|
||||||
|
CUUIDToken(const std::string& uuid_, std::any data_, std::chrono::system_clock::duration expires);
|
||||||
|
|
||||||
|
std::string getUUID();
|
||||||
|
|
||||||
|
std::any data;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string uuid;
|
||||||
|
|
||||||
|
std::chrono::system_clock::time_point expiresAt;
|
||||||
|
|
||||||
|
friend class CTokenManager;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CTokenManager {
|
||||||
|
public:
|
||||||
|
std::string registerNewToken(std::any data, std::chrono::system_clock::duration expires);
|
||||||
|
|
||||||
|
std::shared_ptr<CUUIDToken> getToken(const std::string& uuid);
|
||||||
|
void removeToken(std::shared_ptr<CUUIDToken> token);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, std::shared_ptr<CUUIDToken>> m_mTokens;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline std::unique_ptr<CTokenManager> g_pTokenManager;
|
Loading…
Reference in a new issue