1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-02-24 12:14:57 +00:00

Add initial ssh support in git cpp plugin

This commit is contained in:
Quentin Rouland 2025-02-18 14:18:34 +01:00
parent ec4ebca950
commit 6306af6dde
4 changed files with 50 additions and 24 deletions

View File

@ -30,10 +30,9 @@ bool Git::clone(QString url, QString path, cred_type mode)
return false; return false;
} }
auto v = overload { auto v = overload {
[](const HTTP & x) { return "HTTP"; }, [](const HTTP & x) { UNUSED(x); return "HTTP"; },
[](const HTTPUserPass & x) { return "HTTPAuth"; }, [](const HTTPUserPass & x) { UNUSED(x); return "HTTPAuth"; },
[](const SSHPass & x) { return "SSHAuth"; }, [](const SSHKey & x) { UNUSED(x); return "SSHKey"; },
[](const SSHKey & x) { return "SSHKey"; },
}; };
qDebug() << "Creating clone Job " << url << " " << path << " " << std::visit(v, mode); qDebug() << "Creating clone Job " << url << " " << path << " " << std::visit(v, mode);
CloneJob *clone_job = new CloneJob(url, path, mode); CloneJob *clone_job = new CloneJob(url, path, mode);

View File

@ -18,36 +18,57 @@ GitJob::~GitJob()
git_libgit2_shutdown(); 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, int GitJob::credentialsCB(git_cred **out, const char *url, const char *username_from_url,
unsigned int allowed_types, void *payload) unsigned int allowed_types, void *payload)
{ {
UNUSED(url);
cred_type *cred = (cred_type *)payload; cred_type *cred = (cred_type *)payload;
auto v = overload {
[](const HTTP & x)
{
qDebug() << "[GitJob] credentialsCB : HTTP ";
qWarning() << "[GitJob] credentialsCB : callback should never be call for HTTP ";
return (int) GIT_EUSER;
},
[&out, &username_from_url](const HTTPUserPass & x)
{
qDebug() << "[GitJob] credentialsCB : HTTPUserPass ";
if (!username_from_url) { if (!username_from_url) {
qWarning() << "[GitJob] credentials_cb : no username provided "; qWarning() << "[GitJob] credentials_cb : no username provided ";
return (int) GIT_EUSER; return (int) GIT_EUSER;
} }
auto v = overload {
[](const HTTP & x)
{
UNUSED(x);
qDebug() << "[GitJob] credentialsCB : None ";
qWarning() << "[GitJob] credentialsCB : callback should never be call for None ";
return (int) GIT_EUSER;
},
[allowed_types, &out, &username_from_url](const HTTPUserPass & x)
{
qDebug() << "[GitJob] credentialsCB : HTTPUserPass ";
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()); 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 "; 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 (int) GIT_EUSER;
}, // TODO }
[](const SSHKey & x) return git_cred_ssh_key_new(out, username_from_url, x.pub_key.toLocal8Bit().constData(),
{ x.priv_key.toLocal8Bit().constData(), x.passphrase.toLocal8Bit().constData());
qWarning() << "[GitJob] credentials_cb : SSHKey to be implemented "; }
return (int) GIT_EUSER; };
} // TODO auto error = std::visit(v, *cred);
}; return error;
return std::visit(v, *cred);
} }

View File

@ -12,8 +12,13 @@ struct HTTP { };
struct HTTPUserPass { struct HTTPUserPass {
QString pass; ///< Password for HTTP user authentication. 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. * @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: * This type is used to store one of the following credential types:
* - HTTP * - HTTP
* - HTTPUserPass * - HTTPUserPass
* - SSHPass
* - SSHKey * - SSHKey
*/ */
typedef std::variant<HTTP, HTTPUserPass, SSHPass, SSHKey> cred_type; typedef std::variant<HTTP, HTTPUserPass, SSHKey> cred_type;
/** /**
* @class GitJob * @class GitJob

View File

@ -1,6 +1,8 @@
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
#define UNUSED(x) (void)(x)
/** /**
* @brief A utility structure for enabling function overloading with template-based classes. * @brief A utility structure for enabling function overloading with template-based classes.
* see : https://stackoverflow.com/a/64018031 * see : https://stackoverflow.com/a/64018031