1 package Catalyst::Plugin::Session::State::Cookie;
3 use namespace::autoclean;
5 extends 'Catalyst::Plugin::Session::State';
8 use Catalyst::Utils ();
10 our $VERSION = "0.15";
12 has _deleted_session_id => ( is => 'rw' );
14 # FIXME - Can go away when we dep on new Session..
15 sub _session_plugin_config {
17 my $key = $c->config->{'Plugin::Session'} ?
18 'Plugin::Session' : 'session';
19 $c->config->{$key} ||= {};
25 $c->maybe::next::method(@_);
27 $c->_session_plugin_config->{cookie_name}
28 ||= Catalyst::Utils::appprefix($c) . '_session';
31 sub extend_session_id {
32 my ( $c, $sid, $expires ) = @_;
34 if ( my $cookie = $c->get_session_cookie ) {
35 $c->update_session_cookie( $c->make_session_cookie( $sid ) );
38 $c->maybe::next::method( $sid, $expires );
44 $c->update_session_cookie( $c->make_session_cookie( $sid ) );
46 return $c->maybe::next::method($sid);
49 sub update_session_cookie {
50 my ( $c, $updated ) = @_;
52 unless ( $c->cookie_is_rejecting( $updated ) ) {
53 my $cookie_name = $c->_session_plugin_config->{cookie_name};
54 $c->response->cookies->{$cookie_name} = $updated;
58 sub cookie_is_rejecting {
59 my ( $c, $cookie ) = @_;
61 if ( $cookie->{path} ) {
62 return 1 if index '/'.$c->request->path, $cookie->{path};
68 sub make_session_cookie {
69 my ( $c, $sid, %attrs ) = @_;
71 my $cfg = $c->_session_plugin_config;
74 ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ),
75 ( $cfg->{cookie_path} ? ( path => $cfg->{cookie_path} ) : () ),
79 unless ( exists $cookie->{expires} ) {
80 $cookie->{expires} = $c->calculate_session_cookie_expires();
83 #beware: we have to accept also the old syntax "cookie_secure = true"
84 my $sec = $cfg->{cookie_secure} || 0; # default = 0 (not set)
85 $cookie->{secure} = 1 unless ( ($sec==0) || ($sec==2) );
86 $cookie->{secure} = 1 if ( ($sec==2) && $c->req->secure );
88 $cookie->{httponly} = $cfg->{cookie_httponly};
89 $cookie->{httponly} = 1
90 unless defined $cookie->{httponly}; # default = 1 (set httponly)
95 sub calc_expiry { # compat
97 $c->maybe::next::method( @_ ) || $c->calculate_session_cookie_expires( @_ );
100 sub calculate_session_cookie_expires {
102 my $cfg = $c->_session_plugin_config;
104 my $value = $c->maybe::next::method(@_);
105 return $value if $value;
107 if ( exists $cfg->{cookie_expires} ) {
108 if ( $cfg->{cookie_expires} > 0 ) {
109 return time() + $cfg->{cookie_expires};
116 return $c->session_expires;
120 sub get_session_cookie {
123 my $cookie_name = $c->_session_plugin_config->{cookie_name};
125 return $c->request->cookies->{$cookie_name};
131 if ( !$c->_deleted_session_id and my $cookie = $c->get_session_cookie ) {
132 my $sid = $cookie->value;
133 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
137 $c->maybe::next::method(@_);
140 sub delete_session_id {
141 my ( $c, $sid ) = @_;
143 $c->_deleted_session_id(1); # to prevent get_session_id from returning it
145 $c->update_session_cookie( $c->make_session_cookie( $sid, expires => 0 ) );
147 $c->maybe::next::method($sid);
158 Catalyst::Plugin::Session::State::Cookie - Maintain session IDs using cookies.
162 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
166 In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
167 stored on the client, and the session data needs to be stored on the server.
169 This plugin stores the session ID on the client using the cookie mechanism.
175 =item make_session_cookie
177 Returns a hash reference with the default values for new cookies.
179 =item update_session_cookie $hash_ref
181 Sets the cookie based on C<cookie_name> in the response object.
185 =item calculate_session_cookie_expires
187 =item cookie_is_rejecting
189 =item delete_session_id
191 =item extend_session_id
193 =item get_session_cookie
201 =head1 EXTENDED METHODS
205 =item prepare_cookies
207 Will restore if an appropriate cookie is found.
209 =item finalize_cookies
211 Will set a cookie called C<session> if it doesn't exist or if its value is not
212 the current session id.
216 Will set the C<cookie_name> parameter to its default value if it isn't set.
226 The name of the cookie to store (defaults to C<Catalyst::Utils::apprefix($c) . '_session'>).
230 The name of the domain to store in the cookie (defaults to current host)
234 Number of seconds from now you want to elapse before cookie will expire.
235 Set to 0 to create a session cookie, ie one which will die when the
236 user's browser is shut down.
240 If this attribute B<set to 0> the cookie will not have the secure flag.
242 If this attribute B<set to 1> (or true for backward compatibility) - the cookie
243 send by the server to the client will got the secure flag that tells the browser
244 to send this cookies back to the server only via HTTPS.
246 If this attribute B<set to 2> then the cookie will got the secure flag only if
247 the request that caused cookie generation was sent over https (this option is
248 not good if you are mixing https and http in you application).
252 =item cookie_httponly
254 If this attribute B<set to 0>, the cookie will not have HTTPOnly flag.
256 If this attribute B<set to 1>, the cookie will got HTTPOnly flag that should
257 prevent client side Javascript accessing the cookie value - this makes some
258 sort of session hijacking attacks significantly harder. Unfortunately not all
259 browsers support this flag (MSIE 6 SP1+, Firefox 3.0.0.6+, Opera 9.5+); if
260 a browser is not aware of HTTPOnly the flag will be ignored.
264 Note1: Many peole are confused by the name "HTTPOnly" - it B<does not mean>
265 that this cookie works only over HTTP and not over HTTPS.
267 Note2: This paramater requires Catalyst::Runtime 5.80005 otherwise is skipped.
271 The path of the request url where cookie should be baked.
275 For example, you could stick this in MyApp.pm:
277 __PACKAGE__->config( 'Plugin::Session' => {
278 cookie_domain => '.mydomain.com',
283 Sessions have to be created before the first write to be saved. For example:
286 my ( $self, $c ) = @_;
287 $c->res->write("foo");
292 Will cause a session ID to not be set, because by the time a session is
293 actually created the headers have already been sent to the client.
297 L<Catalyst>, L<Catalyst::Plugin::Session>.
301 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
305 This module is derived from L<Catalyst::Plugin::Session::FastMmap> code, and
306 has been heavily modified since.
312 Jonathan Rockway E<lt>jrockway@cpan.orgE<gt>
317 Copyright (c) 2005, Yuval Kogman C<< <nothingmuch@woobling.org> >>
321 This program is free software, you can redistribute it and/or modify it
322 under the same terms as Perl itself.