2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
# These calls help to authenticate to LDAP by providing username and password
|
|
|
|
#
|
|
|
|
# Since multiple LDAP servers are supported, it will loop through all of them
|
|
|
|
# until a valid bind is found
|
|
|
|
#
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Auth
|
2020-04-08 14:13:33 +05:30
|
|
|
module Ldap
|
2018-03-27 19:54:05 +05:30
|
|
|
class Authentication < Gitlab::Auth::OAuth::Authentication
|
|
|
|
def self.login(login, password)
|
2020-04-08 14:13:33 +05:30
|
|
|
return unless Gitlab::Auth::Ldap::Config.enabled?
|
2018-03-27 19:54:05 +05:30
|
|
|
return unless login.present? && password.present?
|
|
|
|
|
2018-04-04 21:44:52 +05:30
|
|
|
# return found user that was authenticated by first provider for given login credentials
|
2018-03-27 19:54:05 +05:30
|
|
|
providers.find do |provider|
|
|
|
|
auth = new(provider)
|
2018-04-04 21:44:52 +05:30
|
|
|
break auth.user if auth.login(login, password) # true will exit the loop
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.providers
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::Auth::Ldap::Config.providers
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def login(login, password)
|
2018-04-04 21:44:52 +05:30
|
|
|
result = adapter.bind_as(
|
2018-03-27 19:54:05 +05:30
|
|
|
filter: user_filter(login),
|
|
|
|
size: 1,
|
|
|
|
password: password
|
|
|
|
)
|
2018-04-04 21:44:52 +05:30
|
|
|
return unless result
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
@user = Gitlab::Auth::Ldap::User.find_by_uid_and_provider(result.dn, provider)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def adapter
|
|
|
|
OmniAuth::LDAP::Adaptor.new(config.omniauth_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::Auth::Ldap::Config.new(provider)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def user_filter(login)
|
|
|
|
filter = Net::LDAP::Filter.equals(config.uid, login)
|
|
|
|
|
|
|
|
# Apply LDAP user filter if present
|
|
|
|
if config.user_filter.present?
|
|
|
|
filter = Net::LDAP::Filter.join(filter, config.constructed_user_filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
filter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|