X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-Session-State-Cookie.git;a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FSession%2FState%2FCookie.pm;h=2e7a2a13eb52508133d875de96be9b84c71c0030;hp=698063959a6630adb02721aeff8fc94769d8e3d8;hb=58b695072bcbcc0346af9406216c19c761326da8;hpb=cc88d7267f661665aca3f3dda7bb2f0342c237c9 diff --git a/lib/Catalyst/Plugin/Session/State/Cookie.pm b/lib/Catalyst/Plugin/Session/State/Cookie.pm index 6980639..2e7a2a1 100644 --- a/lib/Catalyst/Plugin/Session/State/Cookie.pm +++ b/lib/Catalyst/Plugin/Session/State/Cookie.pm @@ -1,218 +1,322 @@ -package Catalyst::Plugin::Session::FastMmap; +package Catalyst::Plugin::Session::State::Cookie; +use Moose; +use namespace::autoclean; -use strict; -use base qw/Class::Data::Inheritable Class::Accessor::Fast/; -use NEXT; -use Cache::FastMmap; -use Digest::MD5; -use URI; -use URI::Find; -use File::Temp 'tempdir'; +extends 'Catalyst::Plugin::Session::State'; -our $VERSION = '0.13'; +use MRO::Compat; +use Catalyst::Utils (); -__PACKAGE__->mk_classdata('_session'); -__PACKAGE__->mk_accessors('sessionid'); +our $VERSION = "0.17"; -=head1 NAME +has _deleted_session_id => ( is => 'rw' ); -Catalyst::Plugin::Session::FastMmap - FastMmap sessions for Catalyst +sub setup_session { + my $c = shift; -=head1 SYNOPSIS + $c->maybe::next::method(@_); - use Catalyst 'Session::FastMmap'; - - MyApp->config->{session} = { - expires => 3600, - rewrite => 1, - storage => '/tmp/session' - }; + $c->_session_plugin_config->{cookie_name} + ||= Catalyst::Utils::appprefix($c) . '_session'; +} - $c->session->{foo} = 'bar'; - print $c->sessionid; +sub extend_session_id { + my ( $c, $sid, $expires ) = @_; -=head1 DESCRIPTION + if ( my $cookie = $c->get_session_cookie ) { + $c->update_session_cookie( $c->make_session_cookie( $sid ) ); + } -C is a fast session plugin for -Catalyst that uses an mmap'ed file to act as a shared memory -interprocess cache. It is based on C. + $c->maybe::next::method( $sid, $expires ); +} +sub set_session_id { + my ( $c, $sid ) = @_; -=head2 EXTENDED METHODS + $c->update_session_cookie( $c->make_session_cookie( $sid ) ); -=over 4 + return $c->maybe::next::method($sid); +} -=item finalize +sub update_session_cookie { + my ( $c, $updated ) = @_; -=cut + unless ( $c->cookie_is_rejecting( $updated ) ) { + my $cookie_name = $c->_session_plugin_config->{cookie_name}; + $c->response->cookies->{$cookie_name} = $updated; + } +} -sub finalize { - my $c = shift; - if ( $c->config->{session}->{rewrite} ) { - my $redirect = $c->response->redirect; - $c->response->redirect( $c->uri($redirect) ) if $redirect; +sub cookie_is_rejecting { + my ( $c, $cookie ) = @_; + + if ( $cookie->{path} ) { + return 1 if index '/'.$c->request->path, $cookie->{path}; } - if ( my $sid = $c->sessionid ) { - $c->_session->set( $sid, $c->session ); - my $set = 1; - if ( my $cookie = $c->request->cookies->{session} ) { - $set = 0 if $cookie->value eq $sid; - } - if ( $set ) { - $c->response->cookies->{session} = { - value => $sid - }; + + return 0; +} + +sub make_session_cookie { + my ( $c, $sid, %attrs ) = @_; + + my $cfg = $c->_session_plugin_config; + my $cookie = { + value => $sid, + ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ), + ( $cfg->{cookie_path} ? ( path => $cfg->{cookie_path} ) : () ), + %attrs, + }; + + unless ( exists $cookie->{expires} ) { + $cookie->{expires} = $c->calculate_session_cookie_expires(); + } + + #beware: we have to accept also the old syntax "cookie_secure = true" + my $sec = $cfg->{cookie_secure} || 0; # default = 0 (not set) + $cookie->{secure} = 1 unless ( ($sec==0) || ($sec==2) ); + $cookie->{secure} = 1 if ( ($sec==2) && $c->req->secure ); + + $cookie->{httponly} = $cfg->{cookie_httponly}; + $cookie->{httponly} = 1 + unless defined $cookie->{httponly}; # default = 1 (set httponly) + + return $cookie; +} + +sub calc_expiry { # compat + my $c = shift; + $c->maybe::next::method( @_ ) || $c->calculate_session_cookie_expires( @_ ); +} + +sub calculate_session_cookie_expires { + my $c = shift; + my $cfg = $c->_session_plugin_config; + + my $value = $c->maybe::next::method(@_); + return $value if $value; + + if ( exists $cfg->{cookie_expires} ) { + if ( $cfg->{cookie_expires} > 0 ) { + return time() + $cfg->{cookie_expires}; } - if ( $c->config->{session}->{rewrite} ) { - my $finder = URI::Find->new( - sub { - my ( $uri, $orig ) = @_; - my $base = $c->request->base; - return $orig unless $orig =~ /^$base/; - return $orig if $uri->path =~ /\/-\//; - return $c->uri($orig); - } - ); - $finder->find( \$c->res->{body} ) if $c->res->body; + else { + return undef; } } - return $c->NEXT::finalize(@_); + else { + return $c->session_expires; + } } -=item prepare_action +sub get_session_cookie { + my $c = shift; -=cut + my $cookie_name = $c->_session_plugin_config->{cookie_name}; -sub prepare_action { + return $c->request->cookies->{$cookie_name}; +} + +sub get_session_id { my $c = shift; - if ( $c->request->path =~ /^(.*)\/\-\/(.+)$/ ) { - $c->request->path($1); - $c->sessionid($2); - $c->log->debug(qq/Found sessionid "$2" in path/) if $c->debug; - } - if ( my $cookie = $c->request->cookies->{session} ) { + + if ( !$c->_deleted_session_id and my $cookie = $c->get_session_cookie ) { my $sid = $cookie->value; - $c->sessionid($sid); $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug; + return $sid if $sid; } - $c->NEXT::prepare_action(@_); + + $c->maybe::next::method(@_); } -sub session { - my $c = shift; - return $c->{session} if $c->{session}; - my $sid = $c->sessionid; - if ( $sid - && $c->_session - && ( $c->{session} = $c->_session->get($sid) ) ) - { - $c->log->debug(qq/Found session "$sid"/) if $c->debug; - return $c->{session}; - } - else { - my $sid = Digest::MD5::md5_hex( time, rand, $$, 'catalyst' ); - $c->sessionid($sid); - $c->log->debug(qq/Created session "$sid"/) if $c->debug; - return $c->{session} = {}; - } +sub delete_session_id { + my ( $c, $sid ) = @_; + + $c->_deleted_session_id(1); # to prevent get_session_id from returning it + + $c->update_session_cookie( $c->make_session_cookie( $sid, expires => 0 ) ); + + $c->maybe::next::method($sid); } -=item setup +1; +__END__ + +=head1 NAME -Sets up the session cache file. +Catalyst::Plugin::Session::State::Cookie - Maintain session IDs using cookies. -=cut +=head1 SYNOPSIS + + use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/; -sub setup { - my $self = shift; - $self->config->{session}->{storage} ||= '/tmp/session'; - $self->config->{session}->{expires} ||= 60 * 60 * 24; - $self->config->{session}->{rewrite} ||= 0; +=head1 DESCRIPTION - $self->_session( - Cache::FastMmap->new( - share_file => $self->config->{session}->{storage}, - expire_time => $self->config->{session}->{expires} - ) - ); +In order for L to work the session ID needs to be +stored on the client, and the session data needs to be stored on the server. - return $self->NEXT::setup(@_); -} +This plugin stores the session ID on the client using the cookie mechanism. + +=head1 METHODS + +=over 4 + +=item make_session_cookie + +Returns a hash reference with the default values for new cookies. + +=item update_session_cookie $hash_ref + +Sets the cookie based on C in the response object. + +=item calc_expiry + +=item calculate_session_cookie_expires + +=item cookie_is_rejecting + +=item delete_session_id + +=item extend_session_id + +=item get_session_cookie + +=item get_session_id + +=item set_session_id =back -=head2 METHODS +=head1 EXTENDED METHODS =over 4 -=item session +=item prepare_cookies -=item uri +Will restore if an appropriate cookie is found. -Extends an uri with session id if needed. +=item finalize_cookies - my $uri = $c->uri('http://localhost/foo'); +Will set a cookie called C if it doesn't exist or if its value is not +the current session id. -=cut +=item setup_session -sub uri { - my ( $c, $uri ) = @_; - if ( my $sid = $c->sessionid ) { - $uri = URI->new($uri); - my $path = $uri->path; - $path .= '/' unless $path =~ /\/$/; - $uri->path( $path . "-/$sid" ); - return $uri->as_string; - } - return $uri; -} +Will set the C parameter to its default value if it isn't set. =back -=head2 CONFIG OPTIONS +=head1 CONFIGURATION =over 4 -=item rewrite +=item cookie_name + +The name of the cookie to store (defaults to C). + +=item cookie_domain + +The name of the domain to store in the cookie (defaults to current host) + +=item cookie_expires + +Number of seconds from now you want to elapse before cookie will expire. +Set to 0 to create a session cookie, ie one which will die when the +user's browser is shut down. -If set to a true value sessions are automatically stored in the url; -defaults to false. +=item cookie_secure -=item storage +If this attribute B the cookie will not have the secure flag. -Specifies the file to be used for the sharing of session data; -defaults to C. +If this attribute B (or true for backward compatibility) - the cookie +sent by the server to the client will get the secure flag that tells the browser +to send this cookie back to the server only via HTTPS. -Note that the file will be created with mode 0640, which means that it -will only be writeable by processes running with the same uid as the -process that creates the file. If this may be a problem, for example -if you may try to debug the program as one user and run it as another, -specify a filename like C<< /tmp/session-$> >>, which includes the -UID of the process in the filename. +If this attribute B then the cookie will get the secure flag only if +the request that caused cookie generation was sent over https (this option is +not good if you are mixing https and http in your application). +Default value is 0. -=item expires +=item cookie_httponly -Specifies the session expiry time in seconds; defaults to 86,400, -i.e. one day. +If this attribute B, the cookie will not have HTTPOnly flag. + +If this attribute B, the cookie will got HTTPOnly flag that should +prevent client side Javascript accessing the cookie value - this makes some +sort of session hijacking attacks significantly harder. Unfortunately not all +browsers support this flag (MSIE 6 SP1+, Firefox 3.0.0.6+, Opera 9.5+); if +a browser is not aware of HTTPOnly the flag will be ignored. + +Default value is 1. + +Note1: Many people are confused by the name "HTTPOnly" - it B +that this cookie works only over HTTP and not over HTTPS. + +Note2: This parameter requires Catalyst::Runtime 5.80005 otherwise is skipped. + +=item cookie_path + +The path of the request url where cookie should be baked. =back +For example, you could stick this in MyApp.pm: + + __PACKAGE__->config( 'Plugin::Session' => { + cookie_domain => '.mydomain.com', + }); + +=head1 CAVEATS + +Sessions have to be created before the first write to be saved. For example: + + sub action : Local { + my ( $self, $c ) = @_; + $c->res->write("foo"); + $c->session( ... ); + ... + } + +Will cause a session ID to not be set, because by the time a session is +actually created the headers have already been sent to the client. + =head1 SEE ALSO -L, L. +L, L. -=head1 AUTHOR +=head1 AUTHORS -Sebastian Riedel ECE, -Marcus Ramberg ECE, -Andrew Ford ECE +Yuval Kogman + +=head1 CONTRIBUTORS + +This module is derived from L code, and +has been heavily modified since. + +Andrew Ford + +Andy Grundman + +Christian Hansen + +Marcus Ramberg + +Jonathan Rockway + +Sebastian Riedel + +Florian Ragwitz =head1 COPYRIGHT +Copyright (c) 2005 - 2009 +the Catalyst::Plugin::Session::State::Cookie L and L +as listed above. + +=head1 LICENSE + This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =cut - -1;