ASP/Session.pm

filter: Statementstmt 92.1% 59 / 64 Branchbran 78.5% 11 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 14 / 16 Podpod 100% 3 / 3 totaltotal 87.2% 89 / 102 CC 11 SCAR 9.0
CC
11
Cov
89%
FETCH
5.1
NEXTKEY
3.2
TIEHASH
3.0
CRAP 2.5
« ASP/Request.pm ↑ Summary ASP/Dispatcher.pm »
1 package CatalystX::ASP::Session;
2  
3 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use parent 'Tie::Hash';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 has 'asp' => (
8 is => 'ro',
9 isa => 'CatalystX::ASP',
10 required => 1,
11 weak_ref => 1,
12 );
13  
14 =head1 NAME
15  
16 CatalystX::ASP::Session - $Session Object
17  
18 =head1 SYNOPSIS
19  
20 use CatalystX::ASP::Session;
21  
22 my $session = CatalystX::ASP::Session->new(asp => $asp);
23 tie %Session, 'CatalystX::ASP::Session', $session;
24 $Session{foo} = $bar;
25  
26 =head1 DESCRIPTION
27  
28 The C<$Session> object keeps track of user and web client state, in a persistent
29 manner, making it relatively easy to develop web applications. The C<$Session>
30 state is stored across HTTP connections, in database files in the C<Global> or
31 C<StateDir> directories, and will persist across web server restarts.
32  
33 The user session is referenced by a 128 bit / 32 byte MD5 hex hashed cookie, and
34 can be considered secure from session id guessing, or session hijacking. When a
35 hacker fails to guess a session, the system times out for a second, and with
36 2**128 (3.4e38) keys to guess, a hacker will not be guessing an id any time
37 soon.
38  
39 If an incoming cookie matches a timed out or non-existent session, a new session
40 is created with the incoming id. If the id matches a currently active session,
41 the session is tied to it and returned. This is also similar to the Microsoft
42 ASP implementation.
43  
44 The C<$Session> reference is a hash ref, and can be used as such to store data
45 as in:
46  
47 $Session->{count}++; # increment count by one
48 %{$Session} = (); # clear $Session data
49  
50 The C<$Session> object state is implemented through L<MLDBM>, and a user should
51 be aware of the limitations of MLDBM. Basically, you can read complex
52 structures, but not write them, directly:
53  
54 $data = $Session->{complex}{data}; # Read ok.
55 $Session->{complex}{data} = $data; # Write NOT ok.
56 $Session->{complex} = {data => $data}; # Write ok, all at once.
57  
58 Please see L<MLDBM> for more information on this topic. C<$Session> can also be
59 used for the following methods and properties:
60  
61 =cut
62  
63 9 has '_is_new' => (
64 is => 'rw',
65 isa => 'Bool',
66 default => 0,
67 traits => [qw(Bool)],
68 handles => {
69 '_set_is_new' => 'set',
70 '_unset_is_new' => 'unset'
71 },
72 );
73  
74 9 has '_session_key_index' => (
75 is => 'rw',
76 isa => 'Int',
77 default => 0,
78 traits => [qw(Counter)],
79 handles => {
80 _inc_session_key_index => 'inc',
81 _reset_session_key_index => 'reset',
82 },
83 );
84  
85 has '_session_keys' => (
86 is => 'rw',
87 isa => 'ArrayRef',
88 15 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 9 traits => [qw(Array)],
90 handles => {
91 _session_keys_get => 'get',
92 },
93 );
94  
95 =head1 ATTRIBUTES
96  
97 =over
98  
99 =item $Session->{CodePage}
100  
101 Not implemented. May never be until someone needs it.
102  
103 =cut
104  
105 9 has 'CodePage' => (
106 is => 'ro',
107 isa => 'Item',
108 );
109  
110 =item $Session->{LCID}
111  
112 Not implemented. May never be until someone needs it.
113  
114 =cut
115  
116 9 has 'LCID' => (
117 is => 'ro',
118 isa => 'item',
119 );
120  
121 =item $Session->{SessionID}
122  
123 SessionID property, returns the id for the current session, which is exchanged
124 between the client and the server as a cookie.
125  
126 =cut
127  
128 9 has 'SessionID' => (
129 is => 'rw',
130 isa => 'Str',
131 );
132  
133 =item $Session->{Timeout} [= $minutes]
134  
135 Timeout property, if minutes is being assigned, sets this default timeout for
136 the user session, else returns the current session timeout.
137  
138 If a user session is inactive for the full timeout, the session is destroyed by
139 the system. No one can access the session after it times out, and the system
140 garbage collects it eventually.
141  
142 =cut
143  
144 9 has 'Timeout' => (
145 is => 'rw',
146 isa => 'Int',
147 default => 60,
148 );
149  
150 =back
151  
152 =head1 METHODS
153  
154 =over
155  
156 =item $Session->Abandon()
157  
158 The abandon method times out the session immediately. All Session data is
159 cleared in the process, just as when any session times out.
160  
161 =cut
162  
163 9 has 'IsAbandoned' => (
164 is => 'ro',
165 isa => 'Bool',
166 default => 0,
167 traits => [qw(Bool)],
168 handles => {
169 Abandon => 'set',
170 },
171 );
172  
173 =item $Session->Lock()
174  
175 Not implemented. This is a no-op. This was meant to be for performance
176 improvement, but it's not necessary.
177  
178 =cut
179  
180 # TODO: will not implement
181 1 ▶sub Lock {
Subroutine Lock: called CC=1 SCAR=0.0
Pod documented
182 1 my ( $self ) = @_;
183 1 $self->asp->c->log->warn( "\$Session->Lock has not been implemented!" );
184 1 return;
185 }
186  
187 =item $Session->UnLock()
188  
189 Not implemented. This is a no-op. This was meant to be for performance
190 improvement, but it's not necessary.
191  
192 =cut
193  
194 # TODO: will not implement
195 1 ▶sub UnLock {
Subroutine UnLock: called CC=1 SCAR=0.0
Pod documented
196 1 my ( $self ) = @_;
197 1 $self->asp->c->log->warn( "\$Session->UnLock has not been implemented!" );
198 1 return;
199 }
200  
201 =item $Session->Flush()
202  
203 Not implemented.
204  
205 =cut
206  
207 # TODO: will not implement; not part of API so just no-op
208 0 ▶sub Flush { }
Subroutine Flush: not called CC=1 SCAR=0.0
Pod documented
209  
210 # The Session is tied to Catalyst's $c->session so as to skip the storage of the
211 # $asp object
212 14 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=3 SCAR=11.0
213 14 my ( $class, $self ) = @_;
214 14 my $c = $self->asp->c;
215  
216 # By default, assume using Catalyst::Plugin::Session otherwise assume using
217 # Catalyst::Plugin::iParadigms::Session
218 14 ▶ my $session_is_valid = $c->can( 'session_is_valid' ) ? 'session_is_valid' : 'is_valid_session_id';
Branch: 1 branch
BranchTrueFalseTotal
$c->can('session_is_valid') ? : 14 0 14
219 14 ▶ unless ( $c->$session_is_valid( $c->sessionid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->$session_is_valid($c->sessionid)) 9 5 14
220 9 $self->_set_is_new;
221 9 $self->SessionID( $c->sessionid );
222 }
223 14 return $self;
224 }
225  
226 100 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
227 100 my ( $self, $key, $value ) = @_;
228 100 ▶ return $value if $key =~ /asp|_is_new|_session_key/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /asp|_is_new|_session_key/ 56 44 100
229 44 $self->asp->c->session->{$key} = $value;
230 }
231  
232 44 ▶sub FETCH {
Subroutine FETCH: called CC=5 SCAR=16.3
233 44 my ( $self, $key ) = @_;
234 44 for ( $key ) {
235 44 ▶ if ( /asp/ ) { return $self->asp }
Branch: 3 branches
BranchTrueFalseTotal
if (/asp/) { } 2 42 44
elsif (/_is_new/) { } 5 37 42
elsif (/_session_key/) { } 0 37 37
236 5 elsif ( /_is_new/ ) { return $self->_is_new }
237 0 elsif ( /_session_key/ ) {return}
238 37 else { return $self->asp->c->session->{$key} }
239 }
240 }
241  
242 6 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
243 6 my ( $self ) = @_;
244 6 $self->_session_keys( [ keys %{ $self->asp->c->session } ] );
245 6 $self->_reset_session_key_index;
246 6 $self->NEXTKEY;
247 }
248  
249 30 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=3 SCAR=11.6
250 30 my ( $self, $lastkey ) = @_;
251 30 my $key = $self->_session_keys_get( $self->_session_key_index );
252 30 $self->_inc_session_key_index;
253 30 ▶ if ( defined $key && $key =~ m/asp|_is_new|_session_key/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and $key =~ /asp|_is_new|_session_key/) { } 0 30 30
Condition66%2/3
defined $key and $key =~ /asp|_is_new|_session_key/
!ll&&!rl&&r
6240
MC/DC0%0/2
defined $key and $key =~ /asp|_is_new|_session_key/
defined $key $key =~ /asp|_is_new|_session_key/
Truth table66%2/3
A and B
A= defined $keyB= $key =~ /asp|_is_new|_session_key/
ABresult
0X0
100
111
254 0 return $self->NEXTKEY;
255 } else {
256 30 return $key;
257 }
258 }
259  
260 5 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
261 5 my ( $self, $key ) = @_;
262 5 exists $self->asp->c->session->{$key};
263 }
264  
265 4 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
266 4 my ( $self, $key ) = @_;
267 4 delete $self->asp->c->session->{$key};
268 }
269  
270 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=2 SCAR=6.9
271 1 my ( $self ) = @_;
272 1 $self->DELETE( $_ ) for ( keys %{ $self->asp->c->session } );
273 }
274  
275 0 ▶sub SCALAR {
Subroutine SCALAR: not called CC=1 SCAR=6.9
276 0 my ( $self ) = @_;
277 0 scalar %{ $self->asp->c->session };
278 }
279  
280 9 __PACKAGE__->meta->make_immutable;
281  
282 =back
283  
284 =head1 SEE ALSO
285  
286 =over
287  
288 =item * L<CatalystX::ASP::Request>
289  
290 =item * L<CatalystX::ASP::Response>
291  
292 =item * L<CatalystX::ASP::Application>
293  
294 =item * L<CatalystX::ASP::Server>
295  
296 =back
« ASP/Request.pm ↑ Summary ASP/Dispatcher.pm »