Added authentication on App side

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

View file

@ -24,10 +24,6 @@ OmobiDisplayBackend::OmobiDisplayBackend(QObject *parent) : QObject(parent)
this->setState(Idle);
this->ble->startScanningForDevices();
qDebug() << QCryptographicHash::hash("HalloTest", QCryptographicHash::Sha256).toHex();
// dd86fcfda3a20cbb8fbb3026a84550e0d70c2c79e7e8e36d6ffa04b9eef0401f
// dd86fcfda3a20cbb8fbb3026a84550e0d70c2c79e7e8e36d6ffa04b9eef0401f
}
@ -35,6 +31,15 @@ void OmobiDisplayBackend::startScanning() {
this->ble->startScanningForDevices();
}
void OmobiDisplayBackend::authenticate(QString code) {
// tell display to send over existing model data
this->setState(Authenticating);
QString combinedCode = this->ble->getCurrentDevice()->getAddress().toUpper() + code;
QString secret = QCryptographicHash::hash(combinedCode.toUtf8(), QCryptographicHash::Sha256).toHex();
this->sendBluetoothCommand(AuthorizeSessionCommand, QVariantMap{{"secret", secret}});
}
void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::BluetoothLeUartState state){
switch(state){
case QBluetoothLeUart::Idle: {
@ -74,12 +79,8 @@ void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::Bluetooth
}
void OmobiDisplayBackend::handleBluetoothDeviceConected() {
this->setState(Initing);
// tell display to send over existing model data
this->sendBluetoothCommand(AuthorizeSessionCommand, QVariantMap{{"secret", QCryptographicHash::hash("1234", QCryptographicHash::Sha256).toHex()}});
this->sendBluetoothCommand(GetAllTextSetsCommand);
this->sendBluetoothCommand(GetDisplayBrightnessCommand);
this->setState(AuthenticationRequired);
// TODO: stuff
}
void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) {
@ -149,8 +150,16 @@ void OmobiDisplayBackend::handleBluetoothDataReceived(QString s){
switch (header) {
case AuthorizeSessionCommand: {
// TODO: handle error
this->refreshLoadingState();
if(status != Success) {
this->setState(AuthenticationRequired);
return;
}
this->waitingCommands = 0;
this->setState(Initing);
this->sendBluetoothCommand(GetAllTextSetsCommand);
this->sendBluetoothCommand(GetDisplayBrightnessCommand);
break;
}
case KeepAliveCommand: {

View file

@ -25,6 +25,8 @@ public:
Scanning,
ReadyToConnect,
Connecting,
AuthenticationRequired,
Authenticating,
Initing,
Connected,
Loading
@ -57,6 +59,7 @@ private:
public slots:
Q_INVOKABLE void startScanning();
Q_INVOKABLE void authenticate(QString secret);
Q_INVOKABLE QBluetoothLeUart* getBleController();
Q_INVOKABLE OmobiDisplayAppState getState();
Q_INVOKABLE OmobiDisplayTextModel* getDisplayTextModel();

View file

@ -126,6 +126,45 @@ Page {
}
}
Dialog {
id: authenticationDialog
property bool shouldBeOpened: false
parent: Overlay.overlay
x: (parent.width - width) / 2
y: (parent.height - height) / 2
width: parent.width * 0.9
modal: true
closePolicy: Popup.NoAutoClose
standardButtons: Dialog.Ok | Dialog.Cancel
title: qsTr("Input code")
onShouldBeOpenedChanged: {
if(shouldBeOpened)
open()
else
close()
}
onAccepted: {
backend.authenticate(secretTextInput.text)
}
onRejected: {
backend.bleController.disconnectFromDevice()
}
contentItem: TextField {
id: secretTextInput
placeholderText: qsTr("code")
Keys.onReturnPressed: authenticationDialog.accept()
}
}
states: [
State {
name: OmobiDisplayBackend.Idle
@ -154,6 +193,41 @@ Page {
working: false
}
},
State {
name: OmobiDisplayBackend.AuthenticationRequired
PropertyChanges {
target: authenticationDialog
shouldBeOpened: true
}
PropertyChanges {
target: availableDevicesListView
enabled: false
}
PropertyChanges {
target: root
statusText: "trying to authenticate..."
working: true
}
},
State {
name: OmobiDisplayBackend.Authenticating
PropertyChanges {
target: availableDevicesListView
enabled: false
}
PropertyChanges {
target: root
statusText: "trying to authenticate..."
working: true
}
},
State {
name: OmobiDisplayBackend.Connecting

View file

@ -73,6 +73,8 @@ ItemDelegate {
placeholderText: control.placeholderText
text: control.value
Keys.onReturnPressed: textEditDialog.accept()
}
onAccepted: {

View file

@ -104,6 +104,22 @@ ApplicationWindow {
}
},
State {
name: OmobiDisplayBackend.AuthenticationRequired
PropertyChanges {
target: mainStack
currentComponent: connectPageComp
}
},
State {
name: OmobiDisplayBackend.Authenticating
PropertyChanges {
target: mainStack
currentComponent: connectPageComp
}
},
State {
name: OmobiDisplayBackend.Initing
PropertyChanges {