exclude build.pl
[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;
74586782 8use Catalyst::Utils ();
bf2bce67 9
3e8b97ed 10our $VERSION = "0.04";
81eb8ebf 11
5e50008f 12sub setup_session {
20e33791 13 my $c = shift;
5e50008f 14
20e33791 15 $c->NEXT::setup_session(@_);
2bde9162 16
7022ec4c 17 $c->config->{session}{cookie_name}
18 ||= Catalyst::Utils::appprefix($c) . '_session';
5e50008f 19}
20
0ff18b66 21sub extend_session_id {
22 my ( $c, $sid, $expires ) = @_;
1a776a0c 23
2bde9162 24 if ( my $cookie = $c->get_session_cookie ) {
0ff18b66 25 $c->update_session_cookie( $c->make_session_cookie( $sid ) );
58730edc 26 }
db1cda22 27
0ff18b66 28 $c->NEXT::extend_session_id( @_ );
2bde9162 29}
30
31sub set_session_id {
32 my ( $c, $sid ) = @_;
33
34 $c->update_session_cookie( $c->make_session_cookie( $sid ) );
35
36 return $c->NEXT::set_session_id(@_);
db1cda22 37}
38
39sub update_session_cookie {
58730edc 40 my ( $c, $updated ) = @_;
20e33791 41 my $cookie_name = $c->config->{session}{cookie_name};
58730edc 42 $c->response->cookies->{$cookie_name} = $updated;
db1cda22 43}
5e50008f 44
db1cda22 45sub make_session_cookie {
2bde9162 46 my ( $c, $sid, %attrs ) = @_;
58730edc 47
48 my $cfg = $c->config->{session};
49 my $cookie = {
2bde9162 50 value => $sid,
58730edc 51 ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ),
df55e818 52 %attrs,
58730edc 53 };
54
2bde9162 55 unless ( exists $cookie->{expires} ) {
56 $cookie->{expires} = $c->calculate_session_cookie_expires();
57 }
1e986fd5 58
fc4b9d6d 59 $cookie->{secure} = 1 if $cfg->{cookie_secure};
60
1e986fd5 61 return $cookie;
62}
63
2bde9162 64sub calc_expiry { # compat
65 my $c = shift;
66 $c->NEXT::calc_expiry( @_ ) || $c->calculate_session_cookie_expires( @_ );
67}
68
69sub calculate_session_cookie_expires {
70 my $c = shift;
71 my $cfg = $c->config->{session};
72
73 my $value = $c->NEXT::calculate_session_cookie_expires(@_);
1e986fd5 74 return $value if $value;
2bde9162 75
58730edc 76 if ( exists $cfg->{cookie_expires} ) {
7022ec4c 77 if ( $cfg->{cookie_expires} > 0 ) {
1e986fd5 78 return time() + $cfg->{cookie_expires};
7022ec4c 79 }
80 else {
1e986fd5 81 return undef;
7022ec4c 82 }
58730edc 83 }
84 else {
2bde9162 85 return $c->session_expires;
58730edc 86 }
bf2bce67 87}
88
2bde9162 89sub get_session_cookie {
bf2bce67 90 my $c = shift;
1a776a0c 91
20e33791 92 my $cookie_name = $c->config->{session}{cookie_name};
5e50008f 93
2bde9162 94 return $c->request->cookies->{$cookie_name};
95}
96
97sub get_session_id {
98 my $c = shift;
99
100 if ( my $cookie = $c->get_session_cookie ) {
bf2bce67 101 my $sid = $cookie->value;
bf2bce67 102 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
2bde9162 103 return $sid if $sid;
bf2bce67 104 }
bf2bce67 105
2bde9162 106 $c->NEXT::get_session_id(@_);
107}
108
109sub delete_session_id {
df55e818 110 my ( $c, $sid ) = @_;
111
112 $c->update_session_cookie( $c->make_session_cookie( $sid, expires => 0 ) );
113
114 $c->NEXT::delete_session_id($sid);
bf2bce67 115}
116
1a776a0c 117__PACKAGE__
57dbf608 118
1a776a0c 119__END__
bf2bce67 120
1a776a0c 121=pod
b2f8df5e 122
1a776a0c 123=head1 NAME
bf2bce67 124
75d3560d 125Catalyst::Plugin::Session::State::Cookie - Maintain session IDs using cookies.
bf2bce67 126
1a776a0c 127=head1 SYNOPSIS
bf2bce67 128
20e33791 129 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
bf2bce67 130
1a776a0c 131=head1 DESCRIPTION
bf2bce67 132
1a776a0c 133In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
134stored on the client, and the session data needs to be stored on the server.
bf2bce67 135
1a776a0c 136This plugin stores the session ID on the client using the cookie mechanism.
57dbf608 137
724a6173 138=head1 METHODS
139
140=over 4
141
142=item make_session_cookie
143
144Returns a hash reference with the default values for new cookies.
145
146=item update_session_cookie $hash_ref
147
148Sets the cookie based on C<cookie_name> in the response object.
149
150=back
151
1a776a0c 152=head1 EXTENDED METHODS
58c05d1a 153
57dbf608 154=over 4
155
1a776a0c 156=item prepare_cookies
57dbf608 157
1a776a0c 158Will restore if an appropriate cookie is found.
58c05d1a 159
d52e5079 160=item finalize_cookies
58c05d1a 161
19c2baa1 162Will set a cookie called C<session> if it doesn't exist or if it's value is not
163the current session id.
164
165=item setup_session
166
167Will set the C<cookie_name> parameter to it's default value if it isn't set.
58c05d1a 168
57dbf608 169=back
58c05d1a 170
5e50008f 171=head1 CONFIGURATION
172
173=over 4
174
175=item cookie_name
176
ae33e13f 177The name of the cookie to store (defaults to C<Catalyst::Utils::apprefix($c) . '_session'>).
5e50008f 178
41b4b15c 179=item cookie_domain
180
181The name of the domain to store in the cookie (defaults to current host)
182
7022ec4c 183=item cookie_expires
184
185Number of seconds from now you want to elapse before cookie will expire.
186Set to 0 to create a session cookie, ie one which will die when the
187user's browser is shut down.
188
fc4b9d6d 189=item cookie_secure
190
191If this attribute set true, the cookie will only be sent via HTTPS.
192
5e50008f 193=back
194
724a6173 195=head1 CAVEATS
db1cda22 196
197Sessions have to be created before the first write to be saved. For example:
198
199 sub action : Local {
200 my ( $self, $c ) = @_;
201 $c->res->write("foo");
202 $c->session( ... );
203 ...
204 }
205
206Will cause a session ID to not be set, because by the time a session is
207actually created the headers have already been sent to the client.
208
bf2bce67 209=head1 SEE ALSO
210
1a776a0c 211L<Catalyst>, L<Catalyst::Plugin::Session>.
bf2bce67 212
47f47da5 213=head1 AUTHORS
bf2bce67 214
47f47da5 215This module is derived from L<Catalyst::Plugin::Session::FastMmap> code, and
216has been heavily modified since.
217
218Andrew Ford
219Andy Grundman
220Christian Hansen
221Yuval Kogman, C<nothingmuch@woobling.org>
222Marcus Ramberg
223Sebastian Riedel
bf2bce67 224
225=head1 COPYRIGHT
226
bfeb5ca0 227This program is free software, you can redistribute it and/or modify it
228under the same terms as Perl itself.
bf2bce67 229
230=cut
231
2321;