2014-12-17 19:02:21 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012-2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2012, 2014, David Sansome <me@davidsansome.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:35:21 +01:00
|
|
|
#include "internet/core/oauthenticator.h"
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
#include <QDesktopServices>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonParseError>
|
2015-07-07 20:15:23 +02:00
|
|
|
#include <QSslError>
|
2012-07-12 14:09:20 +02:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QUrl>
|
2015-04-11 22:52:31 +02:00
|
|
|
#include <QUrlQuery>
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
#include "core/closure.h"
|
2012-11-22 15:15:07 +01:00
|
|
|
#include "core/logging.h"
|
2014-12-18 23:35:21 +01:00
|
|
|
#include "internet/core/localredirectserver.h"
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2014-12-17 19:02:21 +01:00
|
|
|
const char* OAuthenticator::kRemoteURL =
|
|
|
|
"https://clementine-data.appspot.com/skydrive";
|
2014-03-26 21:33:27 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
OAuthenticator::OAuthenticator(const QString& client_id,
|
|
|
|
const QString& client_secret,
|
|
|
|
RedirectStyle redirect, QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
client_id_(client_id),
|
|
|
|
client_secret_(client_secret),
|
|
|
|
redirect_style_(redirect) {}
|
|
|
|
|
|
|
|
void OAuthenticator::StartAuthorisation(const QString& oauth_endpoint,
|
|
|
|
const QString& token_endpoint,
|
|
|
|
const QString& scope) {
|
2012-12-12 17:41:52 +01:00
|
|
|
token_endpoint_ = QUrl(token_endpoint);
|
2012-11-30 14:40:54 +01:00
|
|
|
LocalRedirectServer* server = new LocalRedirectServer(this);
|
|
|
|
server->Listen();
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-12-12 17:41:52 +01:00
|
|
|
QUrl url = QUrl(oauth_endpoint);
|
2015-04-11 22:52:31 +02:00
|
|
|
QUrlQuery url_query;
|
|
|
|
url_query.addQueryItem("response_type", "code");
|
|
|
|
url_query.addQueryItem("client_id", client_id_);
|
2012-12-12 18:34:15 +01:00
|
|
|
QUrl redirect_url;
|
2015-04-11 22:52:31 +02:00
|
|
|
QUrlQuery redirect_url_query;
|
2014-03-26 21:33:27 +01:00
|
|
|
|
|
|
|
const QString port = QString::number(server->url().port());
|
|
|
|
|
2012-12-12 18:34:15 +01:00
|
|
|
if (redirect_style_ == RedirectStyle::REMOTE) {
|
2014-03-26 21:33:27 +01:00
|
|
|
redirect_url = QUrl(kRemoteURL);
|
2015-04-11 22:52:31 +02:00
|
|
|
redirect_url_query.addQueryItem("port", port);
|
2014-03-26 21:33:27 +01:00
|
|
|
} else if (redirect_style_ == RedirectStyle::REMOTE_WITH_STATE) {
|
|
|
|
redirect_url = QUrl(kRemoteURL);
|
2015-04-11 22:52:31 +02:00
|
|
|
url_query.addQueryItem("state", port);
|
2012-12-12 18:34:15 +01:00
|
|
|
} else {
|
|
|
|
redirect_url = server->url();
|
|
|
|
}
|
2014-03-26 21:33:27 +01:00
|
|
|
|
2021-02-21 09:06:13 +01:00
|
|
|
redirect_url.setQuery(redirect_url_query);
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
url_query.addQueryItem("redirect_uri", redirect_url.toString());
|
2015-09-25 17:18:58 +02:00
|
|
|
if (!scope.isEmpty()) { // Empty scope is valid for Dropbox.
|
2015-12-13 20:05:12 +01:00
|
|
|
url_query.addQueryItem("scope", scope);
|
2015-09-25 17:18:58 +02:00
|
|
|
}
|
2015-04-11 22:52:31 +02:00
|
|
|
|
|
|
|
url.setQuery(url_query);
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(server, SIGNAL(Finished()), this, &OAuthenticator::RedirectArrived,
|
|
|
|
server, redirect_url);
|
2012-12-12 18:34:15 +01:00
|
|
|
|
2012-07-12 14:09:20 +02:00
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OAuthenticator::RedirectArrived(LocalRedirectServer* server, QUrl url) {
|
2012-11-30 14:40:54 +01:00
|
|
|
server->deleteLater();
|
|
|
|
QUrl request_url = server->request_url();
|
2020-09-18 16:15:19 +02:00
|
|
|
RequestAccessToken(QUrlQuery(request_url).queryItemValue("code").toUtf8(),
|
|
|
|
url);
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray OAuthenticator::ParseHttpRequest(const QByteArray& request) const {
|
|
|
|
QList<QByteArray> split = request.split('\r');
|
|
|
|
const QByteArray& request_line = split[0];
|
|
|
|
QByteArray path = request_line.split(' ')[1];
|
|
|
|
QByteArray code = path.split('=')[1];
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OAuthenticator::RequestAccessToken(const QByteArray& code,
|
2021-02-23 21:43:16 +01:00
|
|
|
const QUrl& redirect_url) {
|
2012-07-12 14:09:20 +02:00
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
QList<Param> parameters;
|
2014-12-17 19:02:21 +01:00
|
|
|
parameters << Param("code", code) << Param("client_id", client_id_)
|
2012-07-12 14:09:20 +02:00
|
|
|
<< Param("grant_type", "authorization_code")
|
2014-03-30 07:35:38 +02:00
|
|
|
// Even though we don't use this URI anymore, it must match the
|
|
|
|
// original one.
|
2021-02-23 21:43:16 +01:00
|
|
|
<< Param("redirect_uri", redirect_url.toString());
|
|
|
|
if (!client_secret_.isEmpty()) {
|
|
|
|
parameters << Param("client_secret", client_secret_);
|
|
|
|
}
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
QStringList params;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Param& p : parameters) {
|
2014-02-07 16:34:20 +01:00
|
|
|
params.append(QString("%1=%2").arg(
|
|
|
|
p.first, QString(QUrl::toPercentEncoding(p.second))));
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
QString post_data = params.join("&");
|
|
|
|
qLog(Debug) << post_data;
|
|
|
|
|
2012-12-12 17:41:52 +01:00
|
|
|
QNetworkRequest request = QNetworkRequest(QUrl(token_endpoint_));
|
2012-07-12 14:09:20 +02:00
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_.post(request, post_data.toUtf8());
|
2015-09-25 17:18:58 +02:00
|
|
|
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
|
|
|
|
SLOT(SslErrors(QList<QSslError>)));
|
2012-07-12 14:09:20 +02:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(FetchAccessTokenFinished(QNetworkReply*)), reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OAuthenticator::FetchAccessTokenFinished(QNetworkReply* reply) {
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) != 200) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Error) << "Failed to get access token" << reply->readAll();
|
2012-07-12 14:09:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
QJsonParseError error;
|
2020-09-18 16:15:19 +02:00
|
|
|
QJsonDocument json_document =
|
|
|
|
QJsonDocument::fromJson(reply->readAll(), &error);
|
2015-04-11 22:52:31 +02:00
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2012-07-12 14:09:20 +02:00
|
|
|
qLog(Error) << "Failed to parse oauth reply";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-05 21:28:05 +02:00
|
|
|
QJsonObject result = json_document.object();
|
2012-07-12 14:09:20 +02:00
|
|
|
access_token_ = result["access_token"].toString();
|
|
|
|
refresh_token_ = result["refresh_token"].toString();
|
2012-08-17 14:44:28 +02:00
|
|
|
SetExpiryTime(result["expires_in"].toInt());
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-07-28 18:18:03 +02:00
|
|
|
emit Finished();
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OAuthenticator::RefreshAuthorisation(const QString& token_endpoint,
|
|
|
|
const QString& refresh_token) {
|
2012-07-28 18:18:03 +02:00
|
|
|
refresh_token_ = refresh_token;
|
|
|
|
|
2012-12-12 17:41:52 +01:00
|
|
|
QUrl url(token_endpoint);
|
2012-07-26 16:55:59 +02:00
|
|
|
|
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
QList<Param> parameters;
|
2012-12-12 17:41:52 +01:00
|
|
|
parameters << Param("client_id", client_id_)
|
|
|
|
<< Param("client_secret", client_secret_)
|
2012-07-26 16:55:59 +02:00
|
|
|
<< Param("grant_type", "refresh_token")
|
|
|
|
<< Param("refresh_token", refresh_token);
|
|
|
|
QStringList params;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Param& p : parameters) {
|
2014-02-07 16:34:20 +01:00
|
|
|
params.append(QString("%1=%2").arg(
|
|
|
|
p.first, QString(QUrl::toPercentEncoding(p.second))));
|
2012-07-26 16:55:59 +02:00
|
|
|
}
|
|
|
|
QString post_data = params.join("&");
|
|
|
|
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
QNetworkReply* reply = network_.post(request, post_data.toUtf8());
|
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(RefreshAccessTokenFinished(QNetworkReply*)), reply);
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
2012-08-17 14:44:28 +02:00
|
|
|
void OAuthenticator::SetExpiryTime(int expires_in_seconds) {
|
|
|
|
// Set the expiry time with two minutes' grace.
|
|
|
|
expiry_time_ = QDateTime::currentDateTime().addSecs(expires_in_seconds - 120);
|
2013-02-12 13:54:19 +01:00
|
|
|
qLog(Debug) << "Current oauth access token expires at:" << expiry_time_;
|
2012-08-17 14:44:28 +02:00
|
|
|
}
|
|
|
|
|
2012-07-26 16:55:59 +02:00
|
|
|
void OAuthenticator::RefreshAccessTokenFinished(QNetworkReply* reply) {
|
2012-07-12 14:09:20 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
QJsonObject json_result = QJsonDocument::fromJson(reply->readAll()).object();
|
|
|
|
|
|
|
|
access_token_ = json_result["access_token"].toString();
|
|
|
|
if (json_result.contains("refresh_token")) {
|
|
|
|
refresh_token_ = json_result["refresh_token"].toString();
|
2013-02-26 16:24:47 +01:00
|
|
|
}
|
2015-04-11 22:52:31 +02:00
|
|
|
SetExpiryTime(json_result["expires_in"].toInt());
|
2012-07-28 18:18:03 +02:00
|
|
|
emit Finished();
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
2015-07-07 20:15:23 +02:00
|
|
|
|
|
|
|
void OAuthenticator::SslErrors(const QList<QSslError>& errors) {
|
|
|
|
for (const QSslError& error : errors) {
|
|
|
|
qLog(Debug) << error.errorString();
|
|
|
|
}
|
|
|
|
}
|