Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/MountPrompt.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtCore

// GVfs's password request while connecting to another machine whose keys
// aren't set up, or its question about a server it can't vouch for (a key
Expand All @@ -10,6 +12,12 @@ FolioDialog {

readonly property var mounter: backend.mounter
readonly property bool question: mounter.choices.length > 0
property bool remember: false

Settings {
category: "connect"
property alias rememberPassword: prompt.remember
}

title: question ? qsTr("Check the server") : qsTr("Sign in")
cardWidth: 600
Expand All @@ -27,12 +35,16 @@ FolioDialog {
forceActiveFocus()
} else if (visible) {
user.text = mounter.user
domain.text = mounter.domain
rememberBox.checked = prompt.remember
password.forceActiveFocus()
}
}

function submit() {
prompt.mounter.providePassword(user.text, password.text)
if (prompt.mounter.canRemember)
prompt.remember = rememberBox.checked
prompt.mounter.providePassword(user.text, password.text, domain.text, rememberBox.checked)
}

// A click outside the card does nothing: GVfs waits for an answer,
Expand All @@ -56,6 +68,13 @@ FolioDialog {
Layout.fillWidth: true
placeholderText: qsTr("User")
}
PlainField {
id: domain
visible: !prompt.question && prompt.mounter.needsDomain
Layout.fillWidth: true
placeholderText: qsTr("Domain or workgroup")
onAccepted: password.forceActiveFocus()
}
PlainField {
id: password
visible: !prompt.question
Expand All @@ -65,6 +84,39 @@ FolioDialog {
onAccepted: prompt.submit()
}

AbstractButton {
id: rememberBox
visible: !prompt.question && prompt.mounter.canRemember
checkable: true
text: qsTr("Remember the password in the keyring")
Accessible.name: text
Keys.onReturnPressed: toggle()
Keys.onEnterPressed: toggle()
contentItem: RowLayout {
spacing: 10
Rectangle {
implicitWidth: 18
implicitHeight: 18
radius: Math.min(3, theme.radius)
color: rememberBox.checked ? theme.accent : "transparent"
border.width: rememberBox.checked ? 0 : 1
border.color: rememberBox.visualFocus ? theme.accent : Qt.alpha(theme.foreground, 0.4)
Glyph {
anchors.centerIn: parent
visible: rememberBox.checked
name: "check"
size: 14
stroke: 2
color: theme.accentForeground
}
}
FolioText {
text: rememberBox.text
color: rememberBox.visualFocus ? theme.brightForeground : theme.foreground
}
}
}

RowLayout {
Layout.alignment: Qt.AlignRight
spacing: 10
Expand All @@ -85,6 +137,11 @@ FolioDialog {
text: qsTr("Cancel")
onClicked: prompt.mounter.cancel()
}
PlainButton {
visible: !prompt.question && prompt.mounter.guestAllowed
text: qsTr("Connect as Guest")
onClicked: prompt.mounter.connectAsGuest()
}
PlainButton {
visible: !prompt.question
text: qsTr("Connect")
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"Compressing needs bsdtar, from the libarchive package.": "Compressing needs bsdtar, from the libarchive package.",
"Computer": "Computer",
"Connect": "Connect",
"Connect as Guest": "Connect as Guest",
"Connecting to %1…": "Connecting to %1…",
"Copied %1, but couldn't remove the original.": "Copied %1, but couldn't remove the original.",
"Copy": "Copy",
Expand Down Expand Up @@ -151,6 +152,7 @@
"one": "Do this for the other %Ln clash",
"other": "Do this for the other %Ln clashes"
},
"Domain or workgroup": "Domain or workgroup",
"Drives can't be changed without udisks2.": "Drives can't be changed without udisks2.",
"Drop to pin": "Drop to pin",
"Duplicate": "Duplicate",
Expand Down Expand Up @@ -318,6 +320,7 @@
},
"Redoing…": "Redoing…",
"Reload": "Reload",
"Remember the password in the keyring": "Remember the password in the keyring",
"Remove from the sidebar": "Remove from the sidebar",
"Rename": "Rename",
"Rename…": "Rename…",
Expand Down
36 changes: 32 additions & 4 deletions src/mounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ struct MounterCallbacks {
return;
}
self->m_prompt = QString::fromUtf8(message).trimmed();
self->m_choices.clear();
for (const char **choice = choices; choice && *choice; ++choice)
self->m_choices << QString::fromUtf8(*choice);
self->setAsking(true);
}

static void password(GMountOperation *op, const char *message, const char *user, const char *,
GAskPasswordFlags, gpointer data) {
static void password(GMountOperation *op, const char *message, const char *user, const char *domain,
GAskPasswordFlags flags, gpointer data) {
g_signal_stop_emission_by_name(op, "ask-password");
auto *self = static_cast<Mounter *>(data);
self->m_prompt = QString::fromUtf8(message).trimmed();
self->m_user = QString::fromUtf8(user ? user : "");
self->m_needsDomain = (flags & G_ASK_PASSWORD_NEED_DOMAIN) != 0;
self->m_domain = QString::fromUtf8(domain ? domain : "");
self->m_guestAllowed = (flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED) != 0;
self->m_canRemember = (flags & G_ASK_PASSWORD_SAVING_SUPPORTED) != 0;
self->m_choices.clear();
self->setAsking(true);
}

Expand Down Expand Up @@ -154,14 +160,32 @@ void Mounter::mount(const QString &uri, bool verified) {
g_object_unref(file);
}

void Mounter::providePassword(const QString &user, const QString &password) {
void Mounter::providePassword(const QString &user, const QString &password,
const QString &domain, bool remember) {
if (!m_operation || !m_asking)
return;
const bool savePermanently = remember && m_canRemember;
// GVfs asks again on the same operation after a failed login, so a
// domain cleared since then is set empty rather than left as it was.
const bool setDomain = m_needsDomain;
setAsking(false);
g_mount_operation_set_anonymous(m_operation, FALSE);
if (!user.isEmpty())
g_mount_operation_set_username(m_operation, user.toUtf8().constData());
if (setDomain)
g_mount_operation_set_domain(m_operation, domain.toUtf8().constData());
g_mount_operation_set_password(m_operation, password.toUtf8().constData());
g_mount_operation_set_password_save(m_operation, G_PASSWORD_SAVE_FOR_SESSION);
g_mount_operation_set_password_save(m_operation,
savePermanently ? G_PASSWORD_SAVE_PERMANENTLY
: G_PASSWORD_SAVE_FOR_SESSION);
g_mount_operation_reply(m_operation, G_MOUNT_OPERATION_HANDLED);
}

void Mounter::connectAsGuest() {
if (!m_operation || !m_asking || !m_guestAllowed)
return;
setAsking(false);
g_mount_operation_set_anonymous(m_operation, TRUE);
g_mount_operation_reply(m_operation, G_MOUNT_OPERATION_HANDLED);
}

Expand All @@ -184,6 +208,10 @@ void Mounter::setAsking(bool asking) {
m_asking = asking;
if (!asking) {
m_prompt.clear();
m_needsDomain = false;
m_domain.clear();
m_guestAllowed = false;
m_canRemember = false;
m_choices.clear();
}
emit askingChanged();
Expand Down
22 changes: 19 additions & 3 deletions src/mounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Mounter : public QObject {
Q_PROPERTY(bool asking READ asking NOTIFY askingChanged)
Q_PROPERTY(QString prompt READ prompt NOTIFY askingChanged)
Q_PROPERTY(QString user READ user NOTIFY askingChanged)
Q_PROPERTY(bool needsDomain READ needsDomain NOTIFY askingChanged)
Q_PROPERTY(QString domain READ domain NOTIFY askingChanged)
Q_PROPERTY(bool guestAllowed READ guestAllowed NOTIFY askingChanged)
Q_PROPERTY(bool canRemember READ canRemember NOTIFY askingChanged)
// A question's answers, in GVfs's words; empty for a password request.
Q_PROPERTY(QStringList choices READ choices NOTIFY askingChanged)

Expand All @@ -30,6 +34,10 @@ class Mounter : public QObject {
bool asking() const { return m_asking; }
QString prompt() const { return m_prompt; }
QString user() const { return m_user; }
bool needsDomain() const { return m_needsDomain; }
QString domain() const { return m_domain; }
bool guestAllowed() const { return m_guestAllowed; }
bool canRemember() const { return m_canRemember; }
QStringList choices() const { return m_choices; }

// Mounts the location; `mounted` carries its local GVfs path. A
Expand All @@ -47,9 +55,12 @@ class Mounter : public QObject {
// Each address mounted this session and the folder GVfs opened for it.
static QHash<QString, QString> &known();

// The user's answer to a password request; remembered for this session
// only, never written to a keyring without asking.
Q_INVOKABLE void providePassword(const QString &user, const QString &password);
// The user's answer to a password request; remembered for this session,
// or in the keyring when `remember` is asked for.
Q_INVOKABLE void providePassword(const QString &user, const QString &password,
const QString &domain = {}, bool remember = false);
// Connects anonymously when the server allows guest access.
Q_INVOKABLE void connectAsGuest();
// The user's answer to a question, by its place among the choices.
Q_INVOKABLE void choose(int choice);
// Closes the password request and abandons the mount.
Expand All @@ -63,6 +74,7 @@ class Mounter : public QObject {
private:
// GIO's C callbacks, in mounter.cpp.
friend struct MounterCallbacks;
friend class FolioTests;
void setAsking(bool asking);
void finish();

Expand All @@ -72,5 +84,9 @@ class Mounter : public QObject {
bool m_asking = false;
QString m_prompt;
QString m_user;
bool m_needsDomain = false;
QString m_domain;
bool m_guestAllowed = false;
bool m_canRemember = false;
QStringList m_choices;
};
31 changes: 31 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
#include "translator.h"
#include "windows.h"

// GIO's headers use `signals` as a struct member, which Qt defines as a
// keyword.
#undef signals
#include <gio/gio.h>
#define signals Q_SIGNALS

// Puts XDG_DATA_HOME, where the trash and recent files live, in `path` for
// as long as the returned guard does.
[[nodiscard]] static auto dataHomeAt(const QString &path)
Expand Down Expand Up @@ -4314,6 +4320,31 @@ private slots:
QVERIFY(!open.busy());
}

void mounterSendsTheDomainAsGiven()
{
// GVfs asks again on the same operation after a failed login; a
// domain cleared in between is sent empty, not as it was before.
QTemporaryDir dir;
Mounter mounter;
mounter.mount(QUrl::fromLocalFile(dir.path()).toString(), false);
GMountOperation *operation = mounter.m_operation;
QVERIFY(operation);
g_object_ref(operation);
const auto release = qScopeGuard([&] { g_object_unref(operation); });
const auto ask = [&] {
g_signal_emit_by_name(operation, "ask-password", "Sign in", "me", "WORK",
GAskPasswordFlags(G_ASK_PASSWORD_NEED_PASSWORD | G_ASK_PASSWORD_NEED_DOMAIN));
};
ask();
QVERIFY(mounter.asking());
QCOMPARE(mounter.domain(), QString("WORK"));
mounter.providePassword("me", "wrong", "WORK");
QCOMPARE(QString::fromUtf8(g_mount_operation_get_domain(operation)), QString("WORK"));
ask();
mounter.providePassword("me", "right", "");
QVERIFY(QString::fromUtf8(g_mount_operation_get_domain(operation)).isEmpty());
}

void trashFollowsXdgDataHome()
{
// g_file_trash puts things in $XDG_DATA_HOME/Trash, wherever that
Expand Down