KUnifiedPush

nextcloudauthenticator.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
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
16NextcloudAuthenticator::NextcloudAuthenticator(QObject *parent)
17 : QObject(parent)
18{
19}
20
21NextcloudAuthenticator::~NextcloudAuthenticator() = default;
22
23void NextcloudAuthenticator::setNetworkAccessManager(QNetworkAccessManager *nam)
24{
25 m_nam = nam;
26}
27
28void 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
41void 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());
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
60void 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}
80
81#include "moc_nextcloudauthenticator.cpp"
char * toString(const EngineQuery &query)
bool openUrl(const QUrl &url)
QString errorString() const const
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
NetworkError error() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
VeryCoarseTimer
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:47 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.