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;
}
}
- // 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)
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
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;