| 1 | package Plack::Middleware::Session::Cookie; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use parent qw(Plack::Middleware::Session); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use Plack::Util::Accessor qw(secret session_key domain expires path secure httponly | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | partitioned samesite serializer deserializer); | |||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use Digest::HMAC_SHA1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use MIME::Base64 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Storable (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use Time::HiRes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Plack::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | use Plack::Session::State::Cookie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | ▶ | sub prepare_app { | ||||||||
|
Subroutine
prepare_app: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||
| 17 | 4 | my $self = shift; | |||||||||
| 18 | |||||||||||
| 19 | 4 | ▶ | die "Plack::Session::Middleware::Cookie requires setting 'secret' option." | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | unless $self->secret; | ||||||||||
| 21 | |||||||||||
| 22 | 4 | ▶ | $self->session_key("plack_session") unless $self->session_key; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | |||||||||||
| 24 | 6 | ▶ | $self->serializer(sub {MIME::Base64::encode(Storable::nfreeze($_[0]), '' )}) | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 4 | ▶ | unless $self->serializer; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | $self->deserializer(sub {Storable::thaw(MIME::Base64::decode($_[0]))}) | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 28 | 4 | ▶ | unless $self->deserializer; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 4 | $self->state( Plack::Session::State::Cookie->new ); | |||||||||
| 31 | 4 | for my $attr (qw(session_key path domain expires secure partitioned httponly samesite)) { | |||||||||
| 32 | 32 | $self->state->$attr($self->$attr); | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub _compare { | ||||||||
|
Subroutine
_compare: not called CC=3 SCAR=24.8
| |||||||||||
| 37 | 0 | my($s1, $s2) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | return if length $s1 != length $s2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | my $r = 0; | |||||||||
| 41 | 0 | for my $i (0..length($s1) - 1) { | |||||||||
| 42 | 0 | $r |= ord(substr $s1, $i) ^ ord(substr $s2, $i); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 0 | return $r == 0; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 6 | ▶ | sub get_session { | ||||||||
|
Subroutine
get_session: called CC=3 SCAR=18.1
Pod
undocumented
| |||||||||||
| 49 | 6 | my($self, $request) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 6 | ▶ | my $cookie = $self->state->get_session_id($request) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 0 | my($time, $b64, $sig) = split /:/, $cookie, 3; | |||||||||
| 54 | 0 | ▶ | _compare($self->sig($b64), $sig) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | # NOTE: do something with $time? | ||||||||||
| 57 | |||||||||||
| 58 | 0 | my $session = $self->deserializer->($b64); | |||||||||
| 59 | 0 | return ($self->generate_id, $session); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 6 | ▶ | sub generate_id { | ||||||||
|
Subroutine
generate_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | 6 | my $self = shift; | |||||||||
| 64 | 6 | return scalar Time::HiRes::gettimeofday; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 6 | ▶ | sub commit { } | ||||||||
|
Subroutine
commit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub change_id { | ||||||||
|
Subroutine
change_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 70 | 0 | my($self, $env) = @_; | |||||||||
| 71 | |||||||||||
| 72 | 0 | my $options = $env->{'psgix.session.options'}; | |||||||||
| 73 | |||||||||||
| 74 | 0 | $options->{id} = $self->generate_id($env); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub expire_session { | ||||||||
|
Subroutine
expire_session: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 78 | 0 | my($self, $id, $res, $env) = @_; | |||||||||
| 79 | 0 | $self->state->expire_session_id($id, $res, $env->{'psgix.session.options'}); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 6 | ▶ | sub save_state { | ||||||||
|
Subroutine
save_state: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 83 | 6 | my($self, $id, $res, $env) = @_; | |||||||||
| 84 | |||||||||||
| 85 | 6 | my $cookie = $self->_serialize($id, $env->{'psgix.session'}); | |||||||||
| 86 | 6 | $self->state->finalize($cookie, $res, $env->{'psgix.session.options'}); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 6 | ▶ | sub _serialize { | ||||||||
|
Subroutine
_serialize: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 6 | my($self, $id, $session) = @_; | |||||||||
| 91 | |||||||||||
| 92 | 6 | my $b64 = $self->serializer->($session); | |||||||||
| 93 | 6 | join ":", $id, $b64, $self->sig($b64); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 6 | ▶ | sub sig { | ||||||||
|
Subroutine
sig: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 97 | 6 | my($self, $b64) = @_; | |||||||||
| 98 | 6 | ▶ | return '.' unless $self->secret; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 6 | Digest::HMAC_SHA1::hmac_sha1_hex($b64, $self->secret); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 3 | 1; | |||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||