Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LDAP bind failure cause an ldap_error, rather than an invalid_credentials #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/omniauth-ldap/adaptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def bind_as(args = {})
result = false
@connection.open do |me|
rs = me.search args
if rs and rs.first and dn = rs.first.dn
raise ConnectionError.new("bind failed") unless rs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Net::LDAP#search returns nil if the operation was not successful: https://github.com/ruby-ldap/ruby-net-ldap/blob/master/lib/net/ldap.rb#L722-765

Previously, this was just not entering the if and returning the default result of false. If search can successfully connect, but cannot find results (bad username/password in the params), #search returns an empty array, and we still get invalid_credentials as a result.


if rs.first and dn = rs.first.dn
password = args[:password]
method = args[:method] || @method
password = password.call if password.respond_to?(:call)
Expand Down
9 changes: 9 additions & 0 deletions spec/omniauth-ldap/adaptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,14 @@
adaptor.connection.should_receive(:bind).and_return(true)
adaptor.bind_as(args).should == rs
end

it "should raise a ConnectionError if the bind fails" do
adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.126", method: 'plain', base: 'dc=score, dc=local', port: 389, uid: 'sAMAccountName', bind_dn: 'bind_dn', password: 'password'})
adaptor.connection.should_receive(:open).and_yield(adaptor.connection)
# Net::LDAP#search returns nil if the operation was not successful
adaptor.connection.should_receive(:search).with(args).and_return(nil)
adaptor.connection.should_receive(:bind).never
lambda { adaptor.bind_as(args) }.should raise_error OmniAuth::LDAP::Adaptor::ConnectionError
end
end
end