36639dbf9de655c8517d4d01003c999df24fda63
[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.03';
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use parent 'Plack::Session::State';
9 use Plack::Request;
10 use Plack::Response;
11
12 use Plack::Util::Accessor qw[
13     path
14     domain
15     expires
16     secure
17 ];
18
19 sub get_session_id {
20     my ($self, $env) = @_;
21     Plack::Request->new($env)->cookies->{$self->session_key};
22 }
23
24 sub expire_session_id {
25     my ($self, $id, $res, $options) = @_;
26     $self->_set_cookie($id, $res, expires => time);
27 }
28
29 sub finalize {
30     my ($self, $id, $res, $options) = @_;
31     $self->_set_cookie($id, $res, (defined $self->expires ? (expires => time + $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);
39     $response->cookies->{ $self->session_key } = +{
40         value => $id,
41         path  => ($self->path || '/'),
42         ( defined $self->domain  ? ( domain  => $self->domain  ) : () ),
43         ( defined $self->secure  ? ( secure  => $self->secure  ) : () ),
44         %options,
45     };
46
47     my $final_r = $response->finalize;
48     $res->[1] = $final_r->[1]; # headers
49 }
50
51 1;
52
53 __END__
54
55 =pod
56
57 =head1 NAME
58
59 Plack::Session::State::Cookie - Basic cookie-based session state
60
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
75 =head1 DESCRIPTION
76
77 This is a subclass of L<Plack::Session::State> and implements it's
78 full interface. This is the default state used in
79 L<Plack::Middleware::Session>.
80
81 =head1 METHODS
82
83 =over 4
84
85 =item B<new ( %params )>
86
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
91 =item B<path>
92
93 Path of the cookie, this defaults to "/";
94
95 =item B<domain>
96
97 Domain of the cookie, if nothing is supplied then it will not
98 be included in the cookie.
99
100 =item B<expires>
101
102 Expiration time of the cookie in seconds, if nothing is supplied then
103 it will not be included in the cookie, which means the session expires
104 per browser session.
105
106 =item B<secure>
107
108 Secure flag for the cookie, if nothing is supplied then it will not
109 be included in the cookie.
110
111 =back
112
113 =head1 BUGS
114
115 All complex software has bugs lurking in it, and this module is no
116 exception. If you find a bug please either email me, or add the bug
117 to cpan-RT.
118
119 =head1 AUTHOR
120
121 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
122
123 =head1 COPYRIGHT AND LICENSE
124
125 Copyright 2009, 2010 Infinity Interactive, Inc.
126
127 L<http://www.iinteractive.com>
128
129 This library is free software; you can redistribute it and/or modify
130 it under the same terms as Perl itself.
131
132 =cut
133
134