| 1 | package Mojolicious::Plugin::RoutesAuthDBI::Access; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | use Exporter 'import'; | ||
| 4 | our @EXPORT_OK = qw(load_user validate_user); | ||
| 5 | use Mojolicious::Plugin::RoutesAuthDBI::Util qw(load_class); | ||
| 6 | use Mojo::Util qw(md5_sum); | ||
| 7 | #~ use Mojo::Exception; | ||
| 8 | |||
| 9 | has [qw(app plugin)], undef, weak=>1; | ||
| 10 | has qw(model); | ||
| 11 | |||
| 12 | #~ has model_profiles => sub { shift->plugin->model('Profiles') }; | ||
| 13 | #~ has model => sub { | ||
| 14 | #~ { map $_ => load_class("Mojolicious::Plugin::RoutesAuthDBI::Model::$_")->new, qw(Profiles Namespaces Routes Refs Controllers Actions Roles) } | ||
| 15 | #~ }; | ||
| 16 | |||
| 17 | sub new {# from plugin! init Class vars | ||
| 18 | state $self = shift->SUPER::new(@_); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub load_user {# import for Mojolicious::Plugin::Authentication | ||
| 22 | my ($c, $uid) = @_; | ||
| 23 | |||
| 24 | my $p = load_class("Mojolicious::Plugin::RoutesAuthDBI::Model::Profiles")->get_profile($uid, undef); | ||
| 25 | #~ my $p = $c->model_profiles->get_profile($uid, undef); | ||
| 26 | |||
| 27 | $c->app->log->debug("Loading profile by id=$uid failed") | ||
| 28 | and return undef | ||
| 29 | unless $p && $p->{id}; | ||
| 30 | |||
| 31 | #~ eval { Mojo::Exception->throw('load_user') }; | ||
| 32 | #~ $c->app->log->debug($c->dumper($_)) for @{$@->frames}; | ||
| 33 | |||
| 34 | $c->app->log->debug("Loading profile by id=$uid success"); | ||
| 35 | $p->{pass} = '**********************'; | ||
| 36 | return $p; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub validate_user {# import for Mojolicious::Plugin::Authentication | ||
| 40 | my ($c, $login, $pass, $extradata) = @_; | ||
| 41 | |||
| 42 | return $extradata->{id} | ||
| 43 | if $extradata && $extradata->{id}; | ||
| 44 | |||
| 45 | if (my $p = load_class("Mojolicious::Plugin::RoutesAuthDBI::Model::Profiles")->get_profile(undef, $login)) { | ||
| 46 | #~ if (my $p = $c->model_profiles->get_profile(undef, $login)) { | ||
| 47 | $c->app->log->debug("Success authenticate by login[$login]/pass[$pass] for profile id[$p->{id}]") | ||
| 48 | and return $p->{id} | ||
| 49 | if ($p->{pass} eq $pass || $p->{pass} eq md5_sum($pass)) | ||
| 50 | && !$p->{disable}; | ||
| 51 | |||
| 52 | $c->app->log->debug("Failure authenticate by login[$login]/pass[$pass]:[@{[md5_sum($pass)]}] for profile id[$p->{id}:$p->{pass}]"); | ||
| 53 | |||
| 54 | return undef; | ||
| 55 | } | ||
| 56 | |||
| 57 | $c->app->log->debug("Failure authenticate by login[$login]/pass[$pass]:[@{[md5_sum($pass)]}]"); | ||
| 58 | |||
| 59 | return undef; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub apply_ns {# Plugin | ||
| 63 | my ($self,) = @_; | ||
| 64 | my $ns = $self->plugin->model('Namespaces')->app_ns; | ||
| 65 | return unless @$ns; | ||
| 66 | my $r = $self->app->routes; | ||
| 67 | push @{ $r->namespaces() }, $_->{namespace} for @$ns; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub apply_route {# meth in Plugin | ||
| 71 | my ($self, $r_hash) = @_;# $r_hash - это строка запроса маршрута из БД | ||
| 72 | my $r = $self->app->routes; | ||
| 73 | |||
| 74 | $self->app->log->debug("Skip disabled route id=[$r_hash->{id}] [$r_hash->{request}]") | ||
| 75 | and return undef | ||
| 76 | if $r_hash->{disable}; | ||
| 77 | |||
| 78 | $r_hash->{request} //= $r_hash->{route}; | ||
| 79 | |||
| 80 | $self->app->log->debug("Skip route @{[$self->app->dumper($r_hash) =~ s/\s+//gr]}: empty request") | ||
| 81 | and return undef | ||
| 82 | unless $r_hash->{request}; | ||
| 83 | |||
| 84 | $self->app->log->debug("Skip comment request [$r_hash->{request}]") | ||
| 85 | and return undef | ||
| 86 | if $r_hash->{request} =~ /^#/; | ||
| 87 | |||
| 88 | my @request = grep /\S/, split /\s+/, $r_hash->{request} | ||
| 89 | or $self->app->log->debug("Skip route @{[$self->app->dumper($r_hash) =~ s/\s+//gr]}: bad request") | ||
| 90 | and return; | ||
| 91 | |||
| 92 | my $nr; | ||
| 93 | if (@request eq 2 && $request[0] =~ /websocket|ws/i) { | ||
| 94 | $nr = $r->websocket(pop @request); | ||
| 95 | } else { | ||
| 96 | $nr = $r->any(pop @request);#Mojolicious::Routes::Route::route is DEPRECATED | ||
| 97 | $nr->methods(@request)# Deprecated Mojolicious::Routes::Route::via in favor of Mojolicious::Routes::Route::methods. | ||
| 98 | if @request; | ||
| 99 | } | ||
| 100 | # STEP AUTH не катит! только один over! | ||
| 101 | #~ $nr->over(authenticated=>$r_hash->{auth}); | ||
| 102 | # STEP ACCESS | ||
| 103 | $nr->requires(access => $r_hash);# Deprecated Mojolicious::Routes::Route::over in favor of Mojolicious::Routes::Route::requires | ||
| 104 | my $host = eval($r_hash->{host_re} || $r_hash->{host} || ''); | ||
| 105 | $nr->requires(host => $host) | ||
| 106 | if $host; | ||
| 107 | |||
| 108 | # Controller and action in Mojolicious::Routes::Route->to | ||
| 109 | #~ elsif ($shortcut =~ /^([\w\-:]+)?\#(\w+)?$/) { | ||
| 110 | #~ $defaults{controller} = $1 if defined $1; | ||
| 111 | #~ $defaults{action} = $2 if defined $2; | ||
| 112 | #~ } | ||
| 113 | |||
| 114 | my @to = ($r_hash->{namespace} ? ("namespace" => $r_hash->{namespace}) : (), | ||
| 115 | $r_hash->{controller} ? ("controller"=>$r_hash->{controller},) : ()); | ||
| 116 | |||
| 117 | if ($r_hash->{to}) { | ||
| 118 | $nr->to($r_hash->{to}, @to); | ||
| 119 | } elsif ( $r_hash->{action} ) { | ||
| 120 | |||
| 121 | if ( $r_hash->{action} =~ /#/ ) { $nr->to($r_hash->{action}, @to); } | ||
| 122 | else { $nr->to( action => $r_hash->{action}, @to,); } | ||
| 123 | |||
| 124 | } elsif ( $r_hash->{callback} ) { | ||
| 125 | |||
| 126 | my $cb = eval $r_hash->{callback}; | ||
| 127 | die "Compile error on callback: [$@]", $self->app->dumper($r_hash) | ||
| 128 | if $@; | ||
| 129 | $nr->to(cb => $cb); | ||
| 130 | |||
| 131 | } else { | ||
| 132 | die "No defaults for route: ", $self->app->dumper($r_hash); | ||
| 133 | } | ||
| 134 | $nr->name($r_hash->{name}) | ||
| 135 | if $r_hash->{name}; | ||
| 136 | $self->app->log->debug("Apply DBI route [$r_hash->{request}] ". $self->app->dumper($nr->pattern->defaults) =~ s/\s* | ||
| 137 | \n+\s*//gr); | ||
| 138 | return $nr; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub routes { | ||
| 142 | my ($self,) = @_; | ||
| 143 | $self->plugin->model('Routes')->routes; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub access_explicit {# i.e. by refs table | ||
| 147 | my ($self, $id1, $id2,) = @_; | ||
| 148 | my $r = $self->plugin->model('Refs')->exists($id1, $id2); | ||
| 149 | #~ $self->app->log->debug("Test access_explicit: ", $r); | ||
| 150 | return $r; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | sub access_namespace {#implicit | ||
| 155 | my ($self, $namespace, $id2,) = @_; | ||
| 156 | return scalar $self->plugin->model('Namespaces')->access($namespace, $id2); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub access_controller {#implicit | ||
| 160 | my ($self, $namespace, $controller, $id2,) = @_; | ||
| 161 | my $c = $self->plugin->model('Controllers')->controller_ns( $controller, ($namespace) x 2,) | ||
| 162 | or return undef; | ||
| 163 | $self->access_explicit([$c->{id}], $id2); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub access_action {#implicit | ||
| 167 | my ($self, $namespace, $controller, $action, $id2,) = @_; | ||
| 168 | my $c = $self->plugin->model('Controllers')->controller_ns( $controller, ($namespace) x 2,) | ||
| 169 | or return undef; | ||
| 170 | return scalar $self->plugin->model('Actions')->access( $c->{id}, $action, $id2); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub access_role {#implicit | ||
| 174 | my ($self, $role, $id2,) = @_; | ||
| 175 | return scalar $self->plugin->model('Roles')->access($role =~ /\D/ ? (undef, $role) : ($role, undef), $id2); | ||
| 176 | } | ||
| 177 | |||
| 178 | my $Mojo_Util_loaded; | ||
| 179 | sub auth_cookie { | ||
| 180 | my ($self, $c, $value, $name) = @_; | ||
| 181 | $name ||= $c->app->sessions->cookie_name; | ||
| 182 | return $c->cookie($name)#'mojolicious' | ||
| 183 | unless $value; | ||
| 184 | |||
| 185 | if ($value =~ s/--([^\-]+)$//) { | ||
| 186 | my $signature = $1; | ||
| 187 | load_class 'Mojo::Util' | ||
| 188 | unless $Mojo_Util_loaded++; | ||
| 189 | |||
| 190 | my $valid; | ||
| 191 | my $secrets = $c->app->secrets; | ||
| 192 | for my $secret (@$secrets) { | ||
| 193 | my $check = Mojo::Util::hmac_sha1_sum($value, $secret); | ||
| 194 | ++$valid | ||
| 195 | and last | ||
| 196 | if Mojo::Util::secure_compare($signature, $check); | ||
| 197 | } | ||
| 198 | |||
| 199 | $c->app->log->warn(qq{Cookie [$value] has a bad signature}) | ||
| 200 | and return undef | ||
| 201 | unless $valid; | ||
| 202 | |||
| 203 | } else { | ||
| 204 | $c->app->log->warn(qq{Cookie [$value] is not signed}) | ||
| 205 | and return undef; | ||
| 206 | } | ||
| 207 | |||
| 208 | my $session = $c->app->sessions->deserialize->(Mojo::Util::b64_decode $value) | ||
| 209 | or $c->app->log->warn(qq{Cookie [$value] couldnt deserialize}) | ||
| 210 | and return undef; | ||
| 211 | |||
| 212 | my $key = $self->plugin->merge_conf->{auth}{session_key} || 'auth_data'; | ||
| 213 | my $profile_id = $session->{$key} | ||
| 214 | or $c->app->log->warn(qq{Cookie [$value] doesnt has profile id}) | ||
| 215 | and return undef; | ||
| 216 | #~ $c->app->log->fatal($c->dumper($c->stash->{'mojo.session'}), $c->stash->{'mojo.active_session'}); | ||
| 217 | #~ warn $c->auth_user || 'none auth'; | ||
| 218 | $c->authenticate(undef, undef, {id=> $profile_id}); # session only store | ||
| 219 | #~ $c->app->sessions->store($c); | ||
| 220 | #~ $c->app->log->fatal($c->dumper($c->stash->{'mojo.session'}), $c->stash->{'mojo.active_session'}); | ||
| 221 | #~ $c->app->log->fatal($_->name, $_->{value}) for @{$c->res->cookies}; | ||
| 222 | #~ $c->app->log->fatal( $c->dumper($c->res->cookies) ); | ||
| 223 | #~ $c->app->log->fatal( $c->dumper($c->res->headers) ); | ||
| 224 | #~ $c->res->headers->set_cookie('foo=bar; path=/'); | ||
| 225 | |||
| 226 | my $old_set_cookie = $c->res->headers->set_cookie;# && scalar @{$c->res->headers->{'set-cookie'}}; | ||
| 227 | #~ $c->app->log->fatal( $c->dumper($old_set_cookie) ); | ||
| 228 | #~ $c->app->log->fatal($c->cookie('mojolicious')); | ||
| 229 | $c->app->sessions->store($c); | ||
| 230 | #~ $c->app->log->fatal( $c->dumper( $c->res->headers->to_hash) ); | ||
| 231 | #~ my $cc = scalar @{$c->res->cookies}; | ||
| 232 | #~ $c->app->log->fatal($c->cookie('mojolicious')); | ||
| 233 | #~ $c->app->log->fatal($_->name, $_->value) for @{$c->res->cookies}); | ||
| 234 | #~ my $new_cookie = (grep($_->name eq $name, @{$c->res->cookies}))[0]; | ||
| 235 | my $new_cookie = pop @{$c->res->cookies}; | ||
| 236 | #~ $c->app->log->fatal( $c->dumper($c->res->cookies) ); | ||
| 237 | $c->res->headers->set_cookie($old_set_cookie // 'auth=ok; path=/'); | ||
| 238 | #~ $set_cookie ? pop @{$c->res->headers->{'set-cookie'}} : delete $c->res->headers->{'set-cookie'}; | ||
| 239 | #~ $c->app->log->fatal( $c->dumper($c->res->headers) ); | ||
| 240 | #~ $c->app->log->fatal($c->dumper($c->stash->{'mojo.session'}), $c->stash->{'mojo.active_session'}); | ||
| 241 | |||
| 242 | my $profile = $c->auth_user; | ||
| 243 | $profile->auth_cookie($new_cookie->value) | ||
| 244 | #~ and unshift(@a,$x,$y) splice(@{$c->res->cookies},0,0,$x,$y) | ||
| 245 | if $new_cookie; | ||
| 246 | |||
| 247 | return $profile; | ||
| 248 | } | ||
| 249 | |||
| 250 | 1; | ||
| 251 | |||
| 252 | =pod | ||
| 253 | |||
| 254 | =encoding utf8 | ||
| 255 | |||
| 256 | =head1 Mojolicious::Plugin::RoutesAuthDBI::Access | ||
| 257 | |||
| 258 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||
| 259 | |||
| 260 | =head1 NAME | ||
| 261 | |||
| 262 | Mojolicious::Plugin::RoutesAuthDBI::Access - Generation routes, authentication and controll access to routes trought sintax of ->requires(...), see L<Mojolicious::Routes::Route#requires> | ||
| 263 | |||
| 264 | =head1 DB DESIGN DIAGRAM | ||
| 265 | |||
| 266 | See L<https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/blob/master/Diagram.svg> | ||
| 267 | |||
| 268 | =head1 Generate the routes from DB | ||
| 269 | |||
| 270 | =over 4 | ||
| 271 | |||
| 272 | =item * B<route> -> B<actions.action> <- B<controllers> [ <- B<namespaces>] | ||
| 273 | |||
| 274 | Route to action method on controller. If no ref from namespace to controller then controller will find on $app->routes->namespaces as usual. | ||
| 275 | |||
| 276 | =item * B<routes> -> B<actions.callback> | ||
| 277 | |||
| 278 | Route to callback (no ref to controller, defined I<callback> column (as text "sub {...}") in db table B<actions>) | ||
| 279 | |||
| 280 | =back | ||
| 281 | |||
| 282 | =head2 Access controll flow | ||
| 283 | |||
| 284 | There are two ways of flow: explicit and implicit. | ||
| 285 | |||
| 286 | =over 4 | ||
| 287 | |||
| 288 | =item * Explicit access | ||
| 289 | |||
| 290 | Check by tables ids: routes, actions, controllers, namespaces. Check refs to profile roles ids. | ||
| 291 | |||
| 292 | =item * Implicit access | ||
| 293 | |||
| 294 | Access to routes by names: action, controller, namespace, role. This way used for db route to access namespace and for non db routes by syntax: | ||
| 295 | |||
| 296 | $r->any('/foo')->...->to('foo#bar')->requires(access=>{auth=>1})->...; | ||
| 297 | |||
| 298 | or | ||
| 299 | |||
| 300 | $r->...->requires(access=>{auth=>1, role => <id|name>})->...; # access to route by role id|name | ||
| 301 | |||
| 302 | =back | ||
| 303 | |||
| 304 | See detail L<Mojolicious::Plugin::RoutesAuthDBI#access> | ||
| 305 | |||
| 306 | =head1 SYNOPSIS | ||
| 307 | |||
| 308 | $app->plugin('RoutesAuthDBI', | ||
| 309 | ... | ||
| 310 | access => {< hashref options list below >}, | ||
| 311 | ... | ||
| 312 | ); | ||
| 313 | |||
| 314 | =head1 OPTIONS for plugin | ||
| 315 | |||
| 316 | =head2 namespace | ||
| 317 | |||
| 318 | Default 'Mojolicious::Plugin::RoutesAuthDBI'. | ||
| 319 | |||
| 320 | =head2 module | ||
| 321 | |||
| 322 | Default 'Access' (this module). | ||
| 323 | |||
| 324 | Both above options determining the module which will play as manager of authentication, accessing and generate routing from DBI source. | ||
| 325 | |||
| 326 | =head2 fail_auth_cb | ||
| 327 | |||
| 328 | fail_auth_cb => sub {my $c = shift;...} | ||
| 329 | |||
| 330 | This callback invoke when request need auth route but authentication was failure. | ||
| 331 | |||
| 332 | =head2 fail_access_cb | ||
| 333 | |||
| 334 | fail_access_cb => sub {my ($c, $route, $r_hash, $u) = @_;...} | ||
| 335 | |||
| 336 | This callback invoke when request need auth route but access was failure. $route - L<Mojolicious::Routes::Route> object, $r_hash - route hashref db item, $u - useer hashref. | ||
| 337 | |||
| 338 | =head2 tables | ||
| 339 | |||
| 340 | Hashref of any DB tables names. See L<Mojolicious::Plugin::RoutesAuthDBI::Schema#Default-variables-for-SQL-templates>. | ||
| 341 | |||
| 342 | |||
| 343 | =head1 EXPORT SUBS | ||
| 344 | |||
| 345 | =head2 load_user($c, $uid) | ||
| 346 | |||
| 347 | Fetch user record from table profiles by COOKIES. Import for Mojolicious::Plugin::Authentication. Required. | ||
| 348 | |||
| 349 | =head2 validate_user($c, $login, $pass, $extradata) | ||
| 350 | |||
| 351 | Fetch login record from table logins by Mojolicious::Plugin::Authentication. Required. If hashref $extradata->{id} then no fetch and $extradata->{id} will return. | ||
| 352 | |||
| 353 | =head1 METHODS | ||
| 354 | |||
| 355 | As child of L<Mojolicious::Controller> inherits all parent methods and following ones: | ||
| 356 | |||
| 357 | =head2 new(app=> ..., plugin => ...) | ||
| 358 | |||
| 359 | Return new access object. | ||
| 360 | |||
| 361 | =head2 apply_ns() | ||
| 362 | |||
| 363 | Select from db table I<namespaces> ns thus app_ns=1 and push them to $app->namespaces() | ||
| 364 | |||
| 365 | =head2 apply_route($r_hash) | ||
| 366 | |||
| 367 | Heart of routes generation from db tables and not only. Insert to app->routes an hash item $r_hash. DB schema specific. Return new Mojolicious route. | ||
| 368 | |||
| 369 | =head2 routes() | ||
| 370 | |||
| 371 | Fetch records for apply_routes. Must return arrayref of hashrefs routes. | ||
| 372 | |||
| 373 | =head2 access_explicit($id1, $id2) | ||
| 374 | |||
| 375 | Check access to route ($id1 arrayref - either route id or action id or controller id or namespace id) by roles ids ($id2 arrayref). Must return false for deny access or true - allow access. | ||
| 376 | |||
| 377 | =head2 access_namespace($namespace, $id2) | ||
| 378 | |||
| 379 | Check implicit access to route by $namespace for profile roles ids ($id2 arrayref). Must return false for deny access or true - allow access to all actions of this namespace. | ||
| 380 | |||
| 381 | =head2 access_controller($namespace, $controller, $id2) | ||
| 382 | |||
| 383 | Check implicit access to route by $namespace and $controller for profile roles ids ($id2 arrayref). Must return false for deny access or true - allow access to all actions of this controller. | ||
| 384 | |||
| 385 | =head2 access_action($namespace, $controller, $action, $id2) | ||
| 386 | |||
| 387 | Check implicit access to route by $namespace and $controller and $action for profile roles ids ($id2 arrayref). Must return false for deny access or true - allow access to this action. | ||
| 388 | |||
| 389 | =head2 access_role($role, $id2) | ||
| 390 | |||
| 391 | Check implicit access to route by $role (id|name) and profile roles ids ($id2 arrayref). Must return false for deny access or true - allow access. | ||
| 392 | |||
| 393 | =head2 auth_cookie($c, $cookie_value, $cookie_name) | ||
| 394 | |||
| 395 | Returns C<< $c->cookie($cookie_name) >> unless $cookie_value. | ||
| 396 | |||
| 397 | Returns authenticate profile for $cookie_value. I use this method for cordova mobile app then cookie lost on any reasons. | ||
| 398 | |||
| 399 | C<< $cookie_name >> has defaults to C<< $c->app->sessions->cookie_name >> | ||
| 400 | |||
| 401 | =head1 SEE ALSO | ||
| 402 | |||
| 403 | L<Mojolicious::Plugin::RoutesAuthDBI> | ||
| 404 | |||
| 405 | =head1 AUTHOR | ||
| 406 | |||
| 407 | Михаил Че (Mikhail Che), C<< <mche [on] cpan.org> >> | ||
| 408 | |||
| 409 | =head1 BUGS / CONTRIBUTING | ||
| 410 | |||
| 411 | Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues>. Pull requests welcome also. | ||
| 412 | |||
| 413 | =head1 COPYRIGHT | ||
| 414 | |||
| 415 | Copyright 2016+ Mikhail Che. | ||
| 416 | |||
| 417 | This library is free software; you can redistribute it and/or modify | ||
| 418 | it under the same terms as Perl itself. | ||
| 419 | |||
| 420 | =cut |