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