Test for cookie expiration and extension behavior, add cookie_expires, disable condit...
[catagits/Catalyst-Plugin-Session-State-Cookie.git] / lib / Catalyst / Plugin / Session / State / Cookie.pm
CommitLineData
1a776a0c 1package Catalyst::Plugin::Session::State::Cookie;
2use base qw/Catalyst::Plugin::Session::State/;
bf2bce67 3
4use strict;
1a776a0c 5use warnings;
bf2bce67 6
1a776a0c 7use NEXT;
bf2bce67 8
81eb8ebf 9our $VERSION = "0.01";
10
5e50008f 11sub setup_session {
20e33791 12 my $c = shift;
5e50008f 13
20e33791 14 $c->NEXT::setup_session(@_);
5e50008f 15
20e33791 16 $c->config->{session}{cookie_name} ||= "session";
5e50008f 17}
18
d52e5079 19sub finalize_cookies {
b2f8df5e 20 my $c = shift;
1a776a0c 21
db1cda22 22 if ( $c->sessionid) {
23 $c->update_session_cookie( $c->make_session_cookie );
24 }
25
26 return $c->NEXT::finalize_cookies(@_);
27}
28
29sub update_session_cookie {
30 my ( $c, $updated ) = @_;
20e33791 31 my $cookie_name = $c->config->{session}{cookie_name};
db1cda22 32 $c->response->cookies->{$cookie_name} = $updated;
33}
5e50008f 34
db1cda22 35sub make_session_cookie {
36 my $c = shift;
1a776a0c 37
db1cda22 38 my $cfg = $c->config->{session};
39 my $cookie = {
40 value => $c->sessionid,
41 ($cfg->{cookie_domain} ? (domain => $cfg->{cookie_domain}) : ()),
42 };
43
44 if ( exists $cfg->{cookie_expires} ) {
45 if ( my $ttl = $cfg->{cookie_expires} ) {
46 $cookie->{expires} = time() + $ttl;
47 } # else { cookie is non-persistent }
48 } else {
49 $cookie->{expires} = $c->session->{__expires};
50 }
51
52 return $cookie;
bf2bce67 53}
54
1a776a0c 55sub prepare_cookies {
bf2bce67 56 my $c = shift;
1a776a0c 57
7acb108b 58 my $ret = $c->NEXT::prepare_cookies(@_);
59
20e33791 60 my $cookie_name = $c->config->{session}{cookie_name};
5e50008f 61
62 if ( my $cookie = $c->request->cookies->{$cookie_name} ) {
bf2bce67 63 my $sid = $cookie->value;
64 $c->sessionid($sid);
65 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
66 }
bf2bce67 67
20e33791 68 return $ret;
bf2bce67 69}
70
1a776a0c 71__PACKAGE__
57dbf608 72
1a776a0c 73__END__
bf2bce67 74
1a776a0c 75=pod
b2f8df5e 76
1a776a0c 77=head1 NAME
bf2bce67 78
1a776a0c 79Catalyst::Plugin::Session::State::Cookie - A session ID
bf2bce67 80
1a776a0c 81=head1 SYNOPSIS
bf2bce67 82
20e33791 83 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
bf2bce67 84
1a776a0c 85=head1 DESCRIPTION
bf2bce67 86
1a776a0c 87In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
88stored on the client, and the session data needs to be stored on the server.
bf2bce67 89
1a776a0c 90This plugin stores the session ID on the client using the cookie mechanism.
57dbf608 91
1a776a0c 92=head1 EXTENDED METHODS
58c05d1a 93
57dbf608 94=over 4
95
1a776a0c 96=item prepare_cookies
57dbf608 97
1a776a0c 98Will restore if an appropriate cookie is found.
58c05d1a 99
d52e5079 100=item finalize_cookies
58c05d1a 101
19c2baa1 102Will set a cookie called C<session> if it doesn't exist or if it's value is not
103the current session id.
104
105=item setup_session
106
107Will set the C<cookie_name> parameter to it's default value if it isn't set.
58c05d1a 108
57dbf608 109=back
58c05d1a 110
5e50008f 111=head1 CONFIGURATION
112
113=over 4
114
115=item cookie_name
116
117The name of the cookie to store (defaults to C<session>).
118
41b4b15c 119=item cookie_domain
120
121The name of the domain to store in the cookie (defaults to current host)
122
5e50008f 123=back
124
db1cda22 125=item CAVEATS
126
127Sessions have to be created before the first write to be saved. For example:
128
129 sub action : Local {
130 my ( $self, $c ) = @_;
131 $c->res->write("foo");
132 $c->session( ... );
133 ...
134 }
135
136Will cause a session ID to not be set, because by the time a session is
137actually created the headers have already been sent to the client.
138
bf2bce67 139=head1 SEE ALSO
140
1a776a0c 141L<Catalyst>, L<Catalyst::Plugin::Session>.
bf2bce67 142
143=head1 AUTHOR
144
57dbf608 145Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
146Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
1a776a0c 147Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>,
148Yuval Kogman E<lt>C<nothingmuch@woobling.org>E<gt>
bf2bce67 149
150=head1 COPYRIGHT
151
bfeb5ca0 152This program is free software, you can redistribute it and/or modify it
153under the same terms as Perl itself.
bf2bce67 154
155=cut
156
1571;