Accept expires as seconds since new Plack::Response ditches CGI::Simple::Cookie.
[catagits/Web-Session.git] / lib / Plack / Session / State / Cookie.pm
CommitLineData
bd992981 1package Plack::Session::State::Cookie;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
bd992981 8use parent 'Plack::Session::State';
92edbddf 9use Plack::Request;
10use Plack::Response;
bd992981 11
ac4892f4 12use Plack::Util::Accessor qw[
13 path
14 domain
15 expires
16 secure
17];
bd992981 18
4a0cb5a0 19sub get_session_id {
92edbddf 20 my ($self, $env) = @_;
2c5f34e8 21 Plack::Request->new($env)->cookies->{$self->session_key};
bd992981 22}
23
caf3bd90 24sub expire_session_id {
92edbddf 25 my ($self, $id, $res, $options) = @_;
2c5f34e8 26 $self->_set_cookie($id, $res, expires => time);
caf3bd90 27}
28
bd992981 29sub finalize {
92edbddf 30 my ($self, $id, $res, $options) = @_;
ee51674d 31 $self->_set_cookie($id, $res, (defined $self->expires ? (expires => time + $self->expires) : ()));
92edbddf 32}
33
34sub _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 511;
ac4892f4 52
53__END__
54
55=pod
56
57=head1 NAME
58
59Plack::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 77This is a subclass of L<Plack::Session::State> and implements it's
3d92cf47 78full interface. This is the default state used in
79L<Plack::Middleware::Session>.
43f34c01 80
ac4892f4 81=head1 METHODS
82
83=over 4
84
85=item B<new ( %params )>
86
3d92cf47 87The C<%params> can include I<path>, I<domain>, I<expires> and
88I<secure> options, as well as all the options accepted by
89L<Plack::Session::Store>.
90
ac4892f4 91=item B<path>
92
3d92cf47 93Path of the cookie, this defaults to "/";
94
ac4892f4 95=item B<domain>
96
3d92cf47 97Domain of the cookie, if nothing is supplied then it will not
98be included in the cookie.
99
ac4892f4 100=item B<expires>
101
ee51674d 102Expiration time of the cookie in seconds, if nothing is supplied then
103it will not be included in the cookie, which means the session expires
104per browser session.
3d92cf47 105
ac4892f4 106=item B<secure>
107
3d92cf47 108Secure flag for the cookie, if nothing is supplied then it will not
109be included in the cookie.
110
ac4892f4 111=back
112
ac4892f4 113=head1 BUGS
114
115All complex software has bugs lurking in it, and this module is no
116exception. If you find a bug please either email me, or add the bug
117to cpan-RT.
118
119=head1 AUTHOR
120
121Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
122
123=head1 COPYRIGHT AND LICENSE
124
000c696e 125Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 126
127L<http://www.iinteractive.com>
128
129This library is free software; you can redistribute it and/or modify
130it under the same terms as Perl itself.
131
132=cut
133
134