| 1 | package Concierge::Sessions::File v0.11.3; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use parent 'Concierge::Sessions::Base'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 15 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||||||||||||||||||||||||
| 13 | 15 | ▶ | $self->{storage_dir} = $args{storage_dir} || '/tmp/sessions'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'storage_dir'} || '/tmp/sessions'
MC/DC0%0/1
$args{'storage_dir'} || '/tmp/sessions'
$args{'storage_dir'}
Truth table50%1/2
A
A= $args{'storage_dir'}
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | unless (-d $self->{storage_dir}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | unless (mkdir $self->{storage_dir}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | croak "Failed to create storage directory '$self->{storage_dir}': $!"; | |||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 15 | ▶ | unless (chmod 0700, $self->{storage_dir}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | croak "Failed to set permissions on storage directory '$self->{storage_dir}': $!"; | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 15 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | ▶ | sub create_session { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_session: called CC=10 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | return { success => 0, message => "Cannot create session without user_id" } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | ▶ | unless $args{user_id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | my $user_id = $args{user_id}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # Delete any existing sessions for this user (enforce single session per user) | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | $self->delete_user_session($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 6 | my $session_id = $self->generate_session_id(); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | my $session_file = File::Spec->catfile($self->{storage_dir}, $session_id); | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Write over session file if it already exists (unlikely) | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 6 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | ▶ | unless (open $fh, '>', $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | return { success => 0, message => "Cannot create session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 6 | ▶ | unless (chmod 0600, $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | return { success => 0, message => "Cannot set session file permissions: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # Build session_info structure | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 6 | my $now = time(); | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Handle session timeout: 'indefinite' or numeric value in seconds | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 6 | ▶ | my $timeout = $args{session_timeout} || $self->{session_timeout}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'session_timeout'} || $self->{'session_timeout'}
MC/DC0%0/2
$args{'session_timeout'} || $self->{'session_timeout'}
$args{'session_timeout'}
$self->{'session_timeout'}
Truth table33%1/3
A || B
A= $args{'session_timeout'}B= $self->{'session_timeout'}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | my $expiration; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | ▶ | if (defined $timeout && $timeout eq 'indefinite') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $timeout and $timeout eq "indefinite"
MC/DC0%0/2
defined $timeout and $timeout eq "indefinite"
defined $timeout
$timeout eq "indefinite"
Truth table33%1/3
A and B
A= defined $timeoutB= $timeout eq "indefinite"
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $expiration = 'indefinite'; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | $expiration = $now + $timeout; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | ▶ | my $data = $args{data} || {}; # for app data | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'data'} || {}
MC/DC100%1/1
$args{'data'} || {}
$args{'data'}
Truth table100%2/2
A
A= $args{'data'}
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | my $session_info = { | |||||||||||||||||||||||||||||||||||||||||
| 67 | session_id => $session_id, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | created_at => $now, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | expires_at => $expiration, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | last_updated => $now, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | session_timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | status => { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | state => 'active', | ||||||||||||||||||||||||||||||||||||||||||
| 75 | dirty => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | data => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | # Encode to JSON with pretty formatting and write with trailing newline | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | my $json = JSON::PP->new->utf8->pretty->encode($session_info); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | ▶ | unless (print $fh $json, "\n") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | return { success => 0, message => "Cannot write to session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | unless (close $fh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return { success => 0, message => "Cannot close session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 6 | return { success => 1, session_id => $session_id }; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 14 | ▶ | sub get_session_info { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_session_info: called CC=12 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 14 | my ($self, $session_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 14 | ▶ | unless ($session_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | return { success => 0, message => "Session ID required to retrieve session from File backend" }; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 13 | my $session_file = File::Spec->catfile($self->{storage_dir}, $session_id); | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 13 | ▶ | unless (-f $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | return { success => 0, message => "Session file not found" }; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 13 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 13 | ▶ | unless (open $fh, '<', $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | return { success => 0, message => "Cannot read session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # Read entire file (pretty JSON spans multiple lines) | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 13 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 13 | my $json = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 13 | ▶ | unless (close $fh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | return { success => 0, message => "Error closing session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 13 | ▶ | unless (defined $json) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | return { success => 0, message => "Session file is empty" }; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | # Decode JSON | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 13 | my $session_info; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 13 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 126 | 13 | $session_info = JSON::PP->new->utf8->decode($json); | |||||||||||||||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 13 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | return { success => 0, message => "Invalid JSON in session file: $@" }; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 12 | ▶ | unless ($session_info->{session_id} && $session_info->{created_at} && $session_info->{expires_at}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$session_info->{"session_id"} and $session_info->{"created_at"} and $session_info->{"expires_at"}
$session_info->{"session_id"} and $session_info->{"created_at"}
MC/DC66%2/3
$session_info->{"session_id"} and $session_info->{"created_at"} and $session_info->{"expires_at"}
$session_info->{"session_id"}
$session_info->{"created_at"}
$session_info->{"expires_at"}
Truth table75%3/4
A and B and C
A= $session_info->{"session_id"}B= $session_info->{"created_at"}C= $session_info->{"expires_at"}
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 2 | return { success => 0, message => "Invalid session file: missing system status fields" }; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # Check expiration (skip if indefinite) | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 10 | ▶ | if ($session_info->{expires_at} ne 'indefinite' && time() > $session_info->{expires_at}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$session_info->{"expires_at"} ne "indefinite" and time > $session_info->{"expires_at"}
MC/DC0%0/2
$session_info->{"expires_at"} ne "indefinite" and time > $session_info->{"expires_at"}
$session_info->{"expires_at"} ne "indefinite"
time > $session_info->{"expires_at"}
Truth table33%1/3
A and B
A= $session_info->{"expires_at"} ne "indefinite"B= time > $session_info->{"expires_at"}
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return { success => 0, message => "Session expired" }; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 10 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 143 | message => "Session info retrieved", | ||||||||||||||||||||||||||||||||||||||||||
| 144 | info => $session_info | ||||||||||||||||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 5 | ▶ | sub update_session { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_session: called CC=11 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 5 | my ($self, $session_id, $updates) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 5 | ▶ | unless ($session_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | return { success => 0, message => "Session ID required to update session in File backend" }; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | ▶ | unless ($updates) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | return { success => 1, message => "No updates specified for File backend session update" }; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 3 | my $session_file = File::Spec->catfile($self->{storage_dir}, $session_id); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 3 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 3 | ▶ | unless (open $fh, '+<', $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return { success => 0, message => "Cannot open or update session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | # Read entire file (pretty JSON spans multiple lines) | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | my $json = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | my $session_info; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | if ($json) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | $session_info = JSON::PP->new->utf8->decode($json); | |||||||||||||||||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | return { success => 0, message => "Invalid JSON in session file: $@" }; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | # Apply updates | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 2 | ▶ | if (exists $updates->{data}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | $session_info->{data} = $updates->{data} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$updates->{'data'} || {}
MC/DC0%0/1
$updates->{'data'} || {}
$updates->{'data'}
Truth table50%1/2
A
A= $updates->{'data'}
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 2 | ▶ | if (exists $updates->{expires_at}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | $session_info->{expires_at} = $updates->{expires_at}; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | # Always update last_updated timestamp | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | $session_info->{last_updated} = time(); | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # Encode to JSON with pretty formatting and write to file with trailing newline | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | my $new_json = JSON::PP->new->utf8->pretty->encode($session_info); | |||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | $fh->truncate(0); | |||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | seek $fh, 0, 0; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | ▶ | unless (print $fh $new_json, "\n") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | return { success => 0, message => "Cannot write to session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 2 | ▶ | unless (close $fh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | return { success => 0, message => "Cannot close session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | return { success => 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 2 | ▶ | sub delete_session { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_session: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | my ($self, $session_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 2 | ▶ | unless ($session_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | return { success => 0, message => "Session ID required to delete session from File backend" }; | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | my $session_file = File::Spec->catfile($self->{storage_dir}, $session_id); | |||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 2 | ▶ | unless (-f $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | return { success => 1, message => "Session file not found to delete" }; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | ▶ | unless (unlink $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | return { success => 0, message => "Cannot delete session file: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 1 | return { success => 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | ▶ | sub cleanup_sessions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cleanup_sessions: called CC=8 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | my $dh; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | unless (opendir($dh, $self->{storage_dir})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | return { success => 0, message => "Cannot open sessions directory: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | my $deleted_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | my $active = []; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | ▶ | while (my $file = readdir($dh)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 4 | ▶ | next if $file =~ /^\.\.?$/; # Skip . and .. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | # Skip any files in the dir with suffixes, which session files don't have | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | ▶ | next if $file =~ /\.\w{1,6}$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | # Session files are named after session_ids, so this works: | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | my $get_result = $self->get_session_info($file); | |||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | ▶ | if ($get_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | push $active->@* => $file; | |||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # Session is either expired or invalid, delete the file | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my $delete_result = $self->delete_session($file); | |||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if ($delete_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $deleted_count++; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | ▶ | unless (closedir $dh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | return { success => 0, message => "Error closing sessions directory: $!" }; | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 1 | return { success => 1, deleted_count => $deleted_count, active => $active }; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 7 | ▶ | sub delete_user_session { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_user_session: called CC=11 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 7 | my ($self, $user_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 7 | ▶ | unless ($user_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 1 | return { success => 0, message => "user_id required to delete user sessions from File backend" }; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 6 | ▶ | opendir(my $dh, $self->{storage_dir}) or return { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | success => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 270 | message => "Cannot open sessions directory: $!" | ||||||||||||||||||||||||||||||||||||||||||
| 271 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 6 | my $deleted_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 6 | ▶ | while (my $file = readdir($dh)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 13 | ▶ | next if $file =~ /^\.\.?$/; # Skip . and .. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | ▶ | next if $file =~ /\.\w{1,6}$/; # Skip files with extensions (not session files) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 1 | my $session_file = File::Spec->catfile($self->{storage_dir}, $file); | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | # Read and parse to check user_id | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 1 | ▶ | open my $fh, '<', $session_file or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | my $json = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | ▶ | next unless defined $json; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | my $session_info; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | $session_info = JSON::PP->new->utf8->decode($json); | |||||||||||||||||||||||||||||||||||||||||
| 292 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | ▶ | next if $@; # Skip invalid files | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | # Delete if user_id matches | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | ▶ | if ($session_info->{user_id} eq $user_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if (unlink $session_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $deleted_count++; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 6 | closedir($dh); | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 6 | return { success => 1, deleted_count => $deleted_count }; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 15 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 15 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | __END__ | ||||||||||||||||||||||||||||||||||||||||||