Skip to content

Commit

Permalink
Merge pull request #44 from itzurabhi/feature/protected-download
Browse files Browse the repository at this point in the history
feature: Add authentication support for the download source
  • Loading branch information
alex-spataru authored Sep 3, 2024
2 parents 791c8e2 + 5655479 commit 7f4d8c6
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ find_package(Qt${QT_VERSION_MAJOR} CONFIG REQUIRED COMPONENTS Widgets Network)
add_library(QSimpleUpdater STATIC
etc/resources/qsimpleupdater.qrc
include/QSimpleUpdater.h
src/AuthenticateDialog.cpp
src/AuthenticateDialog.h
src/AuthenticateDialog.ui
src/Downloader.cpp
src/Downloader.h
src/Downloader.ui
Expand Down
2 changes: 2 additions & 0 deletions include/QSimpleUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public slots:
void setUseCustomAppcast(const QString &url, const bool customAppcast);
void setUseCustomInstallProcedures(const QString &url, const bool custom);
void setMandatoryUpdate(const QString &url, const bool mandatory_update);
void setDownloadUserName(const QString &url, const QString &userName);
void setDownloadPassword(const QString &url, const QString &password);

protected:
~QSimpleUpdater();
Expand Down
34 changes: 34 additions & 0 deletions src/AuthenticateDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "AuthenticateDialog.h"
#include "ui_AuthenticateDialog.h"

AuthenticateDialog::AuthenticateDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::AuthenticateDialog)
{
ui->setupUi(this);
}

AuthenticateDialog::~AuthenticateDialog()
{
delete ui;
}

void AuthenticateDialog::setUserName(const QString &userName)
{
ui->userLE->setText(userName);
}

void AuthenticateDialog::setPassword(const QString &password)
{
ui->passwordLE->setText(password);
}

QString AuthenticateDialog::userName() const
{
return ui->userLE->text();
}

QString AuthenticateDialog::password() const
{
return ui->passwordLE->text();
}
28 changes: 28 additions & 0 deletions src/AuthenticateDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef AUTHENTICATEDIALOG_H
#define AUTHENTICATEDIALOG_H

#include <QDialog>

namespace Ui
{
class AuthenticateDialog;
}

class AuthenticateDialog : public QDialog
{
Q_OBJECT

public:
explicit AuthenticateDialog(QWidget *parent = nullptr);
~AuthenticateDialog();

void setUserName(const QString &userName);
void setPassword(const QString &password);
QString userName() const;
QString password() const;

private:
Ui::AuthenticateDialog *ui;
};

#endif // AUTHENTICATEDIALOG_H
111 changes: 111 additions & 0 deletions src/AuthenticateDialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AuthenticateDialog</class>
<widget class="QDialog" name="AuthenticateDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>512</width>
<height>164</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Please provide the user name and password for the download location.</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;User name:</string>
</property>
<property name="buddy">
<cstring>userLE</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="userLE"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&amp;Password:</string>
</property>
<property name="buddy">
<cstring>passwordLE</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="passwordLE">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AuthenticateDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AuthenticateDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
23 changes: 20 additions & 3 deletions src/Downloader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2014-2021 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -30,9 +30,11 @@
#include <QNetworkAccessManager>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QAuthenticator>

#include <math.h>

#include "AuthenticateDialog.h"
#include "Downloader.h"

static const QString PARTIAL_DOWN(".part");
Expand Down Expand Up @@ -66,6 +68,8 @@ Downloader::Downloader(QWidget *parent)
connect(m_ui->stopButton, SIGNAL(clicked()), this, SLOT(cancelDownload()));
connect(m_ui->openButton, SIGNAL(clicked()), this, SLOT(installUpdate()));

connect(m_manager, &QNetworkAccessManager::authenticationRequired, this, &Downloader::authenticate);

/* Resize to fit */
setFixedSize(minimumSizeHint());
}
Expand Down Expand Up @@ -228,7 +232,7 @@ void Downloader::installUpdate()

if (m_mandatoryUpdate)
text = tr("In order to install the update, you may need to "
"quit the application. This is a mandatory update, exiting now will close the application");
"quit the application. This is a mandatory update, exiting now will close the application.");

box.setText("<h3>" + text + "</h3>");

Expand Down Expand Up @@ -356,7 +360,7 @@ void Downloader::metaDataChanged()
if (variant.isValid())
{
QString contentDisposition = QByteArray::fromPercentEncoding(variant.toByteArray()).constData();
QRegularExpression regExp("filename=(\S+)");
QRegularExpression regExp(R"(filename=(\S+))");
QRegularExpressionMatch match = regExp.match(contentDisposition);
if (match.hasMatch())
{
Expand Down Expand Up @@ -446,6 +450,19 @@ void Downloader::calculateTimeRemaining(qint64 received, qint64 total)
}
}

void Downloader::authenticate(QNetworkReply *reply, QAuthenticator *authenticator)
{
Q_UNUSED(reply);
AuthenticateDialog dlg(this);
dlg.setUserName(authenticator->user());
dlg.setPassword(authenticator->password());
if (dlg.exec())
{
authenticator->setUser(dlg.userName());
authenticator->setPassword(dlg.password());
}
}

/**
* Rounds the given \a input to two decimal places
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Downloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Ui
class Downloader;
}

class QAuthenticator;
class QNetworkReply;
class QNetworkAccessManager;

Expand Down Expand Up @@ -72,6 +73,7 @@ private slots:
void calculateSizes(qint64 received, qint64 total);
void updateProgress(qint64 received, qint64 total);
void calculateTimeRemaining(qint64 received, qint64 total);
void authenticate(QNetworkReply *reply, QAuthenticator *authenticator);

private:
qreal round(const qreal &input);
Expand Down
10 changes: 10 additions & 0 deletions src/QSimpleUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ void QSimpleUpdater::setMandatoryUpdate(const QString &url, const bool mandatory
getUpdater(url)->setMandatoryUpdate(mandatory_update);
}

void QSimpleUpdater::setDownloadUserName(const QString &url, const QString &userName)
{
getUpdater(url)->setDownloadUserName(userName);
}

void QSimpleUpdater::setDownloadPassword(const QString &url, const QString &password)
{
getUpdater(url)->setDownloadPassword(password);
}

/**
* Returns the \c Updater instance registered with the given \a url.
*
Expand Down
23 changes: 20 additions & 3 deletions src/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ void Updater::setMandatoryUpdate(const bool mandatory_update)
{
m_mandatoryUpdate = mandatory_update;
}

void Updater::setDownloadUserName(const QString &user_name)
{
m_downloadUserName = user_name;
}

void Updater::setDownloadPassword(const QString &password)
{
m_downloadPassword = password;
}

/**
* Called when the download of the update definitions file is finished.
*/
Expand Down Expand Up @@ -435,9 +446,12 @@ void Updater::setUpdateAvailable(const bool available)
QString text = tr("Would you like to download the update now?");
if (m_mandatoryUpdate)
{
text = tr("Would you like to download the update now? This is a mandatory update, exiting now will close the "
"application");
text = tr("Would you like to download the update now?<br />This is a mandatory update, exiting now will close "
"the application.");
}
text += "<br/><br/>";
if (!m_changelog.isEmpty())
text += tr("<strong>Change log:</strong><br/>%1").arg(m_changelog);

QString title
= "<h3>" + tr("Version %1 of %2 has been released!").arg(latestVersion()).arg(moduleName()) + "</h3>";
Expand All @@ -457,7 +471,10 @@ void Updater::setUpdateAvailable(const bool available)
m_downloader->setUrlId(url());
m_downloader->setFileName(downloadUrl().split("/").last());
m_downloader->setMandatoryUpdate(m_mandatoryUpdate);
m_downloader->startDownload(QUrl(downloadUrl()));
auto url = QUrl(downloadUrl());
url.setUserName(m_downloadUserName);
url.setPassword(m_downloadPassword);
m_downloader->startDownload(url);
}

else
Expand Down
5 changes: 4 additions & 1 deletion src/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public slots:
void setUseCustomAppcast(const bool customAppcast);
void setUseCustomInstallProcedures(const bool custom);
void setMandatoryUpdate(const bool mandatory_update);
void setDownloadUserName(const QString &user_name);
void setDownloadPassword(const QString &password);

private slots:
void onReply(QNetworkReply *reply);
Expand All @@ -106,7 +108,8 @@ private slots:
QString m_downloadUrl;
QString m_moduleVersion;
QString m_latestVersion;

QString m_downloadUserName;
QString m_downloadPassword;
Downloader *m_downloader;
QNetworkAccessManager *m_manager;
};
Expand Down

0 comments on commit 7f4d8c6

Please sign in to comment.