2012-07-12 14:09:20 +02:00
|
|
|
#ifndef OAUTHENTICATOR_H
|
|
|
|
#define OAUTHENTICATOR_H
|
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2012-12-12 17:41:52 +01:00
|
|
|
OAuthenticator(
|
|
|
|
const QString& client_id,
|
|
|
|
const QString& client_secret,
|
2012-12-12 18:34:15 +01:00
|
|
|
RedirectStyle redirect,
|
2012-12-12 17:41:52 +01:00
|
|
|
QObject* parent = 0);
|
|
|
|
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_; }
|
|
|
|
|
2012-07-28 18:18:03 +02:00
|
|
|
signals:
|
|
|
|
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);
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
QByteArray ParseHttpRequest(const QByteArray& request) const;
|
2012-11-30 14:40:54 +01:00
|
|
|
void RequestAccessToken(const QByteArray& code, const QUrl& 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
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|