diff --git a/plugins/Git/git.cpp b/plugins/Git/git.cpp index 8ce3ec9..ce29a1e 100644 --- a/plugins/Git/git.cpp +++ b/plugins/Git/git.cpp @@ -30,10 +30,9 @@ bool Git::clone(QString url, QString path, cred_type mode) return false; } auto v = overload { - [](const HTTP & x) { return "HTTP"; }, - [](const HTTPUserPass & x) { return "HTTPAuth"; }, - [](const SSHPass & x) { return "SSHAuth"; }, - [](const SSHKey & x) { return "SSHKey"; }, + [](const HTTP & x) { UNUSED(x); return "HTTP"; }, + [](const HTTPUserPass & x) { UNUSED(x); return "HTTPAuth"; }, + [](const SSHKey & x) { UNUSED(x); return "SSHKey"; }, }; qDebug() << "Creating clone Job " << url << " " << path << " " << std::visit(v, mode); CloneJob *clone_job = new CloneJob(url, path, mode); diff --git a/plugins/Git/jobs/gitjob.cpp b/plugins/Git/jobs/gitjob.cpp index 53536e8..3c6f809 100644 --- a/plugins/Git/jobs/gitjob.cpp +++ b/plugins/Git/jobs/gitjob.cpp @@ -18,36 +18,57 @@ GitJob::~GitJob() git_libgit2_shutdown(); } +bool GitJob::getUsername(char **username, QString maybe_username, const char *username_from_url) +{ + if (username_from_url) { + *username = strdup(username_from_url); + return true; + } else if (!maybe_username.isNull()) { + *username = maybe_username.toLocal8Bit().data(); + return true; + } + return false; +} + int GitJob::credentialsCB(git_cred **out, const char *url, const char *username_from_url, unsigned int allowed_types, void *payload) { + UNUSED(url); cred_type *cred = (cred_type *)payload; + + if (!username_from_url) { + qWarning() << "[GitJob] credentials_cb : no username provided "; + return (int) GIT_EUSER; + } + auto v = overload { [](const HTTP & x) { - qDebug() << "[GitJob] credentialsCB : HTTP "; - qWarning() << "[GitJob] credentialsCB : callback should never be call for HTTP "; + UNUSED(x); + qDebug() << "[GitJob] credentialsCB : None "; + qWarning() << "[GitJob] credentialsCB : callback should never be call for None "; return (int) GIT_EUSER; }, - [&out, &username_from_url](const HTTPUserPass & x) + [allowed_types, &out, &username_from_url](const HTTPUserPass & x) { qDebug() << "[GitJob] credentialsCB : HTTPUserPass "; - if (!username_from_url) { - qWarning() << "[GitJob] credentials_cb : no username provided "; + if (!(allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT)) { + qWarning() << "[GitJob] credentials_cb : allowed_types is invalid for HTTPUserPass "; return (int) GIT_EUSER; } return git_cred_userpass_plaintext_new(out, username_from_url, x.pass.toLocal8Bit().constData()); }, - [](const SSHPass & x) + [allowed_types, &out, &username_from_url](const SSHKey & x) { - qWarning() << "[GitJob] credentials_cb : SSHAuth to be implemented "; - return (int) GIT_EUSER; - }, // TODO - [](const SSHKey & x) - { - qWarning() << "[GitJob] credentials_cb : SSHKey to be implemented "; - return (int) GIT_EUSER; - } // TODO + qDebug() << "[GitJob] credentialsCB : SSHKey "; + if (!(allowed_types & GIT_CREDTYPE_SSH_KEY)) { + qWarning() << "[GitJob] credentials_cb : allowed_types is invalid for HTTPUserPass "; + return (int) GIT_EUSER; + } + return git_cred_ssh_key_new(out, username_from_url, x.pub_key.toLocal8Bit().constData(), + x.priv_key.toLocal8Bit().constData(), x.passphrase.toLocal8Bit().constData()); + } }; - return std::visit(v, *cred); + auto error = std::visit(v, *cred); + return error; } diff --git a/plugins/Git/jobs/gitjob.h b/plugins/Git/jobs/gitjob.h index 8f0fe10..a56f74e 100644 --- a/plugins/Git/jobs/gitjob.h +++ b/plugins/Git/jobs/gitjob.h @@ -8,12 +8,17 @@ extern "C" { #include // Forward declarations for the different credential types. -struct HTTP { }; +struct HTTP {}; struct HTTPUserPass { QString pass; ///< Password for HTTP user authentication. }; -struct SSHPass { }; -struct SSHKey { }; +struct SSHKey { + QString pub_key; ///< public key path. + QString priv_key; ///< private key path. + QString passphrase; ///< key passphrase. +}; + + /** * @brief Variant type to represent various types of credentials. @@ -21,10 +26,9 @@ struct SSHKey { }; * This type is used to store one of the following credential types: * - HTTP * - HTTPUserPass - * - SSHPass * - SSHKey */ -typedef std::variant cred_type; +typedef std::variant cred_type; /** * @class GitJob diff --git a/plugins/Git/utils.h b/plugins/Git/utils.h index 4e7f9cb..77b3b38 100644 --- a/plugins/Git/utils.h +++ b/plugins/Git/utils.h @@ -1,6 +1,8 @@ #ifndef UTILS_H #define UTILS_H +#define UNUSED(x) (void)(x) + /** * @brief A utility structure for enabling function overloading with template-based classes. * see : https://stackoverflow.com/a/64018031