package Plack::Session::State; use strict; use warnings; our $VERSION = '0.23'; our $AUTHORITY = 'cpan:STEVAN'; use Digest::SHA1 (); use Plack::Request; use Plack::Util::Accessor qw[ session_key sid_generator sid_validator ]; sub new { my ($class, %params) = @_; $params{'session_key'} ||= 'plack_session'; $params{'sid_generator'} ||= sub { Digest::SHA1::sha1_hex(rand() . $$ . {} . time) }; $params{'sid_validator'} ||= qr/\A[0-9a-f]{40}\Z/; bless { %params } => $class; } sub expire_session_id { my ($self, $id, $res) = @_; } sub validate_session_id { my ($self, $id) = @_; $id =~ $self->sid_validator; } sub get_session_id { my ($self, $env) = @_; return Plack::Request->new($env)->param( $self->session_key ); } sub extract { my ($self, $env) = @_; my $id = $self->get_session_id( $env ); return unless defined $id; return $id if $self->validate_session_id( $id ); return; } sub generate { my $self = shift; $self->sid_generator->( @_ ); } sub finalize { my ($self, $id, $res, $options) = @_; (); } 1; __END__ =pod =head1 NAME Plack::Session::State - Basic parameter-based session state =head1 SYNOPSIS use Plack::Builder; use Plack::Middleware::Session; use Plack::Session::State; my $app = sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ]; }; builder { enable 'Session', state => Plack::Session::State->new; $app; }; =head1 DESCRIPTION This will maintain session state by passing the session through the request params. It does not do this automatically though, you are responsible for passing the session param. This should be considered the state "base" class (although subclassing is not a requirement) and defines the spec for all B modules. You will only need to override a couple methods if you do subclass. See L for an example of this. B: parameter based session ID management makes session fixation really easy, and that makes your website vulnerable. You should really avoid using this state in the production environment except when you have to deal with legacy HTTP clients that do not support cookies. In the future this parameter based state handling will be removed from this base class and will be moved to its own State class. =head1 METHODS =over 4 =item B The C<%params> can include I, I and I however in both cases a default will be provided for you. =item B This is the name of the session key, it defaults to 'plack_session'. =item B This is a CODE ref used to generate unique session ids, by default it will generate a SHA1 using fairly sufficient entropy. If you are concerned or interested, just read the source. =item B This is a regex used to validate requested session id. =back =head2 Session ID Managment =over 4 =item B This is the method used to extract the session id from a C<$env>. Subclasses will often only need to override this method and the C method. =item B This will use the C regex and confirm that the C<$session_id> is valid. =item B This will attempt to extract the session from a C<$env> by looking for the C in the request params. It will then check to see if the session is valid and that it has not expired. It will return the session id if everything is good or undef otherwise. =item B This will generate a new session id using the C callback. The C<$request> argument is not used by this method but is there for use by subclasses. The C<$request> is expected to be a L instance or an object with an equivalent interface. =item B Given a C<$session_id> and a C<$response> this will perform any finalization necessary to preserve state. This method is called by the L C method. The C<$response> is expected to be a L instance or an object with an equivalent interface. =back =head2 Session Expiration Handling =over 4 =item B This will mark the session for C<$id> as expired. This method is called by the L C method. =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 Estevan.little@iinteractive.comE =head1 COPYRIGHT AND LICENSE Copyright 2009, 2010 Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut