check wrong format request session_id
[catagits/Web-Session.git] / lib / Plack / Session / State / Cookie.pm
1 package Plack::Session::State::Cookie;
2 use strict;
3 use warnings;
4
5 use parent 'Plack::Session::State';
6
7 use Plack::Util::Accessor qw[
8     path
9     domain
10     expires
11     secure
12 ];
13
14 sub expire_session_id {
15     my ($self, $id) = @_;
16     $self->SUPER::expire_session_id( $id );
17     $self->expires( 0 );
18 }
19
20 sub get_request_session_id {
21     my ($self, $request ) = @_;
22     ($request->cookie( $self->session_key ) || return )->value;
23 }
24
25 sub extract {
26     my ($self, $request) = @_;
27     $self->check_expired( $self->get_request_session_id($request) || return );
28 }
29
30 sub finalize {
31     my ($self, $id, $response) = @_;
32     $response->cookies->{ $self->session_key } = +{
33         value => $id,
34         path  => ($self->path || '/'),
35         ( defined $self->domain  ? ( domain  => $self->domain  ) : () ),
36         ( defined $self->expires ? ( expires => $self->expires ) : () ),
37         ( defined $self->secure  ? ( secure  => $self->secure  ) : () ),
38     };
39
40     # clear the expires after
41     # finalization if the session
42     # has been expired - SL
43     $self->expires( undef )
44         if defined $self->expires
45         && $self->expires == 0
46         && $self->is_session_expired( $id );
47 }
48
49 1;
50
51 __END__
52
53 =pod
54
55 =head1 NAME
56
57 Plack::Session::State::Cookie - Basic cookie-based session state
58
59 =head1 SYNOPSIS
60
61   use Plack::Builder;
62   use Plack::Middleware::Session;
63
64   my $app = sub {
65       return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
66   };
67
68   builder {
69       enable 'Session'; # Cookie is the default state
70       $app;
71   };
72
73 =head1 DESCRIPTION
74
75 This is a subclass of L<Plack::Session::State> and implements it's
76 full interface. This is the default state used in
77 L<Plack::Middleware::Session>.
78
79 =head1 METHODS
80
81 =over 4
82
83 =item B<new ( %params )>
84
85 The C<%params> can include I<path>, I<domain>, I<expires> and
86 I<secure> options, as well as all the options accepted by
87 L<Plack::Session::Store>.
88
89 =item B<path>
90
91 Path of the cookie, this defaults to "/";
92
93 =item B<domain>
94
95 Domain of the cookie, if nothing is supplied then it will not
96 be included in the cookie.
97
98 =item B<expires>
99
100 Expiration time of the cookie, if nothing is supplied then it will
101 not be included in the cookie.
102
103 =item B<secure>
104
105 Secure flag for the cookie, if nothing is supplied then it will not
106 be included in the cookie.
107
108 =back
109
110 =head1 BUGS
111
112 All complex software has bugs lurking in it, and this module is no
113 exception. If you find a bug please either email me, or add the bug
114 to cpan-RT.
115
116 =head1 AUTHOR
117
118 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
119
120 =head1 COPYRIGHT AND LICENSE
121
122 Copyright 2009 Infinity Interactive, Inc.
123
124 L<http://www.iinteractive.com>
125
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself.
128
129 =cut
130
131