Reworked Session to make the expiration a store's responsibility and
[catagits/Web-Session.git] / lib / Plack / Session / State / Cookie.pm
index 1f1bcec..9b51a02 100644 (file)
@@ -2,6 +2,9 @@ package Plack::Session::State::Cookie;
 use strict;
 use warnings;
 
+our $VERSION   = '0.03';
+our $AUTHORITY = 'cpan:STEVAN';
+
 use parent 'Plack::Session::State';
 
 use Plack::Util::Accessor qw[
@@ -11,15 +14,20 @@ use Plack::Util::Accessor qw[
     secure
 ];
 
-sub expire_session_id {
-    my ($self, $id) = @_;
-    $self->SUPER::expire_session_id( $id );
-    $self->expires( 0 );
+sub get_session_id {
+    my ($self, $request) = @_;
+    ( $request->cookie( $self->session_key ) || return )->value;
 }
 
-sub extract {
-    my ($self, $request) = @_;
-    $self->check_expired( ( $request->cookie( $self->session_key ) || return )->value );
+sub expire_session_id {
+    my ($self, $id, $response) = @_;
+    $response->cookies->{ $self->session_key } = +{
+        value => $id,
+        path  => ($self->path || '/'),
+        expires => 0,
+        ( defined $self->domain  ? ( domain  => $self->domain  ) : () ),
+        ( defined $self->secure  ? ( secure  => $self->secure  ) : () ),
+    };
 }
 
 sub finalize {
@@ -27,9 +35,9 @@ sub finalize {
     $response->cookies->{ $self->session_key } = +{
         value => $id,
         path  => ($self->path || '/'),
-        ( $self->domain  ? ( domain  => $self->domain  ) : () ),
-        ( $self->expires ? ( expires => $self->expires ) : () ),
-        ( $self->secure  ? ( secure  => $self->secure  ) : () ),
+        ( defined $self->domain  ? ( domain  => $self->domain  ) : () ),
+        ( defined $self->expires ? ( expires => $self->expires ) : () ),
+        ( defined $self->secure  ? ( secure  => $self->secure  ) : () ),
     };
 }
 
@@ -43,35 +51,54 @@ __END__
 
 Plack::Session::State::Cookie - Basic cookie-based session state
 
+=head1 SYNOPSIS
+
+  use Plack::Builder;
+  use Plack::Middleware::Session;
+
+  my $app = sub {
+      return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
+  };
+
+  builder {
+      enable 'Session'; # Cookie is the default state
+      $app;
+  };
+
 =head1 DESCRIPTION
 
+This is a subclass of L<Plack::Session::State> and implements it's
+full interface. This is the default state used in
+L<Plack::Middleware::Session>.
+
 =head1 METHODS
 
 =over 4
 
 =item B<new ( %params )>
 
-=item B<path>
-
-=item B<domain>
+The C<%params> can include I<path>, I<domain>, I<expires> and
+I<secure> options, as well as all the options accepted by
+L<Plack::Session::Store>.
 
-=item B<expires>
-
-=item B<secure>
+=item B<path>
 
-=back
+Path of the cookie, this defaults to "/";
 
-=over 4
+=item B<domain>
 
-=item B<extract ( $request )>
+Domain of the cookie, if nothing is supplied then it will not
+be included in the cookie.
 
-=item B<finalize ( $session_id, $response )>
+=item B<expires>
 
-=back
+Expiration time of the cookie, if nothing is supplied then it will
+not be included in the cookie.
 
-=over 4
+=item B<secure>
 
-=item B<expire_session_id ( $id )>
+Secure flag for the cookie, if nothing is supplied then it will not
+be included in the cookie.
 
 =back
 
@@ -87,7 +114,7 @@ Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2009 Infinity Interactive, Inc.
+Copyright 2009, 2010 Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>