blib/lib/Dancer2/Session/Cookie.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 83.3% 5 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 1 totaltotal 88.4% 61 / 69 CC 5 SCAR 5.1
CC
5
Cov
92%
__ANON__
3.3
__ANON__
2.0
_build__store
2.0
CRAP 1.7
↑ Summary
1 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Dancer2::Session::Cookie;
6 3 our $AUTHORITY = 'cpan:YANICK';
7 # ABSTRACT: Dancer 2 session storage in secure cookies
8 # VERSION
9 3 $Dancer2::Session::Cookie::VERSION = '0.009';
10 3 ▶use Session::Storage::Secure 0.011 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #--------------------------------------------------------------------------#
16 # Attributes
17 #--------------------------------------------------------------------------#
18  
19  
20 3 has secret_key => (
21 is => 'ro',
22 isa => Str,
23 required => 1,
24 );
25  
26  
27 3 has default_duration => (
28 is => 'ro',
29 isa => Int,
30 predicate => 1,
31 );
32  
33  
34 3 has with_request_address => (
35 is => 'ro',
36 isa => Bool,
37 );
38  
39 3 has _store => (
40 is => 'lazy',
41 isa => InstanceOf ['Session::Storage::Secure'],
42 handles => {
43 '_freeze' => 'encode',
44 '_retrieve' => 'decode',
45 },
46 );
47  
48 67 ▶before [qw/ _freeze _retrieve /] => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
49 67 my $self = shift;
50 67 ▶ return unless $self->with_request_address;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->with_request_address 64 3 67
51 3 $self->_store->{secret_key} = join '-', $self->secret_key, $self->request->address;
52 3 };
53  
54 7 ▶sub _build__store {
Subroutine _build__store: called CC=2 SCAR=6.9
55 7 my ($self) = @_;
56 7 my %args = (
57 secret_key => $self->secret_key,
58 sereal_encoder_options => { snappy => 1, stringify_unknown => 1 },
59 sereal_decoder_options => { validate_utf8 => 1 },
60 );
61 7 ▶ $args{default_duration} = $self->default_duration
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_default_duration 2 5 7
62 if $self->has_default_duration;
63 7 return Session::Storage::Secure->new(%args);
64 }
65  
66 4 ▶sub _change_id {
Subroutine _change_id: called CC=1 SCAR=0.0
67 # This is a noop with session cookies.
68 4 return;
69 }
70  
71 3 with 'Dancer2::Core::Role::SessionFactory';
72  
73 #--------------------------------------------------------------------------#
74 # Modified SessionFactory methods
75 #--------------------------------------------------------------------------#
76  
77 # We don't need to generate an ID. We'll set it during cookie generation
78 25 ▶sub generate_id { '' }
Subroutine generate_id: called CC=1 SCAR=0.0
Pod undocumented
79  
80 # Cookie generation: serialize the session data into the session ID
81 # right before the cookie is generated
82 40 ▶before 'cookie' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
83 40 my ( $self, %params ) = @_;
84 40 my $session = $params{session};
85 40 ▶ return unless ref $session && $session->isa("Dancer2::Core::Session");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $session and $session->isa("Dancer2::Core::Session") 0 40 40
Condition33%1/3
ref $session and $session->isa("Dancer2::Core::Session")
!ll&&!rl&&r
0040
MC/DC0%0/2
ref $session and $session->isa("Dancer2::Core::Session")
ref $session $session->isa("Dancer2::Core::Session")
Truth table33%1/3
A and B
A= ref $sessionB= $session->isa("Dancer2::Core::Session")
ABresult
0X0
100
111
86 40 $session->id( $self->_freeze( $session->data, $session->expires ) );
87 3 };
88  
89 #--------------------------------------------------------------------------#
90 # SessionFactory implementation methods
91 #--------------------------------------------------------------------------#
92  
93 # _retrieve handled by _store
94  
95 # We don't actually flush data; instead we modify cookie generation
96 33 ▶sub _flush { return }
Subroutine _flush: called CC=1 SCAR=0.0
97  
98 # We have nothing to destroy, either; cookie expiration is all that matters
99 8 ▶sub _destroy { return }
Subroutine _destroy: called CC=1 SCAR=0.0
100  
101 # There is no way to know about existing sessions when cookies
102 # are used as the store, so we lie and return an empty list.
103 0 ▶sub _sessions { return [] }
Subroutine _sessions: not called CC=1 SCAR=6.9
104  
105 3 1;
106  
107  
108 # vim: ts=4 sts=4 sw=4 et:
109  
110 __END__
↑ Summary