2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
class Auth
|
|
|
|
include ApplicationLib
|
2013-08-17 21:48:01 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
checks if a given user can login. Checks for
|
|
|
|
- valid user
|
|
|
|
- active state
|
|
|
|
- max failed logins
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
result = Auth.can_login?(user)
|
2013-08-17 21:48:01 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
result = true | false
|
2013-08-17 21:48:01 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
def self.can_login?(user)
|
|
|
|
return false if !user.is_a?(User)
|
|
|
|
return false if !user.active?
|
|
|
|
|
|
|
|
return true if !user.max_login_failed?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
Rails.logger.info "Max login failed reached for user #{user.login}."
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
checks if a given user and password match against multiple auth backends
|
|
|
|
- valid user
|
|
|
|
- active state
|
|
|
|
- max failed logins
|
|
|
|
|
|
|
|
result = Auth.valid?(user, password)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true | false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.valid?(user, password)
|
|
|
|
# try to login against configure auth backends
|
|
|
|
backends.any? do |config|
|
|
|
|
next if !backend_validates?(
|
|
|
|
config: config,
|
|
|
|
user: user,
|
|
|
|
password: password,
|
|
|
|
)
|
|
|
|
|
|
|
|
Rails.logger.info "Authentication against #{config[:adapter]} for user #{user.login} ok."
|
|
|
|
|
|
|
|
# remember last login date
|
|
|
|
user.update_last_login
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
returns a list of all Auth backend configurations
|
|
|
|
|
|
|
|
result = Auth.backends
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [
|
|
|
|
{
|
|
|
|
adapter: 'Auth::Internal',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
adapter: 'Auth::Developer',
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.backends
|
2013-08-17 21:48:01 +00:00
|
|
|
|
|
|
|
# use std. auth backends
|
|
|
|
config = [
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
adapter: 'Auth::Internal',
|
2013-08-17 21:48:01 +00:00
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
adapter: 'Auth::Developer',
|
2013-08-17 21:48:01 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
# added configured backends
|
2017-04-19 10:09:54 +00:00
|
|
|
Setting.where(area: 'Security::Authentication').each do |setting|
|
|
|
|
next if setting.state_current[:value].blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
config.push setting.state_current[:value]
|
|
|
|
end
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
config
|
|
|
|
end
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
def self.backend_validates?(config:, user:, password:)
|
|
|
|
return false if !config[:adapter]
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2019-01-06 18:41:29 +00:00
|
|
|
instance = config[:adapter].constantize.new(config)
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
instance.valid?(user, password)
|
2015-05-01 07:48:10 +00:00
|
|
|
end
|
2017-04-19 10:09:54 +00:00
|
|
|
private_class_method :backend_validates?
|
2013-08-17 21:48:01 +00:00
|
|
|
end
|