]> git.ipfire.org Git - pakfire.git/commitdiff
client: Read/write credentials of the principal
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 12:22:49 +0000 (12:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 12:22:49 +0000 (12:22 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/client.c

index 96e838933a9707d24f3afaffc2c2fbcb82a10c17..fda4edba290d4d27d6219184726a2c9d19e0e00b 100644 (file)
@@ -112,6 +112,83 @@ struct pakfire_client {
        STAILQ_HEAD(uploads, pakfire_client_upload) uploads;
 };
 
+static int pakfire_client_store_read(struct pakfire_client* self) {
+       struct json_object* store = NULL;
+       char path[PATH_MAX];
+       char* error = NULL;
+       int r;
+
+       // Make the path
+       r = pakfire_path_format(path, "%s/pakfire-store.%s", PAKFIRE_TMP_DIR, self->principal);
+       if (r < 0)
+               goto ERROR;
+
+       // Read the store
+       r = pakfire_json_parse_from_file(&store, &error, path);
+       if (r < 0) {
+               switch (-r) {
+                       // Skip if don't have any data to read
+                       case ENOENT:
+                               r = 0;
+                               goto ERROR;
+
+                       default:
+                               if (error)
+                                       ERROR(self->ctx, "Failed to parse store: %s\n", error);
+                               else
+                                       ERROR(self->ctx, "Failed to read store: %s\n", strerror(-r));
+                               goto ERROR;
+               }
+       }
+
+       // XXX Parse the credentials
+
+ERROR:
+       if (store)
+               json_object_put(store);
+       if (error)
+               free(error);
+
+       return r;
+}
+
+static int pakfire_client_store_write(struct pakfire_client* self) {
+       struct json_object* store = NULL;
+       char path[PATH_MAX];
+       int r;
+
+       // Make the path
+       r = pakfire_path_format(path, "%s/pakfire-store.%s", PAKFIRE_TMP_DIR, self->principal);
+       if (r < 0)
+               goto ERROR;
+
+       // Create a new store object
+       r = pakfire_json_new_object(&store);
+       if (r < 0)
+               goto ERROR;
+
+       // Add the refresh token
+       r = pakfire_json_add_string(store, "refresh_token", self->auth.refresh_token);
+       if (r < 0)
+               goto ERROR;
+
+       // Store the data
+       r = pakfire_json_to_file(path, 0, 0, 0600, store);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to write store at %s: %s\n", path, strerror(-r));
+               goto ERROR;
+       }
+
+       // Log action
+       DEBUG(self->ctx, "Stored credentials in %s\n", path);
+
+ERROR:
+       if (store)
+               json_object_put(store);
+
+       return r;
+}
+
 static int pakfire_client_xfer_create(struct pakfire_xfer** xfer,
        struct pakfire_client* self, const char* url, ...) __attribute__((format(printf, 3, 4)));
 
@@ -239,6 +316,11 @@ static int pakfire_client_init(sd_event_source* event, void* data) {
 
        DEBUG(self->ctx, "Initializing client...\n");
 
+       // Read the store
+       r = pakfire_client_store_read(self);
+       if (r < 0)
+               return r;
+
        // Request authentication
        r = pakfire_client_auth_required(self);
        if (r < 0)
@@ -252,6 +334,9 @@ static void pakfire_client_upload_free(struct pakfire_client_upload* upload);
 static void pakfire_client_free(struct pakfire_client* self) {
        struct pakfire_client_upload* upload = NULL;
 
+       // Store any credentials
+       pakfire_client_store_write(self);
+
        if (self->auth.access_timer)
                sd_event_source_unref(self->auth.access_timer);
        if (self->auth.refresh_timer)