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

Include all common names when common_names is in extra attributes #11

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
19 changes: 9 additions & 10 deletions lib/casino/ldap_authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,16 @@ def user_filter(username)
end

def extra_attributes(user_plain)
if @options[:extra_attributes]

Choose a reason for hiding this comment

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

Is it safe to remove this conditional?

Alternatively, you could do (@options[:extra_attributes] || []).each_with_object({})...

Copy link
Author

Choose a reason for hiding this comment

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

I was able to remove it without any specs failing, but that doesn't mean it's safe to remove. It turns out that line 52 already requires extra_attributes to be present. @options[:extra_attributes].values will give you NoMethodError: undefined method `values' for nil:NilClass if extra_attributes isn't included in the config.

Copy link
Author

Choose a reason for hiding this comment

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

Actually, there's an issue about this exact thing #9

result = {}
@options[:extra_attributes].each do |index_result, index_ldap|
value = user_plain[index_ldap]
if value
result[index_result] = "#{value.first}"
end
@options[:extra_attributes].each_with_object({}) do |(index_result, index_ldap), result|
result.merge!(index_result => user_plain[index_ldap].first.to_s)
end.tap do |results|
if @options[:extra_attributes].keys.include?(:common_names)
results[:common_names] = common_name_list(user_plain[:memberof])
end
result
else
nil
end
end

def common_name_list(memberof_list)
memberof_list.map { |memberof| memberof.match(/^CN=(\w*),/)[1] }
end
end
51 changes: 50 additions & 1 deletion spec/casino/ldap_authenticator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
:base => 'dc=users,dc=example.com',
:encryption => 'simple_tls',
:username_attribute => 'uid',
:extra_attributes => { :email => 'mail', :fullname => :displayname, :memberof => 'memberof'}
:extra_attributes => extra_attributes_options
} }
let(:extra_attributes_options) { Hash :email => 'mail', :fullname => :displayname, :memberof => 'memberof' }
let(:subject) { described_class.new(options) }
let(:connection) { Object.new }

Expand Down Expand Up @@ -164,6 +165,54 @@
}
}
end

context 'when supplied the common_names option' do
let(:extra_attributes_options) { super().merge(common_names: 'common_names') }
let(:group_1) { 'CN=group1,OU=Organization,OU=Unit,DC=Domain,DC=Component' }
let(:ldap_entry) do
Net::LDAP::Entry.new.tap do |entry|
entry[:uid] = [username]
entry[:displayname] = [fullname]
entry[:mail] = [email]
end
end

context 'when the user belongs to no groups' do
it 'returns the user data with an empty list of common names' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname,
:memberof => '',
:common_names => []
}
}
end
end

context 'when the user belongs to many groups' do
let(:membership) do
[
group_1,
'CN=group2,OU=Organization,OU=Unit,DC=Domain,DC=Component',
'CN=group3,OU=Organization,OU=Unit,DC=Domain,DC=Component',
]
end
let(:ldap_entry) { super().tap { |entry| entry[:memberof] = membership } }
it 'returns the user data with a list of common names of the groups' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname,
:memberof => group_1,
:common_names => ['group1', 'group2', 'group3']
}
}
end
end
end
end

context 'when validation fails' do
Expand Down