| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | //////////////////////////////////////////////////////////////////////////////// |
| 4 | |
| 5 | // // |
| 6 | // This file is part of QOAuth2. // |
| 7 | // Copyright (c) 2014 Jacob Dawid <[email protected]> // |
| 8 | // // |
| 9 | // QOAuth2 is free software: you can redistribute it and/or modify // |
| 10 | // it under the terms of the GNU Affero General Public License as // |
| 11 | // published by the Free Software Foundation, either version 3 of the // |
| 12 | // License, or (at your option) any later version. // |
| 13 | // // |
| 14 | // QOAuth2 is distributed in the hope that it will be useful, // |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
| 17 | // GNU Affero General Public License for more details. // |
| 18 | // // |
| 19 | // You should have received a copy of the GNU Affero General Public // |
| 20 | // License along with QOAuth2. // |
| 21 | // If not, see <http://www.gnu.org/licenses/>. // |
| 22 | // // |
| 23 | //////////////////////////////////////////////////////////////////////////////// |
| 24 | |
| 25 | #ifndef OAUTH2SERVICE_H |
| 26 | #define OAUTH2SERVICE_H |
| 27 | |
| 28 | #include "network-web/oauthhttphandler.h" |
| 29 | #include "network-web/silentnetworkaccessmanager.h" |
| 30 | |
| 31 | #include <functional> |
| 32 | |
| 33 | #include <QObject> |
| 34 | #include <QUrl> |
| 35 | |
| 36 | class RSSGUARD_DLLSPEC OAuth2Service : public QObject { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | public: |
| 40 | explicit OAuth2Service(const QString& auth_url, |
| 41 | const QString& token_url, |
| 42 | const QString& client_id, |
| 43 | const QString& client_secret, |
| 44 | const QString& scope, |
| 45 | QObject* parent = nullptr); |
| 46 | virtual ~OAuth2Service(); |
| 47 | |
| 48 | // Returns bearer HTTP header value. |
| 49 | // NOTE: If on working thread, then call this only if isFullyLoggedIn() |
| 50 | // returns true. If isFullyLoggedIn() returns false, then you must call login() on |
| 51 | // main GUI thread first. |
| 52 | QString bearer(); |
| 53 | bool isFullyLoggedIn() const; |
| 54 | |
| 55 | void setOAuthTokenGrantType(QString grant_type); |
| 56 | QString oAuthTokenGrantType(); |
| 57 | |
| 58 | QString refreshToken() const; |
| 59 | void setRefreshToken(const QString& refresh_token); |
| 60 | |
| 61 | QString redirectUrl() const; |
| 62 | void setRedirectUrl(const QString& redirect_url, bool start_handler); |
| 63 | |
| 64 | QString clientId() const; |
| 65 | void setClientId(const QString& client_id); |
| 66 | |
| 67 | QString clientSecret() const; |
| 68 | void setClientSecret(const QString& client_secret); |
| 69 | |
| 70 | QDateTime tokensExpireIn() const; |
| 71 | void setTokensExpireIn(const QDateTime& tokens_expire_in); |
| 72 | |
| 73 | QString accessToken() const; |
| 74 | void setAccessToken(const QString& access_token); |
| 75 | |
| 76 | QString id() const; |
| 77 | void setId(const QString& id); |
| 78 | |
| 79 | // Super secret fallback client ID/SECRET. |
| 80 | QString clientSecretId() const; |
| 81 | void setClientSecretId(const QString& client_secret_id); |
| 82 | |
| 83 | QString clientSecretSecret() const; |
| 84 | void setClientSecretSecret(const QString& client_secret_secret); |
| 85 | |
| 86 | bool useHttpBasicAuthWithClientData() const; |
| 87 | void setUseHttpBasicAuthWithClientData(bool use_auth); |
| 88 | |
| 89 | signals: |
| 90 | void tokensRetrieved(QString access_token, QString refresh_token, int expires_in); |
| 91 | void tokensRetrieveError(QString error, QString error_description); |
| 92 | |
| 93 | // User failed to authenticate or rejected it. |
| 94 | void authFailed(); |
| 95 | |
| 96 | public slots: |
| 97 | void retrieveAuthCode(); |
| 98 | void retrieveAccessToken(const QString& auth_code); |
| 99 | void refreshAccessToken(const QString& refresh_token = QString()); |
| 100 | |
| 101 | // Performs login if needed. If some refresh token is set, then |
| 102 | // the initial "auth" step is skipped and attempt to refresh |
| 103 | // access token is made. |
| 104 | // |
| 105 | // Returns true, if user is already logged in (final state). |
| 106 | // Returns false, if user is NOT logged in (asynchronous flow). |
| 107 | bool login(const std::function<void()>& functor_when_logged_in = std::function<void()>()); |
| 108 | |
| 109 | // Removes all state data and stops redirection handler. |
| 110 | void logout(bool stop_redirection_handler = true); |
| 111 | |
| 112 | private slots: |
| 113 | void startRefreshTimer(); |
| 114 | void killRefreshTimer(); |
| 115 | void tokenRequestFinished(QNetworkReply* network_reply); |
| 116 | |
| 117 | private: |
| 118 | QString properClientId() const; |
| 119 | QString properClientSecret() const; |
| 120 | |
| 121 | void timerEvent(QTimerEvent* event); |
| 122 | |
| 123 | private: |
| 124 | QString m_id; |
| 125 | int m_timerId; |
| 126 | QDateTime m_tokensExpireIn; |
| 127 | QString m_accessToken; |
| 128 | QString m_refreshToken; |
| 129 | QString m_tokenGrantType; |
| 130 | QString m_clientId; |
| 131 | QString m_clientSecret; |
| 132 | QString m_clientSecretId; |
| 133 | QString m_clientSecretSecret; |
| 134 | QUrl m_tokenUrl; |
| 135 | QString m_authUrl; |
| 136 | QString m_scope; |
| 137 | bool m_useHttpBasicAuthWithClientData; |
| 138 | SilentNetworkAccessManager m_networkManager; |
| 139 | OAuthHttpHandler* m_redirectionHandler; |
| 140 | std::function<void()> m_functorOnLogin; |
| 141 | }; |
| 142 | |
| 143 | #endif // OAUTH2SERVICE_H |