Skip to content

Commit

Permalink
Support colon in username, use LRO_USERNAME and LRO_PASSWORD
Browse files Browse the repository at this point in the history
Requires librepo version >= 1.18.0
  • Loading branch information
jrohel committed Jun 25, 2024
1 parent 86bbb15 commit 938b9f4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ include_directories(${GLIB_INCLUDE_DIRS})
pkg_check_modules(JSONC REQUIRED json-c)
include_directories(${JSONC_INCLUDE_DIRS})
pkg_check_modules(LIBMODULEMD REQUIRED modulemd-2.0>=2.11.2)
pkg_check_modules(REPO REQUIRED librepo>=1.15.0)
pkg_check_modules(REPO REQUIRED librepo>=1.18.0)
include_directories(${REPO_INCLUDE_DIRS})
link_directories(${REPO_LIBRARY_DIRS})
pkg_check_modules(RPM REQUIRED rpm>=4.15.0)
Expand Down
2 changes: 1 addition & 1 deletion libdnf.spec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%global libsolv_version 0.7.21
%global libmodulemd_version 2.13.0
%global librepo_version 1.15.0
%global librepo_version 1.18.0
%global dnf_conflict 4.11.0
%global swig_version 3.0.12
%global libdnf_major_version 0
Expand Down
27 changes: 10 additions & 17 deletions libdnf/dnf-repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,20 +817,16 @@ dnf_repo_set_metadata_expire(DnfRepo *repo, guint metadata_expire)
/**
* @brief Format user password string
*
* Returns user and password in user:password form. If encode is True,
* special characters in user and password are URL encoded.
* Returns user and password in user:password form.
* Special characters in user and password are URL encoded.
*
* @param user Username
* @param passwd Password
* @param encode If quote is True, special characters in user and password are URL encoded.
* @return User and password in user:password form
*/
static std::string formatUserPassString(const std::string & user, const std::string & passwd, bool encode)
static std::string formatUserPassString(const std::string & user, const std::string & passwd)
{
if (encode)
return libdnf::urlEncode(user) + ":" + libdnf::urlEncode(passwd);
else
return user + ":" + passwd;
return libdnf::urlEncode(user) + ":" + libdnf::urlEncode(passwd);
}

/* Resets repository configuration options previously readed from repository
Expand Down Expand Up @@ -1130,22 +1126,19 @@ dnf_repo_set_keyfile_data(DnfRepo *repo, gboolean reloadFromGKeyFile, GError **e
"repo '%s': 'proxy_username' is set but not 'proxy_password'", repoId);
return FALSE;
}
tmp_str = formatUserPassString(tmp_str, conf->proxy_password().getValue(), true);
tmp_str = formatUserPassString(tmp_str, conf->proxy_password().getValue());
tmp_cstr = tmp_str.c_str();
}
}
if (!lr_handle_setopt(priv->repo_handle, error, LRO_PROXYUSERPWD, tmp_cstr))
return FALSE;

// setup username and password
tmp_cstr = NULL;
tmp_str = conf->username().getValue();
if (!tmp_str.empty()) {
// TODO Use URL encoded form, needs support in librepo
tmp_str = formatUserPassString(tmp_str, conf->password().getValue(), false);
tmp_cstr = tmp_str.c_str();
}
if (!lr_handle_setopt(priv->repo_handle, error, LRO_USERPWD, tmp_cstr))
auto & username = conf->username().getValue();
if (!lr_handle_setopt(priv->repo_handle, error, LRO_USERNAME, username.empty() ? NULL : username.c_str()))
return FALSE;
auto & password = conf->password().getValue();
if (!lr_handle_setopt(priv->repo_handle, error, LRO_PASSWORD, password.empty() ? NULL : password.c_str()))
return FALSE;

auto proxy_sslverify = conf->proxy_sslverify().getValue();
Expand Down
22 changes: 7 additions & 15 deletions libdnf/repo/Repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,16 @@ int Repo::Impl::mirrorFailureCB(void * data, const char * msg, const char * url,
/**
* @brief Format user password string
*
* Returns user and password in user:password form. If quote is True,
* special characters in user and password are URL encoded.
* Returns user and password in user:password form.
* Special characters in user and password are URL encoded.
*
* @param user Username
* @param passwd Password
* @param encode If quote is True, special characters in user and password are URL encoded.
* @return User and password in user:password form
*/
static std::string formatUserPassString(const std::string & user, const std::string & passwd, bool encode)
static std::string formatUserPassString(const std::string & user, const std::string & passwd)
{
if (encode)
return urlEncode(user) + ":" + urlEncode(passwd);
else
return user + ":" + passwd;
return urlEncode(user) + ":" + urlEncode(passwd);
}

Repo::Impl::Impl(Repo & owner, const std::string & id, Type type, std::unique_ptr<ConfigRepo> && conf)
Expand Down Expand Up @@ -524,12 +520,8 @@ static void setHandle(LrHandle * h, ConfigT & config, const char * repoId = null
}

// setup username/password if needed
auto userpwd = config.username().getValue();
if (!userpwd.empty()) {
// TODO Use URL encoded form, needs support in librepo
userpwd = formatUserPassString(userpwd, config.password().getValue(), false);
handleSetOpt(h, LRO_USERPWD, userpwd.c_str());
}
handleSetOpt(h, LRO_USERNAME, config.username().getValue().empty() ? NULL : config.username().getValue().c_str());
handleSetOpt(h, LRO_PASSWORD, config.password().getValue().empty() ? NULL : config.password().getValue().c_str());

if (!config.proxy().empty() && !config.proxy().getValue().empty())
handleSetOpt(h, LRO_PROXY, config.proxy().getValue().c_str());
Expand All @@ -548,7 +540,7 @@ static void setHandle(LrHandle * h, ConfigT & config, const char * repoId = null
else
throw RepoError(_("'proxy_username' is set but not 'proxy_password'"));
}
userpwd = formatUserPassString(userpwd, config.proxy_password().getValue(), true);
userpwd = formatUserPassString(userpwd, config.proxy_password().getValue());
handleSetOpt(h, LRO_PROXYUSERPWD, userpwd.c_str());
}
}
Expand Down

0 comments on commit 938b9f4

Please sign in to comment.