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