abstracted totp
This commit is contained in:
51
src/lock.hpp
Normal file
51
src/lock.hpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#ifndef _LOCK_H_
|
||||||
|
#define _LOCK_H_
|
||||||
|
|
||||||
|
#include <totp.hpp>
|
||||||
|
#include <NTPClient.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
|
||||||
|
#include "settings.hpp"
|
||||||
|
|
||||||
|
static WiFiUDP udp;
|
||||||
|
|
||||||
|
class Lock
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Lock(Settings *settings) : settings{settings}
|
||||||
|
{
|
||||||
|
timeClient = new NTPClient(udp);
|
||||||
|
|
||||||
|
String hmac_str = settings->gettext("totp:hmac");
|
||||||
|
int hmac_len = hmac_str.length();
|
||||||
|
|
||||||
|
uint8_t *base32_key = new uint8_t[hmac_len];
|
||||||
|
memcpy(base32_key, hmac_str.c_str(), hmac_len);
|
||||||
|
|
||||||
|
TOTP(base32_key, 20, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
~Lock() = default;
|
||||||
|
|
||||||
|
void begin()
|
||||||
|
{
|
||||||
|
timeClient->begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool check(String code)
|
||||||
|
{
|
||||||
|
timeClient->update();
|
||||||
|
uint32_t newCode = getCodeFromTimestamp(timeClient->getEpochTime());
|
||||||
|
uint32_t input = code.toInt();
|
||||||
|
|
||||||
|
return newCode == input;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
NTPClient *timeClient;
|
||||||
|
Settings *settings;
|
||||||
|
// MQTT *mqtt;
|
||||||
|
// TOTP *totp; // todo: convert to c++ class
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
83
src/main.cpp
83
src/main.cpp
@@ -11,18 +11,16 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <totp.hpp>
|
|
||||||
|
|
||||||
#include "display.hpp"
|
#include "display.hpp"
|
||||||
#include "keypad.hpp"
|
#include "keypad.hpp"
|
||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
#include "status.hpp"
|
#include "status.hpp"
|
||||||
|
#include "lock.hpp"
|
||||||
#include "touch.hpp"
|
#include "touch.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
#include "mqtt.hpp"
|
#include "mqtt.hpp"
|
||||||
|
|
||||||
static WiFiUDP ntpUDP;
|
|
||||||
static NTPClient timeClient(ntpUDP);
|
|
||||||
static MQTT *mqtt;
|
static MQTT *mqtt;
|
||||||
|
|
||||||
static Display display;
|
static Display display;
|
||||||
@@ -30,6 +28,7 @@ static Keypad *keypad;
|
|||||||
static Status *status;
|
static Status *status;
|
||||||
static Input *input;
|
static Input *input;
|
||||||
static Touch *touch;
|
static Touch *touch;
|
||||||
|
static Lock *keylock;
|
||||||
|
|
||||||
void keypad_state(bool enabled)
|
void keypad_state(bool enabled)
|
||||||
{
|
{
|
||||||
@@ -74,10 +73,7 @@ void unlock()
|
|||||||
|
|
||||||
void submit(String code)
|
void submit(String code)
|
||||||
{
|
{
|
||||||
uint32_t newCode = getCodeFromTimestamp(timeClient.getEpochTime());
|
if (!keylock->check(code))
|
||||||
uint32_t input = code.toInt();
|
|
||||||
|
|
||||||
if (newCode != input)
|
|
||||||
{
|
{
|
||||||
status->set("Invalid");
|
status->set("Invalid");
|
||||||
return;
|
return;
|
||||||
@@ -134,19 +130,6 @@ void initWifi(Settings *settings)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void initTOTP(Settings *settings)
|
|
||||||
{
|
|
||||||
uint8_t *base32_key = new uint8_t[20];
|
|
||||||
const char *hmac = ((String)settings->gettext("totp:hmac")).c_str();
|
|
||||||
|
|
||||||
for (int i = 0; i < 20; i++)
|
|
||||||
{
|
|
||||||
base32_key[i] = (uint8_t)hmac[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
TOTP(base32_key, 20, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
void p(int v)
|
void p(int v)
|
||||||
{
|
{
|
||||||
switch (v)
|
switch (v)
|
||||||
@@ -191,16 +174,19 @@ void setup()
|
|||||||
status->set("Settings Error");
|
status->set("Settings Error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mqtt = new MQTT(settings, mqtt_callback);
|
|
||||||
initWifi(settings);
|
initWifi(settings);
|
||||||
|
|
||||||
|
keylock = new Lock(settings);
|
||||||
|
|
||||||
|
mqtt = new MQTT(settings, mqtt_callback);
|
||||||
mqtt->connect();
|
mqtt->connect();
|
||||||
timeClient.begin();
|
|
||||||
initTOTP(settings);
|
keylock->begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
timeClient.update();
|
|
||||||
mqtt->loop();
|
mqtt->loop();
|
||||||
|
|
||||||
auto tap = touch->tap();
|
auto tap = touch->tap();
|
||||||
@@ -211,52 +197,3 @@ void loop()
|
|||||||
input->draw(&display);
|
input->draw(&display);
|
||||||
status->draw(&display);
|
status->draw(&display);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static Keypad *keypad;
|
|
||||||
|
|
||||||
// void setup()
|
|
||||||
// {
|
|
||||||
// M5.begin();
|
|
||||||
|
|
||||||
// keypad = new Keypad();
|
|
||||||
// status->set("loading");
|
|
||||||
|
|
||||||
// auto settings = new Settings();
|
|
||||||
|
|
||||||
// if (!settings)
|
|
||||||
// {
|
|
||||||
// status->set("unable to load settings");
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// mqtt = new MQTT(settings, mqtt_callback);
|
|
||||||
|
|
||||||
// initWifi(settings);
|
|
||||||
// mqtt->connect();
|
|
||||||
// timeClient.begin();
|
|
||||||
// initTOTP(settings);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// void submit(String code)
|
|
||||||
// {
|
|
||||||
// uint32_t newCode = getCodeFromTimestamp(timeClient.getEpochTime());
|
|
||||||
// uint32_t input = code.toInt();
|
|
||||||
|
|
||||||
// if (newCode == input)
|
|
||||||
// {
|
|
||||||
// unlock();
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// lock();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// void loop()
|
|
||||||
// {
|
|
||||||
// timeClient.update();
|
|
||||||
// mqtt->loop();
|
|
||||||
|
|
||||||
// auto value = keypad->get_input();
|
|
||||||
// if (value.has_value())
|
|
||||||
// submit(value.value());
|
|
||||||
// }
|
|
||||||
|
Reference in New Issue
Block a user