1 package Plack::Session::State;
6 our $AUTHORITY = 'cpan:STEVAN';
10 use Plack::Util::Accessor qw[
17 my ($class, %params) = @_;
19 $params{'session_key'} ||= 'plack_session';
20 $params{'sid_generator'} ||= sub {
21 Digest::SHA1::sha1_hex(rand() . $$ . {} . time)
23 $params{'sid_validator'} ||= qr/\A[0-9a-f]{40}\Z/;
25 bless { %params } => $class;
28 sub expire_session_id {
29 my ($self, $id, $res) = @_;
32 sub validate_session_id {
34 $id =~ $self->sid_validator;
38 my ($self, $env) = @_;
39 return Plack::Request->new($env)->param( $self->session_key );
43 my ($self, $env) = @_;
45 my $id = $self->get_session_id( $env );
46 return unless defined $id;
48 return $id if $self->validate_session_id( $id );
54 $self->sid_generator->( @_ );
59 my ($self, $id, $res, $options) = @_;
71 Plack::Session::State - Basic parameter-based session state
76 use Plack::Middleware::Session;
77 use Plack::Session::State;
80 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
85 state => Plack::Session::State->new;
91 This will maintain session state by passing the session through
92 the request params. It does not do this automatically though,
93 you are responsible for passing the session param.
95 This should be considered the state "base" class (although
96 subclassing is not a requirement) and defines the spec for
97 all B<Plack::Session::State::*> modules. You will only
98 need to override a couple methods if you do subclass. See
99 L<Plack::Session::State::Cookie> for an example of this.
101 B<WARNING>: parameter based session ID management makes session
102 fixation really easy, and that makes your website vulnerable. You
103 should really avoid using this state in the production environment
104 except when you have to deal with legacy HTTP clients that do not
107 In the future this parameter based state handling will be removed from
108 this base class and will be moved to its own State class.
114 =item B<new ( %params )>
116 The C<%params> can include I<session_key>, I<sid_generator> and I<sid_checker>
117 however in both cases a default will be provided for you.
121 This is the name of the session key, it defaults to 'plack_session'.
123 =item B<sid_generator>
125 This is a CODE ref used to generate unique session ids, by default
126 it will generate a SHA1 using fairly sufficient entropy. If you are
127 concerned or interested, just read the source.
129 =item B<sid_validator>
131 This is a regex used to validate requested session id.
135 =head2 Session ID Managment
139 =item B<get_session_id ( $env )>
141 This is the method used to extract the session id from a C<$env>.
142 Subclasses will often only need to override this method and the
145 =item B<validate_session_id ( $session_id )>
147 This will use the C<sid_validator> regex and confirm that the
148 C<$session_id> is valid.
150 =item B<extract ( $env )>
152 This will attempt to extract the session from a C<$env> by looking
153 for the C<session_key> in the request params. It will then check to
154 see if the session is valid and that it has not expired. It will return
155 the session id if everything is good or undef otherwise.
157 =item B<generate ( $request )>
159 This will generate a new session id using the C<sid_generator> callback.
160 The C<$request> argument is not used by this method but is there for
161 use by subclasses. The C<$request> is expected to be a L<Plack::Request>
162 instance or an object with an equivalent interface.
164 =item B<finalize ( $session_id, $response )>
166 Given a C<$session_id> and a C<$response> this will perform any
167 finalization necessary to preserve state. This method is called by
168 the L<Plack::Session> C<finalize> method. The C<$response> is expected
169 to be a L<Plack::Response> instance or an object with an equivalent
174 =head2 Session Expiration Handling
178 =item B<expire_session_id ( $id, $response )>
180 This will mark the session for C<$id> as expired. This method is called
181 by the L<Plack::Session> C<expire> method.
187 All complex software has bugs lurking in it, and this module is no
188 exception. If you find a bug please either email me, or add the bug
193 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
195 =head1 COPYRIGHT AND LICENSE
197 Copyright 2009, 2010 Infinity Interactive, Inc.
199 L<http://www.iinteractive.com>
201 This library is free software; you can redistribute it and/or modify
202 it under the same terms as Perl itself.