Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from dayer4b/extra_attributes-fix
Browse files Browse the repository at this point in the history
Extra attributes fix
  • Loading branch information
pencil committed May 28, 2014
2 parents 64dd5d4 + 292d0a6 commit 871c1dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.1
7 changes: 5 additions & 2 deletions lib/casino/ldap_authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ def authenticate
@ldap.auth(@options[:admin_user], @options[:admin_password])
end
@user_plain = @ldap.bind_as(:base => @options[:base], :size => 1, :password => @password, :filter => user_filter)
if @user_plain.is_a?(Array)
@user_plain = @user_plain.first
if @user_plain != false
@user_plain = @ldap.search(:base => @options[:base], :filter => user_filter, :attributes => @options[:extra_attributes].values)
if @user_plain.is_a?(Array)
@user_plain = @user_plain.first
end
end
end

Expand Down
49 changes: 46 additions & 3 deletions spec/casino/ldap_authenticator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:base => 'dc=users,dc=example.com',
:encryption => 'simple_tls',
:username_attribute => 'uid',
:extra_attributes => { :email => 'mail', :fullname => :displayname }
:extra_attributes => { :email => 'mail', :fullname => :displayname, :memberof => 'memberof'}
} }
let(:subject) { described_class.new(options) }
let(:connection) { Object.new }
Expand All @@ -24,9 +24,11 @@
let(:username) { 'test' }
let(:password) { 'foo' }
let(:user_filter) { Net::LDAP::Filter.eq(options[:username_attribute], username) }
let(:extra_attributes) { ['mail', :displayname, 'memberof'] }

before(:each) do
connection.stub(:bind_as)
connection.stub(:search)
end

it 'does the connection setup' do
Expand All @@ -41,7 +43,12 @@
subject.validate(username, password)
end

context 'when validation succeeds' do
it 'calls the #search method on the LDAP connection' do
connection.should_receive(:search).with(:base => options[:base], :filter => user_filter, :attributes => extra_attributes)
subject.validate(username, password)
end

context 'when validation succeeds for user with missing data' do
let(:fullname) { 'Example User' }
let(:email) { "#{username}@example.org" }
let(:ldap_entry) {
Expand All @@ -55,14 +62,50 @@
connection.stub(:bind_as) do
ldap_entry
end
connection.stub(:search) do
ldap_entry
end
end

it 'returns the user data with blank value for missing data' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname,
:memberof => ''
}
}
end
end

context 'when validation succeeds for user with complete data' do
let(:fullname) { 'Example User' }
let(:email) { "#{username}@example.org" }
let(:membership) { "cn=group1" }
let(:ldap_entry) {
entry = Net::LDAP::Entry.new
{:uid => username, :displayname => fullname, :mail => email, :memberof => membership}.each do |key, value|
entry[key] = [value]
end
entry
}
before(:each) do
connection.stub(:bind_as) do
ldap_entry
end
connection.stub(:search) do
ldap_entry
end
end

it 'returns the user data' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname
:fullname => fullname,
:memberof => membership
}
}
end
Expand Down

0 comments on commit 871c1dd

Please sign in to comment.