Sessions/File.pm

filter: Statementstmt 82.3% 131 / 159 Branchbran 63.0% 58 / 92 Conditioncond 57.1% 12 / 21 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 100% 13 / 13 Podpod 85.7% 6 / 7 totaltotal 73.3% 223 / 304 CC 55 SCAR 23.8
CC
55
Cov
74%
get_session_info
15.0
create_session
13.9
delete_user_session
12.5
CRAP 10.9
↑ Summary Sessions/SQLite.pm »
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'
l!l
150
MC/DC0%0/1
$args{'storage_dir'} || '/tmp/sessions'
$args{'storage_dir'}
Truth table50%1/2
A
A= $args{'storage_dir'}
Aresult
11
00
14  
15 15 ▶ unless (-d $self->{storage_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"storage_dir"}) 14 1 15
16 14 ▶ unless (mkdir $self->{storage_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $self->{"storage_dir"}) 0 14 14
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
BranchTrueFalseTotal
unless (chmod 0700, $self->{"storage_dir"}) 0 15 15
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
BranchTrueFalseTotal
unless $args{"user_id"} 1 6 7
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
BranchTrueFalseTotal
unless (open $fh, ">", $session_file) 0 6 6
46 0 return { success => 0, message => "Cannot create session file: $!" };
47 }
48 6 ▶ unless (chmod 0600, $session_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod 0600, $session_file) 0 6 6
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'}
l!l&&r!l&&!r
600
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'}
ABresult
1X1
011
000
58 6 my $expiration;
59 6 ▶ if (defined $timeout && $timeout eq 'indefinite') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout and $timeout eq 'indefinite') { } 0 6 6
Condition33%1/3
defined $timeout and $timeout eq "indefinite"
!ll&&!rl&&r
060
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"
ABresult
0X0
100
111
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'} || {}
l!l
15
MC/DC100%1/1
$args{'data'} || {}
$args{'data'}
Truth table100%2/2
A
A= $args{'data'}
Aresult
11
00
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
BranchTrueFalseTotal
unless (print $fh $json, "\n") 0 6 6
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
BranchTrueFalseTotal
unless (close $fh) 0 6 6
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
BranchTrueFalseTotal
unless ($session_id) 1 13 14
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
BranchTrueFalseTotal
unless (-f $session_file) 0 13 13
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
BranchTrueFalseTotal
unless (open $fh, "<", $session_file) 0 13 13
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
BranchTrueFalseTotal
unless (close $fh) 0 13 13
116 0 return { success => 0, message => "Error closing session file: $!" };
117 }
118 13 ▶ unless (defined $json) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json) 0 13 13
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
BranchTrueFalseTotal
if ($@) 1 12 13
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
BranchTrueFalseTotal
unless ($session_info->{"session_id"} and $session_info->{"created_at"} and $session_info->{"expires_at"}) 2 10 12
Condition83%5/6
$session_info->{"session_id"} and $session_info->{"created_at"} and $session_info->{"expires_at"}
!ll&&!rl&&r
1110
$session_info->{"session_id"} and $session_info->{"created_at"}
!ll&&!rl&&r
1011
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"}
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if ($session_info->{"expires_at"} ne "indefinite" and time > $session_info->{"expires_at"}) 0 10 10
Condition33%1/3
$session_info->{"expires_at"} ne "indefinite" and time > $session_info->{"expires_at"}
!ll&&!rl&&r
0100
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($session_id) 1 4 5
152 1 return { success => 0, message => "Session ID required to update session in File backend" };
153 }
154  
155 4 ▶ unless ($updates) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($updates) 1 3 4
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
BranchTrueFalseTotal
unless (open $fh, "+<", $session_file) 0 3 3
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
BranchTrueFalseTotal
if ($json) 3 0 3
170 3 eval {
171 3 $session_info = JSON::PP->new->utf8->decode($json);
172 };
173 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
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
BranchTrueFalseTotal
if (exists $updates->{"data"}) 2 0 2
180 2 ▶ $session_info->{data} = $updates->{data} || {};
Condition50%1/2
$updates->{'data'} || {}
l!l
20
MC/DC0%0/1
$updates->{'data'} || {}
$updates->{'data'}
Truth table50%1/2
A
A= $updates->{'data'}
Aresult
11
00
181 }
182  
183 2 ▶ if (exists $updates->{expires_at}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $updates->{"expires_at"}) 2 0 2
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
BranchTrueFalseTotal
unless (print $fh $new_json, "\n") 0 2 2
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
BranchTrueFalseTotal
unless (close $fh) 0 2 2
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
BranchTrueFalseTotal
unless ($session_id) 0 2 2
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
BranchTrueFalseTotal
unless (-f $session_file) 1 1 2
216 1 return { success => 1, message => "Session file not found to delete" };
217 }
218  
219 1 ▶ unless (unlink $session_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $session_file) 0 1 1
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
BranchTrueFalseTotal
unless (opendir $dh, $self->{"storage_dir"}) 0 1 1
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
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 4 1 5
237 4 ▶ next if $file =~ /^\.\.?$/; # Skip . and ..
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^\.\.?$/u 2 2 4
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
BranchTrueFalseTotal
if $file =~ /\.\w{1,6}$/u 0 2 2
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
BranchTrueFalseTotal
if ($get_result->{'success'}) { } 2 0 2
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
BranchTrueFalseTotal
if ($delete_result->{"success"}) 0 0 0
249 0 $deleted_count++;
250 }
251 }
252 }
253  
254 1 ▶ unless (closedir $dh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (closedir $dh) 0 1 1
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
BranchTrueFalseTotal
unless ($user_id) 1 6 7
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
BranchTrueFalseTotal
opendir my $dh, $self->{"storage_dir"} or return {"success", 0, "message", "Cannot open sessions directory: $!"} 6 0 6
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
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 13 6 19
276 13 ▶ next if $file =~ /^\.\.?$/; # Skip . and ..
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^\.\.?$/u 12 1 13
277 1 ▶ next if $file =~ /\.\w{1,6}$/; # Skip files with extensions (not session files)
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.\w{1,6}$/u 0 1 1
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
BranchTrueFalseTotal
open my $fh, "<", $session_file or next 1 0 1
283 1 local $/;
284 1 my $json = <$fh>;
285 1 close $fh;
286  
287 1 ▶ next unless defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 0 1 1
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
BranchTrueFalseTotal
if $@ 0 1 1
294  
295 # Delete if user_id matches
296 1 ▶ if ($session_info->{user_id} eq $user_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session_info->{"user_id"} eq $user_id) 0 1 1
297 0 ▶ if (unlink $session_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $session_file) 0 0 0
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__
↑ Summary Sessions/SQLite.pm »