Commit | Line | Data |
06190e8b |
1 | package Plack::Session::State; |
2 | use strict; |
3 | use warnings; |
4 | |
3b4205cd |
5 | use Digest::SHA1 (); |
6 | |
ac4892f4 |
7 | use Plack::Util::Accessor qw[ |
8 | session_key |
9 | sid_generator |
6a695f07 |
10 | sid_validator |
ac4892f4 |
11 | ]; |
06190e8b |
12 | |
13 | sub 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 | |
26 | sub expire_session_id { |
27 | my ($self, $id) = @_; |
ac4892f4 |
28 | $self->{'_expired'}->{ $id }++; |
06190e8b |
29 | } |
30 | |
05b5f99d |
31 | sub is_session_expired { |
32 | my ($self, $id) = @_; |
33 | exists $self->{'_expired'}->{ $id } |
34 | } |
35 | |
bd992981 |
36 | sub check_expired { |
37 | my ($self, $id) = @_; |
ae35574f |
38 | return if $self->is_session_expired( $id ); |
06190e8b |
39 | return $id; |
40 | } |
41 | |
ae35574f |
42 | sub validate_session_id { |
43 | my ($self, $id) = @_; |
44 | $id =~ $self->sid_validator; |
56b9910a |
45 | } |
46 | |
06190e8b |
47 | sub get_session_id { |
48 | my ($self, $request) = @_; |
ae35574f |
49 | $self->extract( $request ) |
06190e8b |
50 | || |
bd992981 |
51 | $self->generate( $request ) |
52 | } |
53 | |
ae35574f |
54 | sub get_session_id_from_request { |
55 | my ($self, $request) = @_; |
56b9910a |
56 | $request->param( $self->session_key ); |
57 | } |
58 | |
bd992981 |
59 | sub 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 |
70 | sub generate { |
71 | my $self = shift; |
ac4892f4 |
72 | $self->sid_generator->( @_ ); |
bd992981 |
73 | } |
74 | |
fe1bfe7d |
75 | |
bd992981 |
76 | sub finalize { |
77 | my ($self, $id, $response) = @_; |
78 | (); |
06190e8b |
79 | } |
80 | |
fe1bfe7d |
81 | 1; |
ac4892f4 |
82 | |
83 | __END__ |
84 | |
85 | =pod |
86 | |
87 | =head1 NAME |
88 | |
89 | Plack::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 |
109 | This will maintain session state by passing the session through |
110 | the request params. It does not do this automatically though, |
111 | you are responsible for passing the session param. |
112 | |
113 | This should be considered the state "base" class (although |
114 | subclassing is not a requirement) and defines the spec for |
115 | all B<Plack::Session::State::*> modules. You will only |
116 | need to override a couple methods if you do subclass. See |
117 | L<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 |
125 | The C<%params> can include I<session_key>, I<sid_generator> and I<sid_checker> |
3d92cf47 |
126 | however in both cases a default will be provided for you. |
127 | |
ac4892f4 |
128 | =item B<session_key> |
129 | |
43f34c01 |
130 | This is the name of the session key, it default to 'plack_session'. |
131 | |
ac4892f4 |
132 | =item B<sid_generator> |
133 | |
3d92cf47 |
134 | This is a CODE ref used to generate unique session ids, by default |
135 | it will generate a SHA1 using fairly sufficient entropy. If you are |
136 | concerned or interested, just read the source. |
43f34c01 |
137 | |
6a695f07 |
138 | =item B<sid_validator> |
56b9910a |
139 | |
ae35574f |
140 | This 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 |
150 | Given a C<$request> this will first attempt to extract the session, |
151 | if the is expired or does not exist, it will then generate a new |
152 | session. The C<$request> is expected to be a L<Plack::Request> instance |
153 | or an object with an equivalent interface. |
154 | |
ae35574f |
155 | =item B<get_session_id_from_request ( $request )> |
156 | |
157 | This is the method used to extract the session id from a C<$request>. |
158 | Subclasses will often only need to override this method and the |
159 | C<finalize> method. |
160 | |
161 | =item B<validate_session_id ( $session_id )> |
162 | |
163 | This will use the C<sid_validator> regex and confirm that the |
164 | C<$session_id> is valid. |
6a695f07 |
165 | |
ac4892f4 |
166 | =item B<extract ( $request )> |
167 | |
43f34c01 |
168 | This will attempt to extract the session from a C<$request> by looking |
169 | for the C<session_key> in the C<$request> params. It will then check to |
a45f272f |
170 | see if the session is valid and that it has not expired. It will return |
171 | the session id if everything is good or undef otherwise. The C<$request> |
172 | is expected to be a L<Plack::Request> instance or an object with an |
173 | equivalent interface. |
43f34c01 |
174 | |
ac4892f4 |
175 | =item B<generate ( $request )> |
176 | |
43f34c01 |
177 | This will generate a new session id using the C<sid_generator> callback. |
178 | The C<$request> argument is not used by this method but is there for |
179 | use by subclasses. The C<$request> is expected to be a L<Plack::Request> |
180 | instance or an object with an equivalent interface. |
181 | |
ac4892f4 |
182 | =item B<finalize ( $session_id, $response )> |
183 | |
43f34c01 |
184 | Given a C<$session_id> and a C<$response> this will perform any |
185 | finalization nessecary to preserve state. This method is called by |
186 | the L<Plack::Session> C<finalize> method. The C<$response> is expected |
187 | to be a L<Plack::Response> instance or an object with an equivalent |
188 | interface. |
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 |
198 | This will mark the session for C<$id> as expired. This method is called |
199 | by the L<Plack::Session> C<expire> method. |
200 | |
201 | =item B<is_session_expired ( $id )> |
202 | |
203 | This will check to see if the session C<$id> has been marked as |
204 | expired. |
205 | |
ac4892f4 |
206 | =item B<check_expired ( $id )> |
207 | |
43f34c01 |
208 | Given an session C<$id> this will return C<undef> if the session is |
209 | expired or return the C<$id> if it is not. |
210 | |
ac4892f4 |
211 | =back |
212 | |
213 | =head1 BUGS |
214 | |
215 | All complex software has bugs lurking in it, and this module is no |
216 | exception. If you find a bug please either email me, or add the bug |
217 | to cpan-RT. |
218 | |
219 | =head1 AUTHOR |
220 | |
221 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
222 | |
223 | =head1 COPYRIGHT AND LICENSE |
224 | |
225 | Copyright 2009 Infinity Interactive, Inc. |
226 | |
227 | L<http://www.iinteractive.com> |
228 | |
229 | This library is free software; you can redistribute it and/or modify |
230 | it under the same terms as Perl itself. |
231 | |
232 | =cut |
233 | |
234 | |