Frontend: Session storage fixes

This commit is contained in:
mrkubax10 2023-09-23 14:10:19 +02:00
parent e5e82db490
commit 8d60bb55bc
2 changed files with 16 additions and 3 deletions

View File

@ -234,7 +234,7 @@ sub handlePath {
my $response = frontend::getBaseResponse(301, "OK");
$response.="Location: /\r\n";
$response.="Content-Length: 0\r\n";
$response.="Set-Cookie: session=$token\r\n\r\n";
$response.="Set-Cookie: session=$token;expires=".localtime(time()+7*24*3600)."\r\n\r\n";
$aClient->send($response);
return 1;
}

View File

@ -22,9 +22,12 @@ use strict;
use warnings;
our %sessions;
my %sessionAccess;
sub newSessionToken {
return Digest::SHA::sha256_hex(sprintf("%x", rand(0xFFFFFFFF)));
my $session = Digest::SHA::sha256_hex(sprintf("%x", rand(0xFFFFFFFF)));
$sessionAccess{$session} = time();
return $session;
}
sub deleteSession {
@ -32,13 +35,23 @@ sub deleteSession {
if(isValidSession($aSession)) {
delete $sessions{$aSession};
delete $sessionAccess{$aSession};
}
}
sub isValidSession {
my $aSession = $_[0];
return defined($sessions{$aSession});
foreach my $key (keys(%sessionAccess)) {
if(time()-$sessionAccess{$key}>7*24*3600) {
deleteSession($key);
}
}
if(defined($sessions{$aSession})) {
$sessionAccess{$aSession} = time();
return 1;
}
return 0;
}
1;