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