| 1 | package Dancer::Session::Abstract; | ||||||||||||||
| 2 | 97 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||
| 3 | #ABSTRACT: abstract class for session engine | ||||||||||||||
| 4 | 97 | $Dancer::Session::Abstract::VERSION = '1.3522'; | |||||||||||||
| 5 | 97 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 97 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 97 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 97 | ▶ | use base 'Dancer::Engine'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 97 | ▶ | use Dancer::Config 'setting'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 97 | ▶ | use Dancer::Cookies; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 97 | ▶ | use File::Spec; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 97 | __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 | 1 | ▶ | sub retrieve { | ||||||||||||
|
Subroutine
retrieve: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 21 | 1 | confess "retrieve not implemented"; | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | # args: ($class) | ||||||||||||||
| 25 | # create a new empty session, flush it and return it. | ||||||||||||||
| 26 | 1 | ▶ | sub create { | ||||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 27 | 1 | confess "create not implemented"; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | # args: ($self) | ||||||||||||||
| 31 | # write the (serialized) current session to the session storage | ||||||||||||||
| 32 | 1 | ▶ | sub flush { | ||||||||||||
|
Subroutine
flush: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 33 | 1 | confess "flush not implemented"; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | # args: ($self) | ||||||||||||||
| 37 | # remove the session from the session storage | ||||||||||||||
| 38 | 1 | ▶ | sub destroy { | ||||||||||||
|
Subroutine
destroy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 39 | 1 | confess "destroy not implemented"; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | # does nothing in most cases (exception is YAML) | ||||||||||||||
| 43 | 0 | ▶ | sub reset { | ||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 44 | 0 | return; | |||||||||||||
| 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 | 134 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 57 | 134 | my ($self) = @_; | |||||||||||||
| 58 | 134 | ▶ | if (!$self->id) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 134 | $self->id(build_id()); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | # this method can be overwritten in any Dancer::Session::* module | ||||||||||||||
| 64 | 469 | ▶ | sub session_name { | ||||||||||||
|
Subroutine
session_name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 65 | 469 | ▶ | setting('session_name') || 'dancer.session'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | # May be overriden if session key value pairs aren't stored in the | ||||||||||||||
| 69 | # session object's hash | ||||||||||||||
| 70 | 8 | ▶ | sub get_value { | ||||||||||||
|
Subroutine
get_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 71 | 8 | my ( $self, $key ) = @_; | |||||||||||||
| 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 | 8 | ▶ | sub set_value { | ||||||||||||
|
Subroutine
set_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 78 | 8 | my ( $self, $key, $value ) = @_; | |||||||||||||
| 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 | 134 | ▶ | sub build_id { | ||||||||||||
|
Subroutine
build_id: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 94 | 134 | my $session_id = ""; | |||||||||||||
| 95 | 134 | foreach my $seed (rand(1000), rand(1000), rand(1000)) { | |||||||||||||
| 96 | 402 | my $c = 0; | |||||||||||||
| 97 | 402 | $c += ord($_) for (split //, File::Spec->rel2abs(File::Spec->curdir)); | |||||||||||||
| 98 | 402 | my $current = int($seed * 1000000000) + time + $$ + $c; | |||||||||||||
| 99 | 402 | $session_id .= $current; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | 134 | return $session_id; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 177 | ▶ | sub read_session_id { | ||||||||||||
|
Subroutine
read_session_id: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 105 | 177 | my ($class) = @_; | |||||||||||||
| 106 | |||||||||||||||
| 107 | 177 | my $name = $class->session_name(); | |||||||||||||
| 108 | 177 | my $c = Dancer::Cookies->cookies->{$name}; | |||||||||||||
| 109 | 177 | ▶ | return unless defined $c; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 143 | ▶ | if ($class->validate_session_id($c->value)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 143 | 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 | 143 | ▶ | sub validate_session_id { | ||||||||||||
|
Subroutine
validate_session_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 124 | 143 | my ($class, $id) = @_; | |||||||||||||
| 125 | 143 | return $id =~ m{^[A-Za-z0-9_\-~]+$}; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 97 | ▶ | sub write_session_id { | ||||||||||||
|
Subroutine
write_session_id: called CC=7 SCAR=22.0
Pod
documented
| |||||||||||||||
| 129 | 97 | my ($class, $id) = @_; | |||||||||||||
| 130 | |||||||||||||||
| 131 | 97 | 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 | 97 | ▶ | if (my $cookie = Dancer::Cookies->cookie($name)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 63 | ▶ | if ($cookie eq $id) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 137 | 63 | return; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 34 | ▶ | my %cookie = ( | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | same_site => defined(setting("session_same_site")) ? | ||||||||||||||
| 149 | setting("session_same_site") : 'None', | ||||||||||||||
| 150 | ); | ||||||||||||||
| 151 | 34 | ▶ | if (my $expires = setting('session_expires')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | # It's # of seconds from the current time | ||||||||||||||
| 153 | # Otherwise just feed it through. | ||||||||||||||
| 154 | 0 | ▶ | $expires = Dancer::Cookie::_epoch_to_gmtstring(time + $expires) if $expires =~ /^\d+$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | 0 | $cookie{expires} = $expires; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 34 | my $c = Dancer::Cookie->new(%cookie); | |||||||||||||
| 159 | 34 | Dancer::Cookies->set_cookie_object($name => $c); | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 97 | 1; | |||||||||||||
| 163 | |||||||||||||||
| 164 | __END__ | ||||||||||||||