]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Refactor storing authentication credentials
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 10:37:17 +0000 (10:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 10:37:17 +0000 (10:37 +0000)
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 <michael.tremer@ipfire.org>
src/cli/lib/pakfire.c

index 74f67c61b8c7c146b13bfb7e066d379293df82ab..737a07cc86585eb47598b6a0f3b8febed19fdc9c 100644 (file)
@@ -26,6 +26,7 @@
 #include <pakfire/config.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 
 #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)