File indexing completed on 2024-05-12 05:22:14
0001 /* 0002 SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #pragma once 0008 0009 #include <QList> 0010 #include <QObject> 0011 0012 #include "account.h" 0013 #include "kgapicore_export.h" 0014 0015 class QUrl; 0016 0017 namespace KGAPI2 0018 { 0019 0020 class AccountManager; 0021 /** 0022 * @brief AccountPromise is a result of asynchronous operations of AccountManager 0023 * 0024 * The AccountPromise is returned by AccountManager and will emit @p finished() 0025 * signal when the asynchronous retrieval of the Account has finished. 0026 * 0027 * The object will delete itself once @p finished signal is emitted. 0028 */ 0029 class KGAPICORE_EXPORT AccountPromise : public QObject 0030 { 0031 Q_OBJECT 0032 public: 0033 ~AccountPromise() override; 0034 0035 AccountPtr account() const; 0036 0037 bool hasError() const; 0038 QString errorText() const; 0039 0040 Q_SIGNALS: 0041 /** 0042 * @brief The retrieval has finished and the Account can be retrieved. 0043 * 0044 * The object is automatically scheduled for deletion after this signal 0045 * is emitted. 0046 */ 0047 void finished(KGAPI2::AccountPromise *self); 0048 0049 private: 0050 AccountPromise(QObject *parent = nullptr); 0051 Q_DISABLE_COPY(AccountPromise) 0052 0053 friend class AccountManager; 0054 class Private; 0055 QScopedPointer<Private> const d; 0056 }; 0057 0058 class KGAPICORE_EXPORT AccountManager : public QObject 0059 { 0060 Q_OBJECT 0061 public: 0062 ~AccountManager() override; 0063 0064 static AccountManager *instance(); 0065 0066 /** 0067 * @brief Asynchronously returns an authenticated account for given conditions 0068 * 0069 * The returned account is guaranteed to be authenticated against at least the 0070 * requested @p scopes, but it may be authenticated against more scopes. 0071 * If an account for the given @p apiKey and @p accountName already exists 0072 * but is not authenticated against all the scopes the user will be presented 0073 * with a prompt to confirm the missing scopes. 0074 * 0075 * If no such account exists, user will be prompted with full authentication 0076 * process. 0077 * 0078 * The returned account is guaranteed to be authenticated, however the tokens 0079 * may be expired. It's up to the caller to ensure the tokens are refreshed 0080 * using @p refreshTokens method. 0081 * 0082 * @p apiSecret is only used to authenticate new account or missing scopes 0083 * and is not stored anywhere. 0084 * 0085 * @see refreshTokens, hasAccount 0086 */ 0087 AccountPromise *getAccount(const QString &apiKey, const QString &apiSecret, const QString &accountName, const QList<QUrl> &scopes); 0088 0089 /** 0090 * @brief Asynchronously refreshes tokens in given Account 0091 * 0092 * The returned account is guaranteed to be authenticated against at least 0093 * the requested @p scopes, but it may be authenticated against more scopes. 0094 * If an account does not exist, it will be created (see AccountManager::getAccount()). 0095 * 0096 * @p apiSecret is only used to authenticate a new account. 0097 * 0098 * @see getAccount 0099 */ 0100 AccountPromise *refreshTokens(const QString &apiKey, const QString &apiSecret, const QString &accountName); 0101 0102 /** 0103 * @brief Asynchronously checks whether the specified account exists. 0104 * 0105 * Optionally, when non-empty list of @p scopes is provided this method also 0106 * checks whether, if the account exists, it is authenticated against the 0107 * provided scopes. 0108 * 0109 * The AccountPromise will have the discovered account set if it is found, 0110 * otherwise it's set to null. 0111 */ 0112 AccountPromise *findAccount(const QString &aipKey, const QString &accountName, const QList<QUrl> &scopes = {}); 0113 0114 /** 0115 * @brief Asynchronously remove given scopes from authenticated account. 0116 * 0117 * Removes the given scopes from the account authenticated scopes, so that 0118 * next time the account is requested with any of the removed scopes the user 0119 * will be presented with a prompt to confirm access again. 0120 */ 0121 void removeScopes(const QString &apiKey, const QString &accountName, const QList<QUrl> &removeScopes); 0122 0123 protected: 0124 explicit AccountManager(QObject *parent = nullptr); 0125 Q_DISABLE_COPY(AccountManager) 0126 0127 static AccountManager *sInstance; 0128 class Private; 0129 QScopedPointer<Private> const d; 0130 }; 0131 0132 }