1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-07-01 11:27:44 +02:00
doorlockd-mirror/doorlockd/logic.h

79 lines
2.0 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"
class Logic
{
public:
2015-05-13 16:40:30 +02:00
Logic(const std::chrono::seconds tokenTimeout,
const std::string &ldapServer,
const std::string &bindDN,
const std::string &webPrefix,
const std::string &allowedIpPrefix);
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
};
Response parseRequest(const std::string &str);
2015-05-11 00:18:22 +02:00
private:
2015-05-12 15:59:04 +02:00
Response _lock();
Response _unlock();
2015-05-11 00:18:22 +02:00
bool _checkToken(const std::string &token);
2015-05-13 16:40:30 +02:00
Response _checkLDAP(const std::string &user,
const std::string &password);
2015-05-12 01:28:02 +02:00
bool _checkIP(const std::string &ip);
2015-05-11 00:18:22 +02:00
2015-05-12 17:35:57 +02:00
void _createNewToken(const bool stillValid);
2015-05-11 00:18:22 +02:00
const Logger &_logger;
Door &_door;
Epaper &_epaper;
using Token = uint64_t;
2015-05-12 16:10:26 +02:00
Token _curToken = { 0x0000000000000000 };
2015-05-11 00:18:22 +02:00
bool _prevValid = { false };
2015-05-12 16:10:26 +02:00
Token _prevToken = { 0x0000000000000000 };
2015-05-11 00:18:22 +02:00
2015-05-12 17:35:57 +02:00
const std::chrono::seconds _tokenTimeout;
2015-05-13 16:40:30 +02:00
const std::string _ldapServer;
const std::string _bindDN;
const std::string _webPrefix;
const std::string _allowedIpPrefix;
2015-05-11 00:18:22 +02:00
2015-05-12 17:35:57 +02:00
std::thread _tokenUpdater = {};
std::condition_variable _c = {};
std::mutex _mutex = {};
bool _run = true;
2015-05-11 00:18:22 +02:00
enum {LOCKED, UNLOCKED} _state = { LOCKED };
};
#endif