diff options
author | Katja Marttila <katja.marttila@qt.io> | 2024-03-26 16:51:58 +0200 |
---|---|---|
committer | Katja Marttila <katja.marttila@qt.io> | 2024-04-02 14:46:15 +0300 |
commit | d75f906bcfe69d024b1e5d6a2bdaed5c1e25d66a (patch) | |
tree | 928a38006cfb7f007ab46d01d2c68e42728dfa7c /src/libs/installer | |
parent | 7715fdfef978ffeea4879849a7a3fbedc232d3a1 (diff) |
Fix http authorization login
Setting http authorization login credentials had no effect, the metadatajob
did not refetch the repositories after login credentials were given.
Also when using CLI install, installer crashed when login credentials
were needed as it tried to open a dialog.
Fixing the crash so that when using CLI install, user is asked for the
credentials from terminal, and reseting metadatajob so that credentials
are taken into account.
Task-number: QTIFW-3301
Change-Id: I72d254c5c44413e2d893f7c686001c2d0de0e8e9
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Arttu Tarkiainen <arttu.tarkiainen@qt.io>
Diffstat (limited to 'src/libs/installer')
-rw-r--r-- | src/libs/installer/globals.cpp | 57 | ||||
-rw-r--r-- | src/libs/installer/globals.h | 2 | ||||
-rw-r--r-- | src/libs/installer/metadatajob.cpp | 24 |
3 files changed, 77 insertions, 6 deletions
diff --git a/src/libs/installer/globals.cpp b/src/libs/installer/globals.cpp index b6bf0ca3e..3fd084768 100644 --- a/src/libs/installer/globals.cpp +++ b/src/libs/installer/globals.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2023 The Qt Company Ltd. +** Copyright (C) 2024 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Installer Framework. @@ -30,6 +30,14 @@ #include "globals.h" +#if defined(Q_OS_MACOS) || defined(Q_OS_LINUX) +#include <termios.h> +#include <unistd.h> +#elif defined(Q_OS_WIN) +#include <conio.h> +#endif +#include <iostream> + const char IFW_SERVER[] = "ifw.server"; const char IFW_INSTALLER_INSTALLLOG[] = "ifw.installer.installlog"; const char IFW_DEVELOPER_BUILD[] = "ifw.developer.build"; @@ -119,5 +127,52 @@ QString enumToString(const QMetaObject& metaObject, const char *enumerator, int return value; } +void askForCredentials(QString *username, QString *password, const QString &usernameTitle, const QString &passwordTitle) +{ + std::string usernameStdStr; + std::string passwordStdStr; + + std::cout << qPrintable(usernameTitle); + std::cin >> usernameStdStr; + + std::cout << qPrintable(passwordTitle); +#if defined(Q_OS_MACOS) || defined(Q_OS_LINUX) + termios oldTerm; + termios term; + + // Turn off echoing + tcgetattr(STDIN_FILENO, &oldTerm); + term = oldTerm; + term.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &term); + + std::cin >> passwordStdStr; + + // Clear input buffer + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + + // Restore old attributes + tcsetattr(STDIN_FILENO, TCSANOW, &oldTerm); +#elif defined(Q_OS_WIN) + char ch; + while ((ch = _getch()) != '\r') { // Return key + if (ch == '\b') { // Backspace key + if (!passwordStdStr.empty()) + passwordStdStr.pop_back(); + } else { + passwordStdStr.push_back(ch); + } + } + // Clear input buffer + int c; + while ((c = getchar()) != '\n' && c != EOF); + +#endif + std::cout << "\n"; + + *username = username->fromStdString(usernameStdStr); + *password = password->fromStdString(passwordStdStr); +} + } // namespace QInstaller diff --git a/src/libs/installer/globals.h b/src/libs/installer/globals.h index 3b3f4e3ab..2d119048b 100644 --- a/src/libs/installer/globals.h +++ b/src/libs/installer/globals.h @@ -59,6 +59,8 @@ QSet<T> toQSet(const C<T> &container) return QSet<T>(container.begin(), container.end()); } +void askForCredentials(QString *username, QString *password, const QString &usernameTitle, const QString &passwordTitle); + } // QInstaller #endif // GLOBALS_H diff --git a/src/libs/installer/metadatajob.cpp b/src/libs/installer/metadatajob.cpp index 641f2da62..536449ebd 100644 --- a/src/libs/installer/metadatajob.cpp +++ b/src/libs/installer/metadatajob.cpp @@ -41,6 +41,7 @@ #include <QtConcurrent> #include <QtMath> #include <QRandomGenerator> +#include <QApplication> namespace QInstaller { @@ -520,13 +521,25 @@ void MetadataJob::xmlTaskFinished() } } else if (e.type() == AuthenticationRequiredException::Type::Server) { qCWarning(QInstaller::lcInstallerInstallLog) << e.message(); - ServerAuthenticationDialog dlg(e.message(), e.taskItem()); - if (dlg.exec() == QDialog::Accepted) { + QString username; + QString password; + if (m_core->isCommandLineInstance()) { + qCDebug(QInstaller::lcInstallerInstallLog) << "Server Requires Authentication"; + qCDebug(QInstaller::lcInstallerInstallLog) << "You need to supply a username and password to access this site."; + askForCredentials(&username, &password, QLatin1String("Username: "), QLatin1String("Password: ")); + } else { + ServerAuthenticationDialog dlg(e.message(), e.taskItem()); + if (dlg.exec() == QDialog::Accepted) { + username = dlg.user(); + password = dlg.password(); + } + } + if (!username.isEmpty()) { Repository original = e.taskItem().value(TaskRole::UserRole) .value<Repository>(); Repository replacement = original; - replacement.setUsername(dlg.user()); - replacement.setPassword(dlg.password()); + replacement.setUsername(username); + replacement.setPassword(password); Settings &s = m_core->settings(); QSet<Repository> temporaries = s.temporaryRepositories(); @@ -580,8 +593,9 @@ void MetadataJob::xmlTaskFinished() // No new metadata packages to fetch, still need to update the cache // for refreshed repositories. startUpdateCacheTask(); - } + } } else if (status == XmlDownloadRetry) { + reset(); QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection); } else { reset(); |