mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-30 19:15:08 +01:00
Basic authentication for Ubuntu One.
This commit is contained in:
parent
c7be61f11d
commit
81e68145cd
74
src/internet/ubuntuoneauthenticator.cpp
Normal file
74
src/internet/ubuntuoneauthenticator.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
#include "ubuntuoneauthenticator.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QHostInfo>
|
||||
|
||||
#include <qjson/parser.h>
|
||||
|
||||
#include "core/closure.h"
|
||||
#include "core/logging.h"
|
||||
#include "core/network.h"
|
||||
|
||||
namespace {
|
||||
static const char* kUbuntuOneEndpoint =
|
||||
"https://login.ubuntu.com/api/1.0/authentications";
|
||||
static const char* kTokenNameTemplate = "Ubuntu One @ %1 [%2]";
|
||||
}
|
||||
|
||||
UbuntuOneAuthenticator::UbuntuOneAuthenticator(QObject* parent)
|
||||
: QObject(parent),
|
||||
network_(new NetworkAccessManager(this)) {
|
||||
}
|
||||
|
||||
void UbuntuOneAuthenticator::StartAuthorisation(
|
||||
const QString& email,
|
||||
const QString& password) {
|
||||
QUrl url(kUbuntuOneEndpoint);
|
||||
url.addQueryItem("ws.op", "authenticate");
|
||||
QString token_name = QString(kTokenNameTemplate).arg(
|
||||
QHostInfo::localHostName(),
|
||||
QCoreApplication::applicationName());
|
||||
url.addQueryItem("token_name", token_name);
|
||||
|
||||
QByteArray authentication = QString(email + ":" + password).toAscii().toBase64();
|
||||
QString authorisation =
|
||||
QString("Basic %1").arg(QString::fromAscii(authentication));
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setRawHeader("Authorization", authorisation.toAscii());
|
||||
request.setRawHeader("Accept", "application/json");
|
||||
|
||||
QNetworkReply* reply = network_->get(request);
|
||||
NewClosure(reply, SIGNAL(finished()),
|
||||
this, SLOT(AuthorisationFinished(QNetworkReply*)), reply);
|
||||
|
||||
qLog(Debug) << url;
|
||||
qLog(Debug) << authorisation;
|
||||
}
|
||||
|
||||
void UbuntuOneAuthenticator::AuthorisationFinished(QNetworkReply* reply) {
|
||||
reply->deleteLater();
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
qLog(Debug) << data;
|
||||
|
||||
QJson::Parser parser;
|
||||
bool ok = false;
|
||||
QVariant json = parser.parse(data, &ok);
|
||||
if (!ok) {
|
||||
qLog(Error) << "Failed to authenticate to Ubuntu One:" << parser.errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
QVariantMap auth_info = json.toMap();
|
||||
QString consumer_key = auth_info["consumer_key"].toString();
|
||||
QString consumer_secret = auth_info["consumer_secret"].toString();
|
||||
QString token = auth_info["token"].toString();
|
||||
QString token_secret = auth_info["token_secret"].toString();
|
||||
|
||||
qLog(Debug)
|
||||
<< consumer_key
|
||||
<< consumer_secret
|
||||
<< token
|
||||
<< token_secret;
|
||||
}
|
25
src/internet/ubuntuoneauthenticator.h
Normal file
25
src/internet/ubuntuoneauthenticator.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef UBUNTUONEAUTHENTICATOR_H
|
||||
#define UBUNTUONEAUTHENTICATOR_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QNetworkReply;
|
||||
class NetworkAccessManager;
|
||||
|
||||
class UbuntuOneAuthenticator : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit UbuntuOneAuthenticator(QObject* parent = 0);
|
||||
void StartAuthorisation(const QString& email, const QString& password);
|
||||
|
||||
signals:
|
||||
void Finished();
|
||||
|
||||
private slots:
|
||||
void AuthorisationFinished(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
NetworkAccessManager* network_;
|
||||
};
|
||||
|
||||
#endif // UBUNTUONEAUTHENTICATOR_H
|
Loading…
x
Reference in New Issue
Block a user