blib/lib/Web/Authenticate/Session/Storage/Handler/SQL.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 148 CC 15 SCAR 26.0
CC
15
Cov
0%
store_session
30.0
load_session
20.0
invalidate_user_sessions
12.0
CRAP 13.5
1 use strict;
2 package Web::Authenticate::Session::Storage::Handler::SQL;
3 $Web::Authenticate::Session::Storage::Handler::SQL::VERSION = '0.003';
4 use Mouse;
5 use Carp;
6 use DateTime;
7 use DBIx::Raw;
8 use Web::Authenticate::Session;
9 use Web::Authenticate::User::Storage::Handler::SQL;
10 #ABSTRACT: Implementation of Web::Authenticate::Session::Storage::Handler::Role that can be used with MySQL or SQLite.
11  
12 with 'Web::Authenticate::Session::Storage::Handler::Role';
13  
14  
15  
16 has user_storage_handler => (
17 does => 'Web::Authenticate::User::Storage::Handler::Role',
18 is => 'ro',
19 required => 1,
20 lazy => 1,
21 default => sub {
22 my ($self) = @_;
23 Web::Authenticate::User::Storage::Handler::SQL->new(dbix_raw => $self->dbix_raw);
24 },
25 );
26  
27  
28 has sessions_table => (
29 isa => 'Str',
30 is => 'ro',
31 required => 1,
32 default => 'sessions',
33 );
34  
35  
36 has session_id_field => (
37 isa => 'Str',
38 is => 'ro',
39 required => 1,
40 default => 'session_id',
41 );
42  
43  
44 has user_id_field => (
45 isa => 'Str',
46 is => 'ro',
47 required => 1,
48 default => 'user_id',
49 );
50  
51  
52 has expires_field => (
53 isa => 'Str',
54 is => 'ro',
55 required => 1,
56 default => 'expires',
57 );
58  
59  
60 has allow_multiple_session_per_user => (
61 isa => 'Bool',
62 is => 'ro',
63 required => 1,
64 default => undef,
65 );
66  
67  
68 has dbix_raw => (
69 isa => 'DBIx::Raw',
70 is => 'ro',
71 required => 1,
72 );
73  
74  
75 has columns => (
76 isa => 'ArrayRef',
77 is => 'ro',
78 required => 1,
79 default => sub { [] },
80 );
81  
82  
83 sub store_session {
84 my ($self, $user, $session_id, $expires, $session_values) = @_;
85 croak "must provide user" unless $user;
86 croak "must provide session_id" unless $session_id;
87 croak "must provide valid expires" unless $expires or $expires < 1;
88  
89 my $sessions_table = $self->sessions_table;
90 my $user_id_field = $self->user_id_field;
91 my $session_id_field = $self->session_id_field;
92 my $expires_field = $self->expires_field;
93  
94 $session_values //= {};
95 $session_values->{$user_id_field} = $user->id;
96 $session_values->{$session_id_field} = $session_id;
97 $session_values->{$expires_field} = $expires;
98  
99 $self->dbix_raw->insert(href => $session_values, table => $sessions_table);
100  
101 my $selection = $self->_get_selection($user_id_field, $session_id_field, $expires_field);
102 my $session = $self->dbix_raw->raw("SELECT $selection FROM $sessions_table WHERE $session_id_field = ?", $session_id);
103  
104 unless ($session) {
105 carp "failed to insert session to database";
106 return;
107 }
108  
109 return Web::Authenticate::Session->new(id => $session->{$session_id_field}, expires => $session->{$expires_field}, user => $user, row => $session);
110 }
111  
112  
113 sub load_session {
114 my ($self, $session_id) = @_;
115 croak "must provide session_id" unless $session_id;
116  
117 my $sessions_table = $self->sessions_table;
118 my $user_id_field = $self->user_id_field;
119 my $session_id_field = $self->session_id_field;
120 my $expires_field = $self->expires_field;
121  
122 my $now = DateTime->now->epoch;
123 my $selection = $self->_get_selection($user_id_field, $session_id_field, $expires_field);
124 my $session = $self->dbix_raw->raw("SELECT $selection FROM $sessions_table WHERE $session_id_field = ? AND $expires_field >= $now", $session_id);
125  
126 unless ($session) {
127 return;
128 }
129  
130 my $user = $self->user_storage_handler->load_user_by_id($session->{$user_id_field});
131  
132 unless ($user) {
133 carp "failed to load user from database";
134 return;
135 }
136  
137 return Web::Authenticate::Session->new(id => $session->{$session_id_field}, expires => $session->{$expires_field}, user => $user, row => $session);
138 }
139  
140  
141 sub delete_session {
142 my ($self, $session_id) = @_;
143 croak "must provide session_id" unless $session_id;
144  
145 my $sessions_table = $self->sessions_table;
146 my $session_id_field = $self->session_id_field;
147 $self->dbix_raw->raw("DELETE FROM $sessions_table WHERE $session_id_field = ?", $session_id);
148 }
149  
150  
151 sub invalidate_user_sessions {
152 my ($self, $user) = @_;
153 croak "must provide user" unless $user;
154 return if $self->allow_multiple_session_per_user;
155  
156 my $sessions_table = $self->sessions_table;
157 my $user_id_field = $self->user_id_field;
158 $self->dbix_raw->raw("DELETE FROM $sessions_table WHERE $user_id_field = ?", $user->id);
159 }
160  
161  
162 sub update_expires {
163 my ($self, $session_id, $expires) = @_;
164 croak "must provide session_id" unless $session_id;
165 croak "must provide expires" unless $expires;
166  
167 my $sessions_table = $self->sessions_table;
168 my $session_id_field = $self->session_id_field;
169 my $expires_field = $self->expires_field;
170 $self->dbix_raw->raw("UPDATE $sessions_table SET $expires_field = ? WHERE $session_id_field = ?", $expires, $session_id);
171 }
172  
173 sub _get_selection {
174 my ($self, @columns) = @_;
175  
176 die "columns required" unless @columns;
177  
178 my $selection = join ',', @columns;
179  
180 if (@{$self->columns}) {
181 $selection .= ',' . join ',', @{$self->columns};
182 }
183  
184 return $selection;
185 }
186  
187 1;
188  
189 __END__
190  
191 =pod
192  
193 =encoding UTF-8
194  
195 =head1 NAME
196  
197 Web::Authenticate::Session::Storage::Handler::SQL - Implementation of Web::Authenticate::Session::Storage::Handler::Role that can be used with MySQL or SQLite.
198  
199 =head1 VERSION
200  
201 version 0.003
202  
203 =head1 DESCRIPTION
204  
205 This L<Web::Authenticate::Session::Storage::Handler::Role> is meant to be used with a very specific table structure:
206  
207 CREATE TABLE sessions (
208 id INTEGER PRIMARY KEY AUTO_INCREMENT,
209 user_id INTEGER NOT NULL UNIQUE,
210 session_id VARCHAR(255) NOT NULL UNIQUE,
211 expires INTEGER NOT NULL
212 );
213  
214 Other columns can exists, and the names of the table and column can change. But at least these columns must exist
215 in order for this storage handler to work properly. Also, the primary key does not have to be an integer, and could
216 even be the session_id. Also, the user_id does not need to be unique if you want to allow multiple sessions for a
217 user by setting L</allow_multiple_session_per_user> to true.
218  
219 =head1 METHODS
220  
221 =head2 user_storage_handler
222  
223 Sets the L<Web::Authenticate::User::Storage::Handler::Role> to be used. This is required. Default is
224 L<Web::Authenticate::User::Storage::Handler::SQL> created with L</dbix_raw>.
225  
226 =head2 sessions_table
227  
228 Sets the name of the sessions table that will be used when querying the database. Default is 'sessions'.
229  
230 =head2 session_id_field
231  
232 Sets the name of the session id field that will be used when querying the database. Default is 'session_id'.
233  
234 =head2 user_id_field
235  
236 Sets the name of the user_id field that will be used when querying the database. Default is 'user_id'.
237  
238 =head2 expires_field
239  
240 Sets the name of the expires time field that will be used when querying the database. Default is 'expires'.
241  
242 =head2 allow_multiple_session_per_user
243  
244 A bool (1 or undef) whether or not to allow multiple sessions per user. If set to true, when L</invalidate_user_sessions> is
245 called nothing will happen. Default is false.
246  
247 =head2 dbix_raw
248  
249 Sets the L<DBIx::Raw> object that will be used to query the database. This is required with no default.
250  
251 =head2 columns
252  
253 The columns to select. At a minimum, L</session_id_field>, L</user_id_field>, and L</expires_field> will always be selected.
254  
255 $storage_handler->columns([qw/session_data/]);
256  
257 Default is an empty array ref.
258  
259 =head2 store_session
260  
261 Takes in user, session_id, expires, and any additional values for columns in a hash and a session with those values is created.
262 Returns a L<Web::Authenticate::Session> with any values from L</columns> stored in L<Web::Authenticate::Session/row>.
263  
264 my $session_values => {
265 session_data => $session_data,
266 };
267 my $session = $session_storage_handler->store_session($user, $session_id, $expires, $session_values);
268  
269 # if you need no extra user values in the row
270 my $session = $storage_handler->store_session($user, $session_id, $expires);
271  
272 =head2 load_session
273  
274 Loads a L<Web::Authenticate::Session> by session_id. If the session exists, the session is returned.
275 Otherwise, undef is returned. Any additional L</columns> will be stored in L<Web::Authenticate::Session/row>.
276  
277 my $session = $session_storage_handler->load_session($session_id);
278  
279 =head2 delete_session
280  
281 Deletes a session from storage.
282  
283 $session_storage_handler->delete_session($session_id);
284  
285 =head2 invalidate_user_sessions
286  
287 Invalidates (deletes) any user sessions for user unless L</allow_multiple_session_per_user> is set to true.
288  
289 $session_storage_handler->invalidate_user_sessions($user);
290  
291 =head2 update_expires
292  
293 Updates the expires time for the session with the session id session_id.
294  
295 $session_storage_handler->update_expires($session_id, $expires);
296  
297 =head1 AUTHOR
298  
299 Adam Hopkins <srchulo@cpan.org>
300  
301 =head1 COPYRIGHT AND LICENSE
302  
303 This software is copyright (c) 2017 by Adam Hopkins.
304  
305 This is free software; you can redistribute it and/or modify it under
306 the same terms as the Perl 5 programming language system itself.
307  
308 =cut