mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 02:14:26 +01:00
Added condition variable, which is notified on Token updates
This commit is contained in:
parent
a06a2669e5
commit
60bbe3df76
@ -17,13 +17,15 @@ Logic::Logic(const chrono::seconds tokenTimeout,
|
||||
const string &ldapUri,
|
||||
const string &bindDN,
|
||||
const string &webPrefix,
|
||||
const string &serDev) :
|
||||
const string &serDev,
|
||||
condition_variable &onTokenUpdate) :
|
||||
_logger(Logger::get()),
|
||||
_door(serDev),
|
||||
_tokenTimeout(tokenTimeout),
|
||||
_ldapUri(ldapUri),
|
||||
_bindDN(bindDN),
|
||||
_webPrefix(webPrefix)
|
||||
_webPrefix(webPrefix),
|
||||
_onTokenUpdate(onTokenUpdate)
|
||||
{
|
||||
srand(time(NULL));
|
||||
_createNewToken(false);
|
||||
@ -218,17 +220,14 @@ void Logic::_createNewToken(const bool stillValid)
|
||||
|
||||
_curToken = (((uint64_t)rand())<<32) | ((uint64_t)rand());
|
||||
|
||||
// TODO make things more pretty
|
||||
const string uri = _webPrefix + toHexString(_curToken);
|
||||
|
||||
const int ARRAY_SIZE=1024;
|
||||
char buffer[ARRAY_SIZE];
|
||||
snprintf(buffer, ARRAY_SIZE,
|
||||
"qrencode -l M -d 100 -s 5 \"%s\" -t png -o /tmp/qr.png", uri.c_str());
|
||||
system(buffer);
|
||||
|
||||
|
||||
ostringstream message;
|
||||
message << "New Token generated: " << toHexString(_curToken) << " old Token: " << toHexString(_prevToken) << " is " << (_prevValid?"still":"not") << " valid";
|
||||
_logger(message, LogLevel::info);
|
||||
|
||||
_onTokenUpdate.notify_all();
|
||||
}
|
||||
|
||||
std::string Logic::getCurrentToken() const
|
||||
{
|
||||
return _webPrefix + toHexString(_curToken);
|
||||
}
|
||||
|
@ -25,7 +25,8 @@ public:
|
||||
const std::string &ldapUri,
|
||||
const std::string &bindDN,
|
||||
const std::string &webPrefix,
|
||||
const std::string &serDev);
|
||||
const std::string &serDev,
|
||||
std::condition_variable &onTokenUpdate);
|
||||
~Logic();
|
||||
|
||||
enum Response {
|
||||
@ -45,6 +46,9 @@ public:
|
||||
// Parse incoming JSON Requests
|
||||
Response parseRequest(const std::string &str);
|
||||
|
||||
// Returns the current Token
|
||||
std::string getCurrentToken() const;
|
||||
|
||||
private:
|
||||
|
||||
// Internal lock wrapper
|
||||
@ -89,6 +93,9 @@ private:
|
||||
// Token mutex
|
||||
std::mutex _mutex = {};
|
||||
|
||||
// This variable gets notified on token updates
|
||||
std::condition_variable &_onTokenUpdate;
|
||||
|
||||
// The URI of the ldap server
|
||||
const std::string _ldapUri;
|
||||
// LDAP bindDN
|
||||
|
@ -23,6 +23,8 @@ const static Logger &l = Logger::get();
|
||||
static std::unique_ptr<Logic> logic = nullptr;
|
||||
static boost::asio::io_service io_service;
|
||||
|
||||
static std::condition_variable onTokenUpdate;
|
||||
|
||||
static void signal_handler(int signum)
|
||||
{
|
||||
l((std::string)"Received Signal " + std::to_string(signum),
|
||||
@ -159,14 +161,14 @@ int main(int argc, char** argv)
|
||||
signal(SIGUSR1, signal_handler);
|
||||
signal(SIGUSR2, signal_handler);
|
||||
|
||||
|
||||
l(LogLevel::info, "Starting Doorlock Logic");
|
||||
try {
|
||||
logic = std::unique_ptr<Logic>(new Logic(tokenTimeout,
|
||||
ldapUri,
|
||||
bindDN,
|
||||
lockPagePrefix,
|
||||
serDev));
|
||||
serDev,
|
||||
onTokenUpdate));
|
||||
server(port);
|
||||
}
|
||||
catch (...) {
|
||||
|
Loading…
Reference in New Issue
Block a user