Skip to content

Commit 4510ff3

Browse files
author
Douwe Maan
committed
Merge branch 'improve-omniauth-ldap-failure-log-message' into 'master'
Improves log message when invalid credentials are used See merge request !11
2 parents 8e1e0cb + 17cfc1c commit 4510ff3

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
## 2.0.4
2+
- Improve log message when invalid credentials are used
3+
14
## 2.0.3
25
- Protects against wrong request method call to callback

lib/omniauth-ldap/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module OmniAuth
22
module LDAP
3-
VERSION = "2.0.3"
3+
VERSION = "2.0.4"
44
end
55
end

lib/omniauth/strategies/ldap.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module OmniAuth
44
module Strategies
55
class LDAP
66
include OmniAuth::Strategy
7+
8+
InvalidCredentialsError = Class.new(StandardError)
9+
710
@@config = {
811
'name' => 'cn',
912
'first_name' => 'givenName',
@@ -45,7 +48,9 @@ def callback_phase
4548
begin
4649
@ldap_user_info = @adaptor.bind_as(:filter => filter(@adaptor), :size => 1, :password => request['password'])
4750

48-
return fail!(:invalid_credentials) if !@ldap_user_info
51+
unless @ldap_user_info
52+
return fail!(:invalid_credentials, InvalidCredentialsError.new("Invalid credentials for #{request['username']}"))
53+
end
4954

5055
@user_info = self.class.map_user(@@config, @ldap_user_info)
5156
super
@@ -54,7 +59,7 @@ def callback_phase
5459
end
5560
end
5661

57-
def filter adaptor
62+
def filter(adaptor)
5863
if adaptor.filter and !adaptor.filter.empty?
5964
username = Net::LDAP::Filter.escape(@options[:name_proc].call(request['username']))
6065
Net::LDAP::Filter.construct(adaptor.filter % { username: username })

spec/omniauth/strategies/ldap_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ class MyLdapProvider < OmniAuth::Strategies::LDAP; end
6969

7070
it 'should redirect to error page' do
7171
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
72-
last_response.should be_redirect
73-
last_response.headers['Location'].should =~ %r{invalid_credentials}
72+
73+
expect(last_response).to be_redirect
74+
expect(last_response.headers['Location']).to match('invalid_credentials')
75+
expect(last_request.env['omniauth.error'].message).to eq('Invalid credentials for ping')
7476
end
7577

7678
it 'should redirect to error page when there is exception' do
@@ -132,17 +134,19 @@ class MyLdapProvider < OmniAuth::Strategies::LDAP; end
132134
it 'should redirect to error page' do
133135
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
134136

135-
last_response.should be_redirect
136-
last_response.headers['Location'].should =~ %r{invalid_credentials}
137+
expect(last_response).to be_redirect
138+
expect(last_response.headers['Location']).to match('invalid_credentials')
139+
expect(last_request.env['omniauth.error'].message).to eq('Invalid credentials for ping')
137140
end
138141
context 'and filter is set' do
139142
it 'should bind with filter' do
140143
@adaptor.stub(:filter).and_return('uid=%{username}')
141144
Net::LDAP::Filter.should_receive(:construct).with('uid=ping')
142145
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
143146

144-
last_response.should be_redirect
145-
last_response.headers['Location'].should =~ %r{invalid_credentials}
147+
expect(last_response).to be_redirect
148+
expect(last_response.headers['Location']).to match('invalid_credentials')
149+
expect(last_request.env['omniauth.error'].message).to eq('Invalid credentials for ping')
146150
end
147151
end
148152

0 commit comments

Comments
 (0)