docs, docs and more docs
[catagits/Web-Session.git] / lib / Plack / Session / State.pm
CommitLineData
06190e8b 1package Plack::Session::State;
2use strict;
3use warnings;
4
ac4892f4 5use Plack::Util::Accessor qw[
6 session_key
7 sid_generator
8];
06190e8b 9
10sub new {
11 my ($class, %params) = @_;
ac4892f4 12
13 $params{'_expired'} ||= +{};
14 $params{'session_key'} ||= 'plack_session';
15 $params{'sid_generator'} ||= sub {
16 require Digest::SHA1;
17 Digest::SHA1::sha1_hex(rand() . $$ . {} . time)
18 };
19
20 bless { %params } => $class;
06190e8b 21}
22
23sub expire_session_id {
24 my ($self, $id) = @_;
ac4892f4 25 $self->{'_expired'}->{ $id }++;
06190e8b 26}
27
05b5f99d 28sub is_session_expired {
29 my ($self, $id) = @_;
30 exists $self->{'_expired'}->{ $id }
31}
32
bd992981 33sub check_expired {
34 my ($self, $id) = @_;
05b5f99d 35 return unless $id && not $self->is_session_expired( $id );
06190e8b 36 return $id;
37}
38
06190e8b 39sub get_session_id {
40 my ($self, $request) = @_;
41 $self->extract( $request )
42 ||
bd992981 43 $self->generate( $request )
44}
45
46sub extract {
47 my ($self, $request) = @_;
48 $self->check_expired( $request->param( $self->session_key ) );
49}
50
fe1bfe7d 51sub generate {
52 my $self = shift;
ac4892f4 53 $self->sid_generator->( @_ );
bd992981 54}
55
fe1bfe7d 56
bd992981 57sub finalize {
58 my ($self, $id, $response) = @_;
59 ();
06190e8b 60}
61
fe1bfe7d 621;
ac4892f4 63
64__END__
65
66=pod
67
68=head1 NAME
69
70Plack::Session::State - Basic parameter-based session state
71
72=head1 DESCRIPTION
73
74=head1 METHODS
75
76=over 4
77
78=item B<new ( %params )>
79
80=item B<session_key>
81
43f34c01 82This is the name of the session key, it default to 'plack_session'.
83
ac4892f4 84=item B<sid_generator>
85
43f34c01 86This is a CODE ref used to generate unique session ids.
87
ac4892f4 88=back
89
43f34c01 90=head2 Session ID Managment
91
ac4892f4 92=over 4
93
94=item B<get_session_id ( $request )>
95
43f34c01 96Given a C<$request> this will first attempt to extract the session,
97if the is expired or does not exist, it will then generate a new
98session. The C<$request> is expected to be a L<Plack::Request> instance
99or an object with an equivalent interface.
100
ac4892f4 101=item B<extract ( $request )>
102
43f34c01 103This will attempt to extract the session from a C<$request> by looking
104for the C<session_key> in the C<$request> params. It will then check to
105see if the session has expired and return the session id if it is not.
106The C<$request> is expected to be a L<Plack::Request> instance or an
107object with an equivalent interface.
108
ac4892f4 109=item B<generate ( $request )>
110
43f34c01 111This will generate a new session id using the C<sid_generator> callback.
112The C<$request> argument is not used by this method but is there for
113use by subclasses. The C<$request> is expected to be a L<Plack::Request>
114instance or an object with an equivalent interface.
115
ac4892f4 116=item B<finalize ( $session_id, $response )>
117
43f34c01 118Given a C<$session_id> and a C<$response> this will perform any
119finalization nessecary to preserve state. This method is called by
120the L<Plack::Session> C<finalize> method. The C<$response> is expected
121to be a L<Plack::Response> instance or an object with an equivalent
122interface.
123
ac4892f4 124=back
125
43f34c01 126=head2 Session Expiration Handling
127
ac4892f4 128=over 4
129
130=item B<expire_session_id ( $id )>
131
43f34c01 132This will mark the session for C<$id> as expired. This method is called
133by the L<Plack::Session> C<expire> method.
134
135=item B<is_session_expired ( $id )>
136
137This will check to see if the session C<$id> has been marked as
138expired.
139
ac4892f4 140=item B<check_expired ( $id )>
141
43f34c01 142Given an session C<$id> this will return C<undef> if the session is
143expired or return the C<$id> if it is not.
144
ac4892f4 145=back
146
147=head1 BUGS
148
149All complex software has bugs lurking in it, and this module is no
150exception. If you find a bug please either email me, or add the bug
151to cpan-RT.
152
153=head1 AUTHOR
154
155Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
156
157=head1 COPYRIGHT AND LICENSE
158
159Copyright 2009 Infinity Interactive, Inc.
160
161L<http://www.iinteractive.com>
162
163This library is free software; you can redistribute it and/or modify
164it under the same terms as Perl itself.
165
166=cut
167
168