Project

General

Profile

Patch #34071 » 0001-changes-User.try_to_login-to-catch-and-log-AuthSourc.patch

Jens Krämer, 2020-10-06 09:45

View differences:

app/controllers/account_controller.rb
308 308
  end
309 309

  
310 310
  def password_authentication
311
    user = User.try_to_login(params[:username], params[:password], false)
311
    user = User.try_to_login!(params[:username], params[:password], false)
312 312

  
313 313
    if user.nil?
314 314
      invalid_credentials
app/models/user.rb
221 221
  end
222 222

  
223 223
  # Returns the user that matches provided login and password, or nil
224
  # AuthSource errors are caught, logged and nil is returned.
224 225
  def self.try_to_login(login, password, active_only=true)
226
    try_to_login!(login, password, active_only)
227
  rescue AuthSourceException => e
228
    logger.error "An error occured when authenticating #{login}: #{e.message}"
229
    nil
230
  end
231

  
232
  # Returns the user that matches provided login and password, or nil
233
  # AuthSource errors are passed through.
234
  def self.try_to_login!(login, password, active_only=true)
225 235
    login = login.to_s.strip
226 236
    password = password.to_s
227 237

  
test/unit/user_test.rb
695 695
    assert_equal "ADMIN", user.login
696 696
  end
697 697

  
698
  if ldap_configured?
699
    test "#try_to_login using LDAP with failed connection to the LDAP server" do
700
      auth_source = AuthSourceLdap.find(1)
701
      AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
698
  test "#try_to_login! using LDAP with existing user and failed connection to the LDAP server" do
699
    auth_source = AuthSourceLdap.find(1)
700
    user = users(:users_001)
701
    user.update_column :auth_source_id, auth_source.id
702
    AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
703
    assert_raise(AuthSourceException){ User.try_to_login!('admin', 'admin') }
704
  end
702 705

  
703
      assert_nil User.try_to_login('edavis', 'wrong')
704
    end
706
  test "#try_to_login using LDAP with existing user and failed connection to the LDAP server" do
707
    auth_source = AuthSourceLdap.find(1)
708
    user = users(:users_001)
709
    user.update_column :auth_source_id, auth_source.id
710
    AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
711
    assert_nil User.try_to_login('admin', 'admin')
712
  end
713

  
714
  test "#try_to_login using LDAP with new user and failed connection to the LDAP server" do
715
    auth_source = AuthSourceLdap.find(1)
716
    auth_source.update onthefly_register: true
717
    AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
718

  
719
    assert_nil User.try_to_login('edavis', 'wrong')
720
  end
721

  
722
  if ldap_configured?
705 723

  
706 724
    test "#try_to_login using LDAP" do
707 725
      assert_nil User.try_to_login('edavis', 'wrong')
    (1-1/1)