]> git.ipfire.org Git - pakfire.git/commitdiff
client: Break setting the tokens into helper functions
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 12:40:47 +0000 (12:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 12:40:47 +0000 (12:40 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/client.c

index fda4edba290d4d27d6219184726a2c9d19e0e00b..b5a6d0a681ea2669857ab4d1276d02e73a8043d8 100644 (file)
@@ -112,8 +112,99 @@ struct pakfire_client {
        STAILQ_HEAD(uploads, pakfire_client_upload) uploads;
 };
 
+static int pakfire_client_access_timer(sd_event_source* event, uint64_t usec, void* data) {
+       struct pakfire_client* self = data;
+
+       DEBUG(self->ctx, "Authentication access timer fired\n");
+
+       return 0;
+}
+
+static int pakfire_client_set_access_token(struct pakfire_client* self, const char* token) {
+       time_t expires_at = -1;
+       int r;
+
+       // Store the access token
+       r = pakfire_string_set(self->auth.access_token, token);
+       if (r < 0)
+               return r;
+
+       // Extract the expiry time of the access token
+       expires_at = r = pakfire_jwt_expires_at(self->auth.access_token);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to fetch the expiry time of the access token: %s\n", strerror(-r));
+               return r;
+       }
+
+       // Create the access token timer (unless already done so)
+       if (!self->auth.access_timer) {
+               r = sd_event_add_time_relative(self->loop, &self->auth.access_timer,
+                               CLOCK_MONOTONIC, 0, 0, pakfire_client_access_timer, self);
+               if (r < 0) {
+                       ERROR(self->ctx, "Failed to register the auth access timer: %s\n", strerror(-r));
+                       return r;
+               }
+       }
+
+       // Call the timer just before the access token expires
+       r = sd_event_source_set_time(self->auth.access_timer, S_TO_US(expires_at - 60));
+       if (r < 0)
+               return r;
+
+       // Log action
+       DEBUG(self->ctx, "Set access token: %s\n", self->auth.access_token);
+
+       return 0;
+}
+
+static int pakfire_client_refresh_timer(sd_event_source* event, uint64_t usec, void* data) {
+       struct pakfire_client* self = data;
+
+       DEBUG(self->ctx, "Authentication refresh timer fired\n");
+
+       return 0;
+}
+
+static int pakfire_client_set_refresh_token(struct pakfire_client* self, const char* token) {
+       time_t expires_at = -1;
+       int r;
+
+       // Store the refresh token
+       r = pakfire_string_set(self->auth.refresh_token, token);
+       if (r < 0)
+               return r;
+
+       // Extract the expiry time of the refresh token
+       expires_at = r = pakfire_jwt_expires_at(self->auth.refresh_token);
+       if (r < 0) {
+               ERROR(self->ctx, "Failed to fetch the expiry time of the refresh token: %s\n", strerror(-r));
+               return r;
+       }
+
+       // Create the refresh token timer (unless already done so)
+       if (!self->auth.refresh_timer) {
+               r = sd_event_add_time_relative(self->loop, &self->auth.refresh_timer,
+                               CLOCK_MONOTONIC, 0, 0, pakfire_client_refresh_timer, self);
+               if (r < 0) {
+                       ERROR(self->ctx, "Failed to register the auth refresh timer: %s\n", strerror(-r));
+                       return r;
+               }
+       }
+
+       // Call the timer just before the refresh token expires
+       r = sd_event_source_set_time(self->auth.refresh_timer, S_TO_US(expires_at - 60));
+       if (r < 0)
+               return r;
+
+       // Log action
+       DEBUG(self->ctx, "Set refresh token: %s\n", self->auth.refresh_token);
+
+       return 0;
+}
+
 static int pakfire_client_store_read(struct pakfire_client* self) {
        struct json_object* store = NULL;
+       const char* refresh_token = NULL;
        char path[PATH_MAX];
        char* error = NULL;
        int r;
@@ -141,7 +232,17 @@ static int pakfire_client_store_read(struct pakfire_client* self) {
                }
        }
 
-       // XXX Parse the credentials
+       // Fetch the refresh token
+       r = pakfire_json_get_string(store, "refresh_token", &refresh_token);
+       if (r < 0)
+               goto ERROR;
+
+       // Set the refresh token
+       if (refresh_token) {
+               r = pakfire_client_set_refresh_token(self, refresh_token);
+               if (r < 0)
+                       goto ERROR;
+       }
 
 ERROR:
        if (store)
@@ -488,27 +589,10 @@ int pakfire_client_set_auth_callback(struct pakfire_client* self,
        return 0;
 }
 
-static int pakfire_client_access_timer(sd_event_source* event, uint64_t usec, void* data) {
-       struct pakfire_client* self = data;
-
-       DEBUG(self->ctx, "Authentication access timer fired\n");
-
-       return 0;
-}
-
-static int pakfire_client_refresh_timer(sd_event_source* event, uint64_t usec, void* data) {
-       struct pakfire_client* self = data;
-
-       DEBUG(self->ctx, "Authentication refresh timer fired\n");
-
-       return 0;
-}
-
 static int pakfire_client_auth_successful(
                struct pakfire_client* self, struct json_object* response) {
        const char* refresh_token = NULL;
        const char* access_token = NULL;
-       time_t expires_at = -1;
        int r;
 
        // Log action
@@ -521,64 +605,20 @@ static int pakfire_client_auth_successful(
                return r;
        }
 
-       // Fetch the refresh token
-       r = pakfire_json_get_string(response, "refresh_token", &refresh_token);
-       if (r < 0) {
-               ERROR(self->ctx, "Failed to fetch the refresh token: %s\n", strerror(-r));
-               return r;
-       }
-
-       // Store the access token
-       r = pakfire_string_set(self->auth.access_token, access_token);
-       if (r < 0)
-               return r;
-
-       // Store the refresh token
-       r = pakfire_string_set(self->auth.refresh_token, refresh_token);
-       if (r < 0)
-               return r;
-
-       // Extract the expiry time of the access token
-       expires_at = r = pakfire_jwt_expires_at(self->auth.access_token);
-       if (r < 0) {
-               ERROR(self->ctx, "Failed to fetch the expiry time of the access token: %s\n", strerror(-r));
-               return r;
-       }
-
-       // Create the access token timer (unless already done so)
-       if (!self->auth.access_timer) {
-               r = sd_event_add_time_relative(self->loop, &self->auth.access_timer,
-                               CLOCK_MONOTONIC, 0, 0, pakfire_client_access_timer, self);
-               if (r < 0) {
-                       ERROR(self->ctx, "Failed to register the auth access timer: %s\n", strerror(-r));
-                       return r;
-               }
-       }
-
-       // Call the timer just before the access token expires
-       r = sd_event_source_set_time(self->auth.access_timer, S_TO_US(expires_at - 60));
+       // Set the access token
+       r = pakfire_client_set_access_token(self, access_token);
        if (r < 0)
                return r;
 
-       // Extract the expiry time of the refresh token
-       expires_at = r = pakfire_jwt_expires_at(self->auth.refresh_token);
+       // Fetch the refresh token
+       r = pakfire_json_get_string(response, "refresh_token", &refresh_token);
        if (r < 0) {
-               ERROR(self->ctx, "Failed to fetch the expiry time of the refresh token: %s\n", strerror(-r));
+               ERROR(self->ctx, "Failed to fetch the refresh token: %s\n", strerror(-r));
                return r;
        }
 
-       // Create the refresh token timer (unless already done so)
-       if (!self->auth.refresh_timer) {
-               r = sd_event_add_time_relative(self->loop, &self->auth.refresh_timer,
-                               CLOCK_MONOTONIC, 0, 0, pakfire_client_refresh_timer, self);
-               if (r < 0) {
-                       ERROR(self->ctx, "Failed to register the auth refresh timer: %s\n", strerror(-r));
-                       return r;
-               }
-       }
-
-       // Call the timer just before the refresh token expires
-       r = sd_event_source_set_time(self->auth.refresh_timer, S_TO_US(expires_at - 60));
+       // Set the refresh token
+       r = pakfire_client_set_refresh_token(self, refresh_token);
        if (r < 0)
                return r;