diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index c921ec0d50be83b757f0b5cb5cba49bc78a1922b..873ce13669f74b4a6bc7c75740f1ed27b5422289 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -33,7 +33,26 @@ module Grack # Authentication with username and password login, password = @auth.credentials self.user = User.find_by_email(login) || User.find_by_username(login) - return false unless user.try(:valid_password?, password) + self.user = nil unless user.try(:valid_password?, password) + + # Check user against LDAP backend if user is not authenticated + # Only check with valid login and password to prevent anonymous bind results + gl = Gitlab.config + if user.nil? && gl.ldap.enabled && !login.blank? && !password.blank? + require "omniauth-ldap" + ldap = OmniAuth::LDAP::Adaptor.new(gl.ldap) + ldap_user = ldap.bind_as( + filter: Net::LDAP::Filter.eq(ldap.uid, login), + size: 1, + password: password + ) + + if ldap_user + self.user = User.find_by_extern_uid_and_provider(ldap_user.dn, 'ldap') + end + end + + return false unless user # Set GL_USER env variable ENV['GL_USER'] = user.email