abstracted totp

This commit is contained in:
2025-07-07 12:06:38 +02:00
parent b1ac0532c8
commit b93597ee26
2 changed files with 61 additions and 73 deletions

51
src/lock.hpp Normal file
View 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

View File

@@ -11,18 +11,16 @@
#include <WiFi.h>
#include <NTPClient.h>
#include <HTTPClient.h>
#include <totp.hpp>
#include "display.hpp"
#include "keypad.hpp"
#include "input.hpp"
#include "status.hpp"
#include "lock.hpp"
#include "touch.hpp"
#include "settings.hpp"
#include "mqtt.hpp"
static WiFiUDP ntpUDP;
static NTPClient timeClient(ntpUDP);
static MQTT *mqtt;
static Display display;
@@ -30,6 +28,7 @@ static Keypad *keypad;
static Status *status;
static Input *input;
static Touch *touch;
static Lock *keylock;
void keypad_state(bool enabled)
{
@@ -74,10 +73,7 @@ void unlock()
void submit(String code)
{
uint32_t newCode = getCodeFromTimestamp(timeClient.getEpochTime());
uint32_t input = code.toInt();
if (newCode != input)
if (!keylock->check(code))
{
status->set("Invalid");
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)
{
switch (v)
@@ -191,16 +174,19 @@ void setup()
status->set("Settings Error");
return;
}
mqtt = new MQTT(settings, mqtt_callback);
initWifi(settings);
keylock = new Lock(settings);
mqtt = new MQTT(settings, mqtt_callback);
mqtt->connect();
timeClient.begin();
initTOTP(settings);
keylock->begin();
}
void loop()
{
timeClient.update();
mqtt->loop();
auto tap = touch->tap();
@@ -211,52 +197,3 @@ void loop()
input->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());
// }