1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00

Added condition variable, which is notified on Token updates

This commit is contained in:
Ralf Ramsauer 2015-09-22 18:08:12 +02:00
parent a06a2669e5
commit 60bbe3df76
3 changed files with 23 additions and 15 deletions

View File

@ -17,13 +17,15 @@ Logic::Logic(const chrono::seconds tokenTimeout,
const string &ldapUri, const string &ldapUri,
const string &bindDN, const string &bindDN,
const string &webPrefix, const string &webPrefix,
const string &serDev) : const string &serDev,
condition_variable &onTokenUpdate) :
_logger(Logger::get()), _logger(Logger::get()),
_door(serDev), _door(serDev),
_tokenTimeout(tokenTimeout), _tokenTimeout(tokenTimeout),
_ldapUri(ldapUri), _ldapUri(ldapUri),
_bindDN(bindDN), _bindDN(bindDN),
_webPrefix(webPrefix) _webPrefix(webPrefix),
_onTokenUpdate(onTokenUpdate)
{ {
srand(time(NULL)); srand(time(NULL));
_createNewToken(false); _createNewToken(false);
@ -218,17 +220,14 @@ void Logic::_createNewToken(const bool stillValid)
_curToken = (((uint64_t)rand())<<32) | ((uint64_t)rand()); _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; ostringstream message;
message << "New Token generated: " << toHexString(_curToken) << " old Token: " << toHexString(_prevToken) << " is " << (_prevValid?"still":"not") << " valid"; message << "New Token generated: " << toHexString(_curToken) << " old Token: " << toHexString(_prevToken) << " is " << (_prevValid?"still":"not") << " valid";
_logger(message, LogLevel::info); _logger(message, LogLevel::info);
_onTokenUpdate.notify_all();
}
std::string Logic::getCurrentToken() const
{
return _webPrefix + toHexString(_curToken);
} }

View File

@ -25,7 +25,8 @@ public:
const std::string &ldapUri, const std::string &ldapUri,
const std::string &bindDN, const std::string &bindDN,
const std::string &webPrefix, const std::string &webPrefix,
const std::string &serDev); const std::string &serDev,
std::condition_variable &onTokenUpdate);
~Logic(); ~Logic();
enum Response { enum Response {
@ -45,6 +46,9 @@ public:
// Parse incoming JSON Requests // Parse incoming JSON Requests
Response parseRequest(const std::string &str); Response parseRequest(const std::string &str);
// Returns the current Token
std::string getCurrentToken() const;
private: private:
// Internal lock wrapper // Internal lock wrapper
@ -89,6 +93,9 @@ private:
// Token mutex // Token mutex
std::mutex _mutex = {}; std::mutex _mutex = {};
// This variable gets notified on token updates
std::condition_variable &_onTokenUpdate;
// The URI of the ldap server // The URI of the ldap server
const std::string _ldapUri; const std::string _ldapUri;
// LDAP bindDN // LDAP bindDN

View File

@ -23,6 +23,8 @@ const static Logger &l = Logger::get();
static std::unique_ptr<Logic> logic = nullptr; static std::unique_ptr<Logic> logic = nullptr;
static boost::asio::io_service io_service; static boost::asio::io_service io_service;
static std::condition_variable onTokenUpdate;
static void signal_handler(int signum) static void signal_handler(int signum)
{ {
l((std::string)"Received Signal " + std::to_string(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(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler); signal(SIGUSR2, signal_handler);
l(LogLevel::info, "Starting Doorlock Logic"); l(LogLevel::info, "Starting Doorlock Logic");
try { try {
logic = std::unique_ptr<Logic>(new Logic(tokenTimeout, logic = std::unique_ptr<Logic>(new Logic(tokenTimeout,
ldapUri, ldapUri,
bindDN, bindDN,
lockPagePrefix, lockPagePrefix,
serDev)); serDev,
onTokenUpdate));
server(port); server(port);
} }
catch (...) { catch (...) {