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