From: Michael Tremer Date: Wed, 25 Jun 2025 13:01:37 +0000 (+0000) Subject: client: Move authentication refresh function to the top X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=de10b13c7689670a75e0e013ed2468762e58a88e;p=pakfire.git client: Move authentication refresh function to the top Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/client.c b/src/pakfire/client.c index b5a6d0a6..2c3ba19f 100644 --- a/src/pakfire/client.c +++ b/src/pakfire/client.c @@ -112,6 +112,91 @@ struct pakfire_client { STAILQ_HEAD(uploads, pakfire_client_upload) uploads; }; +static int pakfire_client_xfer_create(struct pakfire_xfer** xfer, + struct pakfire_client* self, const char* url, ...) __attribute__((format(printf, 3, 4))); + +static int pakfire_client_xfer_create(struct pakfire_xfer** xfer, + struct pakfire_client* self, const char* url, ...) { + struct pakfire_xfer* x = NULL; + va_list args; + int r; + + // Create a new xfer + va_start(args, url); + r = pakfire_xfer_create(&x, self->ctx, url, args); + va_end(args); + if (r < 0) + goto ERROR; + + // Set the base URL + r = pakfire_xfer_set_baseurl(x, self->url); + if (r < 0) + goto ERROR; + + // Tell the API that we want to receive a JSON response + r = pakfire_xfer_add_header(x, "Accept: application/json"); + if (r < 0) + goto ERROR; + + // Success + *xfer = pakfire_xfer_ref(x); + +ERROR: + if (x) + pakfire_xfer_unref(x); + + return r; +} + +static int pakfire_client_auth_response(struct pakfire_xfer* xfer, + pakfire_xfer_error_code_t code, json_object* response, void* data); + +/* + Triggers a refresh of the access and refresh tokens +*/ +static int pakfire_client_auth_refresh(struct pakfire_client* self) { + struct pakfire_xfer* xfer = NULL; + struct json_object* request = NULL; + int r; + + // Cannot do this if we don't have a refresh token + if (!*self->auth.refresh_token) + return -ENOTSUP; + + // Create a new transfer + r = pakfire_client_xfer_create(&xfer, self, "/api/v1/auth/refresh"); + if (r < 0) + goto ERROR; + + // Create a new JSON request object + r = pakfire_json_new_object(&request); + if (r < 0) + goto ERROR; + + // Add the refresh token + r = pakfire_json_add_string(request, "refresh_token", self->auth.refresh_token); + if (r < 0) + goto ERROR; + + // Set the callback + r = pakfire_xfer_set_response_callback(xfer, pakfire_client_auth_response, self); + if (r < 0) + goto ERROR; + + // Send the request + r = pakfire_httpclient_enqueue(self->httpclient, xfer); + if (r < 0) + goto ERROR; + +ERROR: + if (xfer) + pakfire_xfer_unref(xfer); + if (request) + json_object_put(request); + + return r; +} + static int pakfire_client_access_timer(sd_event_source* event, uint64_t usec, void* data) { struct pakfire_client* self = data; @@ -290,96 +375,6 @@ ERROR: return r; } -static int pakfire_client_xfer_create(struct pakfire_xfer** xfer, - struct pakfire_client* self, const char* url, ...) __attribute__((format(printf, 3, 4))); - -static int pakfire_client_xfer_create(struct pakfire_xfer** xfer, - struct pakfire_client* self, const char* url, ...) { - struct pakfire_xfer* x = NULL; - va_list args; - int r; - - // Create a new xfer - va_start(args, url); - r = pakfire_xfer_create(&x, self->ctx, url, args); - va_end(args); - if (r < 0) - goto ERROR; - - // Set the base URL - r = pakfire_xfer_set_baseurl(x, self->url); - if (r < 0) - goto ERROR; - - // Tell the API that we want to receive a JSON response - r = pakfire_xfer_add_header(x, "Accept: application/json"); - if (r < 0) - goto ERROR; - - // Success - *xfer = pakfire_xfer_ref(x); - -ERROR: - if (x) - pakfire_xfer_unref(x); - - return r; -} - -static int pakfire_client_auth_refresh(struct pakfire_client* self) { - struct pakfire_xfer* xfer = NULL; - struct json_object* request = NULL; - struct json_object* response = NULL; - const char* access_token = NULL; - int r; - - // Cannot do this if we don't have a refresh token - if (!*self->auth.refresh_token) - return -ENOTSUP; - - // Create a new transfer - r = pakfire_client_xfer_create(&xfer, self, "/api/v1/auth/refresh"); - if (r < 0) - goto ERROR; - - // Create a new JSON request object - r = pakfire_json_new_object(&request); - if (r < 0) - goto ERROR; - - // Add the refresh token - r = pakfire_json_add_string(request, "refresh_token", self->auth.refresh_token); - if (r < 0) - goto ERROR; - - // Send the request - r = pakfire_xfer_run_api_request(xfer, request, &response); - if (r < 0) - goto ERROR; - - // Extract the new access token - r = pakfire_json_get_string(response, "access_token", &access_token); - if (r < 0) { - ERROR(self->ctx, "Failed to fetch the access token: %s\n", strerror(-r)); - goto ERROR; - } - - // Store the new access token - r = pakfire_string_set(self->auth.access_token, access_token); - if (r < 0) - goto ERROR; - -ERROR: - if (xfer) - pakfire_xfer_unref(xfer); - if (request) - json_object_put(request); - if (response) - json_object_put(response); - - return r; -} - static int pakfire_client_xfer_auth(struct pakfire_client* self, struct pakfire_xfer* xfer) { int r;