1 package Plack::Session::State;
7 use Plack::Util::Accessor qw[
13 my ($class, %params) = @_;
15 $params{'_expired'} ||= +{};
16 $params{'session_key'} ||= 'plack_session';
17 $params{'sid_generator'} ||= sub {
18 Digest::SHA1::sha1_hex(rand() . $$ . {} . time)
21 bless { %params } => $class;
24 sub expire_session_id {
26 $self->{'_expired'}->{ $id }++;
29 sub is_session_expired {
31 exists $self->{'_expired'}->{ $id }
36 return unless $id && not $self->is_session_expired( $id );
41 my ($self, $request) = @_;
42 $self->extract( $request )
44 $self->generate( $request )
48 my ($self, $request) = @_;
49 $self->check_expired( $request->param( $self->session_key ) );
54 $self->sid_generator->( @_ );
59 my ($self, $id, $response) = @_;
71 Plack::Session::State - Basic parameter-based session state
79 =item B<new ( %params )>
83 This is the name of the session key, it default to 'plack_session'.
85 =item B<sid_generator>
87 This is a CODE ref used to generate unique session ids.
91 =head2 Session ID Managment
95 =item B<get_session_id ( $request )>
97 Given a C<$request> this will first attempt to extract the session,
98 if the is expired or does not exist, it will then generate a new
99 session. The C<$request> is expected to be a L<Plack::Request> instance
100 or an object with an equivalent interface.
102 =item B<extract ( $request )>
104 This will attempt to extract the session from a C<$request> by looking
105 for the C<session_key> in the C<$request> params. It will then check to
106 see if the session has expired and return the session id if it is not.
107 The C<$request> is expected to be a L<Plack::Request> instance or an
108 object with an equivalent interface.
110 =item B<generate ( $request )>
112 This will generate a new session id using the C<sid_generator> callback.
113 The C<$request> argument is not used by this method but is there for
114 use by subclasses. The C<$request> is expected to be a L<Plack::Request>
115 instance or an object with an equivalent interface.
117 =item B<finalize ( $session_id, $response )>
119 Given a C<$session_id> and a C<$response> this will perform any
120 finalization nessecary to preserve state. This method is called by
121 the L<Plack::Session> C<finalize> method. The C<$response> is expected
122 to be a L<Plack::Response> instance or an object with an equivalent
127 =head2 Session Expiration Handling
131 =item B<expire_session_id ( $id )>
133 This will mark the session for C<$id> as expired. This method is called
134 by the L<Plack::Session> C<expire> method.
136 =item B<is_session_expired ( $id )>
138 This will check to see if the session C<$id> has been marked as
141 =item B<check_expired ( $id )>
143 Given an session C<$id> this will return C<undef> if the session is
144 expired or return the C<$id> if it is not.
150 All complex software has bugs lurking in it, and this module is no
151 exception. If you find a bug please either email me, or add the bug
156 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
158 =head1 COPYRIGHT AND LICENSE
160 Copyright 2009 Infinity Interactive, Inc.
162 L<http://www.iinteractive.com>
164 This library is free software; you can redistribute it and/or modify
165 it under the same terms as Perl itself.