2018-10-11 20:12:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-23 12:10:39 +00:00
|
|
|
# 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
|
|
|
|
module LDAP
|
2018-03-05 22:26:40 +00:00
|
|
|
class Authentication < Gitlab::Auth::OAuth::Authentication
|
2018-02-23 12:10:39 +00:00
|
|
|
def self.login(login, password)
|
|
|
|
return unless Gitlab::Auth::LDAP::Config.enabled?
|
|
|
|
return unless login.present? && password.present?
|
|
|
|
|
2018-03-26 09:23:54 +00:00
|
|
|
# return found user that was authenticated by first provider for given login credentials
|
2018-02-23 12:10:39 +00:00
|
|
|
providers.find do |provider|
|
|
|
|
auth = new(provider)
|
2018-03-26 09:23:54 +00:00
|
|
|
break auth.user if auth.login(login, password) # true will exit the loop
|
2018-02-23 12:10:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.providers
|
|
|
|
Gitlab::Auth::LDAP::Config.providers
|
|
|
|
end
|
|
|
|
|
|
|
|
def login(login, password)
|
2018-03-26 09:23:54 +00:00
|
|
|
result = adapter.bind_as(
|
2018-02-23 12:10:39 +00:00
|
|
|
filter: user_filter(login),
|
|
|
|
size: 1,
|
|
|
|
password: password
|
|
|
|
)
|
2018-03-26 09:23:54 +00:00
|
|
|
return unless result
|
|
|
|
|
|
|
|
@user = Gitlab::Auth::LDAP::User.find_by_uid_and_provider(result.dn, provider)
|
2018-02-23 12:10:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def adapter
|
|
|
|
OmniAuth::LDAP::Adaptor.new(config.omniauth_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
|
|
|
Gitlab::Auth::LDAP::Config.new(provider)
|
|
|
|
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
|