Commit | Line | Data |
bd992981 |
1 | package Plack::Session::State::Cookie; |
2 | use strict; |
3 | use warnings; |
4 | |
de784fad |
5 | our $VERSION = '0.02'; |
30cc0a71 |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
bd992981 |
8 | use parent 'Plack::Session::State'; |
9 | |
ac4892f4 |
10 | use Plack::Util::Accessor qw[ |
11 | path |
12 | domain |
13 | expires |
14 | secure |
15 | ]; |
bd992981 |
16 | |
17 | sub expire_session_id { |
18 | my ($self, $id) = @_; |
19 | $self->SUPER::expire_session_id( $id ); |
20 | $self->expires( 0 ); |
21 | } |
22 | |
ae35574f |
23 | sub get_session_id_from_request { |
bd992981 |
24 | my ($self, $request) = @_; |
ae35574f |
25 | ( $request->cookie( $self->session_key ) || return )->value; |
bd992981 |
26 | } |
27 | |
28 | sub finalize { |
29 | my ($self, $id, $response) = @_; |
30 | $response->cookies->{ $self->session_key } = +{ |
31 | value => $id, |
32 | path => ($self->path || '/'), |
05b5f99d |
33 | ( defined $self->domain ? ( domain => $self->domain ) : () ), |
34 | ( defined $self->expires ? ( expires => $self->expires ) : () ), |
35 | ( defined $self->secure ? ( secure => $self->secure ) : () ), |
bd992981 |
36 | }; |
05b5f99d |
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 ); |
bd992981 |
45 | } |
46 | |
fe1bfe7d |
47 | 1; |
ac4892f4 |
48 | |
49 | __END__ |
50 | |
51 | =pod |
52 | |
53 | =head1 NAME |
54 | |
55 | Plack::Session::State::Cookie - Basic cookie-based session state |
56 | |
3d92cf47 |
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 | |
ac4892f4 |
71 | =head1 DESCRIPTION |
72 | |
43f34c01 |
73 | This is a subclass of L<Plack::Session::State> and implements it's |
3d92cf47 |
74 | full interface. This is the default state used in |
75 | L<Plack::Middleware::Session>. |
43f34c01 |
76 | |
ac4892f4 |
77 | =head1 METHODS |
78 | |
79 | =over 4 |
80 | |
81 | =item B<new ( %params )> |
82 | |
3d92cf47 |
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 | |
ac4892f4 |
87 | =item B<path> |
88 | |
3d92cf47 |
89 | Path of the cookie, this defaults to "/"; |
90 | |
ac4892f4 |
91 | =item B<domain> |
92 | |
3d92cf47 |
93 | Domain of the cookie, if nothing is supplied then it will not |
94 | be included in the cookie. |
95 | |
ac4892f4 |
96 | =item B<expires> |
97 | |
3d92cf47 |
98 | Expiration time of the cookie, if nothing is supplied then it will |
99 | not be included in the cookie. |
100 | |
ac4892f4 |
101 | =item B<secure> |
102 | |
3d92cf47 |
103 | Secure flag for the cookie, if nothing is supplied then it will not |
104 | be included in the cookie. |
105 | |
ac4892f4 |
106 | =back |
107 | |
ac4892f4 |
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 | |