From: Michael Tremer Date: Wed, 25 Jun 2025 12:22:49 +0000 (+0000) Subject: client: Read/write credentials of the principal X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48d8da244bce6d7c4c5dddbc35bd84efab2e4b61;p=pakfire.git client: Read/write credentials of the principal Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/client.c b/src/pakfire/client.c index 96e83893..fda4edba 100644 --- a/src/pakfire/client.c +++ b/src/pakfire/client.c @@ -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)