2014-12-17 19:02:21 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Arnaud Bienner <arnaud.bienner@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-12-18 23:54:21 +01:00
|
|
|
#ifndef INTERNET_CORE_OAUTHENTICATOR_H_
|
|
|
|
#define INTERNET_CORE_OAUTHENTICATOR_H_
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-08-17 14:44:28 +02:00
|
|
|
#include <QDateTime>
|
2012-07-12 14:09:20 +02:00
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
#include "core/network.h"
|
|
|
|
|
2012-11-30 14:40:54 +01:00
|
|
|
class LocalRedirectServer;
|
2012-07-12 14:09:20 +02:00
|
|
|
class QTcpSocket;
|
|
|
|
|
|
|
|
class OAuthenticator : public QObject {
|
|
|
|
Q_OBJECT
|
2014-12-17 19:02:21 +01:00
|
|
|
|
2012-07-12 14:09:20 +02:00
|
|
|
public:
|
2012-12-12 18:34:15 +01:00
|
|
|
enum class RedirectStyle {
|
|
|
|
// Redirect to localhost immediately.
|
|
|
|
LOCALHOST = 0,
|
|
|
|
// Redirect via data.clementine-player.org for when localhost is
|
|
|
|
// unsupported (eg. Skydrive).
|
|
|
|
REMOTE = 1,
|
2014-03-31 22:04:03 +02:00
|
|
|
// Same as REMOTE, but pass the 'port' parameter of the redirect URL as
|
|
|
|
// 'state' parameter of the URL, for services which allow only redirect URL
|
|
|
|
// without parameters (e.g. SoundCloud). "state" parameter will be added to
|
|
|
|
// the redirect URL by the service itself.
|
2014-03-26 21:33:27 +01:00
|
|
|
REMOTE_WITH_STATE = 2
|
2012-12-12 18:34:15 +01:00
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
OAuthenticator(const QString& client_id, const QString& client_secret,
|
2014-02-10 16:03:54 +01:00
|
|
|
RedirectStyle redirect, QObject* parent = nullptr);
|
2014-02-07 16:34:20 +01:00
|
|
|
void StartAuthorisation(const QString& oauth_endpoint,
|
|
|
|
const QString& token_endpoint, const QString& scope);
|
|
|
|
void RefreshAuthorisation(const QString& token_endpoint,
|
|
|
|
const QString& refresh_token);
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-07-26 16:55:59 +02:00
|
|
|
// Token to use now.
|
2012-07-28 18:18:03 +02:00
|
|
|
const QString& access_token() const { return access_token_; }
|
|
|
|
|
2012-07-26 16:55:59 +02:00
|
|
|
// Token to use to get a new access token when it expires.
|
2012-07-28 18:18:03 +02:00
|
|
|
const QString& refresh_token() const { return refresh_token_; }
|
|
|
|
|
2012-08-17 14:44:28 +02:00
|
|
|
const QDateTime& expiry_time() const { return expiry_time_; }
|
|
|
|
|
2014-12-17 19:02:21 +01:00
|
|
|
signals:
|
2012-07-28 18:18:03 +02:00
|
|
|
void Finished();
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
private slots:
|
2012-12-12 18:34:15 +01:00
|
|
|
void RedirectArrived(LocalRedirectServer* server, QUrl url);
|
2012-07-12 14:09:20 +02:00
|
|
|
void FetchAccessTokenFinished(QNetworkReply* reply);
|
2012-07-26 16:55:59 +02:00
|
|
|
void RefreshAccessTokenFinished(QNetworkReply* reply);
|
2015-07-07 20:15:23 +02:00
|
|
|
void SslErrors(const QList<QSslError>& errors);
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
private:
|
2014-03-26 21:33:27 +01:00
|
|
|
static const char* kRemoteURL;
|
|
|
|
|
2012-07-12 14:09:20 +02:00
|
|
|
QByteArray ParseHttpRequest(const QByteArray& request) const;
|
2021-02-23 21:43:16 +01:00
|
|
|
void RequestAccessToken(const QByteArray& code, const QUrl& redirect_url);
|
2012-08-17 14:44:28 +02:00
|
|
|
void SetExpiryTime(int expires_in_seconds);
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
NetworkAccessManager network_;
|
|
|
|
|
|
|
|
QString access_token_;
|
|
|
|
QString refresh_token_;
|
2012-08-17 14:44:28 +02:00
|
|
|
QDateTime expiry_time_;
|
2012-12-12 17:41:52 +01:00
|
|
|
|
|
|
|
const QString client_id_;
|
|
|
|
const QString client_secret_;
|
|
|
|
QUrl token_endpoint_;
|
2012-12-12 18:34:15 +01:00
|
|
|
RedirectStyle redirect_style_;
|
2012-07-12 14:09:20 +02:00
|
|
|
};
|
|
|
|
|
2014-12-18 23:54:21 +01:00
|
|
|
#endif // INTERNET_CORE_OAUTHENTICATOR_H_
|