| 1 | use strict; | ||
| 2 | package Web::Authenticate::Session::Storage::Handler::SQL; | ||
| 3 | $Web::Authenticate::Session::Storage::Handler::SQL::VERSION = '0.004'; | ||
| 4 | use Mouse; | ||
| 5 | use Carp; | ||
| 6 | use DateTime; | ||
| 7 | use Digest::SHA; | ||
| 8 | use DBIx::Raw; | ||
| 9 | use Web::Authenticate::Digest; | ||
| 10 | use Web::Authenticate::IpAddressProvider::EnvIpAddressProvider; | ||
| 11 | use Web::Authenticate::UserAgentProvider::EnvUserAgentProvider; | ||
| 12 | use Web::Authenticate::Session; | ||
| 13 | use Web::Authenticate::User::Storage::Handler::SQL; | ||
| 14 | #ABSTRACT: Implementation of Web::Authenticate::Session::Storage::Handler::Role that can be used with MySQL or SQLite. | ||
| 15 | |||
| 16 | with 'Web::Authenticate::Session::Storage::Handler::Role'; | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | has user_storage_handler => ( | ||
| 21 | does => 'Web::Authenticate::User::Storage::Handler::Role', | ||
| 22 | is => 'ro', | ||
| 23 | required => 1, | ||
| 24 | lazy => 1, | ||
| 25 | default => sub { | ||
| 26 | my ($self) = @_; | ||
| 27 | Web::Authenticate::User::Storage::Handler::SQL->new(dbix_raw => $self->dbix_raw); | ||
| 28 | }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | has sessions_table => ( | ||
| 33 | isa => 'Str', | ||
| 34 | is => 'ro', | ||
| 35 | required => 1, | ||
| 36 | default => 'sessions', | ||
| 37 | ); | ||
| 38 | |||
| 39 | |||
| 40 | has session_id_field => ( | ||
| 41 | isa => 'Str', | ||
| 42 | is => 'ro', | ||
| 43 | required => 1, | ||
| 44 | default => 'session_id', | ||
| 45 | ); | ||
| 46 | |||
| 47 | |||
| 48 | has user_id_field => ( | ||
| 49 | isa => 'Str', | ||
| 50 | is => 'ro', | ||
| 51 | required => 1, | ||
| 52 | default => 'user_id', | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | has require_same_user_agent => ( | ||
| 57 | isa => 'Bool', | ||
| 58 | is => 'ro', | ||
| 59 | required => 1, | ||
| 60 | default => undef, | ||
| 61 | |||
| 62 | ); | ||
| 63 | |||
| 64 | |||
| 65 | has require_same_ip_address => ( | ||
| 66 | isa => 'Bool', | ||
| 67 | is => 'ro', | ||
| 68 | required => 1, | ||
| 69 | default => undef, | ||
| 70 | ); | ||
| 71 | |||
| 72 | |||
| 73 | has digest => ( | ||
| 74 | does => 'Web::Authenticate::Digest::Role', | ||
| 75 | is => 'ro', | ||
| 76 | required => 1, | ||
| 77 | default => sub { Web::Authenticate::Digest->new }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | |||
| 81 | has user_agent_provider => ( | ||
| 82 | does => 'Web::Authenticate::UserAgentProvider::Role', | ||
| 83 | is => 'ro', | ||
| 84 | default => sub { Web::Authenticate::UserAgentProvider::EnvUserAgentProvider->new }, | ||
| 85 | ); | ||
| 86 | |||
| 87 | |||
| 88 | has ip_address_provider => ( | ||
| 89 | does => 'Web::Authenticate::IpAddressProvider::Role', | ||
| 90 | is => 'ro', | ||
| 91 | default => sub { Web::Authenticate::IpAddressProvider::EnvIpAddressProvider->new }, | ||
| 92 | ); | ||
| 93 | |||
| 94 | |||
| 95 | has user_agent_field => ( | ||
| 96 | isa => 'Str', | ||
| 97 | is => 'ro', | ||
| 98 | required => 1, | ||
| 99 | default => 'user_agent', | ||
| 100 | ); | ||
| 101 | |||
| 102 | |||
| 103 | has ip_address_field => ( | ||
| 104 | isa => 'Str', | ||
| 105 | is => 'ro', | ||
| 106 | required => 1, | ||
| 107 | default => 'ip_address', | ||
| 108 | ); | ||
| 109 | |||
| 110 | |||
| 111 | has expires_field => ( | ||
| 112 | isa => 'Str', | ||
| 113 | is => 'ro', | ||
| 114 | required => 1, | ||
| 115 | default => 'expires', | ||
| 116 | ); | ||
| 117 | |||
| 118 | |||
| 119 | has allow_multiple_session_per_user => ( | ||
| 120 | isa => 'Bool', | ||
| 121 | is => 'ro', | ||
| 122 | required => 1, | ||
| 123 | default => undef, | ||
| 124 | ); | ||
| 125 | |||
| 126 | |||
| 127 | has dbix_raw => ( | ||
| 128 | isa => 'DBIx::Raw', | ||
| 129 | is => 'ro', | ||
| 130 | required => 1, | ||
| 131 | ); | ||
| 132 | |||
| 133 | |||
| 134 | has columns => ( | ||
| 135 | isa => 'ArrayRef', | ||
| 136 | is => 'ro', | ||
| 137 | required => 1, | ||
| 138 | default => sub { [] }, | ||
| 139 | ); | ||
| 140 | |||
| 141 | |||
| 142 | has session_id_digest_hex => ( | ||
| 143 | isa => 'CodeRef', | ||
| 144 | is => 'ro', | ||
| 145 | required => 1, | ||
| 146 | default => sub { \&Digest::SHA::sha512_hex }, | ||
| 147 | ); | ||
| 148 | |||
| 149 | |||
| 150 | sub store_session { | ||
| 151 | my ($self, $user, $session_id, $expires, $session_values) = @_; | ||
| 152 | croak "must provide user" unless $user; | ||
| 153 | croak "must provide session_id" unless $session_id; | ||
| 154 | croak "must provide valid expires" unless $expires or $expires < 1; | ||
| 155 | |||
| 156 | my $sessions_table = $self->sessions_table; | ||
| 157 | my $user_id_field = $self->user_id_field; | ||
| 158 | my $session_id_field = $self->session_id_field; | ||
| 159 | my $expires_field = $self->expires_field; | ||
| 160 | |||
| 161 | $session_values //= {}; | ||
| 162 | $session_values->{$user_id_field} = $user->id; | ||
| 163 | $session_values->{$session_id_field} = $self->session_id_digest_hex->($session_id); | ||
| 164 | $session_values->{$expires_field} = $expires; | ||
| 165 | |||
| 166 | if ($self->require_same_user_agent) { | ||
| 167 | $session_values->{$self->user_agent_field} = $self->digest->generate($self->user_agent_provider->get_user_agent); | ||
| 168 | } | ||
| 169 | |||
| 170 | if ($self->require_same_ip_address) { | ||
| 171 | $session_values->{$self->ip_address_field} = $self->digest->generate($self->ip_address_provider->get_ip_address); | ||
| 172 | } | ||
| 173 | |||
| 174 | $self->dbix_raw->insert(href => $session_values, table => $sessions_table); | ||
| 175 | |||
| 176 | my $selection = $self->_get_selection($user_id_field, $session_id_field, $expires_field); | ||
| 177 | my $session = $self->dbix_raw->raw("SELECT $selection FROM $sessions_table WHERE $session_id_field = ?", $self->session_id_digest_hex->($session_id)); | ||
| 178 | |||
| 179 | unless ($session) { | ||
| 180 | carp "failed to insert session to database"; | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | return Web::Authenticate::Session->new(id => $session_id, expires => $session->{$expires_field}, user => $user, row => $session); | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | sub load_session { | ||
| 189 | my ($self, $session_id) = @_; | ||
| 190 | croak "must provide session_id" unless $session_id; | ||
| 191 | |||
| 192 | my $sessions_table = $self->sessions_table; | ||
| 193 | my $user_id_field = $self->user_id_field; | ||
| 194 | my $session_id_field = $self->session_id_field; | ||
| 195 | my $expires_field = $self->expires_field; | ||
| 196 | my $user_agent_field = $self->user_agent_field; | ||
| 197 | my $ip_address_field = $self->ip_address_field; | ||
| 198 | |||
| 199 | my $now = DateTime->now->epoch; | ||
| 200 | |||
| 201 | my @select_args; | ||
| 202 | push @select_args, ($user_id_field, $session_id_field, $expires_field); | ||
| 203 | |||
| 204 | if ($self->require_same_user_agent) { | ||
| 205 | push @select_args, $user_agent_field; | ||
| 206 | } | ||
| 207 | |||
| 208 | if ($self->require_same_ip_address) { | ||
| 209 | push @select_args, $ip_address_field; | ||
| 210 | } | ||
| 211 | |||
| 212 | my $selection = $self->_get_selection(@select_args); | ||
| 213 | my $session = $self->dbix_raw->raw("SELECT $selection FROM $sessions_table WHERE $session_id_field = ? AND $expires_field >= $now", $self->session_id_digest_hex->($session_id)); | ||
| 214 | |||
| 215 | unless ($session) { | ||
| 216 | return; | ||
| 217 | } | ||
| 218 | |||
| 219 | if ($self->require_same_user_agent) { | ||
| 220 | my $user_agent = $session->{$user_agent_field}; | ||
| 221 | unless ($user_agent) { | ||
| 222 | croak "there should be a user agent for session $session_id with digest_hex of " . $self->session_id_digest_hex->($session_id); | ||
| 223 | } | ||
| 224 | unless ($self->digest->validate($user_agent, $self->user_agent_provider->get_user_agent)) { | ||
| 225 | $self->delete_session($session_id); | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | if ($self->require_same_ip_address) { | ||
| 231 | my $ip_address = $session->{$ip_address_field}; | ||
| 232 | unless ($ip_address) { | ||
| 233 | croak "there should be an ip address for session $session_id with digest_hex of " . $self->session_id_digest_hex->($session_id); | ||
| 234 | } | ||
| 235 | unless ($self->digest->validate($ip_address, $self->ip_address_provider->get_ip_address)) { | ||
| 236 | $self->delete_session($session_id); | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | my $user = $self->user_storage_handler->load_user_by_id($session->{$user_id_field}); | ||
| 242 | |||
| 243 | unless ($user) { | ||
| 244 | carp "failed to load user from database"; | ||
| 245 | return; | ||
| 246 | } | ||
| 247 | |||
| 248 | return Web::Authenticate::Session->new(id => $session_id, expires => $session->{$expires_field}, user => $user, row => $session); | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | sub delete_session { | ||
| 253 | my ($self, $session_id) = @_; | ||
| 254 | croak "must provide session_id" unless $session_id; | ||
| 255 | |||
| 256 | my $sessions_table = $self->sessions_table; | ||
| 257 | my $session_id_field = $self->session_id_field; | ||
| 258 | $self->dbix_raw->raw("DELETE FROM $sessions_table WHERE $session_id_field = ?", $self->session_id_digest_hex->($session_id)); | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | sub invalidate_user_sessions { | ||
| 263 | my ($self, $user) = @_; | ||
| 264 | croak "must provide user" unless $user; | ||
| 265 | return if $self->allow_multiple_session_per_user; | ||
| 266 | |||
| 267 | my $sessions_table = $self->sessions_table; | ||
| 268 | my $user_id_field = $self->user_id_field; | ||
| 269 | $self->dbix_raw->raw("DELETE FROM $sessions_table WHERE $user_id_field = ?", $user->id); | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | sub update_expires { | ||
| 274 | my ($self, $session_id, $expires) = @_; | ||
| 275 | croak "must provide session_id" unless $session_id; | ||
| 276 | croak "must provide expires" unless $expires; | ||
| 277 | |||
| 278 | my $sessions_table = $self->sessions_table; | ||
| 279 | my $session_id_field = $self->session_id_field; | ||
| 280 | my $expires_field = $self->expires_field; | ||
| 281 | $self->dbix_raw->raw("UPDATE $sessions_table SET $expires_field = ? WHERE $session_id_field = ?", $expires, $self->session_id_digest_hex->($session_id)); | ||
| 282 | } | ||
| 283 | |||
| 284 | sub _get_selection { | ||
| 285 | my ($self, @columns) = @_; | ||
| 286 | |||
| 287 | die "columns required" unless @columns; | ||
| 288 | |||
| 289 | my $selection = join ',', @columns; | ||
| 290 | |||
| 291 | if (@{$self->columns}) { | ||
| 292 | $selection .= ',' . join ',', @{$self->columns}; | ||
| 293 | } | ||
| 294 | |||
| 295 | return $selection; | ||
| 296 | } | ||
| 297 | |||
| 298 | 1; | ||
| 299 | |||
| 300 | __END__ | ||
| 301 | |||
| 302 | =pod | ||
| 303 | |||
| 304 | =encoding UTF-8 | ||
| 305 | |||
| 306 | =head1 NAME | ||
| 307 | |||
| 308 | Web::Authenticate::Session::Storage::Handler::SQL - Implementation of Web::Authenticate::Session::Storage::Handler::Role that can be used with MySQL or SQLite. | ||
| 309 | |||
| 310 | =head1 VERSION | ||
| 311 | |||
| 312 | version 0.004 | ||
| 313 | |||
| 314 | =head1 DESCRIPTION | ||
| 315 | |||
| 316 | This L<Web::Authenticate::Session::Storage::Handler::Role> is meant to be used with a very specific table structure: | ||
| 317 | |||
| 318 | CREATE TABLE sessions ( | ||
| 319 | id INTEGER PRIMARY KEY AUTO_INCREMENT, | ||
| 320 | user_id INTEGER NOT NULL UNIQUE, | ||
| 321 | session_id VARCHAR(255) NOT NULL UNIQUE, | ||
| 322 | expires INTEGER NOT NULL | ||
| 323 | ); | ||
| 324 | |||
| 325 | Other columns can exists, and the names of the table and column can change. But at least these columns must exist | ||
| 326 | in order for this storage handler to work properly. Also, the primary key does not have to be an integer, and could | ||
| 327 | even be the session_id. Also, the user_id does not need to be unique if you want to allow multiple sessions for a | ||
| 328 | user by setting L</allow_multiple_session_per_user> to true. | ||
| 329 | |||
| 330 | =head1 METHODS | ||
| 331 | |||
| 332 | =head2 user_storage_handler | ||
| 333 | |||
| 334 | Sets the L<Web::Authenticate::User::Storage::Handler::Role> to be used. This is required. Default is | ||
| 335 | L<Web::Authenticate::User::Storage::Handler::SQL> created with L</dbix_raw>. | ||
| 336 | |||
| 337 | =head2 sessions_table | ||
| 338 | |||
| 339 | Sets the name of the sessions table that will be used when querying the database. Default is 'sessions'. | ||
| 340 | |||
| 341 | =head2 session_id_field | ||
| 342 | |||
| 343 | Sets the name of the session id field that will be used when querying the database. Default is 'session_id'. | ||
| 344 | |||
| 345 | =head2 user_id_field | ||
| 346 | |||
| 347 | Sets the name of the user_id field that will be used when querying the database. Default is 'user_id'. | ||
| 348 | |||
| 349 | =head2 require_same_user_agent | ||
| 350 | |||
| 351 | A session id will only be valid from the user agent it was originally created on. If invalid, the session will be deleted in storage. | ||
| 352 | If this is set to true, a user agent field is required in the table. It should be a varchar 255 like so: | ||
| 353 | |||
| 354 | CREATE TABLE sessions ( | ||
| 355 | id INTEGER PRIMARY KEY AUTO_INCREMENT, | ||
| 356 | user_id INTEGER NOT NULL UNIQUE, | ||
| 357 | session_id VARCHAR(255) NOT NULL UNIQUE, | ||
| 358 | user_agent VARCHAR(255), | ||
| 359 | expires INTEGER NOT NULL | ||
| 360 | ); | ||
| 361 | |||
| 362 | See L</user_agent_field> to change the default name of the field. | ||
| 363 | |||
| 364 | =head2 require_same_ip_address | ||
| 365 | |||
| 366 | A session id will only be valid from the ip address it was originally created from. If invalid, the session will be deleted in storage. | ||
| 367 | If this is set to true, an ip address field is required in the table. It should be a varchar 255 like so: | ||
| 368 | |||
| 369 | CREATE TABLE sessions ( | ||
| 370 | id INTEGER PRIMARY KEY AUTO_INCREMENT, | ||
| 371 | user_id INTEGER NOT NULL UNIQUE, | ||
| 372 | session_id VARCHAR(255) NOT NULL UNIQUE, | ||
| 373 | ip_address VARCHAR(255), | ||
| 374 | expires INTEGER NOT NULL | ||
| 375 | ); | ||
| 376 | |||
| 377 | See L</ip_address_field> to change the default name of the field. | ||
| 378 | |||
| 379 | =head2 digest | ||
| 380 | |||
| 381 | Sets the L<Web::Authenticate::Digest::Role> that is used for user agents if L</require_same_user_agent> is set to true or ip addresses if | ||
| 382 | L</require_same_ip_address> is set to true. Default is L<Web::Authenticate::Digest>. | ||
| 383 | |||
| 384 | =head2 user_agent_provider | ||
| 385 | |||
| 386 | Sets the object that does L<Web::Authenticate::UserAgentProvider::Role>. Default is L<Web::Authenticate::UserAgentProvider::EnvUserAgentProvider>. | ||
| 387 | |||
| 388 | =head2 ip_address_provider | ||
| 389 | |||
| 390 | Sets the object that does L<Web::Authenticate::IpAddressProvider::Role>. Default is L<Web::Authenticate::IpAddressProvider::EnvIpAdderssProvider>. | ||
| 391 | |||
| 392 | =head2 user_agent_field | ||
| 393 | |||
| 394 | Sets the name of the user agent field if L</require_same_user_agent> is set to 1. Default is 'user_agent'. | ||
| 395 | |||
| 396 | =head2 ip_address_field | ||
| 397 | |||
| 398 | Sets the name of the ip address field if L</require_same_ip_address> is set to 1. Default is 'ip_address'. | ||
| 399 | |||
| 400 | =head2 expires_field | ||
| 401 | |||
| 402 | Sets the name of the expires time field that will be used when querying the database. Default is 'expires'. | ||
| 403 | |||
| 404 | =head2 allow_multiple_session_per_user | ||
| 405 | |||
| 406 | A bool (1 or undef) whether or not to allow multiple sessions per user. If set to true, when L</invalidate_user_sessions> is | ||
| 407 | called nothing will happen. Default is false. | ||
| 408 | |||
| 409 | =head2 dbix_raw | ||
| 410 | |||
| 411 | Sets the L<DBIx::Raw> object that will be used to query the database. This is required with no default. | ||
| 412 | |||
| 413 | =head2 columns | ||
| 414 | |||
| 415 | The columns to select. At a minimum, L</session_id_field>, L</user_id_field>, and L</expires_field> will always be selected. | ||
| 416 | |||
| 417 | $storage_handler->columns([qw/session_data/]); | ||
| 418 | |||
| 419 | Default is an empty array ref. | ||
| 420 | |||
| 421 | =head2 session_id_digest_hex | ||
| 422 | |||
| 423 | This is a subroutine reference to the digest hex that will be used when storing the session id. | ||
| 424 | Default is L<Digest::SHA::sha512_hex|Digest::SHA>. | ||
| 425 | |||
| 426 | =head2 store_session | ||
| 427 | |||
| 428 | Takes in user, session_id, expires, and any additional values for columns in a hash and a session with those values is created. | ||
| 429 | Returns a L<Web::Authenticate::Session> with any values from L</columns> stored in L<Web::Authenticate::Session/row>. | ||
| 430 | |||
| 431 | my $session_values => { | ||
| 432 | session_data => $session_data, | ||
| 433 | }; | ||
| 434 | my $session = $session_storage_handler->store_session($user, $session_id, $expires, $session_values); | ||
| 435 | |||
| 436 | # if you need no extra user values in the row | ||
| 437 | my $session = $storage_handler->store_session($user, $session_id, $expires); | ||
| 438 | |||
| 439 | =head2 load_session | ||
| 440 | |||
| 441 | Loads a L<Web::Authenticate::Session> by session_id. If the session exists, the session is returned. | ||
| 442 | Otherwise, undef is returned. Undef will also be returned if the session is expires, or if the user agent or ip address don't match | ||
| 443 | if L</require_same_user_agent> or L</require_same_ip_address> are set. | ||
| 444 | Any additional L</columns> will be stored in L<Web::Authenticate::Session/row>. | ||
| 445 | |||
| 446 | my $session = $session_storage_handler->load_session($session_id); | ||
| 447 | |||
| 448 | =head2 delete_session | ||
| 449 | |||
| 450 | Deletes a session from storage. | ||
| 451 | |||
| 452 | $session_storage_handler->delete_session($session_id); | ||
| 453 | |||
| 454 | =head2 invalidate_user_sessions | ||
| 455 | |||
| 456 | Invalidates (deletes) any user sessions for user unless L</allow_multiple_session_per_user> is set to true. | ||
| 457 | |||
| 458 | $session_storage_handler->invalidate_user_sessions($user); | ||
| 459 | |||
| 460 | =head2 update_expires | ||
| 461 | |||
| 462 | Updates the expires time for the session with the session id session_id. | ||
| 463 | |||
| 464 | $session_storage_handler->update_expires($session_id, $expires); | ||
| 465 | |||
| 466 | =head1 AUTHOR | ||
| 467 | |||
| 468 | Adam Hopkins <srchulo@cpan.org> | ||
| 469 | |||
| 470 | =head1 COPYRIGHT AND LICENSE | ||
| 471 | |||
| 472 | This software is copyright (c) 2017 by Adam Hopkins. | ||
| 473 | |||
| 474 | This is free software; you can redistribute it and/or modify it under | ||
| 475 | the same terms as the Perl 5 programming language system itself. | ||
| 476 | |||
| 477 | =cut |