| Lines 32-37
          use Koha;
      
      
        Link Here | 
        
          | 32 | use Koha::Logger; | 32 | use Koha::Logger; | 
        
          | 33 | use Koha::Caches; | 33 | use Koha::Caches; | 
        
          | 34 | use Koha::AuthUtils qw( get_script_name hash_password ); | 34 | use Koha::AuthUtils qw( get_script_name hash_password ); | 
            
              |  |  | 35 | use Koha::Auth::TwoFactorAuth; | 
        
          | 35 | use Koha::Checkouts; | 36 | use Koha::Checkouts; | 
        
          | 36 | use Koha::DateUtils qw( dt_from_string ); | 37 | use Koha::DateUtils qw( dt_from_string ); | 
        
          | 37 | use Koha::Library::Groups; | 38 | use Koha::Library::Groups; | 
  
    | Lines 843-848
          sub checkauth {
      
      
        Link Here | 
        
          | 843 |     my $q_userid = $query->param('userid') // ''; | 844 |     my $q_userid = $query->param('userid') // ''; | 
        
          | 844 |  | 845 |  | 
        
          | 845 |     my $session; | 846 |     my $session; | 
            
              |  |  | 847 |     my $invalid_otp_token; | 
            
              | 848 |     my $require_2FA = ( C4::Context->preference('TwoFactorAuthentication') && $type ne "OPAC" ) ? 1 : 0; | 
            
              | 849 |     my $auth_challenge_complete; | 
        
          | 846 |  | 850 |  | 
        
          | 847 |     # Basic authentication is incompatible with the use of Shibboleth, | 851 |     # Basic authentication is incompatible with the use of Shibboleth, | 
        
          | 848 |     # as Shibboleth may return REMOTE_USER as a Shibboleth attribute, | 852 |     # as Shibboleth may return REMOTE_USER as a Shibboleth attribute, | 
  
    | Lines 874-886
          sub checkauth {
      
      
        Link Here | 
        
          | 874 |             { remote_addr => $ENV{REMOTE_ADDR}, skip_version_check => 1 } | 878 |             { remote_addr => $ENV{REMOTE_ADDR}, skip_version_check => 1 } | 
        
          | 875 |         ); | 879 |         ); | 
        
          | 876 |  | 880 |  | 
            
              |  |  | 881 |         if ( $return eq 'ok' || $return eq 'additional-auth-needed' ) { | 
            
              | 882 |             $userid = $session->param('id'); | 
            
              | 883 |         } | 
            
              | 884 |  | 
            
              | 885 |         $additional_auth_needed = ( $return eq 'additional-auth-needed' ) ? 1 : 0; | 
            
              | 886 |  | 
            
              | 887 |         # We are at the second screen if the waiting-for-2FA is set in session | 
            
              | 888 |         # and otp_token param has been passed | 
            
              | 889 |         if (   $require_2FA | 
            
              | 890 |             && $additional_auth_needed | 
            
              | 891 |             && ( my $otp_token = $query->param('otp_token') ) ) | 
            
              | 892 |         { | 
            
              | 893 |             my $patron    = Koha::Patrons->find( { userid => $userid } ); | 
            
              | 894 |             my $auth      = Koha::Auth::TwoFactorAuth::get_auth( { patron => $patron } ); | 
            
              | 895 |             my $verified = $auth->verify($otp_token); | 
            
              | 896 |             $auth->clear; | 
            
              | 897 |             if ( $verified ) { | 
            
              | 898 |                 # The token is correct, the user is fully logged in! | 
            
              | 899 |                 $additional_auth_needed = 0; | 
            
              | 900 |                 $session->param( 'waiting-for-2FA', 0 ); | 
            
              | 901 |                 $return = "ok"; | 
            
              | 902 |                 $auth_challenge_complete = 1; | 
            
              | 903 |  | 
            
              | 904 |                # This is an ugly trick to pass the test | 
            
              | 905 |                # $query->param('koha_login_context') && ( $q_userid ne $userid ) | 
            
              | 906 |                # few lines later | 
            
              | 907 |                 $q_userid = $userid; | 
            
              | 908 |             } | 
            
              | 909 |             else { | 
            
              | 910 |                 $invalid_otp_token = 1; | 
            
              | 911 |             } | 
            
              | 912 |         } | 
            
              | 913 |  | 
        
          | 877 |         if ( $return eq 'ok' ) { | 914 |         if ( $return eq 'ok' ) { | 
        
          | 878 |             Koha::Logger->get->debug(sprintf "AUTH_SESSION: (%s)\t%s %s - %s", map { $session->param($_) || q{} } qw(cardnumber firstname surname branch)); | 915 |             Koha::Logger->get->debug(sprintf "AUTH_SESSION: (%s)\t%s %s - %s", map { $session->param($_) || q{} } qw(cardnumber firstname surname branch)); | 
        
          | 879 |  | 916 |  | 
          
            
              | 880 |             my $s_userid = $session->param('id'); | 917 |             if ( ( $query->param('koha_login_context') && ( $q_userid ne $userid ) ) | 
            
              | 881 |             $userid      = $s_userid; |  |  | 
            
              | 882 |  | 
            
              | 883 |             if ( ( $query->param('koha_login_context') && ( $q_userid ne $s_userid ) ) | 
        
          | 884 |                 || ( $cas && $query->param('ticket') && !C4::Context->userenv->{'id'} ) | 918 |                 || ( $cas && $query->param('ticket') && !C4::Context->userenv->{'id'} ) | 
        
          | 885 |                 || ( $shib && $shib_login && !$logout && !C4::Context->userenv->{'id'} ) | 919 |                 || ( $shib && $shib_login && !$logout && !C4::Context->userenv->{'id'} ) | 
        
          | 886 |             ) { | 920 |             ) { | 
  
    | Lines 891-915
          sub checkauth {
      
      
        Link Here | 
        
          | 891 |                 $session->delete(); | 925 |                 $session->delete(); | 
        
          | 892 |                 $session->flush; | 926 |                 $session->flush; | 
        
          | 893 |                 C4::Context->_unset_userenv($sessionID); | 927 |                 C4::Context->_unset_userenv($sessionID); | 
          
            
              | 894 |             } | 928 |             } elsif (!$logout) { | 
            
              | 895 |             elsif ($logout) { |  |  | 
            
              | 896 |  | 
            
              | 897 |                 # voluntary logout the user | 
            
              | 898 |                 # check wether the user was using their shibboleth session or a local one | 
            
              | 899 |                 my $shibSuccess = C4::Context->userenv->{'shibboleth'}; | 
            
              | 900 |                 $session->delete(); | 
            
              | 901 |                 $session->flush; | 
            
              | 902 |                 C4::Context->_unset_userenv($sessionID); | 
            
              | 903 |  | 
            
              | 904 |                 if ($cas and $caslogout) { | 
            
              | 905 |                     logout_cas($query, $type); | 
            
              | 906 |                 } | 
            
              | 907 |  | 
            
              | 908 |                 # If we are in a shibboleth session (shibboleth is enabled, a shibboleth match attribute is set and matches koha matchpoint) | 
            
              | 909 |                 if ( $shib and $shib_login and $shibSuccess) { | 
            
              | 910 |                     logout_shib($query); | 
            
              | 911 |                 } | 
            
              | 912 |             } else { | 
        
          | 913 |  | 929 |  | 
        
          | 914 |                 $cookie = $query->cookie( | 930 |                 $cookie = $query->cookie( | 
        
          | 915 |                     -name     => 'CGISESSID', | 931 |                     -name     => 'CGISESSID', | 
  
    | Lines 939-952
          sub checkauth {
      
      
        Link Here | 
        
          | 939 |         } | 955 |         } | 
        
          | 940 |     } | 956 |     } | 
        
          | 941 |  | 957 |  | 
          
            
              | 942 |     unless ( $loggedin ) { | 958 |     if ( ( !$loggedin && !$additional_auth_needed ) || $logout ) { | 
        
          | 943 |         $sessionID = undef; | 959 |         $sessionID = undef; | 
            
              |  |  | 960 |         $session   = undef; | 
        
          | 944 |         $userid    = undef; | 961 |         $userid    = undef; | 
        
          | 945 |     } | 962 |     } | 
        
          | 946 |  | 963 |  | 
            
              |  |  | 964 |     if ($logout) { | 
            
              | 965 |  | 
            
              | 966 |         # voluntary logout the user | 
            
              | 967 |         # check wether the user was using their shibboleth session or a local one | 
            
              | 968 |         my $shibSuccess = C4::Context->userenv->{'shibboleth'}; | 
            
              | 969 |         if ( $session ) { | 
            
              | 970 |             $session->delete(); | 
            
              | 971 |             $session->flush; | 
            
              | 972 |         } | 
            
              | 973 |         C4::Context->_unset_userenv($sessionID); | 
            
              | 974 |  | 
            
              | 975 |         if ($cas and $caslogout) { | 
            
              | 976 |             logout_cas($query, $type); | 
            
              | 977 |         } | 
            
              | 978 |  | 
            
              | 979 |         # If we are in a shibboleth session (shibboleth is enabled, a shibboleth match attribute is set and matches koha matchpoint) | 
            
              | 980 |         if ( $shib and $shib_login and $shibSuccess) { | 
            
              | 981 |             logout_shib($query); | 
            
              | 982 |         } | 
            
              | 983 |  | 
            
              | 984 |         $sessionID = undef; | 
            
              | 985 |         $session   = undef; | 
            
              | 986 |         $userid    = undef; | 
            
              | 987 |         $additional_auth_needed = 0; | 
            
              | 988 |     } | 
            
              | 989 |  | 
        
          | 947 |     unless ( $userid ) { | 990 |     unless ( $userid ) { | 
        
          | 948 |         #we initiate a session prior to checking for a username to allow for anonymous sessions... | 991 |         #we initiate a session prior to checking for a username to allow for anonymous sessions... | 
          
            
              | 949 |         my $session = get_session("") or die "Auth ERROR: Cannot get_session()"; | 992 |         $session = get_session("") or die "Auth ERROR: Cannot get_session()"; | 
        
          | 950 |  | 993 |  | 
        
          | 951 |         # Save anonymous search history in new session so it can be retrieved | 994 |         # Save anonymous search history in new session so it can be retrieved | 
        
          | 952 |         # by get_template_and_user to store it in user's search history after | 995 |         # by get_template_and_user to store it in user's search history after | 
  
    | Lines 1233-1241
          sub checkauth {
      
      
        Link Here | 
        
          | 1233 |         } | 1276 |         } | 
        
          | 1234 |     }    # END unless ($userid) | 1277 |     }    # END unless ($userid) | 
        
          | 1235 |  | 1278 |  | 
            
              |  |  | 1279 |     if ( $require_2FA && ( $loggedin && !$auth_challenge_complete)) { | 
            
              | 1280 |         my $patron = Koha::Patrons->find({userid => $userid}); | 
            
              | 1281 |         if ( $patron->auth_method eq 'two-factor' ) { | 
            
              | 1282 |             # Ask for the OTP token | 
            
              | 1283 |             $additional_auth_needed = 1; | 
            
              | 1284 |             $session->param('waiting-for-2FA', 1); | 
            
              | 1285 |             %info = ();# We remove the warnings/errors we may have set incorrectly before | 
            
              | 1286 |         } | 
            
              | 1287 |     } | 
            
              | 1288 |  | 
        
          | 1236 |     # finished authentification, now respond | 1289 |     # finished authentification, now respond | 
          
            
              | 1237 |     if ( $loggedin || $authnotrequired ) | 1290 |     if ( ( $loggedin || $authnotrequired ) && !$additional_auth_needed ) { | 
            
              | 1238 |     { |  |  | 
        
          | 1239 |         # successful login | 1291 |         # successful login | 
        
          | 1240 |         unless ($cookie) { | 1292 |         unless ($cookie) { | 
        
          | 1241 |             $cookie = $query->cookie( | 1293 |             $cookie = $query->cookie( | 
  
    | Lines 1269-1284
          sub checkauth {
      
      
        Link Here | 
        
          | 1269 |     # | 1321 |     # | 
        
          | 1270 |     # | 1322 |     # | 
        
          | 1271 |  | 1323 |  | 
            
              |  |  | 1324 |     my $patron = Koha::Patrons->find({ userid => $q_userid }); # Not necessary logged in! | 
            
              | 1325 |  | 
        
          | 1272 |     # get the inputs from the incoming query | 1326 |     # get the inputs from the incoming query | 
        
          | 1273 |     my @inputs = (); | 1327 |     my @inputs = (); | 
            
              |  |  | 1328 |     my @inputs_to_clean = qw( userid password ticket logout.x otp_token ); | 
        
          | 1274 |     foreach my $name ( param $query) { | 1329 |     foreach my $name ( param $query) { | 
          
            
              | 1275 |         (next) if ( $name eq 'userid' || $name eq 'password' || $name eq 'ticket' ); | 1330 |         next if grep { $name eq $_ } @inputs_to_clean; | 
        
          | 1276 |         my @value = $query->multi_param($name); | 1331 |         my @value = $query->multi_param($name); | 
        
          | 1277 |         push @inputs, { name => $name, value => $_ } for @value; | 1332 |         push @inputs, { name => $name, value => $_ } for @value; | 
        
          | 1278 |     } | 1333 |     } | 
        
          | 1279 |  | 1334 |  | 
            
              | 1280 |     my $patron = Koha::Patrons->find({ userid => $q_userid }); # Not necessary logged in! |  |  | 
            
              | 1281 |  | 
        
          | 1282 |     my $LibraryNameTitle = C4::Context->preference("LibraryName"); | 1335 |     my $LibraryNameTitle = C4::Context->preference("LibraryName"); | 
        
          | 1283 |     $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi; | 1336 |     $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi; | 
        
          | 1284 |     $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg; | 1337 |     $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg; | 
  
    | Lines 1328-1333
          sub checkauth {
      
      
        Link Here | 
        
          | 1328 |     $template->param( SCI_login => 1 ) if ( $query->param('sci_user_login') ); | 1381 |     $template->param( SCI_login => 1 ) if ( $query->param('sci_user_login') ); | 
        
          | 1329 |     $template->param( OpacPublic => C4::Context->preference("OpacPublic") ); | 1382 |     $template->param( OpacPublic => C4::Context->preference("OpacPublic") ); | 
        
          | 1330 |     $template->param( loginprompt => 1 ) unless $info{'nopermission'}; | 1383 |     $template->param( loginprompt => 1 ) unless $info{'nopermission'}; | 
            
              |  |  | 1384 |     if ( $additional_auth_needed ) { | 
            
              | 1385 |         $template->param( | 
            
              | 1386 |             TwoFA_prompt => 1, | 
            
              | 1387 |             invalid_otp_token => $invalid_otp_token, | 
            
              | 1388 |         ); | 
            
              | 1389 |     } | 
        
          | 1331 |  | 1390 |  | 
        
          | 1332 |     if ( $type eq 'opac' ) { | 1391 |     if ( $type eq 'opac' ) { | 
        
          | 1333 |         require Koha::Virtualshelves; | 1392 |         require Koha::Virtualshelves; | 
  
    | Lines 1435-1440
          Possible return values in C<$status> are:
      
      
        Link Here | 
        
          | 1435 |  | 1494 |  | 
        
          | 1436 | =item "restricted" -- The IP has changed (if SessionRestrictionByIP) | 1495 | =item "restricted" -- The IP has changed (if SessionRestrictionByIP) | 
        
          | 1437 |  | 1496 |  | 
            
              |  |  | 1497 | =item "additional-auth-needed -- User is in an authentication process that is not finished | 
            
              | 1498 |  | 
        
          | 1438 | =back | 1499 | =back | 
        
          | 1439 |  | 1500 |  | 
        
          | 1440 | =cut | 1501 | =cut | 
  
    | Lines 1710-1715
          sub check_cookie_auth {
      
      
        Link Here | 
        
          | 1710 |             $session->param( 'lasttime', time() ); | 1771 |             $session->param( 'lasttime', time() ); | 
        
          | 1711 |             my $flags = defined($flagsrequired) ? haspermission( $userid, $flagsrequired ) : 1; | 1772 |             my $flags = defined($flagsrequired) ? haspermission( $userid, $flagsrequired ) : 1; | 
        
          | 1712 |             if ($flags) { | 1773 |             if ($flags) { | 
            
              |  |  | 1774 |  | 
            
              | 1775 |                 return ( "additional-auth-needed", $session ) | 
            
              | 1776 |                     if $session->param('waiting-for-2FA'); | 
            
              | 1777 |  | 
        
          | 1713 |                 return ( "ok", $session ); | 1778 |                 return ( "ok", $session ); | 
        
          | 1714 |             } else { | 1779 |             } else { | 
        
          | 1715 |                 $session->delete(); | 1780 |                 $session->delete(); |