File Coverage

blib/lib/Ark/Plugin/Session/State/Cookie.pm
Criterion Covered Total %
statement 12 12 100.0
branch 3 6 50.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 19 23 82.6


line stmt bran cond sub pod time code
1             package Ark::Plugin::Session::State::Cookie;
2 6     6   4185 use strict;
  6         13  
  6         199  
3 6     6   35 use warnings;
  6         13  
  6         338  
4 6     6   33 use Ark::Plugin 'Session';
  6         20  
  6         42  
5              
6             has cookie_name => (
7             is => 'rw',
8             isa => 'Str',
9             lazy => 1,
10             default => sub {
11             my $self = shift;
12             $self->class_config->{cookie_name} || lc(ref $self->app) . '_session';
13             },
14             );
15              
16             has cookie_domain => (
17             is => 'rw',
18             isa => 'Maybe[Str]',
19             lazy => 1,
20             default => sub {
21             my $self = shift;
22             $self->class_config->{cookie_domain};
23             },
24             );
25              
26             has cookie_path => (
27             is => 'rw',
28             isa => 'Maybe[Str]',
29             lazy => 1,
30             default => sub {
31             my $self = shift;
32             $self->class_config->{cookie_path};
33             },
34             );
35              
36              
37             has cookie_expires => (
38             is => 'rw',
39             lazy => 1,
40             default => sub {
41             my $self = shift;
42             exists $self->class_config->{cookie_expires}
43             ? $self->class_config->{cookie_expires}
44             : exists $self->app->config->{'Plugin::Session'}->{expires}
45             ? $self->app->config->{'Plugin::Session'}->{expires}
46             : '+1d'; # 1day
47             },
48             );
49              
50             has cookie_secure => (
51             is => 'rw',
52             isa => 'Bool',
53             lazy => 1,
54             default => sub {
55             my $self = shift;
56             $self->class_config->{cookie_secure} || 0;
57             },
58             );
59              
60             has cookie_remove_marker => (
61             is => 'rw',
62             isa => 'Bool',
63             default => 0,
64             );
65              
66             has update_cookie => (
67             is => 'rw',
68             isa => 'HashRef',
69             );
70              
71             around 'get_session_id' => sub {
72             my $next = shift;
73             my $prev = $next->(@_);
74             return $prev if $prev;
75              
76             my ($self) = @_;
77             my $request = $self->context->request;
78              
79             unless ($self->cookie_remove_marker) {
80             if ( my $cookie = $request->cookies->{ $self->cookie_name } ) {
81             my $sid = ref $cookie ? $cookie->value : $cookie;
82             $self->log( debug => q[Found sessionid "%s" in cookie], $sid );
83             return $sid if $sid;
84             }
85             }
86              
87             return;
88             };
89              
90             around 'set_session_id' => sub {
91             my $next = shift;
92             my ($self, $sid) = @_;
93              
94             $self->update_cookie( $self->make_cookie($sid) );
95              
96             $next->(@_);
97             };
98              
99             around 'remove_session_id' => sub {
100             my $next = shift;
101             my ($self, $sid) = @_;
102              
103             $self->session_id(undef);
104             $self->cookie_remove_marker(1);
105             $self->update_cookie(
106             $self->make_cookie( $sid, { expires => 0 } )
107             );
108              
109             $next->(@_);
110             };
111              
112             around 'finalize_session' => sub {
113             my $next = shift;
114             my ($self, $res) = @_;
115              
116             my $cookie = $self->update_cookie;
117             my $sid = $self->get_session_id;
118              
119             if (!$cookie && $sid) {
120             $cookie = $self->make_cookie($sid);
121             }
122              
123             if ($cookie) {
124             $res->cookies->{ $self->cookie_name } = $cookie;
125             }
126              
127             $next->(@_);
128             };
129              
130             sub make_cookie {
131 66     66 0 138 my ($self, $sid, $attrs) = @_;
132              
133 66 50       650 my $cookie = {
134             value => $sid,
135             expires => $self->cookie_expires,
136             secure => $self->cookie_secure,
137             $self->cookie_domain ? (domain => $self->cookie_domain) : (),
138             $self->cookie_path ? (path => $self->cookie_path) : (),
139 66 50       403 %{ $attrs || {} },
    50          
140             };
141             }
142              
143             1;