2012-07-12 14:09:20 +02:00
|
|
|
#include "oauthenticator.h"
|
|
|
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include <qjson/parser.h>
|
|
|
|
|
|
|
|
#include "core/closure.h"
|
2012-11-22 15:15:07 +01:00
|
|
|
#include "core/logging.h"
|
2012-11-30 14:40:54 +01:00
|
|
|
#include "internet/localredirectserver.h"
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* kGoogleOAuthEndpoint = "https://accounts.google.com/o/oauth2/auth";
|
|
|
|
const char* kGoogleOAuthTokenEndpoint =
|
|
|
|
"https://accounts.google.com/o/oauth2/token";
|
2012-08-17 22:48:45 +02:00
|
|
|
const char* kGoogleOAuthScope =
|
|
|
|
"https://www.googleapis.com/auth/drive.readonly "
|
|
|
|
"https://www.googleapis.com/auth/userinfo.email";
|
|
|
|
const char* kGoogleOAuthUserInfoEndpoint =
|
|
|
|
"https://www.googleapis.com/oauth2/v1/userinfo";
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
const char* kClientId = "679260893280.apps.googleusercontent.com";
|
|
|
|
const char* kClientSecret = "l3cWb8efUZsrBI4wmY3uKl6i";
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
OAuthenticator::OAuthenticator(QObject* parent)
|
|
|
|
: QObject(parent) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void OAuthenticator::StartAuthorisation() {
|
2012-11-30 14:40:54 +01:00
|
|
|
LocalRedirectServer* server = new LocalRedirectServer(this);
|
|
|
|
server->Listen();
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-11-30 14:40:54 +01:00
|
|
|
NewClosure(server, SIGNAL(Finished()),
|
|
|
|
this, SLOT(RedirectArrived(LocalRedirectServer*)), server);
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
QUrl url = QUrl(kGoogleOAuthEndpoint);
|
|
|
|
url.addQueryItem("response_type", "code");
|
|
|
|
url.addQueryItem("client_id", kClientId);
|
2012-11-30 14:40:54 +01:00
|
|
|
url.addQueryItem("redirect_uri", server->url().toString());
|
2012-08-17 14:44:28 +02:00
|
|
|
url.addQueryItem("scope", kGoogleOAuthScope);
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
}
|
|
|
|
|
2012-11-30 14:40:54 +01:00
|
|
|
void OAuthenticator::RedirectArrived(LocalRedirectServer* server) {
|
|
|
|
server->deleteLater();
|
|
|
|
QUrl request_url = server->request_url();
|
|
|
|
qLog(Debug) << Q_FUNC_INFO << request_url;
|
|
|
|
RequestAccessToken(
|
|
|
|
request_url.queryItemValue("code").toUtf8(),
|
|
|
|
server->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;
|
|
|
|
}
|
|
|
|
|
2012-11-30 14:40:54 +01:00
|
|
|
void OAuthenticator::RequestAccessToken(const QByteArray& code, const QUrl& url) {
|
2012-07-12 14:09:20 +02:00
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
QList<Param> parameters;
|
|
|
|
parameters << Param("code", code)
|
|
|
|
<< Param("client_id", kClientId)
|
|
|
|
<< Param("client_secret", kClientSecret)
|
|
|
|
<< Param("grant_type", "authorization_code")
|
|
|
|
// Even though we don't use this URI anymore, it must match the
|
|
|
|
// original one.
|
2012-11-30 14:40:54 +01:00
|
|
|
<< Param("redirect_uri", url.toString());
|
2012-07-12 14:09:20 +02:00
|
|
|
|
|
|
|
QStringList params;
|
|
|
|
foreach (const Param& p, parameters) {
|
|
|
|
params.append(QString("%1=%2").arg(p.first, QString(QUrl::toPercentEncoding(p.second))));
|
|
|
|
}
|
|
|
|
QString post_data = params.join("&");
|
|
|
|
qLog(Debug) << post_data;
|
|
|
|
|
|
|
|
QNetworkRequest request = QNetworkRequest(QUrl(kGoogleOAuthTokenEndpoint));
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
|
|
"application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_.post(request, post_data.toUtf8());
|
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(FetchAccessTokenFinished(QNetworkReply*)), reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OAuthenticator::FetchAccessTokenFinished(QNetworkReply* reply) {
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) != 200) {
|
|
|
|
qLog(Error) << "Failed to get access token"
|
|
|
|
<< reply->readAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJson::Parser parser;
|
|
|
|
bool ok = false;
|
|
|
|
QVariantMap result = parser.parse(reply, &ok).toMap();
|
|
|
|
if (!ok) {
|
|
|
|
qLog(Error) << "Failed to parse oauth reply";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qLog(Debug) << result;
|
|
|
|
|
|
|
|
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-08-17 22:48:45 +02:00
|
|
|
// Fetch some basic user information
|
|
|
|
QUrl url = QUrl(kGoogleOAuthUserInfoEndpoint);
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
request.setRawHeader(
|
|
|
|
"Authorization", QString("Bearer %1").arg(access_token_).toUtf8());
|
|
|
|
|
|
|
|
QNetworkReply* user_info_reply = network_.get(request);
|
|
|
|
NewClosure(user_info_reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(FetchUserInfoFinished(QNetworkReply*)), user_info_reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OAuthenticator::FetchUserInfoFinished(QNetworkReply* reply) {
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) != 200) {
|
|
|
|
qLog(Warning) << "Failed to get user info" << reply->readAll();
|
|
|
|
} else {
|
|
|
|
QJson::Parser parser;
|
|
|
|
bool ok = false;
|
|
|
|
QVariantMap result = parser.parse(reply, &ok).toMap();
|
|
|
|
if (!ok) {
|
|
|
|
qLog(Error) << "Failed to parse user info reply";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qLog(Debug) << result;
|
|
|
|
|
|
|
|
user_email_ = result["email"].toString();
|
|
|
|
qLog(Debug) << user_email_;
|
|
|
|
}
|
|
|
|
|
2012-07-28 18:18:03 +02:00
|
|
|
emit Finished();
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|
|
|
|
|
2012-07-26 16:55:59 +02:00
|
|
|
void OAuthenticator::RefreshAuthorisation(const QString& refresh_token) {
|
2012-07-28 18:18:03 +02:00
|
|
|
refresh_token_ = refresh_token;
|
|
|
|
|
2012-07-26 16:55:59 +02:00
|
|
|
QUrl url = QUrl(kGoogleOAuthTokenEndpoint);
|
|
|
|
|
|
|
|
typedef QPair<QString, QString> Param;
|
|
|
|
QList<Param> parameters;
|
|
|
|
parameters << Param("client_id", kClientId)
|
|
|
|
<< Param("client_secret", kClientSecret)
|
|
|
|
<< Param("grant_type", "refresh_token")
|
|
|
|
<< Param("refresh_token", refresh_token);
|
|
|
|
QStringList params;
|
|
|
|
foreach (const Param& p, parameters) {
|
|
|
|
params.append(QString("%1=%2").arg(p.first, QString(QUrl::toPercentEncoding(p.second))));
|
|
|
|
}
|
|
|
|
QString post_data = params.join("&");
|
|
|
|
qLog(Debug) << "Refresh post data:" << post_data;
|
|
|
|
|
|
|
|
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);
|
|
|
|
qLog(Debug) << "Current Google Drive token expires at:" << expiry_time_;
|
|
|
|
}
|
|
|
|
|
2012-07-26 16:55:59 +02:00
|
|
|
void OAuthenticator::RefreshAccessTokenFinished(QNetworkReply* reply) {
|
2012-07-12 14:09:20 +02:00
|
|
|
reply->deleteLater();
|
2012-07-26 16:55:59 +02:00
|
|
|
QJson::Parser parser;
|
|
|
|
bool ok = false;
|
2012-07-12 14:09:20 +02:00
|
|
|
|
2012-07-26 16:55:59 +02:00
|
|
|
QVariantMap result = parser.parse(reply, &ok).toMap();
|
2012-07-28 18:18:03 +02:00
|
|
|
access_token_ = result["access_token"].toString();
|
2012-08-17 14:44:28 +02:00
|
|
|
SetExpiryTime(result["expires_in"].toInt());
|
2012-07-28 18:18:03 +02:00
|
|
|
emit Finished();
|
2012-07-12 14:09:20 +02:00
|
|
|
}
|