Commit | Line | Data |
bd992981 |
1 | package Plack::Session::State::Cookie; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use parent 'Plack::Session::State'; |
6 | |
ac4892f4 |
7 | use Plack::Util::Accessor qw[ |
8 | path |
9 | domain |
10 | expires |
11 | secure |
12 | ]; |
bd992981 |
13 | |
14 | sub expire_session_id { |
15 | my ($self, $id) = @_; |
16 | $self->SUPER::expire_session_id( $id ); |
17 | $self->expires( 0 ); |
18 | } |
19 | |
56b9910a |
20 | sub get_request_session_id { |
21 | my ($self, $request ) = @_; |
22 | ($request->cookie( $self->session_key ) || return )->value; |
23 | } |
24 | |
bd992981 |
25 | sub extract { |
26 | my ($self, $request) = @_; |
56b9910a |
27 | $self->check_expired( $self->get_request_session_id($request) || return ); |
bd992981 |
28 | } |
29 | |
30 | sub 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 |
49 | 1; |
ac4892f4 |
50 | |
51 | __END__ |
52 | |
53 | =pod |
54 | |
55 | =head1 NAME |
56 | |
57 | Plack::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 |
75 | This is a subclass of L<Plack::Session::State> and implements it's |
3d92cf47 |
76 | full interface. This is the default state used in |
77 | L<Plack::Middleware::Session>. |
43f34c01 |
78 | |
ac4892f4 |
79 | =head1 METHODS |
80 | |
81 | =over 4 |
82 | |
83 | =item B<new ( %params )> |
84 | |
3d92cf47 |
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 | |
ac4892f4 |
89 | =item B<path> |
90 | |
3d92cf47 |
91 | Path of the cookie, this defaults to "/"; |
92 | |
ac4892f4 |
93 | =item B<domain> |
94 | |
3d92cf47 |
95 | Domain of the cookie, if nothing is supplied then it will not |
96 | be included in the cookie. |
97 | |
ac4892f4 |
98 | =item B<expires> |
99 | |
3d92cf47 |
100 | Expiration time of the cookie, if nothing is supplied then it will |
101 | not be included in the cookie. |
102 | |
ac4892f4 |
103 | =item B<secure> |
104 | |
3d92cf47 |
105 | Secure flag for the cookie, if nothing is supplied then it will not |
106 | be included in the cookie. |
107 | |
ac4892f4 |
108 | =back |
109 | |
ac4892f4 |
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 | |