From: Michael Tremer Date: Wed, 25 Jun 2025 10:37:17 +0000 (+0000) Subject: cli: Refactor storing authentication credentials X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4748b56473ff51587ac109035be348133ff07e25;p=pakfire.git cli: Refactor storing authentication credentials The config object will be removed once we have finished initializing the other objects. Therefore we will have to copy the credentials somewhere else before. Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/pakfire.c b/src/cli/lib/pakfire.c index 74f67c61..737a07cc 100644 --- a/src/cli/lib/pakfire.c +++ b/src/cli/lib/pakfire.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "config.h" #include "pakfire.h" @@ -140,27 +141,23 @@ ERROR: return r; } -static int auth_callback(struct pakfire_client* client, void* data) { - struct pakfire_config* config = data; - const char* username = NULL; - const char* password = NULL; - - // Fetch the credentials from the configuration - username = pakfire_config_get(config, "client", "username", NULL); - password = pakfire_config_get(config, "client", "password", NULL); +struct auth_credentials { + char username[NAME_MAX]; + char password[NAME_MAX]; +}; - // Fail if we have no credentials - if (!username || !password) { - fprintf(stderr, "Client credentials are missing\n"); - return -EINVAL; - } +static int auth_callback(struct pakfire_client* client, void* data) { + const struct auth_credentials* creds = data; // Authenticate! - return pakfire_client_auth_user(client, username, password); + return pakfire_client_auth_user(client, creds->username, creds->password); } int cli_setup_client(struct pakfire_client** client, struct cli_global_args* args) { + static struct auth_credentials creds = {}; struct pakfire_config* config = NULL; + const char* username = NULL; + const char* password = NULL; const char* url = NULL; int r; @@ -177,6 +174,27 @@ int cli_setup_client(struct pakfire_client** client, struct cli_global_args* arg // Fetch the URL url = pakfire_config_get(config, "client", "url", "https://pakfire.ipfire.org/"); + // Fetch the credentials from the configuration + username = pakfire_config_get(config, "client", "username", NULL); + password = pakfire_config_get(config, "client", "password", NULL); + + // Fail if we have no credentials + if (!username || !password) { + fprintf(stderr, "Client credentials are missing\n"); + r = -EINVAL; + goto ERROR; + } + + // Copy the username + r = pakfire_string_set(creds.username, username); + if (r < 0) + goto ERROR; + + // Copy the password + r = pakfire_string_set(creds.password, password); + if (r < 0) + goto ERROR; + // Connect to the build service r = pakfire_client_create(client, args->ctx, NULL, url); if (r < 0) { @@ -185,7 +203,7 @@ int cli_setup_client(struct pakfire_client** client, struct cli_global_args* arg } // Set authentication callback - pakfire_client_set_auth_callback(*client, auth_callback, config); + pakfire_client_set_auth_callback(*client, auth_callback, &creds); ERROR: if (config)