preliminary docs added, improved support for subclassing and sid_generator callback...
Stevan Little [Sat, 12 Dec 2009 15:28:47 +0000 (10:28 -0500)]
lib/Plack/Middleware/Session.pm
lib/Plack/Session.pm
lib/Plack/Session/State.pm
lib/Plack/Session/State/Cookie.pm
lib/Plack/Session/Store.pm
lib/Plack/Session/Store/CHI.pm
t/010_middleware.t [moved from t/middleware.t with 100% similarity]

index 951b3d9..9aa12d2 100644 (file)
@@ -44,3 +44,40 @@ sub call {
 1;
 
 __END__
+
+=pod
+
+=head1 NAME
+
+Plack::Middleware::Session - Middleware for session management
+
+=head1 SYNOPSIS
+
+  use Plack::Middleware::Session;
+
+=head1 DESCRIPTION
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Tatsuhiko Miyagawa
+
+Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2009 Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+
index 5a41010..223cf37 100644 (file)
@@ -10,11 +10,9 @@ use Plack::Util::Accessor qw[
 
 sub new {
     my ($class, %params) = @_;
-    bless {
-        id    => $params{ state }->get_session_id( $params{ request } ),
-        state => $params{ state },
-        store => $params{ store },
-    } => $class;
+    my $request = delete $params{'request'};
+    $params{'id'} = $params{'state'}->get_session_id( $request );
+    bless { %params } => $class;
 }
 
 ## Data Managment
@@ -48,4 +46,72 @@ sub finalize {
     $self->state->finalize( $self->id, $response );
 }
 
-1;
\ No newline at end of file
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Plack::Session - Middleware for session management
+
+=head1 SYNOPSIS
+
+  use Plack::Session;
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=over 4
+
+=item B<new ( %params )>
+
+=item B<id>
+
+=item B<state>
+
+=item B<store>
+
+=back
+
+=over 4
+
+=item B<get ( $key )>
+
+=item B<set ( $key, $value )>
+
+=item B<remove ( $key )>
+
+=back
+
+=over 4
+
+=item B<expire>
+
+=item B<finalize ( $response )>
+
+=back
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2009 Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
index f28ca40..4f818c1 100644 (file)
@@ -2,30 +2,35 @@ package Plack::Session::State;
 use strict;
 use warnings;
 
-use Plack::Util::Accessor qw[ session_key ];
-use Digest::SHA1;
+use Plack::Util::Accessor qw[
+    session_key
+    sid_generator
+];
 
 sub new {
     my ($class, %params) = @_;
-    bless {
-        session_key => $params{ session_key } || 'plack_session',
-        expired     => {}
-    } => $class;
+
+    $params{'_expired'}      ||= +{};
+    $params{'session_key'}   ||= 'plack_session';
+    $params{'sid_generator'} ||= sub {
+        require Digest::SHA1;
+        Digest::SHA1::sha1_hex(rand() . $$ . {} . time)
+    };
+
+    bless { %params } => $class;
 }
 
 sub expire_session_id {
     my ($self, $id) = @_;
-    $self->{expired}->{ $id }++;
+    $self->{'_expired'}->{ $id }++;
 }
 
 sub check_expired {
     my ($self, $id) = @_;
-    return unless $id && not exists $self->{expired}->{ $id };
+    return unless $id && not exists $self->{'_expired'}->{ $id };
     return $id;
 }
 
-# given a request, get the
-# session id from it
 sub get_session_id {
     my ($self, $request) = @_;
     $self->extract( $request )
@@ -40,7 +45,7 @@ sub extract {
 
 sub generate {
     my $self = shift;
-    return Digest::SHA1::sha1_hex(rand() . $$ . {} . time);
+    $self->sid_generator->( @_ );
 }
 
 
@@ -50,3 +55,68 @@ sub finalize {
 }
 
 1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Plack::Session::State - Basic parameter-based session state
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=over 4
+
+=item B<new ( %params )>
+
+=item B<session_key>
+
+=item B<sid_generator>
+
+=back
+
+=over 4
+
+=item B<get_session_id ( $request )>
+
+=item B<extract ( $request )>
+
+=item B<generate ( $request )>
+
+=item B<finalize ( $session_id, $response )>
+
+=back
+
+=over 4
+
+=item B<expire_session_id ( $id )>
+
+=item B<check_expired ( $id )>
+
+=back
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2009 Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+
index 018f724..1f1bcec 100644 (file)
@@ -4,7 +4,12 @@ use warnings;
 
 use parent 'Plack::Session::State';
 
-use Plack::Util::Accessor qw[ path domain expires secure ];
+use Plack::Util::Accessor qw[
+    path
+    domain
+    expires
+    secure
+];
 
 sub expire_session_id {
     my ($self, $id) = @_;
@@ -29,3 +34,66 @@ sub finalize {
 }
 
 1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Plack::Session::State::Cookie - Basic cookie-based session state
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=over 4
+
+=item B<new ( %params )>
+
+=item B<path>
+
+=item B<domain>
+
+=item B<expires>
+
+=item B<secure>
+
+=back
+
+=over 4
+
+=item B<extract ( $request )>
+
+=item B<finalize ( $session_id, $response )>
+
+=back
+
+=over 4
+
+=item B<expire_session_id ( $id )>
+
+=back
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2009 Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+
index 3f52679..e20c196 100644 (file)
@@ -4,7 +4,11 @@ use warnings;
 
 use Plack::Util::Accessor qw[ _stash ];
 
-sub new { bless { _stash => {} } => shift }
+sub new {
+    my ($class, %params) = @_;
+    $params{'_stash'} ||= +{};
+    bless { %params } => $class;
+}
 
 sub fetch {
     my ($self, $session_id, $key) = @_;
@@ -21,14 +25,72 @@ sub delete {
     delete $self->_stash->{ $session_id }->{ $key };
 }
 
-sub persist {
-    my ($self, $session_id) = @_;
-    ()
-}
-
 sub cleanup {
     my ($self, $session_id) = @_;
     delete $self->_stash->{ $session_id }
 }
 
-1;
\ No newline at end of file
+sub persist {
+    my ($self, $session_id, $response) = @_;
+    ()
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Plack::Session::Store - Basic in-memory session store
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=over 4
+
+=item B<new ( %params )>
+
+=back
+
+=over 4
+
+=item B<fetch ( $session_id, $key )>
+
+=item B<store ( $session_id, $key, $data )>
+
+=item B<delete ( $session_id, $key )>
+
+=back
+
+=over 4
+
+=item B<persist ( $session_id, $response )>
+
+=item B<cleanup ( $session_id )>
+
+=back
+
+=head1 BUGS
+
+All complex software has bugs lurking in it, and this module is no
+exception. If you find a bug please either email me, or add the bug
+to cpan-RT.
+
+=head1 AUTHOR
+
+Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2009 Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
index 4776ea5..91579ef 100644 (file)
@@ -2,17 +2,18 @@ package Plack::Session::Store::CHI;
 use strict;
 use warnings;
 
-use Plack::Util::Accessor qw[ chi ];
 use Scalar::Util qw/blessed/;
 
+use parent 'Plack::Session::Store';
+
+use Plack::Util::Accessor qw[ chi ];
+
 sub new {
     my ($class, %params) = @_;
     unless ( blessed $params{chi} and $params{chi}->isa('CHI::Driver') ) {
         die('require chi driver');
     }
-    bless {
-        chi => $params{chi},
-    } => $class;
+    bless { %params } => $class;
 }
 
 sub fetch {
@@ -43,11 +44,6 @@ sub delete {
     $self->chi->set($session_id => $cache);
 }
 
-sub persist {
-    my ($self, $session_id) = @_;
-    ()
-}
-
 sub cleanup {
     my ($self, $session_id) = @_;
     $self->chi->remove($session_id);
@@ -59,7 +55,7 @@ __END__
 
 =head1 NAME
 
-Plack::Session::Store::CHI
+Plack::Session::Store::CHI - CHI session store
 
 =head1 SYNOPSIS
 
@@ -76,6 +72,29 @@ Plack::Session::Store::CHI
       $app;
   };
 
+=head1 METHODS
+
+=over 4
+
+=item B<new ( %params )>
+
+=back
+
+=over 4
+
+=item B<fetch ( $session_id, $key )>
+
+=item B<store ( $session_id, $key, $data )>
+
+=item B<delete ( $session_id, $key )>
+
+=back
+
+=over 4
+
+=item B<cleanup ( $session_id )>
+
+=back
 
 =head1 AUTHOR
 
similarity index 100%
rename from t/middleware.t
rename to t/010_middleware.t