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->setState(Idle);
this->ble->startScanningForDevices(); this->ble->startScanningForDevices();
qDebug() << QCryptographicHash::hash("HalloTest", QCryptographicHash::Sha256).toHex();
// dd86fcfda3a20cbb8fbb3026a84550e0d70c2c79e7e8e36d6ffa04b9eef0401f
// dd86fcfda3a20cbb8fbb3026a84550e0d70c2c79e7e8e36d6ffa04b9eef0401f
} }
@ -35,6 +31,15 @@ void OmobiDisplayBackend::startScanning() {
this->ble->startScanningForDevices(); 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){ void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::BluetoothLeUartState state){
switch(state){ switch(state){
case QBluetoothLeUart::Idle: { case QBluetoothLeUart::Idle: {
@ -74,12 +79,8 @@ void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::Bluetooth
} }
void OmobiDisplayBackend::handleBluetoothDeviceConected() { void OmobiDisplayBackend::handleBluetoothDeviceConected() {
this->setState(Initing); this->setState(AuthenticationRequired);
// TODO: stuff
// 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);
} }
void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) { void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) {
@ -149,8 +150,16 @@ void OmobiDisplayBackend::handleBluetoothDataReceived(QString s){
switch (header) { switch (header) {
case AuthorizeSessionCommand: { case AuthorizeSessionCommand: {
// TODO: handle error if(status != Success) {
this->refreshLoadingState(); this->setState(AuthenticationRequired);
return;
}
this->waitingCommands = 0;
this->setState(Initing);
this->sendBluetoothCommand(GetAllTextSetsCommand);
this->sendBluetoothCommand(GetDisplayBrightnessCommand);
break; break;
} }
case KeepAliveCommand: { case KeepAliveCommand: {

View file

@ -25,6 +25,8 @@ public:
Scanning, Scanning,
ReadyToConnect, ReadyToConnect,
Connecting, Connecting,
AuthenticationRequired,
Authenticating,
Initing, Initing,
Connected, Connected,
Loading Loading
@ -57,6 +59,7 @@ private:
public slots: public slots:
Q_INVOKABLE void startScanning(); Q_INVOKABLE void startScanning();
Q_INVOKABLE void authenticate(QString secret);
Q_INVOKABLE QBluetoothLeUart* getBleController(); Q_INVOKABLE QBluetoothLeUart* getBleController();
Q_INVOKABLE OmobiDisplayAppState getState(); Q_INVOKABLE OmobiDisplayAppState getState();
Q_INVOKABLE OmobiDisplayTextModel* getDisplayTextModel(); 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: [ states: [
State { State {
name: OmobiDisplayBackend.Idle name: OmobiDisplayBackend.Idle
@ -154,6 +193,41 @@ Page {
working: false 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 { State {
name: OmobiDisplayBackend.Connecting name: OmobiDisplayBackend.Connecting

View file

@ -73,6 +73,8 @@ ItemDelegate {
placeholderText: control.placeholderText placeholderText: control.placeholderText
text: control.value text: control.value
Keys.onReturnPressed: textEditDialog.accept()
} }
onAccepted: { 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 { State {
name: OmobiDisplayBackend.Initing name: OmobiDisplayBackend.Initing
PropertyChanges { PropertyChanges {