Mojo/Hakkefuin/Sessions.pm untested

filter: Statementstmt 0.0% 0 / 20 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 59 CC 7 SCAR 39.0
CC
7
Cov
0%
store
56.0
BEGIN
2.0
CRAP 49.2
1 package Mojo::Hakkefuin::Sessions;
2 use Mojo::Base 'Mojolicious::Sessions';
3  
4 has 'max_age';
5  
6 sub store {
7 my ($self, $c) = @_;
8  
9 # Make sure session was active
10 my $stash = $c->stash;
11 return unless my $session = $stash->{'mojo.session'};
12 return unless keys %$session || $stash->{'mojo.active_session'};
13  
14 # Don't reset flash for static files
15 my $old = delete $session->{flash};
16 $session->{new_flash} = $old if $stash->{'mojo.static'};
17 delete $session->{new_flash} unless keys %{$session->{new_flash}};
18  
19 # Generate "expires" value from "expiration" if necessary
20 my $expiration = $session->{expiration} // $self->default_expiration;
21 my $default = delete $session->{expires};
22 $session->{expires} = $default || time + $expiration
23 if $expiration || $default;
24  
25 # "max-age" is set if necessary
26 my $max_age = $session->{expires} - time
27 if $self->max_age && $session->{expires} > time;
28  
29 my $value = Mojo::Util::b64_encode $self->serialize->($session), '';
30 $value =~ y/=/-/;
31 my $options = {
32 domain => $self->cookie_domain,
33 expires => $session->{expires},
34 httponly => 1,
35 max_age => $max_age,
36 path => $self->cookie_path,
37 samesite => $self->samesite,
38 secure => $self->secure
39 };
40 $c->signed_cookie($self->cookie_name, $value, $options);
41 }
42  
43 1;
44  
45 =encoding utf8
46  
47 =head1 NAME
48  
49 Mojo::Hakkefuin::Sessions - Session manager with available set up max-age
50  
51 =head1 SYNOPSIS
52  
53 use Mojo::Hakkefuin::Sessions;
54  
55 my $sessions = Mojo::Hakkefuin::Sessions->new;
56 $sessions->cookie_name('myapp');
57 $sessions->default_expiration(86400);
58 $sessions->max_age(1);
59  
60 =head1 DESCRIPTION
61  
62 L<Mojo::Hakkefuin::Sessions> inherits all from L<Mojolicious::Sessions>.
63 Its meant to available setup B<max-age>.
64  
65 =head1 ATTRIBUTES
66  
67 L<Mojo::Hakkefuin::Sessions> implements the attributes from
68 L<Mojolicious::Sessions>, and additional attributes as the following.
69  
70 =head2 max_age
71
72 my $bool = $sessions->max_age;
73 $sessions = $sessions->max_age($bool);
74  
75 Set the C<max-age> for all session cookies. If "max_age" is set, the session cookie
76 will have the "expires" and "max-age" attributes, and when the browser finds the "max-age"
77 attribute in a cookie, the cookie expiration will use "max-age" as a top priority.
78 The "max-age" attribute only applies if the browser supports this attribute.
79 Before set this attribute, please see
80 L<Browser Compatibility|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/#Browser_compatibility>
81  
82 =head1 METHODS
83  
84 L<Mojo::Hakkefuin::Sessions> from all methods L<Mojolicious::Sessions>
85 and implements the following new ones.
86  
87 =head2 store
88  
89 $sessions->store;
90  
91 Store session data in signed cookie.
92  
93 =head1 SEE ALSO
94  
95 L<Mojolicious::Sessions>, L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
96  
97 =cut