File Coverage

blib/lib/Dancer2/Session/PSGI.pm
Criterion Covered Total %
statement 20 21 95.2
branch 4 4 100.0
condition n/a
subroutine 7 9 77.7
pod 0 3 0.0
total 31 37 83.7


line stmt bran cond sub pod time code
1             package Dancer2::Session::PSGI;
2              
3             # ABSTRACT: Dancer2 session storage via Plack::Middleware::Session
4              
5 2     2   1449 use Moo;
  2         5  
  2         10  
6             with 'Dancer2::Core::Role::SessionFactory';
7              
8             our $VERSION = '0.010'; # VERSION
9              
10             #-----------------------------------------#
11             # Alter SessionFactory attribute defaults
12             #-----------------------------------------#
13              
14             has '+cookie_name' => ( default => 'plack_session' );
15              
16             #-----------------------------------------#
17             # SessionFactory implementation methods
18             #-----------------------------------------#
19              
20             # Get middleware session hash
21             sub _retrieve {
22 7     7   40 shift->request->env->{'psgix.session'};
23             }
24              
25             # Put data back/into middleware session hash
26             sub _flush {
27 8     8   243330 my ( $self, $id, $data ) = @_;
28 8         37 $self->request->env->{'psgix.session'} = $data;
29             }
30              
31             # Do nothing - we override change_id below but this method must exist to
32             # force Dance2::Core::App::change_session_id to call our change_id method
33       0     sub _change_id {}
34              
35             # Middleware handles cookie expiry
36 1     1   1522 sub _destroy { return }
37              
38             # Its the responsibility of Plack::Middleware::Session for
39             # tracking other sessions (we know nothing about them).
40             # So return an empty list.
41 0     0   0 sub _sessions { return [] }
42              
43             #-----------------------------------------#
44             # Overridden methods from SessionFactory
45             #-----------------------------------------#
46              
47             # Plack::Middleware::Session is responsible for fetch and id generation
48             # any ID passed to this engine is considered valid
49 7     7 0 51663 sub validate_id { 1 }
50              
51             sub change_id {
52 1     1 0 193 my ( $self, %params ) = @_;
53 1         3 my $session = $params{session};
54              
55 1         16 $self->execute_hook( 'engine.session.before_change_id', $session->id );
56 1         62 $self->request->env->{'psgix.session.options'}->{'change_id'} = 1;
57             # we don't know what the new session ID will be yet so make it undef
58 1         5 $self->execute_hook( 'engine.session.before_change_id', undef );
59             }
60              
61             # Middleware sets the cookie.
62             sub set_cookie_header {
63 10     10 0 6450 my ( $self, %params ) = @_;
64              
65 10         20 my $session = $params{session};
66 10         36 my $options = $self->request->env->{'psgix.session.options'};
67              
68 10 100       155 if ( my $expires = $session->expires ) {
69 2 100       35 if ( $session->expires < time ) { # Cookie has expired.
70 1         13 $options->{expire} = 1;
71             }
72             else { # Pass upstream the cookie expire time
73 1         10 $options->{expires} = $expires;
74             }
75             }
76             }
77              
78             1;
79              
80             __END__