nopenpilot/selfdrive/ui/qt/widgets/ssh_keys.cc

111 lines
3.1 KiB
C++

#include <QNetworkReply>
#include <QHBoxLayout>
#include "widgets/input.hpp"
#include "widgets/ssh_keys.hpp"
#include "common/params.h"
SshControl::SshControl() : AbstractControl("SSH Keys", "Warning: This grants SSH access to all public keys in your GitHub settings. Never enter a GitHub username other than your own. A comma employee will NEVER ask you to add their GitHub username.", "") {
// setup widget
hlayout->addStretch(1);
username_label.setAlignment(Qt::AlignVCenter);
username_label.setStyleSheet("color: #aaaaaa");
hlayout->addWidget(&username_label);
btn.setStyleSheet(R"(
padding: 0;
border-radius: 50px;
font-size: 35px;
font-weight: 500;
color: #E4E4E4;
background-color: #393939;
)");
btn.setFixedSize(250, 100);
hlayout->addWidget(&btn);
QObject::connect(&btn, &QPushButton::released, [=]() {
if (btn.text() == "ADD") {
username = InputDialog::getText("Enter your GitHub username");
if (username.length() > 0) {
btn.setText("LOADING");
btn.setEnabled(false);
getUserKeys(username);
}
} else {
Params().remove("GithubUsername");
Params().remove("GithubSshKeys");
refresh();
}
});
// setup networking
manager = new QNetworkAccessManager(this);
networkTimer = new QTimer(this);
networkTimer->setSingleShot(true);
networkTimer->setInterval(5000);
connect(networkTimer, SIGNAL(timeout()), this, SLOT(timeout()));
refresh();
}
void SshControl::refresh() {
QString param = QString::fromStdString(Params().get("GithubSshKeys"));
if (param.length()) {
username_label.setText(QString::fromStdString(Params().get("GithubUsername")));
btn.setText("REMOVE");
} else {
username_label.setText("");
btn.setText("ADD");
}
btn.setEnabled(true);
}
void SshControl::getUserKeys(QString username){
QString url = "https://github.com/" + username + ".keys";
QNetworkRequest request;
request.setUrl(QUrl(url));
#ifdef QCOM
QSslConfiguration ssl = QSslConfiguration::defaultConfiguration();
ssl.setCaCertificates(QSslCertificate::fromPath("/usr/etc/tls/cert.pem",
QSsl::Pem, QRegExp::Wildcard));
request.setSslConfiguration(ssl);
#endif
reply = manager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(parseResponse()));
networkTimer->start();
}
void SshControl::timeout(){
reply->abort();
}
void SshControl::parseResponse(){
QString err = "";
if (reply->error() != QNetworkReply::OperationCanceledError) {
networkTimer->stop();
QString response = reply->readAll();
if (reply->error() == QNetworkReply::NoError && response.length()) {
Params().put("GithubUsername", username.toStdString());
Params().put("GithubSshKeys", response.toStdString());
} else if(reply->error() == QNetworkReply::NoError){
err = "Username '" + username + "' has no keys on GitHub";
} else {
err = "Username '" + username + "' doesn't exist on GitHub";
}
} else {
err = "Request timed out";
}
if (err.length()) {
ConfirmationDialog::alert(err);
}
refresh();
reply->deleteLater();
reply = nullptr;
}