2b33edbafdb000ffa91fb2576a5ed017b410b720
[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)->cookie( $self->session_key ) || return )->value;
22 }
23
24 sub expire_session_id {
25     my ($self, $id, $res, $options) = @_;
26     $self->_set_cookie($id, $res, expires => 0);
27 }
28
29 sub finalize {
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);
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, if nothing is supplied then it will
103 not be included in the cookie.
104
105 =item B<secure>
106
107 Secure flag for the cookie, if nothing is supplied then it will not
108 be included in the cookie.
109
110 =back
111
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
124 Copyright 2009, 2010 Infinity Interactive, Inc.
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