2015-05-04 08:09:03 -04:00
|
|
|
# These calls help to authenticate to LDAP by providing username and password
|
2014-10-13 11:24:05 -04:00
|
|
|
#
|
|
|
|
# Since multiple LDAP servers are supported, it will loop through all of them
|
|
|
|
# until a valid bind is found
|
|
|
|
#
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module LDAP
|
|
|
|
class Authentication
|
|
|
|
def self.login(login, password)
|
|
|
|
return unless Gitlab::LDAP::Config.enabled?
|
|
|
|
return unless login.present? && password.present?
|
|
|
|
|
|
|
|
auth = nil
|
|
|
|
# loop through providers until valid bind
|
|
|
|
providers.find do |provider|
|
|
|
|
auth = new(provider)
|
|
|
|
auth.login(login, password) # true will exit the loop
|
|
|
|
end
|
|
|
|
|
2014-10-14 05:14:57 -04:00
|
|
|
# If (login, password) was invalid for all providers, the value of auth is now the last
|
|
|
|
# Gitlab::LDAP::Authentication instance we tried.
|
2014-10-13 11:24:05 -04:00
|
|
|
auth.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.providers
|
|
|
|
Gitlab::LDAP::Config.providers
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :provider, :ldap_user
|
|
|
|
|
|
|
|
def initialize(provider)
|
|
|
|
@provider = provider
|
|
|
|
end
|
|
|
|
|
|
|
|
def login(login, password)
|
|
|
|
@ldap_user = adapter.bind_as(
|
|
|
|
filter: user_filter(login),
|
|
|
|
size: 1,
|
|
|
|
password: password
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def adapter
|
2014-10-23 08:21:21 -04:00
|
|
|
OmniAuth::LDAP::Adaptor.new(config.options.symbolize_keys)
|
2014-10-13 11:24:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
|
|
|
Gitlab::LDAP::Config.new(provider)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_filter(login)
|
2015-03-06 07:26:33 -05:00
|
|
|
filter = Net::LDAP::Filter.equals(config.uid, login)
|
2014-10-14 04:08:47 -04:00
|
|
|
|
|
|
|
# Apply LDAP user filter if present
|
|
|
|
if config.user_filter.present?
|
|
|
|
filter = Net::LDAP::Filter.join(
|
|
|
|
filter,
|
|
|
|
Net::LDAP::Filter.construct(config.user_filter)
|
|
|
|
)
|
2014-10-13 11:24:05 -04:00
|
|
|
end
|
2014-10-14 04:08:47 -04:00
|
|
|
filter
|
2014-10-13 11:24:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
|
|
|
return nil unless ldap_user
|
|
|
|
Gitlab::LDAP::User.find_by_uid_and_provider(ldap_user.dn, provider)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-23 08:21:21 -04:00
|
|
|
end
|