aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/git/gerrit/authenticationdialog.cpp
blob: a12c62f2b08d39f7ef678480e981c928546ca5f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (C) 2017 Orgad Shaneh <orgads@gmail.com>.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "authenticationdialog.h"

#include "gerritserver.h"

#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/layoutbuilder.h>

#include <QClipboard>
#include <QDialogButtonBox>
#include <QDir>
#include <QFile>
#include <QGuiApplication>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRegularExpression>
#include <QTextStream>
#include <QTimer>

using namespace Utils;

namespace Gerrit {
namespace Internal {

static QRegularExpressionMatch entryMatch(const QString &line, const QString &type)
{
    const QRegularExpression regexp("(?:^|\\s)" + type + "\\s(\\S+)");
    return regexp.match(line);
}

static QString findEntry(const QString &line, const QString &type)
{
    const QRegularExpressionMatch match = entryMatch(line, type);
    if (match.hasMatch())
        return match.captured(1);
    return QString();
}

static bool replaceEntry(QString &line, const QString &type, const QString &value)
{
    const QRegularExpressionMatch match = entryMatch(line, type);
    if (!match.hasMatch())
        return false;
    line.replace(match.capturedStart(1), match.capturedLength(1), value);
    return true;
}

AuthenticationDialog::AuthenticationDialog(GerritServer *server)
        : m_server(server)
{
    setWindowTitle(tr("Authentication"));
    resize(400, 334);

    // FIXME: Take html out of this translatable string.
    const QString desc = tr(
        "<html><head/><body><p>Gerrit server with HTTP was detected, but you need "
        "to set up credentials for it.</p><p>To get your password, "
        "<a href=\"LINK_PLACEHOLDER\"><span style=\" text-decoration: "
        "underline; color:#007af4;\">click here</span></a> (sign in if needed). "
        "Click Generate Password if the password is blank, and copy the user name "
        "and password to this form.</p><p>Choose Anonymous if you do not want "
        "authentication for this server. In this case, changes that require "
        "authentication (like draft changes or private projects) will not be displayed."
        "</p></body></html>")
        .replace("LINK_PLACEHOLDER", server->url() + "/#/settings/#HTTPCredentials");

    auto descriptionLabel = new QLabel(desc);
    descriptionLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
    descriptionLabel->setTextFormat(Qt::RichText);
    descriptionLabel->setWordWrap(true);
    descriptionLabel->setOpenExternalLinks(true);

    m_userLineEdit = new QLineEdit(server->user.userName);

    m_passwordLineEdit = new QLineEdit;

    auto serverLineEdit = new QLineEdit(server->host);
    serverLineEdit->setEnabled(false);

    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

    m_netrcFileName = QDir::homePath() + '/' +
            QLatin1String(Utils::HostOsInfo::isWindowsHost() ? "_netrc" : ".netrc");

    using namespace Layouting;
    Column {
        descriptionLabel,
        Form {
            tr("Server:"), serverLineEdit, br,
            tr("&User:"), m_userLineEdit, br,
            tr("&Password:"), m_passwordLineEdit, br,
        },
        m_buttonBox,
    }.attachTo(this);

    readExistingConf();

    QPushButton *anonymous = m_buttonBox->addButton(tr("Anonymous"), QDialogButtonBox::AcceptRole);
    connect(m_buttonBox, &QDialogButtonBox::clicked,
            this, [this, anonymous](QAbstractButton *button) {
        if (button == anonymous)
            m_authenticated = false;
    });
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    connect(m_passwordLineEdit, &QLineEdit::editingFinished,
            this, &AuthenticationDialog::checkCredentials);
    m_checkTimer = new QTimer(this);
    m_checkTimer->setSingleShot(true);
    connect(m_checkTimer, &QTimer::timeout, this, &AuthenticationDialog::checkCredentials);
    connect(m_passwordLineEdit, &QLineEdit::textChanged, [this]() {
        if (QGuiApplication::clipboard()->text() == m_passwordLineEdit->text()) {
            checkCredentials();
            return;
        }

        m_checkTimer->start(2000);
    });
    if (!m_userLineEdit->text().isEmpty())
        m_passwordLineEdit->setFocus();

    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

AuthenticationDialog::~AuthenticationDialog() = default;

void AuthenticationDialog::readExistingConf()
{
    QFile netrcFile(m_netrcFileName);
    if (!netrcFile.open(QFile::ReadOnly | QFile::Text))
        return;

    QTextStream stream(&netrcFile);
    QString line;
    while (stream.readLineInto(&line)) {
        m_allMachines << line;
        const QString machine = findEntry(line, "machine");
        if (machine == m_server->host) {
            const QString login = findEntry(line, "login");
            const QString password = findEntry(line, "password");
            if (!login.isEmpty())
                m_userLineEdit->setText(login);
            if (!password.isEmpty())
                m_passwordLineEdit->setText(password);
        }
    }
    netrcFile.close();
}

bool AuthenticationDialog::setupCredentials()
{
    QString netrcContents;
    QTextStream out(&netrcContents);
    bool found = false;
    const QString user = m_userLineEdit->text().trimmed();
    const QString password = m_passwordLineEdit->text().trimmed();

    if (user.isEmpty() || password.isEmpty())
        return false;

    m_server->user.userName = user;
    for (QString &line : m_allMachines) {
        const QString machine = findEntry(line, "machine");
        if (machine == m_server->host) {
            found = true;
            replaceEntry(line, "login", user);
            replaceEntry(line, "password", password);
        }
        out << line << '\n';
    }
    if (!found)
        out << "machine " << m_server->host << " login " << user << " password " << password << '\n';
    Utils::FileSaver saver(Utils::FilePath::fromString(m_netrcFileName),
                           QFile::WriteOnly | QFile::Truncate | QFile::Text);
    saver.write(netrcContents.toUtf8());
    return saver.finalize();
}

void AuthenticationDialog::checkCredentials()
{
    int result = 400;
    if (setupCredentials())
        result = m_server->testConnection();
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(result == 200);
}

} // Internal
} // Gerrit