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
end
def password_authentication
user = User.try_to_login(params[:username], params[:password], false)
user = User.try_to_login!(params[:username], params[:password], false)
if user.nil?
invalid_credentials
app/models/user.rb
end
# Returns the user that matches provided login and password, or nil
# AuthSource errors are caught, logged and nil is returned.
def self.try_to_login(login, password, active_only=true)
try_to_login!(login, password, active_only)
rescue AuthSourceException => e
logger.error "An error occured when authenticating #{login}: #{e.message}"
nil
end
# Returns the user that matches provided login and password, or nil
# AuthSource errors are passed through.
def self.try_to_login!(login, password, active_only=true)
login = login.to_s.strip
password = password.to_s
test/unit/user_test.rb
assert_equal "ADMIN", user.login
end
if ldap_configured?
test "#try_to_login using LDAP with failed connection to the LDAP server" do
auth_source = AuthSourceLdap.find(1)
AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
test "#try_to_login! using LDAP with existing user and failed connection to the LDAP server" do
auth_source = AuthSourceLdap.find(1)
user = users(:users_001)
user.update_column :auth_source_id, auth_source.id
AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
assert_raise(AuthSourceException){ User.try_to_login!('admin', 'admin') }
end
assert_nil User.try_to_login('edavis', 'wrong')
end
test "#try_to_login using LDAP with existing user and failed connection to the LDAP server" do
auth_source = AuthSourceLdap.find(1)
user = users(:users_001)
user.update_column :auth_source_id, auth_source.id
AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
assert_nil User.try_to_login('admin', 'admin')
end
test "#try_to_login using LDAP with new user and failed connection to the LDAP server" do
auth_source = AuthSourceLdap.find(1)
auth_source.update onthefly_register: true
AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error, 'Cannot connect')
assert_nil User.try_to_login('edavis', 'wrong')
end
if ldap_configured?
test "#try_to_login using LDAP" do
assert_nil User.try_to_login('edavis', 'wrong')
    (1-1/1)