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} = SL::Layout::Dispatcher->new(style => $::myconfig{menustyle});
 
  31   my $ok   =  $::auth->is_api_token_cookie_valid;
 
  32   $ok    ||=  $::form->{'{AUTH}login'}                      && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, $::form->{'{AUTH}password'}));
 
  33   $ok    ||= !$::form->{'{AUTH}login'} &&  $http_auth_login && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, $http_auth_password));
 
  34   $ok    ||= !$::form->{'{AUTH}login'} && !$http_auth_login && (SL::Auth::OK() == $::auth->authenticate($::myconfig{login}, undef));
 
  36   return $self->_error(%param) if !$ok;
 
  38   $::auth->create_or_refresh_session;
 
  39   $::auth->delete_session_value('FLASH');
 
  40   $::instance_conf->reload->data;
 
  46   my ($self, %param) = @_;
 
  48   $::auth->punish_wrong_login;
 
  49   $::dispatcher->handle_login_error(%param, error => 'password');
 
  54 sub _parse_http_basic_auth {
 
  59   # Requires that the server passes the 'Authorization' header as the
 
  60   # environment variable 'HTTP_AUTHORIZATION'. Example code for
 
  63   # SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
 
  65   my $data = $ENV{HTTP_AUTHORIZATION};
 
  67   return unless ($data // '') =~ m{^basic +(.+)}i;
 
  69   $data = Encode::decode('utf-8', MIME::Base64::decode($1));
 
  71   return unless $data =~ m{(.+?):(.+)};