1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-09-16 03:09:50 +02:00
doorlockd-mirror/doorlockd/logic.h

105 lines
2.9 KiB
C
Raw Normal View History

2015-05-11 00:18:22 +02:00
#ifndef LOGIC_H
#define LOGIC_H
#include <cstdint>
#include <string>
2015-05-12 17:35:57 +02:00
#include <thread>
#include <condition_variable>
#include <mutex>
2015-05-11 00:18:22 +02:00
#include "config.h"
#include "epaper.h"
#include "door.h"
#include "logger.h"
2015-05-21 13:35:30 +02:00
/* The "Logic" class
*
* This class is initilized by all settings.
*
* It parses incoming JSON-Requests and returns the Response Code.
*/
2015-05-11 00:18:22 +02:00
class Logic
{
public:
2015-05-13 16:40:30 +02:00
Logic(const std::chrono::seconds tokenTimeout,
2015-05-24 19:15:47 +02:00
const std::string &ldapUri,
2015-05-13 16:40:30 +02:00
const std::string &bindDN,
2015-05-21 13:35:30 +02:00
const std::string &webPrefix);
2015-05-11 00:18:22 +02:00
~Logic();
2015-05-12 15:59:04 +02:00
enum Response {
Success = 0, // Request successful
Fail, // General non-specified error
AlreadyUnlocked, // Authentication successful, but door is already unlocked
AlreadyLocked, // Authentication successful, but door is already locked
NotJson, // Request is not a valid JSON object
JsonError, // Request is valid JSON, but does not contain necessary material
InvalidToken, // Request contains invalid token
InvalidCredentials, // Invalid LDAP credentials
InvalidIP, // IP check failure
UnknownAction, // Unknown action
LDAPInit, // Ldap initialization failed
};
2015-05-21 13:35:30 +02:00
// Parse incoming JSON Requests
2015-05-12 15:59:04 +02:00
Response parseRequest(const std::string &str);
2015-05-11 00:18:22 +02:00
private:
2015-05-21 13:35:30 +02:00
// Internal lock wrapper
2015-05-12 15:59:04 +02:00
Response _lock();
2015-05-21 13:35:30 +02:00
// Internal unlock wrapper
2015-05-12 15:59:04 +02:00
Response _unlock();
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
// Checks if the incoming token is valid
2015-05-11 00:18:22 +02:00
bool _checkToken(const std::string &token);
2015-05-21 13:35:30 +02:00
// Checks if incoming credentials against LDAP
2015-05-13 16:40:30 +02:00
Response _checkLDAP(const std::string &user,
const std::string &password);
2015-05-21 13:35:30 +02:00
// Creates a new random token and draws it on the epaper.
// stillValid indicates whether the old (previous) token is still valid
2015-05-12 17:35:57 +02:00
void _createNewToken(const bool stillValid);
2015-05-11 00:18:22 +02:00
const Logger &_logger;
2015-05-21 13:35:30 +02:00
// Door reference
2015-05-11 00:18:22 +02:00
Door &_door;
2015-05-21 13:35:30 +02:00
// Epaper reference
2015-05-11 00:18:22 +02:00
Epaper &_epaper;
2015-05-21 13:35:30 +02:00
// Tokens are 64-bit hexadecimal values
2015-05-11 00:18:22 +02:00
using Token = uint64_t;
2015-05-21 13:35:30 +02:00
// The current token
2015-05-12 16:10:26 +02:00
Token _curToken = { 0x0000000000000000 };
2015-05-21 13:35:30 +02:00
// The previous token
2015-05-12 16:10:26 +02:00
Token _prevToken = { 0x0000000000000000 };
2015-05-21 13:35:30 +02:00
// Indicates whether the previous token is valid
bool _prevValid = { false };
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
// Tokens are refreshed all tokenTimout seconds
2015-05-12 17:35:57 +02:00
const std::chrono::seconds _tokenTimeout;
2015-05-21 13:35:30 +02:00
// Thread for asynchronosly updating tokens
std::thread _tokenUpdater = {};
// Thread can be force-triggered for updates using the condition variable
std::condition_variable _tokenCondition = {};
// stop indicator for the thread
bool _run = true;
// Token mutex
std::mutex _mutex = {};
// The URI of the ldap server
2015-05-24 19:15:47 +02:00
const std::string _ldapUri;
2015-05-21 13:35:30 +02:00
// LDAP bindDN
2015-05-13 16:40:30 +02:00
const std::string _bindDN;
2015-05-21 13:35:30 +02:00
// Prefix of the website
2015-05-13 16:40:30 +02:00
const std::string _webPrefix;
2015-05-11 00:18:22 +02:00
2015-05-21 13:35:30 +02:00
Door::State _state = { Door::State::Locked };
2015-05-11 00:18:22 +02:00
};
#endif