Sort out dropbox authentication & storing credentials;

This commit is contained in:
John Maguire 2012-11-29 18:43:56 +01:00
parent ea8655af83
commit c66295c8d9
6 changed files with 90 additions and 60 deletions

View File

@ -37,24 +37,10 @@ DropboxAuthenticator::DropboxAuthenticator(QObject* parent)
void DropboxAuthenticator::StartAuthorisation() {
QUrl url(kRequestTokenEndpoint);
typedef QPair<QString, QString> Param;
QByteArray signature = QUrl::toPercentEncoding(QString(kAppSecret) + "&");
QList<Param> params;
params << Param("oauth_consumer_key", kAppKey)
<< Param("oauth_signature_method", "PLAINTEXT")
<< Param("oauth_timestamp", QString::number(time(NULL)))
<< Param("oauth_nonce", QString::number(qrand()))
<< Param("oauth_signature", signature);
QStringList encoded_params;
foreach (const Param& p, params) {
encoded_params << QString("%1=\"%2\"").arg(p.first, p.second);
}
QString authorisation_header = QString("OAuth ") + encoded_params.join(", ");
qLog(Debug) << authorisation_header;
QByteArray authorisation_header = GenerateAuthorisationHeader(
QString::null, QString::null);
QNetworkRequest request(url);
request.setRawHeader("Authorization", authorisation_header.toUtf8());
request.setRawHeader("Authorization", authorisation_header);
QNetworkReply* reply = network_->post(request, QByteArray());
NewClosure(reply, SIGNAL(finished()),
@ -124,25 +110,10 @@ void DropboxAuthenticator::RedirectArrived(QTcpSocket* socket, QByteArray buffer
void DropboxAuthenticator::RequestAccessToken() {
QUrl url(kAccessTokenEndpoint);
typedef QPair<QString, QString> Param;
QByteArray signature = QUrl::toPercentEncoding(
QString("%1&%2").arg(kAppSecret, secret_));
QList<Param> params;
params << Param("oauth_consumer_key", kAppKey)
<< Param("oauth_signature_method", "PLAINTEXT")
<< Param("oauth_timestamp", QString::number(time(NULL)))
<< Param("oauth_nonce", QString::number(qrand()))
<< Param("oauth_signature", signature)
<< Param("oauth_token", token_);
QStringList encoded_params;
foreach (const Param& p, params) {
encoded_params << QString("%1=\"%2\"").arg(p.first, p.second);
}
QString authorisation_header = QString("OAuth ") + encoded_params.join(", ");
qLog(Debug) << authorisation_header;
QNetworkRequest request(url);
request.setRawHeader("Authorization", authorisation_header.toUtf8());
QByteArray authorisation_header = GenerateAuthorisationHeader(
token_, secret_);
request.setRawHeader("Authorization", authorisation_header);
QNetworkReply* reply = network_->post(request, QByteArray());
NewClosure(reply, SIGNAL(finished()),
@ -161,16 +132,24 @@ void DropboxAuthenticator::RequestAccessTokenFinished(QNetworkReply* reply) {
}
QByteArray DropboxAuthenticator::GenerateAuthorisationHeader() {
return GenerateAuthorisationHeader(access_token_, access_token_secret_);
}
QByteArray DropboxAuthenticator::GenerateAuthorisationHeader(
const QString& token,
const QString& token_secret) {
typedef QPair<QString, QString> Param;
QByteArray signature = QUrl::toPercentEncoding(
QString("%1&%2").arg(kAppSecret, access_token_secret_));
QString("%1&%2").arg(kAppSecret, token_secret));
QList<Param> params;
params << Param("oauth_consumer_key", kAppKey)
<< Param("oauth_signature_method", "PLAINTEXT")
<< Param("oauth_timestamp", QString::number(time(NULL)))
<< Param("oauth_nonce", QString::number(qrand()))
<< Param("oauth_signature", signature)
<< Param("oauth_token", access_token_);
<< Param("oauth_signature", signature);
if (!token.isNull()) {
params << Param("oauth_token", token);
}
QStringList encoded_params;
foreach (const Param& p, params) {
encoded_params << QString("%1=\"%2\"").arg(p.first, p.second);

View File

@ -18,6 +18,9 @@ class DropboxAuthenticator : public QObject {
const QString& uid() const { return uid_; }
const QString& name() const { return name_; }
static QByteArray GenerateAuthorisationHeader(
const QString& token, const QString& secret);
signals:
void Finished();

View File

@ -1,5 +1,7 @@
#include "dropboxservice.h"
#include "core/logging.h"
#include "core/network.h"
#include "internet/dropboxauthenticator.h"
const char* DropboxService::kServiceName = "Dropbox";
@ -9,6 +11,11 @@ namespace {
static const char* kServiceId = "dropbox";
static const char* kMetadataEndpoint =
"https://api.dropbox.com/1/metadata/dropbox/";
//static const char* kMediaEndpoint =
// "https://api.dropbox.com/1/media/dropbox/";
} // namespace
DropboxService::DropboxService(Application* app, InternetModel* parent)
@ -16,7 +23,12 @@ DropboxService::DropboxService(Application* app, InternetModel* parent)
app, parent,
kServiceName, kServiceId,
QIcon(":/providers/dropbox.png"),
SettingsDialog::Page_Dropbox) {
SettingsDialog::Page_Dropbox),
network_(new NetworkAccessManager(this)) {
QSettings settings;
settings.beginGroup(kSettingsGroup);
access_token_ = settings.value("access_token").toString();
access_token_secret_ = settings.value("access_token_secret").toString();
}
bool DropboxService::has_credentials() const {
@ -24,12 +36,8 @@ bool DropboxService::has_credentials() const {
}
void DropboxService::Connect() {
if (!has_credentials()) {
DropboxAuthenticator* authenticator = new DropboxAuthenticator;
authenticator->StartAuthorisation();
NewClosure(authenticator, SIGNAL(Finished()),
this, SLOT(AuthenticationFinished(DropboxAuthenticator*)),
authenticator);
if (has_credentials()) {
RequestFileList("");
} else {
ShowSettingsDialog();
}
@ -38,12 +46,38 @@ void DropboxService::Connect() {
void DropboxService::AuthenticationFinished(DropboxAuthenticator* authenticator) {
authenticator->deleteLater();
access_token_ = authenticator->access_token();
access_token_secret_ = authenticator->access_token_secret();
QSettings settings;
settings.beginGroup(kSettingsGroup);
settings.setValue("access_token", authenticator->access_token());
settings.setValue("access_token_secret", authenticator->access_token_secret());
settings.setValue("access_token", access_token_);
settings.setValue("access_token_secret", access_token_secret_);
settings.setValue("name", authenticator->name());
emit Connected();
RequestFileList("");
}
QByteArray DropboxService::GenerateAuthorisationHeader() {
return DropboxAuthenticator::GenerateAuthorisationHeader(
access_token_,
access_token_secret_);
}
void DropboxService::RequestFileList(const QString& path) {
QUrl url(QString(kMetadataEndpoint) + path);
QNetworkRequest request(url);
request.setRawHeader("Authorization", GenerateAuthorisationHeader());
QNetworkReply* reply = network_->get(request);
NewClosure(reply, SIGNAL(finished()),
this, SLOT(RequestFileListFinished(QNetworkReply*)), reply);
}
void DropboxService::RequestFileListFinished(QNetworkReply* reply) {
reply->deleteLater();
qLog(Debug) << reply->readAll();
}

View File

@ -6,6 +6,8 @@
#include "core/tagreaderclient.h"
class DropboxAuthenticator;
class NetworkAccessManager;
class QNetworkReply;
class DropboxService : public CloudFileService {
Q_OBJECT
@ -22,13 +24,20 @@ class DropboxService : public CloudFileService {
public slots:
void Connect();
void AuthenticationFinished(DropboxAuthenticator* authenticator);
private slots:
void AuthenticationFinished(DropboxAuthenticator* authenticator);
void RequestFileListFinished(QNetworkReply* reply);
private:
void RequestFileList(const QString& path);
QByteArray GenerateAuthorisationHeader();
private:
QString access_token_;
QString access_token_secret_;
NetworkAccessManager* network_;
};
#endif // DROPBOXSERVICE_H

View File

@ -19,6 +19,7 @@
#include "ui_dropboxsettingspage.h"
#include "core/application.h"
#include "internet/dropboxauthenticator.h"
#include "internet/dropboxservice.h"
#include "internet/internetmodel.h"
#include "ui/settingsdialog.h"
@ -46,10 +47,10 @@ void DropboxSettingsPage::Load() {
QSettings s;
s.beginGroup(DropboxService::kSettingsGroup);
const QString user_email = s.value("user_email").toString();
const QString name = s.value("name").toString();
if (!user_email.isEmpty()) {
ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedIn, user_email);
if (!name.isEmpty()) {
ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedIn, name);
}
}
@ -59,7 +60,14 @@ void DropboxSettingsPage::Save() {
}
void DropboxSettingsPage::LoginClicked() {
service_->Connect();
DropboxAuthenticator* authenticator = new DropboxAuthenticator;
NewClosure(authenticator, SIGNAL(Finished()),
this, SLOT(Connected(DropboxAuthenticator*)),
authenticator);
NewClosure(authenticator, SIGNAL(Finished()),
service_, SLOT(AuthenticationFinished(DropboxAuthenticator*)),
authenticator);
ui_->login_button->setEnabled(false);
}
@ -76,11 +84,7 @@ void DropboxSettingsPage::LogoutClicked() {
ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedOut);
}
void DropboxSettingsPage::Connected() {
QSettings s;
s.beginGroup(DropboxService::kSettingsGroup);
const QString user_email = s.value("user_email").toString();
ui_->login_state->SetLoggedIn(LoginStateWidget::LoggedIn, user_email);
void DropboxSettingsPage::Connected(DropboxAuthenticator* authenticator) {
ui_->login_state->SetLoggedIn(
LoginStateWidget::LoggedIn, authenticator->name());
}

View File

@ -23,6 +23,7 @@
#include <QModelIndex>
#include <QWidget>
class DropboxAuthenticator;
class DropboxService;
class Ui_DropboxSettingsPage;
@ -42,7 +43,7 @@ public:
private slots:
void LoginClicked();
void LogoutClicked();
void Connected();
void Connected(DropboxAuthenticator* authenticator);
private:
Ui_DropboxSettingsPage* ui_;