File indexing completed on 2024-12-22 04:45:23

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "generatepersonalaccesstokenjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 GeneratePersonalAccessTokenJob::GeneratePersonalAccessTokenJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 GeneratePersonalAccessTokenJob::~GeneratePersonalAccessTokenJob() = default;
0021 
0022 bool GeneratePersonalAccessTokenJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     addStartRestApiInfo("GeneratePersonalAccessTokenJob::start");
0029     submitPostRequest(json());
0030 
0031     return true;
0032 }
0033 
0034 void GeneratePersonalAccessTokenJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037 
0038     if (replyObject[QLatin1String("success")].toBool()) {
0039         addLoggerInfo(QByteArrayLiteral("GeneratePersonalAccessTokenJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0040         Q_EMIT generateTokenDone(replyObject);
0041     } else {
0042         emitFailedMessage(replyErrorString, replyObject);
0043         addLoggerWarning(QByteArrayLiteral("GeneratePersonalAccessTokenJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0044     }
0045 }
0046 
0047 bool GeneratePersonalAccessTokenJob::bypassTwoFactor() const
0048 {
0049     return mBypassTwoFactor;
0050 }
0051 
0052 void GeneratePersonalAccessTokenJob::setBypassTwoFactor(bool newBypassTwoFactor)
0053 {
0054     mBypassTwoFactor = newBypassTwoFactor;
0055 }
0056 
0057 const QString &GeneratePersonalAccessTokenJob::tokenName() const
0058 {
0059     return mTokenName;
0060 }
0061 
0062 void GeneratePersonalAccessTokenJob::setTokenName(const QString &newTokenName)
0063 {
0064     mTokenName = newTokenName;
0065 }
0066 
0067 bool GeneratePersonalAccessTokenJob::requireHttpAuthentication() const
0068 {
0069     return true;
0070 }
0071 
0072 bool GeneratePersonalAccessTokenJob::canStart() const
0073 {
0074     if (!RestApiAbstractJob::canStart()) {
0075         return false;
0076     }
0077     if (mTokenName.isEmpty()) {
0078         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "GeneratePersonalAccessTokenJob: mTokenName is empty";
0079         return false;
0080     }
0081     return true;
0082 }
0083 
0084 QNetworkRequest GeneratePersonalAccessTokenJob::request() const
0085 {
0086     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersGeneratePersonalAccessToken);
0087     QNetworkRequest request(url);
0088     addAuthRawHeader(request);
0089     addRequestAttribute(request);
0090     return request;
0091 }
0092 
0093 QJsonDocument GeneratePersonalAccessTokenJob::json() const
0094 {
0095     QJsonObject jsonObj;
0096     jsonObj[QLatin1String("tokenName")] = mTokenName;
0097     jsonObj[QLatin1String("bypassTwoFactor")] = mBypassTwoFactor;
0098     const QJsonDocument postData = QJsonDocument(jsonObj);
0099     return postData;
0100 }
0101 
0102 bool GeneratePersonalAccessTokenJob::requireTwoFactorAuthentication() const
0103 {
0104     return true;
0105 }
0106 
0107 #include "moc_generatepersonalaccesstokenjob.cpp"