blib/lib/Dancer/Session/Abstract.pm

filter: Statementstmt 92.5% 62 / 67 Branchbran 61.1% 11 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.2% 20 / 21 Podpod 85.7% 12 / 14 totaltotal 87.5% 105 / 120 CC 12 SCAR 10.1
CC
12
Cov
86%
write_session_id
7.3
read_session_id
3.1
build_id
3.0
CRAP 2.7
« blib/lib/Dancer/Serializer/JSON.pm ↑ Summary blib/lib/Dancer/Exception/Base.pm »
1 package Dancer::Session::Abstract;
2 96 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: abstract class for session engine
4 96 $Dancer::Session::Abstract::VERSION = '1.3513';
5 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 96 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 96 ▶use base 'Dancer::Engine';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 96 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 96 ▶use Dancer::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 96 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 96 __PACKAGE__->attributes('id');
16  
17 # args: ($class, $id)
18 # receives a session id and should return a session object if found, or undef
19 # otherwise.
20 sub retrieve {
21 1 ▶ confess "retrieve not implemented";
Subroutine retrieve: called CC=1 SCAR=0.0
Pod documented
22 }
23  
24 # args: ($class)
25 # create a new empty session, flush it and return it.
26 sub create {
27 1 ▶ confess "create not implemented";
Subroutine create: called CC=1 SCAR=0.0
Pod documented
28 }
29  
30 # args: ($self)
31 # write the (serialized) current session to the session storage
32 sub flush {
33 1 ▶ confess "flush not implemented";
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
34 }
35  
36 # args: ($self)
37 # remove the session from the session storage
38 sub destroy {
39 1 ▶ confess "destroy not implemented";
Subroutine destroy: called CC=1 SCAR=0.0
Pod documented
40 }
41  
42 # does nothing in most cases (exception is YAML)
43 sub reset {
44 0 ▶ return;
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
45 }
46  
47 # if subclass overrides to true, flush will not be called after write
48 # and subclass or application must call flush (perhaps in an after hook)
49 8 ▶sub is_lazy { 0 };
Subroutine is_lazy: called CC=1 SCAR=0.0
Pod documented
50  
51 # This is the default constructor for the session object, the only mandatory
52 # attribute is 'id'. The whole object should be serialized by the session
53 # engine.
54 # If you override this constructor, remember to call $self->SUPER::init() so
55 # that the session ID is still generated.
56 sub init {
57 132 ▶ my ($self) = @_;
Subroutine init: called CC=2 SCAR=7.1
Pod documented
58 132 ▶ if (!$self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->id) 132 0 132
59 132 $self->id(build_id());
60 }
61 }
62  
63 # this method can be overwritten in any Dancer::Session::* module
64 sub session_name {
65 463 ▶ setting('session_name') || 'dancer.session';
Subroutine session_name: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
setting("session_name") or "dancer.session" 455 8 463
66 }
67  
68 # May be overriden if session key value pairs aren't stored in the
69 # session object's hash
70 sub get_value {
71 8 ▶ my ( $self, $key ) = @_;
Subroutine get_value: called CC=1 SCAR=0.0
Pod documented
72 8 return $self->{$key};
73 }
74  
75 # May be overriden if session key value pairs aren't stored in the
76 # session object's hash
77 sub set_value {
78 8 ▶ my ( $self, $key, $value ) = @_;
Subroutine set_value: called CC=1 SCAR=0.0
Pod documented
79 8 $self->{$key} = $value;
80 }
81  
82  
83 # Methods below this line should not be overloaded.
84  
85 # we try to make the best random number
86 # with native Perl 5 code.
87 # to rebuild a session id, an attacker should know:
88 # - the running PID of the server
89 # - the current timestamp of the time it was built
90 # - the path of the installation directory
91 # - guess the correct number between 0 and 1000000000
92 # - should be able to reproduce that 3 times
93 sub build_id {
94 132 ▶ my $session_id = "";
Subroutine build_id: called CC=3 SCAR=11.0
Pod documented
95 132 foreach my $seed (rand(1000), rand(1000), rand(1000)) {
96 396 my $c = 0;
97 396 $c += ord($_) for (split //, File::Spec->rel2abs(File::Spec->curdir));
98 396 my $current = int($seed * 1000000000) + time + $$ + $c;
99 396 $session_id .= $current;
100 }
101 132 return $session_id;
102 }
103  
104 sub read_session_id {
105 175 ▶ my ($class) = @_;
Subroutine read_session_id: called CC=3 SCAR=11.4
Pod documented
106  
107 175 my $name = $class->session_name();
108 175 my $c = Dancer::Cookies->cookies->{$name};
109 175 ▶ return unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 33 142 175
110 142 ▶ if ($class->validate_session_id($c->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->validate_session_id($c->value)) { } 142 0 142
111 142 return $c->value;
112 } else {
113 0 warn "Rejecting invalid session ID ". $c->value;
114 0 return;
115 }
116 }
117  
118 # Validate session ID (base64 chars plus hyphen by default) to avoid potential
119 # issues, e.g. if the ID is used insecurely elsewhere. If a session provider
120 # expects more unusual IDs, it can override this class method with one that
121 # validates according to that provider's expectation of how a session ID should
122 # look.
123 sub validate_session_id {
124 142 ▶ my ($class, $id) = @_;
Subroutine validate_session_id: called CC=1 SCAR=0.0
Pod undocumented
125 142 return $id =~ m{^[A-Za-z0-9_\-~]+$};
126 }
127  
128 sub write_session_id {
129 95 ▶ my ($class, $id) = @_;
Subroutine write_session_id: called CC=6 SCAR=19.9
Pod documented
130  
131 95 my $name = $class->session_name();
132  
133 # If we've already pushed the appropriate cookie to the response, then we
134 # don't need to do any more
135 95 ▶ if (my $cookie = Dancer::Cookies->cookie($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cookie = "Dancer::Cookies"->cookie($name)) 62 33 95
136 62 ▶ if ($cookie eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie eq $id) 62 0 62
137 62 return;
138 }
139 }
140  
141 33 ▶ my %cookie = (
Branch: 1 branch
BranchTrueFalseTotal
defined setting('session_is_http_only') ? : 0 33 33
142 name => $name,
143 value => $id,
144 domain => setting('session_domain'),
145 secure => setting('session_secure'),
146 http_only => defined(setting("session_is_http_only")) ?
147 setting("session_is_http_only") : 1,
148 );
149 33 ▶ if (my $expires = setting('session_expires')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires = setting("session_expires")) 0 33 33
150 # It's # of seconds from the current time
151 # Otherwise just feed it through.
152 0 ▶ $expires = Dancer::Cookie::_epoch_to_gmtstring(time + $expires) if $expires =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $expires =~ /^\d+$/ 0 0 0
153 0 $cookie{expires} = $expires;
154 }
155  
156 33 my $c = Dancer::Cookie->new(%cookie);
157 33 Dancer::Cookies->set_cookie_object($name => $c);
158 }
159  
160 96 1;
161  
162 __END__
« blib/lib/Dancer/Serializer/JSON.pm ↑ Summary blib/lib/Dancer/Exception/Base.pm »