Session, fixed bug in algorithm detection, it was always choosing SHA-256
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session.pm
index e4d90d8..ae79a8e 100644 (file)
@@ -10,12 +10,11 @@ use NEXT;
 use Catalyst::Exception ();
 use Digest              ();
 use overload            ();
-use List::Util          ();
 
-our $VERSION = "0.01";
+our $VERSION = "0.02";
 
 BEGIN {
-    __PACKAGE__->mk_accessors(qw/sessionid session_delete_reason/);
+    __PACKAGE__->mk_accessors(qw/_sessionid _session _session_delete_reason _flash _flash_stale_keys/);
 }
 
 sub setup {
@@ -58,50 +57,113 @@ sub setup_session {
     $c->NEXT::setup_session();
 }
 
-sub finalize {
+sub prepare_action {
     my $c = shift;
 
-    if ( $c->{session} ) {
-
-        # all sessions are extended at the end of the request
-        my $now = time;
-        @{ $c->{session} }{qw/__updated __expires/} =
-          ( $now, $c->config->{session}{expires} + $now );
-        $c->store_session_data( $c->sessionid, $c->{session} );
+    if ( $c->config->{session}{flash_to_stash} and $c->_sessionid and my $flash_data = $c->flash ) {
+        @{ $c->stash }{ keys %$flash_data } = values %$flash_data;
     }
 
+    $c->NEXT::prepare_action(@_);
+}
+
+sub finalize {
+    my $c = shift;
+
+    $c->_save_session;
+    $c->_save_flash;
+
     $c->NEXT::finalize(@_);
 }
 
-sub prepare_action {
+sub _save_session {
     my $c = shift;
+    
+    if ( my $sid = $c->_sessionid ) {
+        if ( my $session_data = $c->_session ) {
 
-    my $ret = $c->NEXT::prepare_action;
+            # all sessions are extended at the end of the request
+            my $now = time;
+            @{ $session_data }{qw/__updated __expires/} =
+              ( $now, $c->config->{session}{expires} + $now );
 
-    my $sid = $c->sessionid || return;
+            $c->store_session_data( "session:$sid", $session_data );
+        }
+    }
+}
 
-    $c->log->debug(qq/Found session "$sid"/) if $c->debug;
+sub _save_flash {
+    my $c = shift;
+
+    if ( my $sid = $c->_sessionid ) {
+        if ( my $flash_data = $c->_flash ) {
+            if ( %$flash_data ) { # damn 'my' declarations
+                delete @{ $flash_data }{ @{ $c->_flash_stale_keys || [] } };
+                $c->store_session_data( "flash:$sid", $flash_data );
+            }
+        } else {
+            $c->delete_session_data( "flash:$sid" );
+        }
+    }
+}
 
-    my $s = $c->{session} ||= $c->get_session_data($sid);
-    if ( !$s or $s->{__expires} < time ) {
+sub _load_session {
+    my $c = shift;
 
-        # session expired
-        $c->log->debug("Deleting session $sid (expired)") if $c->debug;
-        $c->delete_session("session expired");
-        return $ret;
+    if ( my $sid = $c->_sessionid ) {
+               no warnings 'uninitialized'; # ne __address
+
+        my $session_data = $c->_session || $c->_session( $c->get_session_data( "session:$sid" ) );
+        if ( !$session_data or $session_data->{__expires} < time ) {
+
+            # session expired
+            $c->log->debug("Deleting session $sid (expired)") if $c->debug;
+            $c->delete_session("session expired");
+        }
+        elsif ($c->config->{session}{verify_address}
+            && $session_data->{__address} ne $c->request->address )
+        {
+            $c->log->warn(
+                    "Deleting session $sid due to address mismatch ("
+                  . $session_data->{__address} . " != "
+                  . $c->request->address . ")",
+            );
+            $c->delete_session("address mismatch");
+        }
+        else {
+            $c->log->debug(qq/Restored session "$sid"/) if $c->debug;
+        }
+       
+        $c->_expire_ession_keys;
+
+        return $session_data;
     }
 
-    if (   $c->config->{session}{verify_address}
-        && $c->{session}{__address}
-        && $c->{session}{__address} ne $c->request->address )
-    {
-        $c->log->warn(
-                "Deleting session $sid due to address mismatch ("
-              . $c->{session}{__address} . " != "
-              . $c->request->address . ")",
-        );
-        $c->delete_session("address mismatch");
-        return $ret;
+    return undef;
+}
+
+sub _load_flash {
+    my $c = shift;
+
+    if ( my $sid = $c->_sessionid ) {
+        if ( my $flash_data = $c->_flash || $c->_flash( $c->get_session_data( "flash:$sid" ) ) ) {
+            $c->_flash_stale_keys([ keys %$flash_data ]);
+            return $flash_data;
+        }
+    }
+
+    return undef;
+}
+
+sub _expire_ession_keys {
+    my ( $c, $data ) = @_;
+
+    my $now = time;
+
+    my $expiry = ($data || $c->_session || {})->{__expire_keys} || {};
+    foreach my $key (grep { $expiry->{$_} < $now } keys %$expiry ) {
+        delete $c->_session->{$key};
+        delete $expiry->{$key};
     }
 }
 
@@ -109,26 +171,71 @@ sub delete_session {
     my ( $c, $msg ) = @_;
 
     # delete the session data
-    my $sid = $c->sessionid;
-    $c->delete_session_data($sid);
+    my $sid = $c->_sessionid || return;
+    $c->delete_session_data( "session:$sid" );
 
     # reset the values in the context object
-    $c->{session} = undef;
-    $c->sessionid(undef);
-    $c->session_delete_reason($msg);
+    $c->_session(undef);
+    $c->_sessionid(undef);
+    $c->_session_delete_reason($msg);
+}
+
+sub session_delete_reason {
+    my $c = shift;
+
+    $c->_load_session if ( $c->_sessionid && !$c->_session ); # must verify session data
+
+    $c->_session_delete_reason( @_ );
+}
+
+sub sessionid {
+       my $c = shift;
+    
+       if ( @_ ) {
+               if ( $c->validate_session_id( my $sid = shift ) ) {
+                       $c->_sessionid( $sid );
+            return unless defined wantarray;
+               } else {
+                       my $err = "Tried to set invalid session ID '$sid'";
+                       $c->log->error( $err );
+                       Catalyst::Exception->throw( $err );
+               }
+       }
+    
+    $c->_load_session if ( $c->_sessionid && !$c->_session ); # must verify session data
+
+       return $c->_sessionid;
+}
+
+sub validate_session_id {
+       my ( $c, $sid ) = @_;
+
+       $sid and $sid =~ /^[a-f\d]+$/i;
 }
 
 sub session {
     my $c = shift;
 
-    return $c->{session} if $c->{session};
+    $c->_session || $c->_load_session || do {
+        $c->create_session_id;
 
-    my $sid = $c->generate_session_id;
-    $c->sessionid($sid);
+        $c->initialize_session_data;
+       };
+}
+
+sub flash {
+    my $c = shift;
+    $c->_flash || $c->_load_flash || do {
+        $c->create_session_id;
+        $c->_flash( {} );
+    }
+}
 
-    $c->log->debug(qq/Created session "$sid"/) if $c->debug;
+sub session_expire_key {
+    my ( $c, %keys ) = @_;
 
-    return $c->initialize_session_data;
+    my $now = time;
+    @{ $c->session->{__expire_keys} }{keys %keys} = map { $now + $_ } values %keys;
 }
 
 sub initialize_session_data {
@@ -136,7 +243,7 @@ sub initialize_session_data {
 
     my $now = time;
 
-    return $c->{session} = {
+    return $c->_session({
         __created => $now,
         __updated => $now,
         __expires => $now + $c->config->{session}{expires},
@@ -146,7 +253,7 @@ sub initialize_session_data {
             ? ( __address => $c->request->address )
             : ()
         ),
-    };
+    });
 }
 
 sub generate_session_id {
@@ -157,6 +264,18 @@ sub generate_session_id {
     return $digest->hexdigest;
 }
 
+sub create_session_id {
+    my $c = shift;
+
+    if ( !$c->_sessionid ) {
+        my $sid = $c->generate_session_id;
+
+        $c->log->debug(qq/Created session "$sid"/) if $c->debug;
+
+        $c->sessionid($sid);
+    }
+}
+
 my $counter;
 
 sub session_hash_seed {
@@ -169,12 +288,16 @@ my $usable;
 
 sub _find_digest () {
     unless ($usable) {
-        $usable = List::Util::first(
-            sub {
-                eval { Digest->new($_) };
-            },
-            qw/SHA-1 MD5 SHA-256/
-          )
+        foreach my $alg (qw/SHA-1 MD5 SHA-256/) {
+            eval {
+                Digest->new($alg);
+            };
+            unless ($@) {
+                $usable = $alg;
+                last;
+            }
+        }
+        $usable
           or Catalyst::Exception->throw(
                 "Could not find a suitable Digest module. Please install "
               . "Digest::SHA1, Digest::SHA, or Digest::MD5" );
@@ -183,6 +306,18 @@ sub _find_digest () {
     return Digest->new($usable);
 }
 
+sub dump_these {
+    my $c = shift;
+
+    (
+        $c->NEXT::dump_these(),
+
+        $c->sessionid
+        ? ( [ "Session ID" => $c->sessionid ], [ Session => $c->session ], )
+        : ()
+    );
+}
+
 __PACKAGE__;
 
 __END__
@@ -192,19 +327,36 @@ __END__
 =head1 NAME
 
 Catalyst::Plugin::Session - Generic Session plugin - ties together server side
-storage and client side tickets required to maintain session data.
+storage and client side state required to maintain session data.
 
 =head1 SYNOPSIS
 
-    use Catalyst qw/Session Session::Store::FastMmap Session::State::Cookie/;
+    # To get sessions to "just work", all you need to do is use these plugins:
+
+    use Catalyst qw/
+      Session
+      Session::Store::FastMmap
+      Session::State::Cookie
+      /;
+
+       # you can replace Store::FastMmap with Store::File - both have sensible
+       # default configurations (see their docs for details)
+
+       # more complicated backends are available for other scenarios (DBI storage,
+       # etc)
+
+
+    # after you've loaded the plugins you can save session data
+    # For example, if you are writing a shopping cart, it could be implemented
+    # like this:
 
     sub add_item : Local {
         my ( $self, $c ) = @_;
 
         my $item_id = $c->req->param("item");
 
-        # $c->session is stored across requests, so
-        # other actions will see these values
+        # $c->session is a hash ref, a bit like $c->stash
+        # the difference is that it' preserved across requests
 
         push @{ $c->session->{items} }, $item_id;
 
@@ -216,7 +368,7 @@ storage and client side tickets required to maintain session data.
 
         # values in $c->session are restored
         $c->stash->{items_to_display} =
-            [ map { MyModel->retrieve($_) } @{ $c->session->{items} } ];
+          [ map { MyModel->retrieve($_) } @{ $c->session->{items} } ];
 
         $c->forward("MyView");
     }
@@ -236,6 +388,24 @@ made by the same client.
 
 This plugin links the two pieces together.
 
+=head1 RECCOMENDED BACKENDS
+
+=over 4
+
+=item Session::State::Cookie
+
+The only really sane way to do state is using cookies.
+
+=item Session::Store::File
+
+A portable backend, based on Cache::File.
+
+=item Session::Store::FastMmap
+
+A fast and flexible backend, based on Cache::FastMmap.
+
+=back
+
 =head1 METHODS
 
 =over 4
@@ -253,6 +423,36 @@ requests.
 This method will automatically create a new session and session ID if none
 exists.
 
+=item flash
+
+This is like Ruby on Rails' flash data structure. Think of it as a stash that
+lasts a single redirect, not only a forward.
+
+    sub moose : Local {
+        my ( $self, $c ) = @_;
+
+        $c->flash->{beans} = 10;
+        $c->response->redirect( $c->uri_for("foo") );
+    }
+
+    sub foo : Local {
+        my ( $self, $c ) = @_;
+
+        my $value = $c->flash->{beans};
+
+        # ...
+
+        $c->response->redirect( $c->uri_for("bar") );
+    }
+
+    sub bar : Local {
+        my ( $self, $c ) = @_;
+
+        if ( exists $c->flash->{beans} ) { # false
+        
+        }
+    }
+
 =item session_delete_reason
 
 This accessor contains a string with the reason a session was deleted. Possible
@@ -270,6 +470,30 @@ C<session expired>
 
 =back
 
+=item session_expire_key $key, $ttl
+
+Mark a key to expire at a certain time (only useful when shorter than the
+expiry time for the whole session).
+
+For example:
+
+    __PACKAGE__->config->{session}{expires} = 1000000000000; # forever
+
+    # later
+
+    $c->session_expire_key( __user => 3600 );
+
+Will make the session data survive, but the user will still be logged out after
+an hour.
+
+Note that these values are not auto extended.
+
+=back
+
+=item INTERNAL METHODS
+
+=over 4
+
 =item setup
 
 This method is extended to also make calls to
@@ -287,9 +511,11 @@ listed in L</CONFIGURATION>.
 
 =item prepare_action
 
-This methoid is extended, and will restore session data and check it for
-validity if a session id is defined. It assumes that the State plugin will
-populate the C<sessionid> key beforehand.
+This methoid is extended.
+
+It's only effect is if the (off by default) C<flash_to_stash> configuration
+parameter is on - then it will copy the contents of the flash to the stash at
+prepare time.
 
 =item finalize
 
@@ -306,6 +532,11 @@ which will be saved in C<session_delete_reason> if provided.
 This method will initialize the internal structure of the session, and is
 called by the C<session> method if appropriate.
 
+=item create_session_id
+
+Creates a new session id using C<generate_session_id> if there is no session ID
+yet.
+
 =item generate_session_id
 
 This method will return a string that can be used as a session ID. It is
@@ -320,6 +551,13 @@ overridable in case you want to provide more random data.
 
 Currently it returns a concatenated string which contains:
 
+=item validate_session_id SID
+
+Make sure a session ID is of the right format.
+
+This currently ensures that the session ID string is any amount of case
+insensitive hexadecimal characters.
+
 =over 4
 
 =item *
@@ -366,8 +604,33 @@ Or even more directly, replace C<generate_session_id>:
 Also have a look at L<Crypt::Random> and the various openssl bindings - these
 modules provide APIs for cryptographically secure random data.
 
+=item dump_these
+
+See L<Catalyst/dump_these> - ammends the session data structure to the list of
+dumped objects if session ID is defined.
+
 =back
 
+=head1 USING SESSIONS DURING PREPARE
+
+The earliest point in time at which you may use the session data is after
+L<Catalyst::Plugin::Session>'s C<prepare_action> has finished.
+
+State plugins must set $c->session ID before C<prepare_action>, and during
+C<prepare_action> L<Catalyst::Plugin::Session> will actually load the data from
+the store.
+
+       sub prepare_action {
+               my $c = shift;
+
+               # don't touch $c->session yet!
+
+               $c->NEXT::prepare_action( @_ );
+
+               $c->session;  # this is OK
+               $c->sessionid; # this is also OK
+       }
+
 =head1 CONFIGURATION
 
     $c->config->{session} = {
@@ -386,9 +649,14 @@ hours).
 
 =item verify_address
 
-When false, C<< $c->request->address >> will be checked at prepare time. If it
-is not the same as the address that initiated the session, the session is
-deleted.
+When true, C<<$c->request->address>> will be checked at prepare time. If it is
+not the same as the address that initiated the session, the session is deleted.
+
+=item flash_to_stash
+
+This option makes it easier to have actions behave the same whether they were
+forwarded to or redirected to. On prepare time it copies the contents of
+C<flash> (if any) to the stash.
 
 =back
 
@@ -408,7 +676,7 @@ is deleted.
 =item __updated
 
 The last time a session was saved. This is the value of
-C<< $c->{session}{__expires} - $c->config->{session}{expires} >>.
+C<< $c->session->{__expires} - $c->config->session->{expires} >>.
 
 =item __created
 
@@ -417,12 +685,14 @@ The time when the session was first created.
 =item __address
 
 The value of C<< $c->request->address >> at the time the session was created.
-This value is only populated of C<verify_address> is true in the configuration.
+This value is only populated if C<verify_address> is true in the configuration.
 
 =back
 
 =head1 CAVEATS
 
+=head2 Round the Robin Proxies
+
 C<verify_address> could make your site inaccessible to users who are behind
 load balanced proxies. Some ISPs may give a different IP to each request by the
 same client due to this type of proxying. If addresses are verified these
@@ -434,6 +704,70 @@ that it's OK for the address of the client to change. When the server sees that
 this box is checked it should delete the C<__address> sepcial key from the
 session hash when the hash is first created.
 
+=head2 Race Conditions
+
+In this day and age where cleaning detergents and dutch football (not the
+american kind) teams roam the plains in great numbers, requests may happen
+simultaneously. This means that there is some risk of session data being
+overwritten, like this:
+
+=over 4
+
+=item 1.
+
+request a starts, request b starts, with the same session id
+
+=item 2.
+
+session data is loaded in request a
+
+=item 3.
+
+session data is loaded in request b
+
+=item 4.
+
+session data is changed in request a
+
+=item 5.
+
+request a finishes, session data is updated and written to store
+
+=item 6.
+
+request b finishes, session data is updated and written to store, overwriting
+changes by request a
+
+=back
+
+If this is a concern in your application, a soon to be developed locking
+solution is the only safe way to go. This will have a bigger overhead.
+
+For applications where any given user is only making one request at a time this
+plugin should be safe enough.
+
+=head1 AUTHORS
+
+=over 4
+
+=item Andy Grundman
+
+=item Christian Hansen
+
+=item Yuval Kogman, C<nothingmuch@woobling.org> (current maintainer)
+
+=item Sebastian Riedel
+
+=back
+
+And countless other contributers from #catalyst. Thanks guys!
+
+=head1 COPYRIGHT & LICENSE
+
+       Copyright (c) 2005 the aforementioned authors. All rights
+       reserved. This program is free software; you can redistribute
+       it and/or modify it under the same terms as Perl itself.
+
 =cut