improve aws connection setup flow
This commit is contained in:
parent
cbb834b92c
commit
cd9c668f6e
@ -228,21 +228,6 @@ void AWSClient::login(const QString &username, const QString &password, int atte
|
|||||||
QJsonDocument tokenPayloadJsonDoc = QJsonDocument::fromJson(QByteArray::fromBase64(jwtParts.at(1)));
|
QJsonDocument tokenPayloadJsonDoc = QJsonDocument::fromJson(QByteArray::fromBase64(jwtParts.at(1)));
|
||||||
m_userId = tokenPayloadJsonDoc.toVariant().toMap().value("cognito:username").toByteArray();
|
m_userId = tokenPayloadJsonDoc.toVariant().toMap().value("cognito:username").toByteArray();
|
||||||
|
|
||||||
QSettings settings;
|
|
||||||
settings.remove("cloud");
|
|
||||||
|
|
||||||
settings.beginGroup("cloud");
|
|
||||||
settings.setValue("username", m_username);
|
|
||||||
settings.setValue("userId", m_userId);
|
|
||||||
settings.setValue("password", m_password);
|
|
||||||
settings.setValue("accessToken", m_accessToken);
|
|
||||||
settings.setValue("accessTokenExpiry", m_accessTokenExpiry);
|
|
||||||
settings.setValue("idToken", m_idToken);
|
|
||||||
settings.setValue("refreshToken", m_refreshToken);
|
|
||||||
|
|
||||||
qDebug() << "AWS login successful. Userid:" << m_userId;// << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented));
|
|
||||||
emit isLoggedInChanged();
|
|
||||||
|
|
||||||
qDebug() << "Getting cognito ID";
|
qDebug() << "Getting cognito ID";
|
||||||
getId();
|
getId();
|
||||||
});
|
});
|
||||||
@ -486,7 +471,7 @@ void AWSClient::deleteAccount()
|
|||||||
if (tokensExpired()) {
|
if (tokensExpired()) {
|
||||||
qDebug() << "Cannot unpair device. Need to refresh our tokens";
|
qDebug() << "Cannot unpair device. Need to refresh our tokens";
|
||||||
refreshAccessToken();
|
refreshAccessToken();
|
||||||
m_callQueue.append(QueuedCall("deleteAccount"));
|
QueuedCall::enqueue(m_callQueue, QueuedCall("deleteAccount"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
qDebug() << "Deleting account";
|
qDebug() << "Deleting account";
|
||||||
@ -533,7 +518,7 @@ void AWSClient::unpairDevice(const QString &boxId)
|
|||||||
if (tokensExpired()) {
|
if (tokensExpired()) {
|
||||||
qDebug() << "Cannot unpair device. Need to refresh our tokens";
|
qDebug() << "Cannot unpair device. Need to refresh our tokens";
|
||||||
refreshAccessToken();
|
refreshAccessToken();
|
||||||
m_callQueue.append(QueuedCall("unpairDevice", boxId));
|
QueuedCall::enqueue(m_callQueue, QueuedCall("unpairDevice", boxId));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
qDebug() << "unpairing device";
|
qDebug() << "unpairing device";
|
||||||
@ -624,8 +609,8 @@ void AWSClient::registerPushNotificationEndpoint(const QString ®istrationId,
|
|||||||
}
|
}
|
||||||
if (tokensExpired()) {
|
if (tokensExpired()) {
|
||||||
qDebug() << "Cannot register push endpoint. Need to refresh our tokens";
|
qDebug() << "Cannot register push endpoint. Need to refresh our tokens";
|
||||||
|
QueuedCall::enqueue(m_callQueue, QueuedCall("registerPushNotificationEndpoint", registrationId, deviceDisplayName, mobileDeviceId));
|
||||||
refreshAccessToken();
|
refreshAccessToken();
|
||||||
m_callQueue.append(QueuedCall("registerPushNotificationEndpoint", registrationId, deviceDisplayName, mobileDeviceId));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -642,7 +627,7 @@ void AWSClient::registerPushNotificationEndpoint(const QString ®istrationId,
|
|||||||
payload.insert("mobileDeviceUuid", mobileDeviceId);
|
payload.insert("mobileDeviceUuid", mobileDeviceId);
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(payload);
|
QJsonDocument jsonDoc = QJsonDocument::fromVariant(payload);
|
||||||
|
|
||||||
// qDebug() << "Registering push notification endpoint:";
|
qDebug() << "Registering push notification endpoint";
|
||||||
// qDebug() << "POST" << url.toString();
|
// qDebug() << "POST" << url.toString();
|
||||||
// qDebug() << "HEADERS:";
|
// qDebug() << "HEADERS:";
|
||||||
// foreach (const QByteArray &hdr, request.rawHeaderList()) {
|
// foreach (const QByteArray &hdr, request.rawHeaderList()) {
|
||||||
@ -658,7 +643,7 @@ void AWSClient::registerPushNotificationEndpoint(const QString ®istrationId,
|
|||||||
qWarning() << "Error registering push notification endpoint:" << reply->error() << reply->errorString() << qUtf8Printable(data);
|
qWarning() << "Error registering push notification endpoint:" << reply->error() << reply->errorString() << qUtf8Printable(data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// qDebug() << "Push notification endpoint registered" << data;
|
qDebug() << "Push notification endpoint registered" << data;
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -776,19 +761,39 @@ void AWSClient::getCredentialsForIdentity(const QString &identityId)
|
|||||||
m_secretKey = credentialsMap.value("SecretKey").toByteArray();
|
m_secretKey = credentialsMap.value("SecretKey").toByteArray();
|
||||||
m_sessionToken = credentialsMap.value("SessionToken").toByteArray();
|
m_sessionToken = credentialsMap.value("SessionToken").toByteArray();
|
||||||
m_sessionTokenExpiry = QDateTime::fromSecsSinceEpoch(credentialsMap.value("Expiration").toLongLong());
|
m_sessionTokenExpiry = QDateTime::fromSecsSinceEpoch(credentialsMap.value("Expiration").toLongLong());
|
||||||
|
qDebug() << "AWS Credentials for Identity received.";// << data;
|
||||||
|
|
||||||
|
qDebug() << "AWS login successful. Userid:" << m_userId;
|
||||||
|
|
||||||
|
|
||||||
QSettings settings;
|
QSettings settings;
|
||||||
|
|
||||||
|
bool newLogin = !settings.childGroups().contains("cloud");
|
||||||
|
|
||||||
|
settings.remove("cloud");
|
||||||
settings.beginGroup("cloud");
|
settings.beginGroup("cloud");
|
||||||
|
settings.setValue("username", m_username);
|
||||||
|
settings.setValue("userId", m_userId);
|
||||||
|
settings.setValue("password", m_password);
|
||||||
|
settings.setValue("accessToken", m_accessToken);
|
||||||
|
settings.setValue("accessTokenExpiry", m_accessTokenExpiry);
|
||||||
|
settings.setValue("idToken", m_idToken);
|
||||||
|
settings.setValue("refreshToken", m_refreshToken);
|
||||||
settings.setValue("accessKeyId", m_accessKeyId);
|
settings.setValue("accessKeyId", m_accessKeyId);
|
||||||
settings.setValue("secretKey", m_secretKey);
|
settings.setValue("secretKey", m_secretKey);
|
||||||
settings.setValue("sessionToken", m_sessionToken);
|
settings.setValue("sessionToken", m_sessionToken);
|
||||||
settings.setValue("sessionTokenExpiry", m_sessionTokenExpiry);
|
settings.setValue("sessionTokenExpiry", m_sessionTokenExpiry);
|
||||||
|
|
||||||
qDebug() << "AWS Credentials for Identity received.";// << data;
|
|
||||||
emit loginResult(LoginErrorNoError);
|
emit loginResult(LoginErrorNoError);
|
||||||
|
|
||||||
|
if (newLogin) {
|
||||||
|
// qDebug() << "new login!";
|
||||||
|
emit isLoggedInChanged();
|
||||||
|
}
|
||||||
|
|
||||||
while (!m_callQueue.isEmpty()) {
|
while (!m_callQueue.isEmpty()) {
|
||||||
QueuedCall qc = m_callQueue.takeFirst();
|
QueuedCall qc = m_callQueue.takeFirst();
|
||||||
|
// qDebug() << "Calling from queue:" << qc.method;
|
||||||
if (qc.method == "fetchDevices") {
|
if (qc.method == "fetchDevices") {
|
||||||
fetchDevices();
|
fetchDevices();
|
||||||
} else if (qc.method == "postToMQTT") {
|
} else if (qc.method == "postToMQTT") {
|
||||||
@ -797,6 +802,8 @@ void AWSClient::getCredentialsForIdentity(const QString &identityId)
|
|||||||
deleteAccount();
|
deleteAccount();
|
||||||
} else if (qc.method == "registerPushNotificationEndpoint") {
|
} else if (qc.method == "registerPushNotificationEndpoint") {
|
||||||
registerPushNotificationEndpoint(qc.arg1, qc.arg2, qc.arg3);
|
registerPushNotificationEndpoint(qc.arg1, qc.arg2, qc.arg3);
|
||||||
|
} else if (qc.method == "unpairDevice") {
|
||||||
|
unpairDevice(qc.arg1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -816,7 +823,7 @@ bool AWSClient::postToMQTT(const QString &boxId, const QString ×tamp, std::
|
|||||||
if (tokensExpired()) {
|
if (tokensExpired()) {
|
||||||
qDebug() << "Cannot post to MQTT. Need to refresh the tokens first";
|
qDebug() << "Cannot post to MQTT. Need to refresh the tokens first";
|
||||||
refreshAccessToken();
|
refreshAccessToken();
|
||||||
m_callQueue.append(QueuedCall("postToMQTT", boxId, timestamp, callback));
|
QueuedCall::enqueue(m_callQueue, QueuedCall("postToMQTT", boxId, timestamp, callback));
|
||||||
return true; // So far it looks we're doing ok... let's return true
|
return true; // So far it looks we're doing ok... let's return true
|
||||||
}
|
}
|
||||||
QString topic = QString("%1/%2/proxy").arg(boxId).arg(QString(m_identityId));
|
QString topic = QString("%1/%2/proxy").arg(boxId).arg(QString(m_identityId));
|
||||||
@ -884,7 +891,7 @@ void AWSClient::fetchDevices()
|
|||||||
if (tokensExpired()) {
|
if (tokensExpired()) {
|
||||||
qDebug() << "Cannot fetch devices. Need to refresh our tokens";
|
qDebug() << "Cannot fetch devices. Need to refresh our tokens";
|
||||||
refreshAccessToken();
|
refreshAccessToken();
|
||||||
m_callQueue.append(QueuedCall("fetchDevices"));
|
QueuedCall::enqueue(m_callQueue, QueuedCall("fetchDevices"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// qDebug() << "Fetching cloud devices";
|
// qDebug() << "Fetching cloud devices";
|
||||||
|
|||||||
@ -184,6 +184,19 @@ private:
|
|||||||
QString arg2;
|
QString arg2;
|
||||||
QString arg3;
|
QString arg3;
|
||||||
std::function<void(bool)> callback;
|
std::function<void(bool)> callback;
|
||||||
|
|
||||||
|
static void enqueue(QList<QueuedCall> &queue, const QueuedCall &call) {
|
||||||
|
foreach (const QueuedCall &existingCall, queue) {
|
||||||
|
if (existingCall.method == call.method &&
|
||||||
|
existingCall.arg1 == call.arg1 &&
|
||||||
|
existingCall.arg2 == call.arg2 &&
|
||||||
|
existingCall.arg3 == call.arg3 &&
|
||||||
|
&existingCall.callback == &call.callback) {
|
||||||
|
return; // Already in queue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
queue.append(call);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
QList<QueuedCall> m_callQueue;
|
QList<QueuedCall> m_callQueue;
|
||||||
|
|||||||
@ -70,7 +70,7 @@ void ZeroconfDiscovery::serviceEntryAdded(const QZeroConfService &entry)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "zeroconf service discovered" << entry.type() << entry.name() << " IP:" << entry.ip() << "IPv6:" << entry.ipv6() << entry.txt();
|
// qDebug() << "zeroconf service discovered" << entry.type() << entry.name() << " IP:" << entry.ip() << "IPv6:" << entry.ipv6() << entry.txt();
|
||||||
|
|
||||||
QString uuid;
|
QString uuid;
|
||||||
bool sslEnabled = false;
|
bool sslEnabled = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user