From b93597ee2608dd6a7e9577210475ff509b88bdc1 Mon Sep 17 00:00:00 2001 From: Avii Date: Mon, 7 Jul 2025 12:06:38 +0200 Subject: [PATCH] abstracted totp --- src/lock.hpp | 51 ++++++++++++++++++++++++++++++++ src/main.cpp | 83 +++++++--------------------------------------------- 2 files changed, 61 insertions(+), 73 deletions(-) create mode 100644 src/lock.hpp diff --git a/src/lock.hpp b/src/lock.hpp new file mode 100644 index 0000000..9c58fde --- /dev/null +++ b/src/lock.hpp @@ -0,0 +1,51 @@ +#ifndef _LOCK_H_ +#define _LOCK_H_ + +#include +#include +#include + +#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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6fe7aae..c78b454 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,18 +11,16 @@ #include #include #include -#include #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()); -// }