File Coverage

blib/lib/Dancer2/Session/Cookie.pm
Criterion Covered Total %
statement 24 26 92.3
branch 2 2 100.0
condition n/a
subroutine 10 12 83.3
pod 0 1 0.0
total 36 41 87.8


line stmt bran cond sub pod time code
1 1     1   1000 use 5.008001;
  1         3  
2 1     1   3 use strict;
  1         2  
  1         18  
3 1     1   2 use warnings;
  1         1  
  1         69  
4              
5             package Dancer2::Session::Cookie;
6             our $AUTHORITY = 'cpan:YANICK';
7             # ABSTRACT: Dancer 2 session storage in secure cookies
8             # VERSION
9             $Dancer2::Session::Cookie::VERSION = '0.007';
10 1     1   733 use Session::Storage::Secure 0.010 ();
  1         20285  
  1         28  
11              
12 1     1   8 use Moo;
  1         1  
  1         3  
13 1     1   178 use Dancer2::Core::Types;
  1         1  
  1         501  
14              
15             #--------------------------------------------------------------------------#
16             # Attributes
17             #--------------------------------------------------------------------------#
18              
19              
20             has secret_key => (
21             is => 'ro',
22             isa => Str,
23             required => 1,
24             );
25              
26              
27             has default_duration => (
28             is => 'ro',
29             isa => Int,
30             predicate => 1,
31             );
32              
33             has _store => (
34             is => 'lazy',
35             isa => InstanceOf ['Session::Storage::Secure'],
36             handles => {
37             '_freeze' => 'encode',
38             '_retrieve' => 'decode',
39             },
40             );
41              
42             sub _build__store {
43 4     4   1230 my ($self) = @_;
44 4         43 my %args = (
45             secret_key => $self->secret_key,
46             sereal_encoder_options => { snappy => 1, stringify_unknown => 1 },
47             sereal_decoder_options => { validate_utf8 => 1 },
48             );
49 4 100       23 $args{default_duration} = $self->default_duration
50             if $self->has_default_duration;
51 4         69 return Session::Storage::Secure->new(%args);
52             }
53              
54             sub _change_id {
55             # This is a noop with session cookies.
56 0     0   0 return;
57             }
58              
59             with 'Dancer2::Core::Role::SessionFactory';
60              
61             #--------------------------------------------------------------------------#
62             # Modified SessionFactory methods
63             #--------------------------------------------------------------------------#
64              
65             # We don't need to generate an ID. We'll set it during cookie generation
66 12     12 0 173409 sub generate_id { '' }
67              
68             # Cookie generation: serialize the session data into the session ID
69             # right before the cookie is generated
70             before 'cookie' => sub {
71             my ( $self, %params ) = @_;
72             my $session = $params{session};
73             return unless ref $session && $session->isa("Dancer2::Core::Session");
74             $session->id( $self->_freeze( $session->data, $session->expires ) );
75             };
76              
77             #--------------------------------------------------------------------------#
78             # SessionFactory implementation methods
79             #--------------------------------------------------------------------------#
80              
81             # _retrieve handled by _store
82              
83             # We don't actually flush data; instead we modify cookie generation
84 24     24   21946 sub _flush { return }
85              
86             # We have nothing to destroy, either; cookie expiration is all that matters
87 8     8   67635 sub _destroy { return }
88              
89             # There is no way to know about existing sessions when cookies
90             # are used as the store, so we lie and return an empty list.
91 0     0     sub _sessions { return [] }
92              
93             1;
94              
95              
96             # vim: ts=4 sts=4 sw=4 et:
97              
98             __END__