From: Michael Tremer Date: Sat, 21 Jun 2025 16:21:51 +0000 (+0000) Subject: JWT: Make the functions context-free X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=40295e7388ca4ee96b35999923482e2864b37693;p=pakfire.git JWT: Make the functions context-free Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/buildservice.c b/src/pakfire/buildservice.c index 2867e56d..9ce03081 100644 --- a/src/pakfire/buildservice.c +++ b/src/pakfire/buildservice.c @@ -107,9 +107,11 @@ static int pakfire_buildservice_set_access_token(struct pakfire_buildservice* se return r; // Fetch the expiry time - self->access_token_expires_at = pakfire_jwt_expires_at(self->ctx, self->access_token); - if (self->access_token_expires_at < 0) + self->access_token_expires_at = pakfire_jwt_expires_at(self->access_token); + if (self->access_token_expires_at < 0) { + ERROR(self->ctx, "Failed to fetch the JWT expiry time: %s\n", strerror(-r)); return self->access_token_expires_at; + } // Format the expiry time r = pakfire_strftime(expires_at, "%Y-%m-%dT%H:%M:%SZ", self->access_token_expires_at); @@ -133,9 +135,11 @@ static int pakfire_buildservice_set_refresh_token(struct pakfire_buildservice* s return r; // Fetch the expiry time - self->refresh_token_expires_at = pakfire_jwt_expires_at(self->ctx, self->refresh_token); - if (self->refresh_token_expires_at < 0) + self->refresh_token_expires_at = pakfire_jwt_expires_at(self->refresh_token); + if (self->refresh_token_expires_at < 0) { + ERROR(self->ctx, "Failed to fetch the JWT expiry time: %s\n", strerror(-r)); return self->refresh_token_expires_at; + } // Format the expiry time r = pakfire_strftime(expires_at, "%Y-%m-%dT%H:%M:%SZ", self->refresh_token_expires_at); diff --git a/src/pakfire/jwt.c b/src/pakfire/jwt.c index 0682650d..9a694fac 100644 --- a/src/pakfire/jwt.c +++ b/src/pakfire/jwt.c @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -88,40 +87,31 @@ ERROR: return r; } -int pakfire_jwt_payload(struct pakfire_ctx* ctx, const char* token, struct json_object** payload) { - char* error = NULL; +int pakfire_jwt_payload(const char* token, struct json_object** payload) { char* p = NULL; size_t l = 0; int r; // Decode the payload r = pakfire_jwt_decode_payload(&p, &l, token); - if (r < 0) { - ERROR(ctx, "Failed to decode the JWT payload: %s\n", strerror(-r)); - goto ERROR; - } + if (r < 0) + return r; // Parse the JSON - r = pakfire_json_parse(payload, &error, p, l); - if (r < 0) { - ERROR(ctx, "Failed to parse JSON payload: %s\n", error); - goto ERROR; - } - -ERROR: - if (error) - free(error); + r = pakfire_json_parse(payload, NULL, p, l); + if (r < 0) + return r; return 0; } -time_t pakfire_jwt_expires_at(struct pakfire_ctx* ctx, const char* token) { +time_t pakfire_jwt_expires_at(const char* token) { struct json_object* payload = NULL; time_t expires_at = -1; int r; // Parse the payload - r = pakfire_jwt_payload(ctx, token, &payload); + r = pakfire_jwt_payload(token, &payload); if (r < 0) goto ERROR; diff --git a/src/pakfire/jwt.h b/src/pakfire/jwt.h index 1d4f6f27..72c70a1e 100644 --- a/src/pakfire/jwt.h +++ b/src/pakfire/jwt.h @@ -23,11 +23,8 @@ #include -#include +int pakfire_jwt_payload(const char* token, struct json_object** payload); -int pakfire_jwt_payload(struct pakfire_ctx* ctx, - const char* token, struct json_object** payload); - -time_t pakfire_jwt_expires_at(struct pakfire_ctx* ctx, const char* token); +time_t pakfire_jwt_expires_at(const char* token); #endif /* PAKFIRE_JWT_H */