1 package SL::Dispatcher::AuthHandler::User;
 
   4 use parent qw(Rose::Object);
 
   9 use SL::Layout::Dispatcher;
 
  12   my ($self, %param) = @_;
 
  14   my ($http_auth_login, $http_auth_password) = $self->_parse_http_basic_auth;
 
  16   my $login = $::form->{'{AUTH}login'} // $http_auth_login // $::auth->get_session_value('login');
 
  18   return $self->_error(%param) if !defined $login;
 
  20   my $client_id = $::form->{'{AUTH}client_id'} // $::auth->get_session_value('client_id') // $::auth->get_default_client_id;
 
  22   return $self->_error(%param) if !$client_id || !$::auth->set_client($client_id);
 
  24   %::myconfig = User->get_default_myconfig($::auth->read_user(login => $login));
 
  26   return $self->_error(%param) unless $::myconfig{login};
 
  28   $::locale = Locale->new($::myconfig{countrycode});
 
  29   $::request->{layout} = $::request->is_mobile
 
  30     ? SL::Layout::Dispatcher->new(style => 'mobile')
 
  31     : SL::Layout::Dispatcher->new(style => $::myconfig{menustyle});
 
  33   my $ok   =  $::auth->is_api_token_cookie_valid;
 
  34   $ok    ||=  $::form->{'{AUTH}login'}                      && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, $::form->{'{AUTH}password'}));
 
  35   $ok    ||= !$::form->{'{AUTH}login'} &&  $http_auth_login && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, $http_auth_password));
 
  36   $ok    ||= !$::form->{'{AUTH}login'} && !$http_auth_login && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, undef));
 
  38   return $self->_error(%param) if !$ok;
 
  40   $::auth->create_or_refresh_session;
 
  41   $::auth->delete_session_value('FLASH');
 
  42   $::instance_conf->reload->data;
 
  48   my ($self, %param) = @_;
 
  50   $::auth->punish_wrong_login;
 
  51   $::dispatcher->handle_login_error(%param, error => 'password');
 
  56 sub _parse_http_basic_auth {
 
  61   # Requires that the server passes the 'Authorization' header as the
 
  62   # environment variable 'HTTP_AUTHORIZATION'. Example code for
 
  65   # SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
 
  67   my $data = $ENV{HTTP_AUTHORIZATION};
 
  69   return unless ($data // '') =~ m{^basic +(.+)}i;
 
  71   $data = Encode::decode('utf-8', MIME::Base64::decode($1));
 
  73   return unless $data =~ m{(.+?):(.+)};