Sessions/Storage.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« Service/SessionMemory.pm ↑ Summary
1 package Mojolicious::Sessions::Storage;
2 0 ▶use Mojo::Base 'Mojolicious::Sessions';
Subroutine BEGIN: called CC=1 SCAR=2.6
3 use Digest::SHA1 ();
4  
5 our $VERSION = '0.01';
6  
7 has 'session_store';
8 has token_name => "token";
9  
10  
11 has sid_generator => sub{
12 sub{
13 Digest::SHA1::sha1_hex( rand() . $$ . {} . time );
14 };
15 };
16  
17 sub new{
18 my $class = shift;
19 my $self = $class->SUPER::new(@_);
20
21 if($self->session_store
22 && !Mojolicious::Controller->can('session_options'))
23 {
24 Mojolicious::Controller->attr(
25 session_options => sub{
26 $_[0]->stash->{'mojox.session.options'} ||= {};
27 },
28 );
29 }
30
31 return $self;
32 }
33  
34 sub load{
35 my ( $self, $c ) = @_;
36
37 return $self->SUPER::load($c) unless( $self->session_store );
38
39 my $stash = $c->stash;
40
41 my ( $session_id, $session ) = $self->get_session($c);
42 unless($session_id && $session){
43 $session_id = $self->generate_id($c->req->env);
44 }
45 $stash->{'mojox.session.options'} = {id => $session_id};
46 return unless($session);
47
48 # "expiration" value is inherited
49 my $expiration = $session->{expiration} // $self->default_expiration;
50 return if(!(my $expires = delete $session->{expires}) && $expiration);
51 return if(defined $expires && $expires <= time);
52
53 return unless($stash->{'mojo.active_session'} = keys %{$session});
54 $stash->{'mojo.session'} = $session;
55 $session->{flash} = delete $session->{new_flash} if($session->{new_flash});
56 }
57  
58 sub store{
59 my ( $self, $c ) = @_;
60
61 return $self->SUPER::store($c) unless( $self->session_store );
62
63 # Make sure session was active
64 my $stash = $c->stash;
65 return unless(my $session = $stash->{'mojo.session'});
66 return unless(keys %{$session} || $stash->{'mojo.active_session'});
67
68 # Don't reset flash for static files
69 my $old = delete $session->{flash};
70 @{ $session->{new_flash} }{ keys %{$old} } = values %{$old}
71 if($stash->{'mojo.static'});
72 delete $session->{new_flash} unless(keys %{ $session->{new_flash} });
73
74 # Generate "expires" value from "expiration" if necessary
75 my $expiration = $session->{expiration} // $self->default_expiration;
76 my $default = delete $session->{expires};
77 $session->{expires} = $default || time + $expiration
78 if($expiration || $default);
79
80 $self->set_session($c, $session) unless($c->session_options->{no_store});
81
82 my $options = {
83 domain => $self->cookie_domain,
84 expires => $session->{expires},
85 httponly => 1,
86 path => $self->cookie_path,
87 secure => $self->secure
88 };
89 $c->signed_cookie(
90 $self->cookie_name,
91 $c->session_options->{id},
92 $options,
93 );
94 }
95  
96 sub generate_id{
97 my ( $self, $env ) = @_;
98 $self->sid_generator->($env);
99 }
100  
101 sub get_session{
102 my ( $self, $c ) = @_;
103
104 my $session_id = $c->param($self->token_name) || $c->signed_cookie($self->cookie_name) or return;
105 my $session = $self->session_store->fetch($session_id) or return;
106
107 return ( $session_id, $session );
108 }
109  
110 sub set_session{
111 my ( $self, $c, $session ) = @_;
112
113 if($c->session_options->{expire} || ( defined $session->{expires} && $session->{expires} <= time )){
114 $session->{expires} = 1;
115 $self->session_store->remove($c->session_options->{id});
116 }
117 elsif($c->session_options->{change_id}){
118 $self->session_store->remove($c->session_options->{id});
119 $c->session_options->{id} = $self->generate_id($c->req->env);
120 $self->session_store->store($c->session_options->{id}, $session);
121 }
122 else{
123 $self->session_store->store($c->session_options->{id}, $session);
124 }
125 }
126  
127 1;
128  
129 1; # End of Mojolicious::Sessions::Storage
« Service/SessionMemory.pm ↑ Summary