void CloudConnection::onAuthenticationChanged() { if (authenticator()->authenticated()) { qCDebug(dcCloud()) << "Connecting to" << m_proxyServerUrl.toString(); m_error = Cloud::CloudErrorNoError; m_connection->open(m_proxyServerUrl); } else { qCWarning(dcCloud()) << "Could not authenticate"; m_error = m_authenticator->error(); } }
void CloudAuthenticator::onSslErrors(QNetworkReply *reply, const QList<QSslError> &errors) { // Ignore SSL errors, but inform in the logs reply->ignoreSslErrors(); if (m_refreshTokenRequests.contains(reply)) { qCWarning(dcCloud()) << "SSL errors occured for token refresh reply:"; } else if (m_tokenRequests.contains(reply)) { qCWarning(dcCloud()) << "SSL errors occured for token reply:"; } else { qCWarning(dcCloud()) << "SSL errors occured for unknown reply:"; } foreach (const QSslError &error, errors) { qCWarning(dcCloud()) << " " << (int)error.error() << error.errorString(); }
void CloudConnection::onConnected() { qCDebug(dcCloud()) << "Connected to cloud proxy server" << m_proxyServerUrl.toString(); m_error = Cloud::CloudErrorNoError; setConnected(true); m_reconnectionTimer->stop(); }
void CloudConnection::onError(const QAbstractSocket::SocketError &error) { if (!m_reconnectionTimer->isActive()) qCWarning(dcCloud()) << "Websocket error:" << error << m_connection->errorString(); m_error = Cloud::CloudErrorProxyServerNotReachable; m_reconnectionTimer->start(10000); }
void CloudConnection::onDisconnected() { if (!m_reconnectionTimer->isActive()) qCDebug(dcCloud()) << "Disconnected from cloud:" << m_connection->closeReason(); m_error = Cloud::CloudErrorProxyServerNotReachable; setConnected(false); m_reconnectionTimer->start(10000); }
void CloudConnection::onTextMessageReceived(const QString &message) { QJsonParseError error; QJsonDocument jsonDoc = QJsonDocument::fromJson(message.toUtf8(), &error); if (error.error != QJsonParseError::NoError) { qCWarning(dcCloud()) << "Could not parse json data from guh" << message.toUtf8() << error.errorString(); return; } emit dataReceived(jsonDoc.toVariant().toMap()); }
bool CloudAuthenticator::startAuthentication() { qCDebug(dcCloud()) << "Authenticator: Start authenticating" << m_username; // Check if we have username and password if(!m_username.isEmpty() && !m_password.isEmpty()) { m_refreshToken.clear(); stopAuthentication(); QUrlQuery query; query.addQueryItem("grant_type", "password"); query.addQueryItem("username", m_username); query.addQueryItem("password", m_password); setQuery(query); QNetworkRequest request(m_url); QByteArray data = QString(m_clientId + ":" + m_clientSecret).toUtf8().toBase64(); QString header = "Basic " + data; request.setRawHeader("Authorization", header.toLocal8Bit()); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QNetworkReply *reply = m_networkManager->post(request, m_query.toString().toUtf8()); m_tokenRequests.append(reply); return true; } else if (!m_refreshToken.isEmpty()) { // Use the refreshtoken if there is one refreshTimeout(); return true; } qCWarning(dcCloud()) << "Authenticator: Cannot start authentication. There is no refresh token, username or password around."; stopAuthentication(); m_error = Cloud::CloudErrorLoginCredentialsMissing; return false; }
void CloudAuthenticator::refreshTimeout() { qCDebug(dcCloud()) << "Authenticator: Refresh authentication token for" << m_username; QUrlQuery query; query.addQueryItem("grant_type", "refresh_token"); query.addQueryItem("refresh_token", m_refreshToken); QNetworkRequest request(m_url); QByteArray data = QString(m_clientId + ":" + m_clientSecret).toUtf8().toBase64(); QString header = "Basic " + data; request.setRawHeader("Authorization", header.toLocal8Bit()); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QNetworkReply *reply = m_networkManager->post(request, query.toString().toUtf8()); m_refreshTokenRequests.append(reply); }
void CloudAuthenticator::replyFinished(QNetworkReply *reply) { int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); // token request if (m_tokenRequests.contains(reply)) { QByteArray data = reply->readAll(); m_tokenRequests.removeAll(reply); if (reply->error() != QNetworkReply::NoError) { qCWarning(dcCloud()) << "Authenticator: Request token reply error:" << status << reply->errorString(); m_error = Cloud::CloudErrorIdentityServerNotReachable; setAuthenticated(false); reply->deleteLater(); return; } // check HTTP status code if (status != 200) { qCWarning(dcCloud()) << "Authenticator: Request token reply HTTP error:" << status << reply->errorString(); qCWarning(dcCloud()) << data; m_error = Cloud::CloudErrorAuthenticationFailed; setAuthenticated(false); reply->deleteLater(); return; } // check JSON QJsonParseError error; QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); if (error.error != QJsonParseError::NoError) { qCWarning(dcCloud()) << "Authenticator: Request token reply JSON error:" << error.errorString(); m_error = Cloud::CloudErrorAuthenticationFailed; setAuthenticated(false); reply->deleteLater(); return; } if (!jsonDoc.toVariant().toMap().contains("access_token")) { qCWarning(dcCloud()) << "Authenticator: Could not get access token" << jsonDoc.toJson(); m_error = Cloud::CloudErrorAuthenticationFailed; setAuthenticated(false); reply->deleteLater(); return; } m_error = Cloud::CloudErrorNoError; setToken(jsonDoc.toVariant().toMap().value("access_token").toString()); setAuthenticated(true); // Save the username GuhSettings settings(GuhSettings::SettingsRoleDevices); settings.beginGroup("Cloud"); settings.setValue("userName", m_username); settings.endGroup(); if (jsonDoc.toVariant().toMap().contains("expires_in") && jsonDoc.toVariant().toMap().contains("refresh_token")) { int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt(); setRefreshToken(jsonDoc.toVariant().toMap().value("refresh_token").toString()); qCDebug(dcCloud()) << "Authenticator: Token will be refreshed in" << expireTime << "[s]"; m_timer->start((expireTime - 20) * 1000); } } else if (m_refreshTokenRequests.contains(reply)) { QByteArray data = reply->readAll(); m_refreshTokenRequests.removeAll(reply); if (reply->error() != QNetworkReply::NoError) { qCWarning(dcCloud()) << "Authenticator: Request token reply error:" << status << reply->errorString(); m_error = Cloud::CloudErrorIdentityServerNotReachable; setAuthenticated(false); reply->deleteLater(); return; } // check HTTP status code if (status != 200) { qCWarning(dcCloud()) << "Authenticator: Refresh token reply HTTP error:" << status << reply->errorString(); m_error = Cloud::CloudErrorAuthenticationFailed; setAuthenticated(false); reply->deleteLater(); return; } // check JSON QJsonParseError error; QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); if (error.error != QJsonParseError::NoError) { qCWarning(dcCloud()) << "Authenticator: Refresh token reply JSON error:" << error.errorString(); m_error = Cloud::CloudErrorAuthenticationFailed; setAuthenticated(false); reply->deleteLater(); return; } if (!jsonDoc.toVariant().toMap().contains("access_token")) { qCWarning(dcCloud()) << "Authenticator: Could not get access token after refresh" << jsonDoc.toJson(); m_error = Cloud::CloudErrorAuthenticationFailed; setAuthenticated(false); reply->deleteLater(); return; } m_error = Cloud::CloudErrorNoError; setToken(jsonDoc.toVariant().toMap().value("access_token").toString()); qCDebug(dcCloud()) << "Authenticator: Token refreshed successfully"; if (jsonDoc.toVariant().toMap().contains("expires_in") && jsonDoc.toVariant().toMap().contains("refresh_token")) { int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt(); setRefreshToken(jsonDoc.toVariant().toMap().value("refresh_token").toString()); qCDebug(dcCloud()) << "Authenticator: Token will be refreshed in" << expireTime << "[s]"; m_timer->start((expireTime - 20) * 1000); } if (!authenticated()) setAuthenticated(true); } reply->deleteLater(); }