From: Michael Tremer Date: Wed, 25 Jun 2025 13:17:16 +0000 (+0000) Subject: jwt: Add a convenience function to check whether a token has expires X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f83928f021d7a7d90ad58a75c850f8c47a05fa19;p=pakfire.git jwt: Add a convenience function to check whether a token has expires Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/jwt.c b/src/pakfire/jwt.c index 9a694fac..4a89fba3 100644 --- a/src/pakfire/jwt.c +++ b/src/pakfire/jwt.c @@ -129,3 +129,21 @@ ERROR: return r; } + +int pakfire_jwt_has_expired(const char* token) { + time_t expires_at = -1; + time_t now = -1; + + // Fetch the current time + now = time(NULL); + if (now < 0) + return -errno; + + // Fetch the expiry time of the token + expires_at = pakfire_jwt_expires_at(token); + if (expires_at < 0) + return expires_at; + + // We consider the token as expired if there are less than 60 seconds left + return (expires_at - now) < 60; +} diff --git a/src/pakfire/jwt.h b/src/pakfire/jwt.h index 72c70a1e..ab92af25 100644 --- a/src/pakfire/jwt.h +++ b/src/pakfire/jwt.h @@ -26,5 +26,6 @@ int pakfire_jwt_payload(const char* token, struct json_object** payload); time_t pakfire_jwt_expires_at(const char* token); +int pakfire_jwt_has_expired(const char* token); #endif /* PAKFIRE_JWT_H */