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