Commit | Line | Data |
06190e8b |
1 | package Plack::Session::State; |
2 | use strict; |
3 | use warnings; |
4 | |
ac4892f4 |
5 | use Plack::Util::Accessor qw[ |
6 | session_key |
7 | sid_generator |
8 | ]; |
06190e8b |
9 | |
10 | sub 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 | |
23 | sub expire_session_id { |
24 | my ($self, $id) = @_; |
ac4892f4 |
25 | $self->{'_expired'}->{ $id }++; |
06190e8b |
26 | } |
27 | |
05b5f99d |
28 | sub is_session_expired { |
29 | my ($self, $id) = @_; |
30 | exists $self->{'_expired'}->{ $id } |
31 | } |
32 | |
bd992981 |
33 | sub check_expired { |
34 | my ($self, $id) = @_; |
05b5f99d |
35 | return unless $id && not $self->is_session_expired( $id ); |
06190e8b |
36 | return $id; |
37 | } |
38 | |
06190e8b |
39 | sub get_session_id { |
40 | my ($self, $request) = @_; |
41 | $self->extract( $request ) |
42 | || |
bd992981 |
43 | $self->generate( $request ) |
44 | } |
45 | |
46 | sub extract { |
47 | my ($self, $request) = @_; |
48 | $self->check_expired( $request->param( $self->session_key ) ); |
49 | } |
50 | |
fe1bfe7d |
51 | sub generate { |
52 | my $self = shift; |
ac4892f4 |
53 | $self->sid_generator->( @_ ); |
bd992981 |
54 | } |
55 | |
fe1bfe7d |
56 | |
bd992981 |
57 | sub finalize { |
58 | my ($self, $id, $response) = @_; |
59 | (); |
06190e8b |
60 | } |
61 | |
fe1bfe7d |
62 | 1; |
ac4892f4 |
63 | |
64 | __END__ |
65 | |
66 | =pod |
67 | |
68 | =head1 NAME |
69 | |
70 | Plack::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 |
82 | This is the name of the session key, it default to 'plack_session'. |
83 | |
ac4892f4 |
84 | =item B<sid_generator> |
85 | |
43f34c01 |
86 | This 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 |
96 | Given a C<$request> this will first attempt to extract the session, |
97 | if the is expired or does not exist, it will then generate a new |
98 | session. The C<$request> is expected to be a L<Plack::Request> instance |
99 | or an object with an equivalent interface. |
100 | |
ac4892f4 |
101 | =item B<extract ( $request )> |
102 | |
43f34c01 |
103 | This will attempt to extract the session from a C<$request> by looking |
104 | for the C<session_key> in the C<$request> params. It will then check to |
105 | see if the session has expired and return the session id if it is not. |
106 | The C<$request> is expected to be a L<Plack::Request> instance or an |
107 | object with an equivalent interface. |
108 | |
ac4892f4 |
109 | =item B<generate ( $request )> |
110 | |
43f34c01 |
111 | This will generate a new session id using the C<sid_generator> callback. |
112 | The C<$request> argument is not used by this method but is there for |
113 | use by subclasses. The C<$request> is expected to be a L<Plack::Request> |
114 | instance or an object with an equivalent interface. |
115 | |
ac4892f4 |
116 | =item B<finalize ( $session_id, $response )> |
117 | |
43f34c01 |
118 | Given a C<$session_id> and a C<$response> this will perform any |
119 | finalization nessecary to preserve state. This method is called by |
120 | the L<Plack::Session> C<finalize> method. The C<$response> is expected |
121 | to be a L<Plack::Response> instance or an object with an equivalent |
122 | interface. |
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 |
132 | This will mark the session for C<$id> as expired. This method is called |
133 | by the L<Plack::Session> C<expire> method. |
134 | |
135 | =item B<is_session_expired ( $id )> |
136 | |
137 | This will check to see if the session C<$id> has been marked as |
138 | expired. |
139 | |
ac4892f4 |
140 | =item B<check_expired ( $id )> |
141 | |
43f34c01 |
142 | Given an session C<$id> this will return C<undef> if the session is |
143 | expired or return the C<$id> if it is not. |
144 | |
ac4892f4 |
145 | =back |
146 | |
147 | =head1 BUGS |
148 | |
149 | All complex software has bugs lurking in it, and this module is no |
150 | exception. If you find a bug please either email me, or add the bug |
151 | to cpan-RT. |
152 | |
153 | =head1 AUTHOR |
154 | |
155 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
156 | |
157 | =head1 COPYRIGHT AND LICENSE |
158 | |
159 | Copyright 2009 Infinity Interactive, Inc. |
160 | |
161 | L<http://www.iinteractive.com> |
162 | |
163 | This library is free software; you can redistribute it and/or modify |
164 | it under the same terms as Perl itself. |
165 | |
166 | =cut |
167 | |
168 | |