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