[pve-devel] [PATCH pve-apiclient v3 1/1] try to refresh ticket before an api call
Dominik Csapak
d.csapak at proxmox.com
Fri Nov 14 15:42:00 CET 2025
when the ticket is an hour old (or older).
This can help for long running tasks that reuse the api client
throughout the whole task.
It will not work though if there is more than 2 hours between api calls,
but that should not be the case that often.
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
new in v3
src/PVE/APIClient/LWP.pm | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/src/PVE/APIClient/LWP.pm b/src/PVE/APIClient/LWP.pm
index 7407dc4..f0c81bc 100755
--- a/src/PVE/APIClient/LWP.pm
+++ b/src/PVE/APIClient/LWP.pm
@@ -276,7 +276,26 @@ sub call {
my $ua = $self->{useragent};
- # fixme: check ticket lifetime?
+ if ($ticket && $ticket =~ m/^(\S+)::[^:\s]+$/) {
+ my $plain = $1;
+
+ # only the last 8 characters from the plain part are the timestamp
+ if ($plain =~ m/([A-Z0-9]{8})$/) {
+ my $timestamp = $1;
+ my $ttime = hex($timestamp);
+ my $age = time() - $ttime;
+
+ if ($age > 3600) { # older than one hour
+ if (!defined($self->{password})) {
+ $self->{password} = $ticket;
+ $self->login();
+ $self->{password} = undef;
+ } else {
+ $self->login();
+ }
+ }
+ }
+ }
if (!$ticket && !$apitoken && $self->{username} && $self->{password}) {
$self->login();
--
2.47.3
More information about the pve-devel
mailing list