Added authentication on ESP side

This commit is contained in:
Dorian Zedler 2020-10-17 01:08:43 +02:00
parent 458eba1f8f
commit 2edc225c35
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
6 changed files with 32 additions and 23 deletions

View file

@ -62,6 +62,8 @@ public:
bool getDeviceConnected(); bool getDeviceConnected();
bool disconnectCurrentDevice(); bool disconnectCurrentDevice();
String getDeviceAddress();
protected: protected:
// callbacks for BLEServer // callbacks for BLEServer
void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override; void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override;

View file

@ -27,25 +27,22 @@ private:
template <typename T> template <typename T>
const T &writeToEeprom(int address, size_t size, const T &t) const T &writeToEeprom(int address, size_t size, const T &t)
{ {
Serial.println("Writing at: " + String(address) + " size: " + String(sizeof(T)));
if (sizeof(T) > size) { if (sizeof(T) > size) {
Serial.println("Error writing: Size should be: " + String(size)); Serial.println("[Error][EepromManager] writing: Size should be: " + String(size) + " but was: " + String(sizeof(T)));
return t; return t;
} }
const T &res = EEPROM.put(address, t); const T &res = EEPROM.put(address, t);
Serial.println("Eeprom commit returned: " + String(EEPROM.commit())); EEPROM.commit();
return res; return res;
} }
template <typename T> template <typename T>
T &readFromEeprom(int address, size_t size, T &t) T &readFromEeprom(int address, size_t size, T &t)
{ {
Serial.println("Reading at: " + String(address) + " size: " + String(sizeof(T)));
if (sizeof(T) > size) { if (sizeof(T) > size) {
Serial.println("Error reading: Size should be: " + String(size)); Serial.println("[Error][EepromManager] reading: Size should be: " + String(size) + " but was: " + String(sizeof(T)));
return t; return t;
} }

View file

@ -56,6 +56,27 @@ void BluetoothLeUartServer::sendData(String data)
txCharacteristic->notify(); txCharacteristic->notify();
} }
bool BluetoothLeUartServer::getDeviceConnected()
{
return this->deviceConnected;
}
bool BluetoothLeUartServer::disconnectCurrentDevice() {
if(!this->getDeviceConnected())
return false;
this->bleServer->disconnect(this->deviceConnectionId);
return true;
}
String BluetoothLeUartServer::getDeviceAddress() {
String address = BLEDevice::getAddress().toString().c_str();
address.toUpperCase();
return address;
}
void BluetoothLeUartServer::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) void BluetoothLeUartServer::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param)
{ {
// only allow one device // only allow one device
@ -84,17 +105,4 @@ void BluetoothLeUartServer::onWrite(BLECharacteristic *rxCharacteristic)
{ {
if (this->callbacks != nullptr) if (this->callbacks != nullptr)
this->callbacks->onDataReceived(rxCharacteristic->getValue().c_str()); this->callbacks->onDataReceived(rxCharacteristic->getValue().c_str());
}
bool BluetoothLeUartServer::getDeviceConnected()
{
return this->deviceConnected;
}
bool BluetoothLeUartServer::disconnectCurrentDevice() {
if(!this->getDeviceConnected())
return false;
this->bleServer->disconnect(this->deviceConnectionId);
return true;
} }

View file

@ -8,7 +8,6 @@ EepromManager::EepromManager()
EepromUnit *EepromManager::registerEempromUnit(size_t size) EepromUnit *EepromManager::registerEempromUnit(size_t size)
{ {
Serial.println("Registering new EepromUnit with size: " + String(size) + " at " + String(this->currentAddressEnding));
// create a new Unit at the current address ending // create a new Unit at the current address ending
EepromUnit *newUnit = new EepromUnit(this, this->currentAddressEnding, size); EepromUnit *newUnit = new EepromUnit(this, this->currentAddressEnding, size);
// move the new address ending // move the new address ending

View file

@ -65,18 +65,21 @@ void OmobiLedDisplay::onDataReceived(String dataString)
OmobiDisplayStatusCode replyStatus = InternalError; OmobiDisplayStatusCode replyStatus = InternalError;
JsonObject replyData = replyDoc.createNestedObject("data"); JsonObject replyData = replyDoc.createNestedObject("data");
if (requestHeader != AuthorizeSessionCommand && !this->sessionAuthorized) if (requestHeader > KeepAliveCommand && !this->sessionAuthorized)
replyStatus = Unauthorized; replyStatus = Unauthorized;
else else
switch (requestHeader) switch (requestHeader)
{ {
case AuthorizeSessionCommand: case AuthorizeSessionCommand:
{ {
String combinedCode = this->bleServer->getDeviceAddress() + String(this->properties.deviceCode);
String secret = this->sha256(combinedCode);
if (this->sessionAuthorized) if (this->sessionAuthorized)
{ {
replyStatus = Success; replyStatus = Success;
} }
else if (requestData["secret"] == this->sha256(this->properties.deviceCode)) else if (requestData["secret"] == secret)
{ {
replyStatus = Success; replyStatus = Success;
this->sessionAuthorized = true; this->sessionAuthorized = true;

View file

@ -13,7 +13,7 @@ void setup()
Adafruit_NeoMatrix *displayMatrix = new Adafruit_NeoMatrix( Adafruit_NeoMatrix *displayMatrix = new Adafruit_NeoMatrix(
8, 8,
8, 8,
1, 2,
1, 1,
PIN, PIN,
NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE + NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE +