Merge PR #230: Add support for OAuth during thing setup
This commit is contained in:
commit
1fdb1a450f
@ -297,6 +297,7 @@ void DeviceManager::pairDeviceResponse(const QVariantMap ¶ms)
|
||||
|
||||
void DeviceManager::confirmPairingResponse(const QVariantMap ¶ms)
|
||||
{
|
||||
qDebug() << "ConfirmPairingResponse" << params;
|
||||
emit confirmPairingReply(params.value("params").toMap());
|
||||
}
|
||||
|
||||
@ -362,12 +363,25 @@ void DeviceManager::pairDevice(const QUuid &deviceClassId, const QUuid &deviceDe
|
||||
m_jsonClient->sendCommand("Devices.PairDevice", params, this, "pairDeviceResponse");
|
||||
}
|
||||
|
||||
void DeviceManager::confirmPairing(const QUuid &pairingTransactionId, const QString &secret)
|
||||
void DeviceManager::pairDevice(const QUuid &deviceClassId, const QString &name, const QVariantList &deviceParams)
|
||||
{
|
||||
qDebug() << "JsonRpc: pair device " << deviceClassId.toString();
|
||||
QVariantMap params;
|
||||
params.insert("name", name);
|
||||
params.insert("deviceClassId", deviceClassId.toString());
|
||||
params.insert("deviceParams", deviceParams);
|
||||
m_jsonClient->sendCommand("Devices.PairDevice", params, this, "pairDeviceResponse");
|
||||
}
|
||||
|
||||
void DeviceManager::confirmPairing(const QUuid &pairingTransactionId, const QString &secret, const QString &username)
|
||||
{
|
||||
qDebug() << "JsonRpc: confirm pairing" << pairingTransactionId.toString();
|
||||
QVariantMap params;
|
||||
params.insert("pairingTransactionId", pairingTransactionId.toString());
|
||||
params.insert("secret", secret);
|
||||
if (!username.isEmpty()) {
|
||||
params.insert("username", username);
|
||||
}
|
||||
m_jsonClient->sendCommand("Devices.ConfirmPairing", params, this, "confirmPairingResponse");
|
||||
}
|
||||
|
||||
|
||||
@ -69,7 +69,8 @@ public:
|
||||
Q_INVOKABLE void addDevice(const QUuid &deviceClassId, const QString &name, const QVariantList &deviceParams);
|
||||
Q_INVOKABLE void addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name, const QVariantList &deviceParams);
|
||||
Q_INVOKABLE void pairDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name);
|
||||
Q_INVOKABLE void confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString());
|
||||
Q_INVOKABLE void pairDevice(const QUuid &deviceClassId, const QString &name, const QVariantList &deviceParams);
|
||||
Q_INVOKABLE void confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString(), const QString &username = QString());
|
||||
Q_INVOKABLE void removeDevice(const QUuid &deviceId, RemovePolicy policy = RemovePolicyNone);
|
||||
Q_INVOKABLE void editDevice(const QUuid &deviceId, const QString &name);
|
||||
Q_INVOKABLE void setDeviceSettings(const QUuid &deviceId, const QVariantList &settings);
|
||||
|
||||
@ -492,6 +492,10 @@ DeviceClass::SetupMethod JsonTypes::stringToSetupMethod(const QString &setupMeth
|
||||
return DeviceClass::SetupMethodEnterPin;
|
||||
} else if (setupMethodString == "SetupMethodPushButton") {
|
||||
return DeviceClass::SetupMethodPushButton;
|
||||
} else if (setupMethodString == "SetupMethodOAuth") {
|
||||
return DeviceClass::SetupMethodOAuth;
|
||||
} else if (setupMethodString == "SetupMethodUserAndPassword") {
|
||||
return DeviceClass::SetupMethodUserAndPassword;
|
||||
}
|
||||
return DeviceClass::SetupMethodJustAdd;
|
||||
}
|
||||
|
||||
@ -61,7 +61,9 @@ public:
|
||||
SetupMethodJustAdd,
|
||||
SetupMethodDisplayPin,
|
||||
SetupMethodEnterPin,
|
||||
SetupMethodPushButton
|
||||
SetupMethodPushButton,
|
||||
SetupMethodOAuth,
|
||||
SetupMethodUserAndPassword,
|
||||
};
|
||||
Q_ENUM(SetupMethod)
|
||||
|
||||
|
||||
@ -3,6 +3,8 @@ import QtQuick.Layouts 1.1
|
||||
import QtQuick.Controls 2.1
|
||||
import QtQuick.Controls.Material 2.1
|
||||
import Nymea 1.0
|
||||
import QtWebView 1.1
|
||||
|
||||
import "../components"
|
||||
import "../delegates"
|
||||
|
||||
@ -60,23 +62,31 @@ Page {
|
||||
target: engine.deviceManager
|
||||
onPairDeviceReply: {
|
||||
busyOverlay.shown = false
|
||||
if (params["deviceError"] !== "DeviceErrorNoError") {
|
||||
busyOverlay.shown = false;
|
||||
internalPageStack.push(resultsPage, {success: false})
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
d.pairingTransactionId = params["pairingTransactionId"];
|
||||
|
||||
switch (params["setupMethod"]) {
|
||||
case "SetupMethodPushButton":
|
||||
d.pairingTransactionId = params["pairingTransactionId"];
|
||||
print("response", params["displayMessage"], d.pairingTransactionId)
|
||||
internalPageStack.push(pairingPageComponent, {text: params["displayMessage"]})
|
||||
break;
|
||||
case "SetupMethodDisplayPin":
|
||||
d.pairingTransactionId = params["pairingTransactionId"];
|
||||
case "SetupMethodUserAndPassword":
|
||||
internalPageStack.push(pairingPageComponent, {text: params["displayMessage"], setupMethod: params["setupMethod"]})
|
||||
break;
|
||||
case "SetupMethodOAuth":
|
||||
internalPageStack.push(oAuthPageComponent, {oAuthUrl: params["oAuthUrl"]})
|
||||
break;
|
||||
default:
|
||||
print("Setup method", params["setupMethod"], "not handled");
|
||||
print("Setup method reply not handled:", JSON.stringify(params));
|
||||
}
|
||||
}
|
||||
onConfirmPairingReply: {
|
||||
busyOverlay.shown = false
|
||||
internalPageStack.push(resultsPage, {success: params["deviceError"] === "DeviceErrorNoError", deviceId: params["deviceId"]})
|
||||
internalPageStack.push(resultsPage, {success: params["deviceError"] === "DeviceErrorNoError", deviceId: params["deviceId"], message: params["displayMessage"]})
|
||||
}
|
||||
onAddDeviceReply: {
|
||||
busyOverlay.shown = false;
|
||||
@ -364,7 +374,24 @@ Page {
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
engine.deviceManager.pairDevice(root.deviceClass.id, d.deviceDescriptor.id, nameTextField.text);
|
||||
case 4:
|
||||
case 5:
|
||||
if (root.device) {
|
||||
// if (d.deviceDescriptor) {
|
||||
// engine.deviceManager.pairDevice(root.deviceClass.id, d.deviceDescriptor.id, nameTextField.text);
|
||||
// } else {
|
||||
// engine.deviceManager.pairDevice(root.deviceClass.id, nameTextField.text, params);
|
||||
// }
|
||||
console.warn("Unhandled setupMethod!")
|
||||
return;
|
||||
} else {
|
||||
if (d.deviceDescriptor) {
|
||||
engine.deviceManager.pairDevice(root.deviceClass.id, d.deviceDescriptor.id, nameTextField.text);
|
||||
} else {
|
||||
engine.deviceManager.pairDevice(root.deviceClass.id, nameTextField.text, params);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@ -405,17 +432,48 @@ Page {
|
||||
wrapMode: Text.WordWrap
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
}
|
||||
|
||||
TextField {
|
||||
id: usernameTextField
|
||||
Layout.fillWidth: true
|
||||
visible: pairingPage.setupMethod === "SetupMethodUserAndPassword"
|
||||
}
|
||||
|
||||
TextField {
|
||||
id: pinTextField
|
||||
Layout.fillWidth: true
|
||||
visible: pairingPage.setupMethod === "SetupMethodDisplayPin"
|
||||
visible: pairingPage.setupMethod === "SetupMethodDisplayPin" || pairingPage.setupMethod === "SetupMethodUserAndPassword"
|
||||
echoMode: TextField.Password
|
||||
}
|
||||
|
||||
|
||||
Button {
|
||||
Layout.fillWidth: true
|
||||
text: "OK"
|
||||
onClicked: {
|
||||
engine.deviceManager.confirmPairing(d.pairingTransactionId, pinTextField.displayText);
|
||||
engine.deviceManager.confirmPairing(d.pairingTransactionId, pinTextField.text, usernameTextField.displayText);
|
||||
busyOverlay.shown = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Component {
|
||||
id: oAuthPageComponent
|
||||
Page {
|
||||
id: oAuthPage
|
||||
property alias oAuthUrl: oAuthWebView.url
|
||||
|
||||
WebView {
|
||||
id: oAuthWebView
|
||||
anchors.fill: parent
|
||||
|
||||
onUrlChanged: {
|
||||
print("OAUTH URL changed", url)
|
||||
if (url.toString().indexOf("https://127.0.0.1") == 0) {
|
||||
print("Redirect URL detected!");
|
||||
engine.deviceManager.confirmPairing(d.pairingTransactionId, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -430,6 +488,7 @@ Page {
|
||||
|
||||
property bool success
|
||||
property string deviceId
|
||||
property string message
|
||||
|
||||
readonly property var device: root.device ? root.device : engine.deviceManager.devices.getDevice(deviceId)
|
||||
|
||||
@ -452,6 +511,13 @@ Page {
|
||||
text: resultsView.success ? qsTr("All done. You can now start using %1.").arg(resultsView.device.name) : qsTr("Something went wrong setting up this thing...");
|
||||
}
|
||||
|
||||
Label {
|
||||
Layout.fillWidth: true
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
wrapMode: Text.WordWrap
|
||||
text: resultsView.message
|
||||
}
|
||||
|
||||
Button {
|
||||
Layout.fillWidth: true
|
||||
text: qsTr("Ok")
|
||||
|
||||
Reference in New Issue
Block a user