diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..37a97d76ed570b97bfd472d1eaaedf9403a635a8 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,12 @@ +image: "ruby:2.3.1" + +before_script: + - bundle install + +stages: + - test + +rspec: + stage: test + script: + - bundle exec rake spec diff --git a/Gemfile.lock b/Gemfile.lock index ab3d3d919152adb806e048ba4f1fcb0bb02a4e2c..13b7bdfd2d59a7dccba93580c8658a318ed4cfb4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ PATH remote: . specs: gitlab_omniauth-ldap (1.2.1) - net-ldap (~> 0.9) + net-ldap (~> 0.12) omniauth (~> 1.0) pyu-ruby-sasl (~> 0.0.3.1) rubyntlm (~> 0.3) @@ -12,12 +12,12 @@ GEM specs: coderay (1.0.8) diff-lcs (1.1.3) - hashie (3.4.0) + hashie (3.4.4) method_source (0.8.1) - net-ldap (0.11) - omniauth (1.2.2) + net-ldap (0.15.0) + omniauth (1.3.1) hashie (>= 1.2, < 4) - rack (~> 1.0) + rack (>= 1.0, < 3) pry (0.9.10) coderay (~> 1.0.5) method_source (~> 0.8) @@ -35,7 +35,7 @@ GEM rspec-expectations (2.12.1) diff-lcs (~> 1.1.3) rspec-mocks (2.12.1) - rubyntlm (0.5.0) + rubyntlm (0.5.2) slop (3.3.3) PLATFORMS @@ -47,3 +47,6 @@ DEPENDENCIES rack-test rake rspec + +BUNDLED WITH + 1.12.5 diff --git a/gitlab_omniauth-ldap.gemspec b/gitlab_omniauth-ldap.gemspec index 940512d4d8ed1c3c034d3d4dff4c83ab4755a4d6..c9606db0e4558726abb2766532a2e70b19f921ca 100644 --- a/gitlab_omniauth-ldap.gemspec +++ b/gitlab_omniauth-ldap.gemspec @@ -10,7 +10,7 @@ Gem::Specification.new do |gem| gem.license = "MIT" gem.add_runtime_dependency 'omniauth', '~> 1.0' - gem.add_runtime_dependency 'net-ldap', '~> 0.9' + gem.add_runtime_dependency 'net-ldap', '~> 0.12' gem.add_runtime_dependency 'pyu-ruby-sasl', '~> 0.0.3.1' gem.add_runtime_dependency 'rubyntlm', '~> 0.3' diff --git a/lib/omniauth-ldap/adaptor.rb b/lib/omniauth-ldap/adaptor.rb index 68b4e14cc4c6d7a316a3bf5d8f5b284592108b62..eb5603e9f2f1ed31372460a30c19c9c059f26ef2 100644 --- a/lib/omniauth-ldap/adaptor.rb +++ b/lib/omniauth-ldap/adaptor.rb @@ -13,10 +13,19 @@ module OmniAuth class AuthenticationError < StandardError; end class ConnectionError < StandardError; end - VALID_ADAPTER_CONFIGURATION_KEYS = [:host, :port, :method, :bind_dn, :password, :try_sasl, :sasl_mechanisms, :uid, :base, :allow_anonymous, :filter] + VALID_ADAPTER_CONFIGURATION_KEYS = [ + :hosts, :host, :port, :method, :bind_dn, :password, :try_sasl, + :sasl_mechanisms, :uid, :base, :allow_anonymous, :filter + ] # A list of needed keys. Possible alternatives are specified using sub-lists. - MUST_HAVE_KEYS = [:host, :port, :method, [:uid, :filter], :base] + MUST_HAVE_KEYS = [ + :base, + :method, + [:hosts, :host], + [:hosts, :port], + [:uid, :filter] + ] METHOD = { :ssl => :simple_tls, @@ -47,12 +56,12 @@ module OmniAuth end method = ensure_method(@method) config = { - :host => @host, - :port => @port, - :encryption => method, - :base => @base + base: @base, + hosts: @hosts, + host: @host, + port: @port, + method: @method } - @bind_method = @try_sasl ? :sasl : (@allow_anonymous||!@bind_dn||!@password ? :anonymous : :simple) diff --git a/lib/omniauth-ldap/version.rb b/lib/omniauth-ldap/version.rb index 4e21beeb41bd6f3a04bde3256385b232f87e3dc8..951e9faca762858d65a8da57391ad5512e8915d2 100644 --- a/lib/omniauth-ldap/version.rb +++ b/lib/omniauth-ldap/version.rb @@ -1,5 +1,5 @@ module OmniAuth module LDAP - VERSION = "1.2.1" + VERSION = "1.3.1" end end diff --git a/spec/omniauth-ldap/adaptor_spec.rb b/spec/omniauth-ldap/adaptor_spec.rb index e6a304fcee7dc375ed7ddb626c8be147dc3ad301..8b6a474c4d8fc02cee30dead5e6af5e25ec7d857 100644 --- a/spec/omniauth-ldap/adaptor_spec.rb +++ b/spec/omniauth-ldap/adaptor_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper' -describe "OmniAuth::LDAP::Adaptor" do +describe OmniAuth::LDAP::Adaptor do describe 'initialize' do it 'should throw exception when must have field is not set' do @@ -7,6 +7,17 @@ describe "OmniAuth::LDAP::Adaptor" do lambda { OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain'})}.should raise_error(ArgumentError) end + it 'should not throw an error if hosts is set but host and port are not' do + expect( + described_class.new( + hosts: [['192.168.1.145', 389], ['192.168.1.146', 389]], + method: 'plain', + base: 'dc=example,dc=com', + uid: 'uid' + ) + ).not_to raise_error(ArgumentError) + end + it 'should throw exception when method is not supported' do lambda { OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'myplain', uid: 'uid', port: 389, base: 'dc=com'})}.should raise_error(OmniAuth::LDAP::Adaptor::ConfigurationError) end @@ -52,6 +63,33 @@ describe "OmniAuth::LDAP::Adaptor" do adaptor.connection.instance_variable_get('@auth')[:initial_credential].should =~ /^NTLMSSP/ adaptor.connection.instance_variable_get('@auth')[:challenge_response].should_not be_nil end + + it 'sets up a connection with the proper host and port' do + adapter = described_class.new( + host: '192.168.1.145', + method: 'plain', + base: 'dc=example,dc=com', + port: 3890, + uid: 'uid' + ) + + expect(adapter.connection.host).to eq('192.168.1.145') + expect(adapter.connection.port).to eq(3890) + expect(adapter.connection.hosts).to be_nil + end + + it 'sets up a connection with a enumerable pairs of hosts' do + adapter = described_class.new( + hosts: [['192.168.1.145', 636], ['192.168.1.146', 636]], + method: 'plain', + base: 'dc=example,dc=com', + uid: 'uid' + ) + + expect(adapter.connection.host).to eq('127.0.0.1') + expect(adapter.connection.port).to eq(389) + expect(adapter.connection.hosts).to match_array([['192.168.1.145', 636], ['192.168.1.146', 636]]) + end end describe 'bind_as' do