KUnifiedPush

nextcloudauthenticator.cpp
1 /*
2  SPDX-FileCopyrightText: 2022 Volker Krause <[email protected]>
3  SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5 
6 #include "nextcloudauthenticator.h"
7 
8 #include <QDebug>
9 #include <QDesktopServices>
10 #include <QJsonDocument>
11 #include <QJsonObject>
12 #include <QNetworkAccessManager>
13 #include <QNetworkReply>
14 #include <QTimer>
15 
16 NextcloudAuthenticator::NextcloudAuthenticator(QObject *parent)
17  : QObject(parent)
18 {
19 }
20 
21 NextcloudAuthenticator::~NextcloudAuthenticator() = default;
22 
23 void NextcloudAuthenticator::setNetworkAccessManager(QNetworkAccessManager *nam)
24 {
25  m_nam = nam;
26 }
27 
28 void NextcloudAuthenticator::authenticate(const QUrl &baseUrl, const QString &appName)
29 {
30  Q_ASSERT(m_nam);
31 
32  QUrl login2Url(baseUrl);
33  login2Url.setPath(login2Url.path() + QLatin1String("/index.php/login/v2"));
34 
35  QNetworkRequest req(login2Url);
36  req.setHeader(QNetworkRequest::UserAgentHeader, appName);
37  auto reply = m_nam->post(req, QByteArray());
38  connect(reply, &QNetworkReply::finished, this, [this, reply]() { post1Finished(reply); });
39 }
40 
41 void NextcloudAuthenticator::post1Finished(QNetworkReply *reply)
42 {
43  reply->deleteLater();
44  if (reply->error() != QNetworkReply::NoError) {
45  qWarning() << reply->errorString();
46  return;
47  }
48 
49  const auto obj = QJsonDocument::fromJson(reply->readAll()).object();
50  const auto loginUrl = QUrl(obj.value(QLatin1String("login")).toString());
51  QDesktopServices::openUrl(loginUrl);
52 
53  const auto pollObj = obj.value(QLatin1String("poll")).toObject();
54  m_pollEndpoint = QUrl(pollObj.value(QLatin1String("endpoint")).toString());
55  m_pollToken = "token=" + pollObj.value(QLatin1String("token")).toString().toUtf8();
56 
57  QTimer::singleShot(std::chrono::seconds(5), Qt::VeryCoarseTimer, this, &NextcloudAuthenticator::login2Poll);
58 }
59 
60 void NextcloudAuthenticator::login2Poll()
61 {
62  QNetworkRequest req(m_pollEndpoint);
63  req.setHeader(QNetworkRequest::ContentTypeHeader, QByteArray("application/x-www-form-urlencoded"));
64  auto reply = m_nam->post(req, m_pollToken);
65  connect(reply, &QNetworkReply::finished, this, [this, reply]() {
66  reply->deleteLater();
68  QTimer::singleShot(std::chrono::seconds(5), Qt::VeryCoarseTimer, this, &NextcloudAuthenticator::login2Poll);
69  return;
70  }
71  if (reply->error() != QNetworkReply::NoError) {
72  qWarning() << reply->errorString();
73  return;
74  }
75 
76  const auto obj = QJsonDocument::fromJson(reply->readAll()).object();
77  Q_EMIT authenticated(obj.value(QLatin1String("loginName")).toString(), obj.value(QLatin1String("appPassword")).toString());
78  });
79 }
QString errorString() const const
QJsonObject object() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QNetworkReply::NetworkError error() const const
bool openUrl(const QUrl &url)
VeryCoarseTimer
void deleteLater()
QByteArray readAll()
char * toString(const EngineQuery &query)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 3 2023 03:50:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.