inflate store/state with strings like "store => 'File'" with autoloading.
[catagits/Web-Session.git] / lib / Plack / Session / State.pm
CommitLineData
06190e8b 1package Plack::Session::State;
2use strict;
3use warnings;
4
3b4205cd 5use Digest::SHA1 ();
6
ac4892f4 7use Plack::Util::Accessor qw[
8 session_key
9 sid_generator
10];
06190e8b 11
12sub new {
13 my ($class, %params) = @_;
ac4892f4 14
15 $params{'_expired'} ||= +{};
16 $params{'session_key'} ||= 'plack_session';
17 $params{'sid_generator'} ||= sub {
ac4892f4 18 Digest::SHA1::sha1_hex(rand() . $$ . {} . time)
19 };
20
21 bless { %params } => $class;
06190e8b 22}
23
24sub expire_session_id {
25 my ($self, $id) = @_;
ac4892f4 26 $self->{'_expired'}->{ $id }++;
06190e8b 27}
28
05b5f99d 29sub is_session_expired {
30 my ($self, $id) = @_;
31 exists $self->{'_expired'}->{ $id }
32}
33
bd992981 34sub check_expired {
35 my ($self, $id) = @_;
05b5f99d 36 return unless $id && not $self->is_session_expired( $id );
06190e8b 37 return $id;
38}
39
06190e8b 40sub get_session_id {
41 my ($self, $request) = @_;
42 $self->extract( $request )
43 ||
bd992981 44 $self->generate( $request )
45}
46
47sub extract {
48 my ($self, $request) = @_;
49 $self->check_expired( $request->param( $self->session_key ) );
50}
51
fe1bfe7d 52sub generate {
53 my $self = shift;
ac4892f4 54 $self->sid_generator->( @_ );
bd992981 55}
56
fe1bfe7d 57
bd992981 58sub finalize {
59 my ($self, $id, $response) = @_;
60 ();
06190e8b 61}
62
fe1bfe7d 631;
ac4892f4 64
65__END__
66
67=pod
68
69=head1 NAME
70
71Plack::Session::State - Basic parameter-based session state
72
3d92cf47 73=head1 SYNOPSIS
74
75 use Plack::Builder;
76 use Plack::Middleware::Session;
77 use Plack::Session::State;
78
79 my $app = sub {
80 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
81 };
82
83 builder {
84 enable 'Session',
85 state => Plack::Session::State->new;
86 $app;
87 };
88
ac4892f4 89=head1 DESCRIPTION
90
3d92cf47 91This will maintain session state by passing the session through
92the request params. It does not do this automatically though,
93you are responsible for passing the session param.
94
95This should be considered the state "base" class (although
96subclassing is not a requirement) and defines the spec for
97all B<Plack::Session::State::*> modules. You will only
98need to override a couple methods if you do subclass. See
99L<Plack::Session::State::Cookie> for an example of this.
100
ac4892f4 101=head1 METHODS
102
103=over 4
104
105=item B<new ( %params )>
106
3d92cf47 107The C<%params> can include I<session_key> and I<sid_generator>,
108however in both cases a default will be provided for you.
109
ac4892f4 110=item B<session_key>
111
43f34c01 112This is the name of the session key, it default to 'plack_session'.
113
ac4892f4 114=item B<sid_generator>
115
3d92cf47 116This is a CODE ref used to generate unique session ids, by default
117it will generate a SHA1 using fairly sufficient entropy. If you are
118concerned or interested, just read the source.
43f34c01 119
ac4892f4 120=back
121
43f34c01 122=head2 Session ID Managment
123
ac4892f4 124=over 4
125
126=item B<get_session_id ( $request )>
127
43f34c01 128Given a C<$request> this will first attempt to extract the session,
129if the is expired or does not exist, it will then generate a new
130session. The C<$request> is expected to be a L<Plack::Request> instance
131or an object with an equivalent interface.
132
ac4892f4 133=item B<extract ( $request )>
134
43f34c01 135This will attempt to extract the session from a C<$request> by looking
136for the C<session_key> in the C<$request> params. It will then check to
137see if the session has expired and return the session id if it is not.
138The C<$request> is expected to be a L<Plack::Request> instance or an
139object with an equivalent interface.
140
ac4892f4 141=item B<generate ( $request )>
142
43f34c01 143This will generate a new session id using the C<sid_generator> callback.
144The C<$request> argument is not used by this method but is there for
145use by subclasses. The C<$request> is expected to be a L<Plack::Request>
146instance or an object with an equivalent interface.
147
ac4892f4 148=item B<finalize ( $session_id, $response )>
149
43f34c01 150Given a C<$session_id> and a C<$response> this will perform any
151finalization nessecary to preserve state. This method is called by
152the L<Plack::Session> C<finalize> method. The C<$response> is expected
153to be a L<Plack::Response> instance or an object with an equivalent
154interface.
155
ac4892f4 156=back
157
43f34c01 158=head2 Session Expiration Handling
159
ac4892f4 160=over 4
161
162=item B<expire_session_id ( $id )>
163
43f34c01 164This will mark the session for C<$id> as expired. This method is called
165by the L<Plack::Session> C<expire> method.
166
167=item B<is_session_expired ( $id )>
168
169This will check to see if the session C<$id> has been marked as
170expired.
171
ac4892f4 172=item B<check_expired ( $id )>
173
43f34c01 174Given an session C<$id> this will return C<undef> if the session is
175expired or return the C<$id> if it is not.
176
ac4892f4 177=back
178
179=head1 BUGS
180
181All complex software has bugs lurking in it, and this module is no
182exception. If you find a bug please either email me, or add the bug
183to cpan-RT.
184
185=head1 AUTHOR
186
187Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
188
189=head1 COPYRIGHT AND LICENSE
190
191Copyright 2009 Infinity Interactive, Inc.
192
193L<http://www.iinteractive.com>
194
195This library is free software; you can redistribute it and/or modify
196it under the same terms as Perl itself.
197
198=cut
199
200