Reworked Session to make the expiration a store's responsibility and
[catagits/Web-Session.git] / lib / Plack / Session / State.pm
CommitLineData
06190e8b 1package Plack::Session::State;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
3b4205cd 8use Digest::SHA1 ();
9
ac4892f4 10use Plack::Util::Accessor qw[
11 session_key
12 sid_generator
6a695f07 13 sid_validator
ac4892f4 14];
06190e8b 15
16sub new {
17 my ($class, %params) = @_;
ac4892f4 18
ac4892f4 19 $params{'session_key'} ||= 'plack_session';
20 $params{'sid_generator'} ||= sub {
ac4892f4 21 Digest::SHA1::sha1_hex(rand() . $$ . {} . time)
22 };
6a695f07 23 $params{'sid_validator'} ||= qr/\A[0-9a-f]{40}\Z/;
ac4892f4 24
25 bless { %params } => $class;
06190e8b 26}
27
28sub expire_session_id {
caf3bd90 29 my ($self, $id, $response) = @_;
06190e8b 30}
31
ae35574f 32sub validate_session_id {
33 my ($self, $id) = @_;
34 $id =~ $self->sid_validator;
56b9910a 35}
36
06190e8b 37sub get_session_id {
38 my ($self, $request) = @_;
4a0cb5a0 39 return $request->param( $self->session_key );
56b9910a 40}
41
bd992981 42sub extract {
43 my ($self, $request) = @_;
56b9910a 44
4a0cb5a0 45 my $id = $self->get_session_id( $request );
ae35574f 46 return unless defined $id;
47
caf3bd90 48 return $id if $self->validate_session_id( $id );
49 return;
bd992981 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
56b9910a 107The C<%params> can include I<session_key>, I<sid_generator> and I<sid_checker>
3d92cf47 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
6a695f07 120=item B<sid_validator>
56b9910a 121
ae35574f 122This is a regex used to validate requested session id.
56b9910a 123
ac4892f4 124=back
125
43f34c01 126=head2 Session ID Managment
127
ac4892f4 128=over 4
129
130=item B<get_session_id ( $request )>
131
ae35574f 132This is the method used to extract the session id from a C<$request>.
133Subclasses will often only need to override this method and the
134C<finalize> method.
135
136=item B<validate_session_id ( $session_id )>
137
138This will use the C<sid_validator> regex and confirm that the
139C<$session_id> is valid.
6a695f07 140
ac4892f4 141=item B<extract ( $request )>
142
43f34c01 143This will attempt to extract the session from a C<$request> by looking
144for the C<session_key> in the C<$request> params. It will then check to
a45f272f 145see if the session is valid and that it has not expired. It will return
146the session id if everything is good or undef otherwise. The C<$request>
147is expected to be a L<Plack::Request> instance or an object with an
148equivalent interface.
43f34c01 149
ac4892f4 150=item B<generate ( $request )>
151
43f34c01 152This will generate a new session id using the C<sid_generator> callback.
153The C<$request> argument is not used by this method but is there for
154use by subclasses. The C<$request> is expected to be a L<Plack::Request>
155instance or an object with an equivalent interface.
156
ac4892f4 157=item B<finalize ( $session_id, $response )>
158
43f34c01 159Given a C<$session_id> and a C<$response> this will perform any
160finalization nessecary to preserve state. This method is called by
161the L<Plack::Session> C<finalize> method. The C<$response> is expected
162to be a L<Plack::Response> instance or an object with an equivalent
163interface.
164
ac4892f4 165=back
166
43f34c01 167=head2 Session Expiration Handling
168
ac4892f4 169=over 4
170
caf3bd90 171=item B<expire_session_id ( $id, $response )>
ac4892f4 172
43f34c01 173This will mark the session for C<$id> as expired. This method is called
174by the L<Plack::Session> C<expire> method.
175
ac4892f4 176=back
177
178=head1 BUGS
179
180All complex software has bugs lurking in it, and this module is no
181exception. If you find a bug please either email me, or add the bug
182to cpan-RT.
183
184=head1 AUTHOR
185
186Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
187
188=head1 COPYRIGHT AND LICENSE
189
000c696e 190Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 191
192L<http://www.iinteractive.com>
193
194This library is free software; you can redistribute it and/or modify
195it under the same terms as Perl itself.
196
197=cut
198
199