]> git.ipfire.org Git - pakfire.git/commitdiff
jwt: Add a convenience function to check whether a token has expires
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 13:17:16 +0000 (13:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 25 Jun 2025 13:17:16 +0000 (13:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/jwt.c
src/pakfire/jwt.h

index 9a694fac6d0cb56b04c7ba4c3529a86e7ab33680..4a89fba312da4245566984b273121b6638ee4103 100644 (file)
@@ -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;
+}
index 72c70a1e01de0a1f6d453d06451972f860f0d0d0..ab92af25398edcae07972c5403a817522932c336 100644 (file)
@@ -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 */