1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

Add method to make HawkPost and HawkGet Requests

This commit is contained in:
Prasenjit Kumar Shaw 2019-07-14 13:13:48 +05:30
parent f0e656e719
commit f06b61a448
12 changed files with 977 additions and 68 deletions

View File

@ -33,6 +33,7 @@ include_directories(
session
sidebar
sync
sync/hawk
tabwidget
tools
webengine
@ -220,6 +221,8 @@ set(SRCS ${SRCS}
sync/communicator.cpp
sync/fxalogin.cpp
sync/synccrypto.cpp
sync/hawk/hawk.cpp
sync/syncmanager.cpp
tabwidget/combotabbar.cpp
tabwidget/tabbar.cpp
tabwidget/tabicon.cpp

View File

@ -48,6 +48,7 @@
#include "closedwindowsmanager.h"
#include "protocolhandlermanager.h"
#include "../config.h"
#include "syncmanager.h"
#include <QWebEngineSettings>
#include <QDesktopServices>
@ -106,6 +107,7 @@ MainApplication::MainApplication(int &argc, char** argv)
, m_desktopNotifications(nullptr)
, m_webProfile(nullptr)
, m_autoSaver(nullptr)
, m_syncManager(nullptr)
#if defined(Q_OS_WIN) && !defined(Q_OS_OS2)
, m_registerQAppAssociation(0)
#endif
@ -289,6 +291,8 @@ MainApplication::MainApplication(int &argc, char** argv)
m_networkManager = new NetworkManager(this);
setupUserScripts();
m_syncManager = new SyncManager(this);
if (!isPrivate() && !isTestModeEnabled()) {
m_sessionManager = new SessionManager(this);
@ -378,6 +382,8 @@ MainApplication::~MainApplication()
delete m_cookieJar;
m_cookieJar = nullptr;
m_syncManager->deleteLater();
Settings::syncSettings();
}
@ -636,6 +642,12 @@ QWebEngineSettings *MainApplication::webSettings() const
return m_webProfile->settings();
}
SyncManager * MainApplication::syncManager()
{
return m_syncManager;
}
// static
MainApplication* MainApplication::instance()
{

View File

@ -53,6 +53,7 @@ class ProxyStyle;
class SessionManager;
class ClosedWindowsManager;
class ProtocolHandlerManager;
class SyncManager;
class FALKON_EXPORT MainApplication : public QtSingleApplication
{
@ -114,6 +115,7 @@ public:
DesktopNotificationsFactory* desktopNotifications();
QWebEngineProfile* webProfile() const;
QWebEngineSettings *webSettings() const;
SyncManager* syncManager();
QByteArray saveState() const;
@ -188,6 +190,7 @@ private:
HTML5PermissionsManager* m_html5PermissionsManager;
DesktopNotificationsFactory* m_desktopNotifications;
QWebEngineProfile* m_webProfile;
SyncManager* m_syncManager;
AutoSaver* m_autoSaver;
ProxyStyle *m_proxyStyle = nullptr;

View File

@ -18,9 +18,12 @@
#include "syncoptions.h"
#include "ui_syncoptions.h"
#include "fxalogin.h"
#include "mainapplication.h"
#include "syncmanager.h"
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QPushButton>
SyncOptions::SyncOptions(QWidget* parent)
: QWidget(parent)
@ -30,6 +33,8 @@ SyncOptions::SyncOptions(QWidget* parent)
loginPage = new FxALoginPage(this);
ui->fxaloginframe->addWidget(loginPage);
connect(ui->btnSyncNow, &QPushButton::clicked, mApp->syncManager(), &SyncManager::sync);
}
SyncOptions::~SyncOptions()

View File

@ -7,79 +7,30 @@
<x>0</x>
<y>0</y>
<width>607</width>
<height>749</height>
<height>604</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>511</height>
</rect>
</property>
<layout class="QVBoxLayout" name="fxaloginframe"/>
</widget>
<widget class="QWidget" name="verticalLayoutWidget_2">
<property name="geometry">
<rect>
<x>0</x>
<y>530</y>
<width>601</width>
<height>211</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QWidget" name="options" native="true">
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>51</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Sync Interval :</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton">
<property name="text">
<string>5 minutes</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_3">
<property name="text">
<string>15 minutes</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_2">
<property name="text">
<string>30 minutes</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0" colspan="2">
<layout class="QVBoxLayout" name="fxaloginframe"/>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="btnSyncNow">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Sync Now</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>

View File

@ -100,6 +100,7 @@ void FxALoginPage::parseMessage(QJsonObject *msg)
settings.setValue(QSL("KeyFetchToken"), key_fetch_token);
settings.setValue(QSL("UnwrapBKey"), unwrap_kb);
settings.endGroup();
// derive the various tokens and save into settings right here and instead of synccreds, create a user_data object
}
}

362
src/lib/sync/hawk/hawk.cpp Normal file
View File

@ -0,0 +1,362 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Prasenjit Kumar Shaw <shawprasenjit07@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "hawk.h"
#include "synccrypto.h"
#include <QByteArray>
#include <QString>
#include <QCryptographicHash>
#include <QUrl>
#include <QDateTime>
#include <QDebug>
#include <nettle/hmac.h>
#include <nettle/sha2.h>
HawkOptions::HawkOptions(const char* app, const char* dlg, const char* ext, const char* contentType, const char* hash, const char* localTimeOffset, const char* nonce, const char* payload, const char* timestamp)
{
m_app = new QByteArray(app);
m_dlg = new QByteArray(dlg);
m_ext = new QByteArray(ext);
m_contentType = new QByteArray(contentType);
m_hash = new QByteArray(hash);
m_localTimeOffset = new QByteArray(localTimeOffset);
m_nonce = new QByteArray(nonce);
m_payload = new QByteArray(payload);
m_timestamp = new QByteArray(timestamp);
qDebug() << "New HawkOptions created: "
<< "\n m_app: " << m_app->data()
<< "\n m_dlg: " << m_dlg->data()
<< "\n m_ext: " << m_ext->data()
<< "\n m_contentType: " << m_contentType->data()
<< "\n m_hash: " << m_hash->data()
<< "\n m_localTimeOffset: " << m_localTimeOffset->data()
<< "\n m_nonce: " << m_nonce->data()
<< "\n m_payload: " << m_payload->data()
<< "\n m_timestamp: " << m_timestamp->data();
}
HawkOptions::~HawkOptions()
{
delete m_app;
delete m_dlg;
delete m_ext;
delete m_contentType;
delete m_hash;
delete m_localTimeOffset;
delete m_nonce;
delete m_payload;
delete m_timestamp;
}
HawkArtifacts::HawkArtifacts(const char* app, const char* dlg, const char* ext, const char* hash, const char* host, const char* method, const char* nonce, const quint16 port, const char* resource, quint64 timestamp)
{
m_app = new QByteArray(app);
m_dlg = new QByteArray(dlg);
m_ext = new QByteArray(ext);
m_hash = new QByteArray(hash);
m_host = new QByteArray(host);
m_method = new QByteArray(method);
m_nonce = new QByteArray(nonce);
m_resource = new QByteArray(resource);
if (port) {
QString tempPort = QString("%1").arg(port);
m_port = new QByteArray(tempPort.toUtf8().data());
} else {
m_port = new QByteArray();
}
if (timestamp) {
QString tempTS = QString("%1").arg(timestamp);
m_timestamp = new QByteArray(tempTS.toUtf8().data());
} else {
m_timestamp = new QByteArray();
}
qDebug() << "New HawkOptions created: "
<< "\n m_app: " << m_app->data()
<< "\n m_dlg: " << m_dlg->data()
<< "\n m_ext: " << m_ext->data()
<< "\n m_hash: " << m_hash->data()
<< "\n m_host: " << m_host->data()
<< "\n m_method: " << m_method->data()
<< "\n m_nonce: " << m_nonce->data()
<< "\n m_resource: " << m_resource->data()
<< "\n m_port: " << m_port->data()
<< "\n m_timestamp: " << m_timestamp->data();
}
HawkArtifacts::~HawkArtifacts()
{
delete m_app;
delete m_dlg;
delete m_ext;
delete m_hash;
delete m_host;
delete m_method;
delete m_nonce;
delete m_port;
delete m_timestamp;
}
HawkHeader::HawkHeader(const char* url, const char* method, const char* id, const char* key, size_t keyLen, HawkOptions* option)
{
qDebug() << "Inside HawkHeader constructor";
qint64 ts = QDateTime::currentSecsSinceEpoch();
qDebug() << "timestamp: " << ts;
QString *hash = new QString(option->m_hash->data());
QString *payload = new QString(option->m_payload->data());
QString *timestamp = new QString(option->m_timestamp->data());
qDebug() << "Debug#1";
QUrl uri = QUrl(QString(url));
qDebug() << "URL query: " << uri.query() << " length: " << uri.query().length();
QString *resource = (uri.query().length() == 0) ?
(new QString(uri.path())) :
(new QString(uri.path() + QString("?") + uri.query()));
QString *nonce = nullptr;
if (option->m_nonce->length() > 0) {
nonce = new QString(option->m_nonce->data());
} else {
qDebug() << "Creating random nonce";
u_char *bytes = new u_char(NONCE_LEN / 2);
generateRandomBytes(nullptr, NONCE_LEN / 2, bytes);
QByteArray temp = QByteArray::fromRawData((const char *)bytes, NONCE_LEN / 2);
nonce = new QString(temp.toHex().data());
delete bytes;
}
if (timestamp->length() > 0) {
QString *localTimeOffset = new QString(option->m_localTimeOffset->data());
quint64 offset = 0;
if (localTimeOffset->length() > 0) {
qDebug() << "Adding offset using localTimeOffset";
offset = localTimeOffset->toInt(nullptr, 10);
}
ts = timestamp->toInt(nullptr, 10) + offset;
delete localTimeOffset;
}
if (hash->length() == 0 && payload->length() > 0) {
QByteArray *contentType = nullptr;
if (option) {
contentType = new QByteArray(option->m_contentType->data());
} else {
contentType = new QByteArray("text/plain");
}
QByteArray *tempPayload = new QByteArray();
tempPayload->append(option->m_payload->data());
qDebug() << "Going to hawkComputePayloadHash";
QByteArray *tempHash = hawkComputePayloadHash(tempPayload, contentType);
hash = new QString(tempHash->data());
delete contentType;
delete tempPayload;
delete tempHash;
}
qint64 defaultPort = 0;
if (uri.scheme() == QLatin1String("http")) {
defaultPort = 80;
} else if (uri.scheme() == QLatin1String("https")) {
defaultPort = 443;
}
qint64 port = uri.port(defaultPort);
qDebug() << "Creating Hawk Artifact";
m_artifacts = new HawkArtifacts(option ? option->m_app->data() : nullptr,
option ? option->m_dlg->data() : nullptr,
option ? option->m_ext->data() : nullptr,
hash->toUtf8().data(),
uri.host().toUtf8().data(),
method,
nonce->toUtf8().data(),
port,
resource->toUtf8().data(),
ts);
QByteArray *header = new QByteArray(QString("Hawk id=\"%1\", ts=\"%2\", nonce=\"%3\"").arg(id).arg(m_artifacts->m_timestamp->data()).arg(m_artifacts->m_nonce->data()).toUtf8().data());
if (m_artifacts->m_hash->length() > 0) {
QByteArray *name = new QByteArray("hash");
QByteArray *value = new QByteArray(m_artifacts->m_hash->toHex().data());
header = hawkAppendToHeader(header, name, value);
delete name;
delete value;
}
if (m_artifacts->m_ext->length() > 0) {
QString ext(m_artifacts->m_ext->data());
ext.replace(QString("\\"), QString("\\\\"));
ext.replace(QString("\n"), QString("\\n"));
QByteArray *name = new QByteArray("ext");
QByteArray *value = new QByteArray(ext.toUtf8().data());
header = hawkAppendToHeader(header, name, value);
delete name;
delete value;
}
if (true) {
QByteArray tempType("header");
QByteArray tempKey(key);
QByteArray *tempMac = hawkComputeMac(&tempType, &tempKey, keyLen, m_artifacts);
QByteArray *mac = new QByteArray(tempMac->toBase64(QByteArray::Base64Encoding).data());
QByteArray *name = new QByteArray("mac");
header = hawkAppendToHeader(header, name, mac);
delete name;
delete tempMac;
delete mac;
}
if (m_artifacts->m_app->length() > 0) {
QByteArray *name = new QByteArray("app");
QByteArray *value = new QByteArray(m_artifacts->m_app->data());
header = hawkAppendToHeader(header, name, value);
delete name;
delete value;
if (m_artifacts->m_dlg->length() > 0) {
name = new QByteArray("dlg");
value = new QByteArray(m_artifacts->m_dlg->data());
header = hawkAppendToHeader(header, name, value);
delete name;
delete value;
}
}
m_header = new QByteArray(header->data());
delete hash;
delete payload;
delete timestamp;
delete resource;
delete nonce;
delete header;
qDebug() << "Inside newHawkHeader, header data: " << m_header->data();
}
HawkHeader::~HawkHeader()
{
delete m_artifacts;
delete m_header;
}
QByteArray * HawkHeader::hawkParseContentType(QByteArray* contentType)
{
QByteArray temp;
int index = contentType->indexOf(';', 0);
temp.append(*contentType);
temp.truncate(index + 1);
QByteArray *ret = new QByteArray();
ret->append(temp.toLower());
qDebug() << "Inside hawkParseContentType: " << ret->data();
return ret;
}
QByteArray * HawkHeader::hawkComputePayloadHash(QByteArray* payload, QByteArray* contentType)
{
QByteArray *content = hawkParseContentType(contentType);
QString tempString = QString("hawk.%1.payload\n%2\n%3\n").arg(HAWK_VERSION).arg(content->data()).arg(payload->data());
QByteArray update(tempString.toUtf8().data());
size_t length = update.size();
u_char *digestOut = new u_char[SHA256_DIGEST_SIZE];
sha256_ctx *ctx = new sha256_ctx;
sha256_init(ctx);
sha256_update(ctx, length, (u_char *)(update.data()));
sha256_digest(ctx, SHA256_DIGEST_SIZE, digestOut);
QByteArray *outTemp = new QByteArray(QByteArray::fromRawData((const char *)digestOut, SHA256_DIGEST_SIZE));
QByteArray *out = new QByteArray(outTemp->toBase64().data());
delete ctx;
delete content;
delete outTemp;
qDebug() << "Payload: " << payload->data();
qDebug() << "Inside hawkComputePayloadHash: " << out->data();
return out;
}
QByteArray * HawkHeader::hawkAppendToHeader(QByteArray* header, QByteArray* name, QByteArray* value)
{
QString tempString = QString("%1, %2=\"%3\"").arg(header->data()).arg(name->data()).arg(value->data());
QByteArray *out = new QByteArray(tempString.toUtf8().data());
qDebug() << "Inside hawkAppendToHeader: " << out->data();
return out;
}
QByteArray * HawkHeader::hawkNormalizeString(QByteArray* type, HawkArtifacts* artifact)
{
QString host(artifact->m_host->toLower().data());
QString info = QString("hawk.%1.%2").arg(HAWK_VERSION).arg(type->data());
QString method(artifact->m_method->toUpper().data());
QString normalized = info + QString("\n") + QString(artifact->m_timestamp->data())
+ QString("\n") + QString(artifact->m_nonce->data()) + QString("\n")
+ method + QString("\n") + QString(artifact->m_resource->data())
+ QString("\n") + host + QString("\n") + QString(artifact->m_port->data())
+ QString("\n");
if (artifact->m_hash->length() > 0) {
normalized = normalized + QString(artifact->m_hash->data()) + QString("\n");
} else {
normalized = normalized + QString("") + QString("\n");
}
if (artifact->m_ext->length() > 0) {
QString ext(artifact->m_ext->data());
ext.replace(QString("\\"), QString("\\\\"));
ext.replace(QString("\n"), QString("\\n"));
normalized = normalized + ext + QString("\n");
} else {
normalized = normalized + QString("\n");
}
if (artifact->m_app->length() > 0) {
normalized = normalized + QString(artifact->m_app->data()) + QString("\n");
if (artifact->m_dlg->length() > 0) {
normalized = normalized + QString(artifact->m_dlg->data()) + QString("\n");
}
}
QByteArray *out = new QByteArray(normalized.toUtf8().data());
qDebug() << "Inside hawkNormalizeString:\n" << out->data();
return out;
}
QByteArray * HawkHeader::hawkComputeMac(QByteArray* type, QByteArray* key, size_t keyLen, HawkArtifacts* artifact)
{
QByteArray *normalized = hawkNormalizeString(type, artifact);
hmac_sha256_ctx *ctx = new hmac_sha256_ctx;
hmac_sha256_set_key(ctx, keyLen, (u_char *)key->data());
hmac_sha256_update(ctx, normalized->length(), (u_char *)normalized->data());
u_char *out = new u_char(SHA256_DIGEST_SIZE);
hmac_sha256_digest(ctx, SHA256_DIGEST_SIZE, out);
QByteArray *mac = new QByteArray(QByteArray::fromRawData((const char *)out, SHA256_DIGEST_SIZE));
return mac; // convert to hex and then to base64 encoding before using
}

78
src/lib/sync/hawk/hawk.h Normal file
View File

@ -0,0 +1,78 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Prasenjit Kumar Shaw <shawprasenjit07@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include <QByteArray>
#define HAWK_VERSION 1
#define NONCE_LEN 6
class HawkOptions
{
public:
HawkOptions(const char *app, const char *dlg, const char *ext, const char *contentType, const char *hash, const char *localTimeOffset, const char *nonce, const char *payload, const char *timestamp);
~HawkOptions();
QByteArray *m_app;
QByteArray *m_dlg;
QByteArray *m_ext;
QByteArray *m_contentType;
QByteArray *m_hash;
QByteArray *m_localTimeOffset;
QByteArray *m_nonce;
QByteArray *m_payload;
QByteArray *m_timestamp;
};
class HawkArtifacts
{
public:
HawkArtifacts(const char *app, const char *dlg, const char *ext, const char *hash, const char *host, const char *method, const char *nonce, const quint16 port, const char *resource, quint64 timestamp);
~HawkArtifacts();
QByteArray *m_app;
QByteArray *m_dlg;
QByteArray *m_ext;
QByteArray *m_hash;
QByteArray *m_host;
QByteArray *m_method;
QByteArray *m_nonce;
QByteArray *m_port;
QByteArray *m_resource;
QByteArray *m_timestamp;
};
class HawkHeader
{
public:
HawkHeader(const char *url, const char *method, const char *id, const char *key, size_t keyLen, HawkOptions *option);
~HawkHeader();
QByteArray *m_header;
HawkArtifacts *m_artifacts;
private:
QByteArray *hawkParseContentType(QByteArray *contentType);
QByteArray *hawkComputePayloadHash(QByteArray *payload, QByteArray *contentType);
QByteArray *hawkAppendToHeader(QByteArray *header, QByteArray *name, QByteArray *value);
QByteArray *hawkNormalizeString(QByteArray *type, HawkArtifacts *artifact);
QByteArray *hawkComputeMac(QByteArray *type, QByteArray *key, size_t keyLen, HawkArtifacts *artifact);
};

View File

@ -0,0 +1,310 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Prasenjit Kumar Shaw <shawprasenjit07@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "syncmanager.h"
#include "syncrequest.h"
#include "synccrypto.h"
#include "settings.h"
#include "hawk.h"
#include "mainapplication.h"
#include "networkmanager.h"
#include <QString>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
SyncCredentials::SyncCredentials()
{
}
SyncCredentials::~SyncCredentials()
{
qDebug() << "Deleting SyncCreds...";
}
void SyncCredentials::addSyncCredentials(QString key, QString value)
{
Settings settings;
settings.beginGroup(QSL("SyncCredentials"));
settings.setValue(key, value);
settings.endGroup();
}
QString SyncCredentials::getValue(QString key)
{
Settings settings;
settings.beginGroup(QSL("SyncCredentials"));
QString value = settings.value(key, QString("")).toString();
settings.endGroup();
return value;
}
SyncState::SyncState()
{
m_lastSyncTime = new QDateTime();
Settings settings;
settings.beginGroup(QSL("SyncState"));
m_isInitialSync = settings.value(QSL("IsInitialSync"), true).toBool();
*m_lastSyncTime = settings.value(QSL("LastSyncTime"), QDateTime()).toDateTime();
settings.endGroup();
qDebug() << "Loaded SyncState" << "isInitialSync = " << m_isInitialSync << " m_lastSyncTime = " << m_lastSyncTime->toString();
}
SyncState::~SyncState()
{
delete m_lastSyncTime;
qDebug() << "Deleting SyncState...";
}
void SyncState::saveSyncState(bool syncSuccess)
{
qDebug() << "Entered saveSyncState syncSuccess=" << syncSuccess;
if (syncSuccess) {
*m_lastSyncTime = QDateTime::currentDateTimeUtc();
m_isInitialSync = false;
qDebug() << "lastSyncTime = " << m_lastSyncTime->toString() << " isInitialSync= " << m_isInitialSync;
Settings settings;
settings.beginGroup(QSL("SyncState"));
settings.setValue(QSL("IsInitialSync"), m_isInitialSync);
settings.setValue(QSL("LastSyncTime"), *m_lastSyncTime);
settings.endGroup();
}
qDebug() << "SyncState saved...";
}
bool SyncState::isInitialSync()
{
return m_isInitialSync;
}
QDateTime SyncState::lastSyncTime()
{
return *m_lastSyncTime;
}
SyncManager::SyncManager(QObject *parent)
: QObject(parent)
{
m_syncCreds = new SyncCredentials();
m_syncState = new SyncState();
m_networkManager = new QNetworkAccessManager(this);
}
SyncManager::~SyncManager()
{
qDebug() << "Deleting SyncManager...";
saveSyncState();
delete m_syncCreds;
delete m_syncState;
//m_networkManager->deleteLater();
}
void SyncManager::saveSyncState()
{
qDebug() << QSL("saveSyncState(%1)").arg(m_syncSuccess ? "true" : "false");
m_syncState->saveSyncState(m_syncSuccess);
}
void SyncManager::sync()
{
bool error = false;
if (m_storageCredentialsExpired) {
error = getBrowserSignedCertificate();
if (!error) {
m_storageCredentialsExpired = false;
}
qDebug() << "Got Browser Certs...";
}
/*
if(true) {
qDebug() << "Getting Crypto Keys";
getCryptoKeys();
}
*/
if (error) {
m_syncSuccess = false;
qDebug() << "Sync Failed";
} else {
m_syncSuccess = true;
}
saveSyncState();
}
void SyncManager::startSync()
{
sync();
}
bool SyncManager::getBrowserSignedCertificate()
{
bool error = false;
QString endpoint("certificate/sign");
RSAKeyPair *keyPair = generateRSAKeyPair();
QByteArray *sessionToken = new QByteArray(m_syncCreds->getValue("SessionToken").toUtf8().data());
QByteArray *tokenId = new QByteArray();
QByteArray *requestHMACKey = new QByteArray();
QByteArray *requestKey = new QByteArray();
deriveSessionToken(sessionToken, tokenId, requestHMACKey, requestKey);
QByteArray *tokenIdHex = new QByteArray(tokenId->toHex().data());
qDebug() << "TokenId hex: " << tokenIdHex->data();
char *n;
char *e;
n = mpz_get_str(nullptr, 10, keyPair->m_publicKey.n);
e = mpz_get_str(nullptr, 10, keyPair->m_publicKey.e);
QJsonObject objBody;
QJsonObject objKey;
objBody.insert("duration", 1 * 60 * 60 * 1000); // 1 day
objKey.insert("algorithm", "RS");
objKey.insert("n", n);
objKey.insert("e", e);
objBody.insert("publicKey", objKey);
QJsonDocument doc(objBody);
QByteArray *requestBody = new QByteArray(doc.toJson(QJsonDocument::Compact));
createHawkPostReqeuest(endpoint, tokenIdHex, requestHMACKey, 32, requestBody);
delete keyPair;
delete sessionToken;
delete tokenId;
delete requestHMACKey;
delete requestKey;
delete tokenIdHex;
delete n;
delete e;
delete requestBody;
return error;
}
bool SyncManager::getCryptoKeys()
{
QString endpoint("account/keys");
QByteArray *keyFetchToken = new QByteArray(m_syncCreds->getValue("KeyFetchToken").toUtf8().data());
QByteArray *tokenId = new QByteArray();
QByteArray *reqHMACKey = new QByteArray();
QByteArray *respHMACKey = new QByteArray();
QByteArray *respXorKey = new QByteArray();
deriveKeyFetchToken(keyFetchToken, tokenId, reqHMACKey, respHMACKey, respXorKey);
QByteArray *tokenIdHex = new QByteArray(tokenId->toHex().data());
createHawkGetRequest(endpoint, tokenIdHex, reqHMACKey, 32);
delete keyFetchToken;
delete tokenId;
delete reqHMACKey;
delete respHMACKey;
delete respXorKey;
delete tokenIdHex;
return false;
}
void SyncManager::createHawkGetRequest(QString endpoint, QByteArray* id, QByteArray* key, size_t keyLen)
{
QString url = m_FxAServerUrl + QString("/") + endpoint;
QNetworkRequest request;
request.setUrl(QUrl(url.toUtf8()));
qDebug() << "Hawk GET req";
HawkOptions *nullOption = new HawkOptions(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
HawkHeader *header = new HawkHeader(url.toUtf8(), "GET", id->data(), key->data(), keyLen, nullOption);
request.setRawHeader("Authorization", header->m_header->data());
QList<QByteArray> headerList = request.rawHeaderList();
for (int i = 0; i < headerList.size(); ++i) {
QByteArray item = headerList[i];
qDebug() << " " << item.data() << " : " << request.rawHeader(item).data();
}
QNetworkReply *reply = m_networkManager->get(request);
connect(reply, &QNetworkReply::finished, this, [ = ]() {
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Reply Error" << reply->error() << reply->errorString();
qDebug() << "Reply: " << reply->readAll();
reply->deleteLater();
return;
}
qDebug() << "Reply: " << reply->readAll();
reply->deleteLater();
});
delete nullOption;
delete header;
}
void SyncManager::createHawkPostReqeuest(QString endpoint, QByteArray *id, QByteArray *key, size_t keyLen, QByteArray *data)
{
QString contentType = QString("application/json; charset=utf-8");
QString url = m_FxAServerUrl + QString("/") + endpoint;
QNetworkRequest request;
request.setUrl(QUrl(url.toUtf8()));
qDebug() << "Creating Hawk Option";
HawkOptions *options = new HawkOptions(nullptr, nullptr, nullptr, contentType.toUtf8(), nullptr, nullptr, nullptr, data->data(), nullptr);
qDebug() << "Creating Hawk Header";
HawkHeader *header = new HawkHeader(url.toUtf8(), "POST", id->data(), key->data(), keyLen, options);
qDebug() << "Inside createHawkPostReqeuest header data: " << header->m_header->data();
request.setRawHeader("Authorization", header->m_header->data());
request.setRawHeader("Content-Type", contentType.toUtf8());
QByteArray contentLength(QSL("%1").arg(data->size()).toUtf8());
request.setRawHeader("Content-Length", contentLength);
qDebug() << "Request content:";
QList<QByteArray> headerList = request.rawHeaderList();
for (int i = 0; i < headerList.size(); ++i) {
QByteArray item = headerList[i];
qDebug() << " " << item.data() << " : " << request.rawHeader(item).data();
}
qDebug() << " Data: " << data->data();
qDebug() << " Url: " << url;
QNetworkReply *reply = m_networkManager->post(request, *data);
connect(reply, &QNetworkReply::finished, this, [ = ]() {
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Reply Error" << reply->error() << reply->errorString();
qDebug() << "Reply: " << reply->readAll();
return;
}
qDebug() << "Reply: " << reply->readAll();
});
delete options;
delete header;
}

View File

@ -0,0 +1,83 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Prasenjit Kumar Shaw <shawprasenjit07@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include "hawk.h"
#include <QVariant>
#include <QHash>
#include <QObject>
class QDateTime;
class QNetworkAccessManager;
class QNetworkReply;
class QNetworkRequest;
class QByteArray;
class SyncCredentials
{
public:
SyncCredentials(); // load the sync credentials from settings
~SyncCredentials();
void addSyncCredentials(QString key, QString value);
QString getValue(QString key);
};
class SyncState
{
public:
SyncState();
~SyncState();
void saveSyncState(bool syncSuccess);
bool isInitialSync();
QDateTime lastSyncTime();
private:
bool m_isInitialSync;
QDateTime *m_lastSyncTime;
};
class SyncManager : public QObject
{
public:
explicit SyncManager(QObject *parent = nullptr); // load m_syncCreds
~SyncManager();
void sync(); // call it from fxalogin.cpp/mainapplication.cpp after recieving sessionToken
void saveSyncState();
public Q_SLOTS:
void startSync();
private:
bool getBrowserSignedCertificate();
bool getCryptoKeys();
void createHawkPostReqeuest(QString endpoint, QByteArray *id, QByteArray *key, size_t keyLen, QByteArray *data);
void createHawkGetRequest(QString endpoint, QByteArray *id, QByteArray *key, size_t keyLen);
bool m_storageCredentialsExpired = true;
bool m_syncSuccess = false;
bool m_syncReady = false;
const QString m_FxAServerUrl = QString("https://api.accounts.firefox.com/v1");
QNetworkAccessManager *m_networkManager;
SyncCredentials *m_syncCreds;
SyncState *m_syncState;
};

View File

@ -0,0 +1,56 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Prasenjit Kumar Shaw <shawprasenjit07@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "syncrequest.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QQueue>
#include <QObject>
SyncRequestManager::SyncRequestManager(QObject* parent)
: QObject(parent)
{
m_requestManager = new QNetworkAccessManager(this);
m_requestQueue = new QQueue<RequestPair>();
startRequests = false;
}
void SyncRequestManager::addRequest(QNetworkRequest *request, bool post)
{
RequestPair temp = {*request, post};
m_requestQueue->enqueue(temp);
}
void SyncRequestManager::startSync(bool start)
{
startRequests = start;
if (startRequests) {
if (!m_requestQueue->isEmpty()) {
RequestPair req = m_requestQueue->dequeue();
QNetworkRequest request = req.request;
QNetworkReply *reply = nullptr;
if (req.post) {
reply = m_requestManager->get(request);
} else {
reply = m_requestManager->get(request);
}
}
}
}

View File

@ -0,0 +1,45 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Prasenjit Kumar Shaw <shawprasenjit07@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QQueue>
#include <QObject>
class SyncRequestManager : public QObject
{
Q_OBJECT
public:
explicit SyncRequestManager(QObject *parent = nullptr);
void addRequest(QNetworkRequest *request, bool post);
void startSync(bool start);
private:
struct RequestPair {
QNetworkRequest request;
bool post;
};
QNetworkAccessManager *m_requestManager;
QQueue<RequestPair> *m_requestQueue;
bool startRequests;
};