Session, fixed bug in algorithm detection, it was always choosing SHA-256
[catagits/Catalyst-Plugin-Session.git] / lib / Catalyst / Plugin / Session.pm
index 7e2ac41..ae79a8e 100644 (file)
@@ -14,7 +14,7 @@ use overload            ();
 our $VERSION = "0.02";
 
 BEGIN {
-    __PACKAGE__->mk_accessors(qw/_sessionid _session _session_delete_reason/);
+    __PACKAGE__->mk_accessors(qw/_sessionid _session _session_delete_reason _flash _flash_stale_keys/);
 }
 
 sub setup {
@@ -57,20 +57,54 @@ sub setup_session {
     $c->NEXT::setup_session();
 }
 
+sub prepare_action {
+    my $c = shift;
+
+    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;
 
-    if ( my $session_data = $c->_session ) {
+    $c->_save_session;
+    $c->_save_flash;
+
+    $c->NEXT::finalize(@_);
+}
+
+sub _save_session {
+    my $c = shift;
+    
+    if ( my $sid = $c->_sessionid ) {
+        if ( my $session_data = $c->_session ) {
+
+            # all sessions are extended at the end of the request
+            my $now = time;
+            @{ $session_data }{qw/__updated __expires/} =
+              ( $now, $c->config->{session}{expires} + $now );
 
-        # all sessions are extended at the end of the request
-        my $now = time;
-        @{ $session_data }{qw/__updated __expires/} =
-          ( $now, $c->config->{session}{expires} + $now );
-        delete @{ $session_data->{__flash} }{ @{ delete $session_data->{__flash_stale_keys} || [] } };
-        $c->store_session_data( $c->sessionid, $session_data );
+            $c->store_session_data( "session:$sid", $session_data );
+        }
     }
+}
 
-    $c->NEXT::finalize(@_);
+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" );
+        }
+    }
 }
 
 sub _load_session {
@@ -79,7 +113,7 @@ sub _load_session {
     if ( my $sid = $c->_sessionid ) {
                no warnings 'uninitialized'; # ne __address
 
-        my $session_data = $c->_session || $c->_session( $c->get_session_data($sid) );
+        my $session_data = $c->_session || $c->_session( $c->get_session_data( "session:$sid" ) );
         if ( !$session_data or $session_data->{__expires} < time ) {
 
             # session expired
@@ -101,7 +135,6 @@ sub _load_session {
         }
        
         $c->_expire_ession_keys;
-        $session_data->{__flash_stale_keys} = [ keys %{ $session_data->{__flash} } ];
 
         return $session_data;
     }
@@ -109,6 +142,19 @@ sub _load_session {
     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 ) = @_;
 
@@ -126,7 +172,7 @@ sub delete_session {
 
     # delete the session data
     my $sid = $c->_sessionid || return;
-    $c->delete_session_data($sid);
+    $c->delete_session_data( "session:$sid" );
 
     # reset the values in the context object
     $c->_session(undef);
@@ -164,17 +210,14 @@ sub sessionid {
 sub validate_session_id {
        my ( $c, $sid ) = @_;
 
-       $sid =~ /^[a-f\d]+$/i;
+       $sid and $sid =~ /^[a-f\d]+$/i;
 }
 
 sub session {
     my $c = shift;
 
     $c->_session || $c->_load_session || do {
-        my $sid = $c->generate_session_id;
-        $c->sessionid($sid);
-
-        $c->log->debug(qq/Created session "$sid"/) if $c->debug;
+        $c->create_session_id;
 
         $c->initialize_session_data;
        };
@@ -182,7 +225,10 @@ sub session {
 
 sub flash {
     my $c = shift;
-    return $c->session->{__flash} ||= {};
+    $c->_flash || $c->_load_flash || do {
+        $c->create_session_id;
+        $c->_flash( {} );
+    }
 }
 
 sub session_expire_key {
@@ -218,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 {
@@ -232,10 +290,12 @@ sub _find_digest () {
     unless ($usable) {
         foreach my $alg (qw/SHA-1 MD5 SHA-256/) {
             eval {
-                my $obj = Digest->new($alg);
-                $usable = $alg;
-                return $obj;
+                Digest->new($alg);
             };
+            unless ($@) {
+                $usable = $alg;
+                last;
+            }
         }
         $usable
           or Catalyst::Exception->throw(
@@ -451,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
 
@@ -470,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
@@ -585,6 +652,12 @@ hours).
 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
 
 =head1 SPECIAL KEYS