Skip to content

Commit 6717bbe

Browse files
authored
Merge pull request #78 from omniauth/update
Bring update-to-date
2 parents 5f29d6a + 822febc commit 6717bbe

File tree

3 files changed

+112
-104
lines changed

3 files changed

+112
-104
lines changed

omniauth-ldap.gemspec

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ Gem::Specification.new do |gem|
99
gem.homepage = "https://github.com/intridea/omniauth-ldap"
1010
gem.license = "MIT"
1111

12-
gem.add_runtime_dependency 'omniauth', '~> 1.0'
13-
gem.add_runtime_dependency 'net-ldap', '~> 0.12'
14-
gem.add_runtime_dependency 'pyu-ruby-sasl', '~> 0.0.3.2'#0.0.3.1 has been yanked
15-
gem.add_runtime_dependency 'rubyntlm', '~> 0.3.4'
16-
gem.add_development_dependency 'rspec', '~> 2.7'
12+
gem.add_runtime_dependency 'omniauth', '~> 1.8.1'
13+
gem.add_runtime_dependency 'net-ldap', '~> 0.16'
14+
gem.add_runtime_dependency 'pyu-ruby-sasl', '~> 0.0.3.3'
15+
gem.add_runtime_dependency 'rubyntlm', '~> 0.6.2'
16+
gem.add_development_dependency 'rspec', '~> 3.0'
1717
gem.add_development_dependency 'simplecov'
1818
gem.add_development_dependency 'rack-test'
19-
gem.add_development_dependency 'libnotify'
20-
gem.add_development_dependency 'ruby-debug19'
2119

2220
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
2321
gem.files = `git ls-files`.split("\n")

spec/omniauth-ldap/adaptor_spec.rb

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,83 @@
44
describe 'initialize' do
55
it 'should throw exception when must have field is not set' do
66
#[:host, :port, :method, :bind_dn]
7-
lambda { OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain'})}.should raise_error(ArgumentError)
7+
expect {
8+
OmniAuth::LDAP::Adaptor.new({ host: "192.168.1.145", method: 'plain' })
9+
}.to raise_error(ArgumentError)
810
end
911

1012
it 'should throw exception when method is not supported' do
11-
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)
13+
expect {
14+
OmniAuth::LDAP::Adaptor.new({ host: "192.168.1.145", method: 'myplain', uid: 'uid', port: 389, base: 'dc=com' })
15+
}.to raise_error(OmniAuth::LDAP::Adaptor::ConfigurationError)
1216
end
1317

1418
it 'should setup ldap connection with anonymous' do
15-
adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName'})
16-
adaptor.connection.should_not == nil
17-
adaptor.connection.host.should == '192.168.1.145'
18-
adaptor.connection.port.should == 389
19-
adaptor.connection.base.should == 'dc=intridea, dc=com'
20-
adaptor.connection.instance_variable_get('@auth').should == {:method => :anonymous, :username => nil, :password => nil}
19+
adaptor = OmniAuth::LDAP::Adaptor.new({ host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName' })
20+
expect(adaptor.connection).to_not be_nil
21+
expect(adaptor.connection.host).to eq '192.168.1.145'
22+
expect(adaptor.connection.port).to eq 389
23+
expect(adaptor.connection.base).to eq 'dc=intridea, dc=com'
24+
expect(adaptor.connection.instance_variable_get('@auth')).to eq({ method: :anonymous, username: nil, password: nil })
2125
end
2226

2327
it 'should setup ldap connection with simple' do
24-
adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', bind_dn: 'bind_dn', password: 'password'})
25-
adaptor.connection.should_not == nil
26-
adaptor.connection.host.should == '192.168.1.145'
27-
adaptor.connection.port.should == 389
28-
adaptor.connection.base.should == 'dc=intridea, dc=com'
29-
adaptor.connection.instance_variable_get('@auth').should == {:method => :simple, :username => 'bind_dn', :password => 'password'}
28+
adaptor = OmniAuth::LDAP::Adaptor.new({ host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', bind_dn: 'bind_dn', password: 'password' })
29+
expect(adaptor.connection).to_not be_nil
30+
expect(adaptor.connection.host).to eq '192.168.1.145'
31+
expect(adaptor.connection.port).to eq 389
32+
expect(adaptor.connection.base).to eq 'dc=intridea, dc=com'
33+
expect(adaptor.connection.instance_variable_get('@auth')).to eq({ method: :simple, username: 'bind_dn', password: 'password' })
3034
end
3135

3236
it 'should setup ldap connection with sasl-md5' do
33-
adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["DIGEST-MD5"], bind_dn: 'bind_dn', password: 'password'})
34-
adaptor.connection.should_not == nil
35-
adaptor.connection.host.should == '192.168.1.145'
36-
adaptor.connection.port.should == 389
37-
adaptor.connection.base.should == 'dc=intridea, dc=com'
38-
adaptor.connection.instance_variable_get('@auth')[:method].should == :sasl
39-
adaptor.connection.instance_variable_get('@auth')[:mechanism].should == 'DIGEST-MD5'
40-
adaptor.connection.instance_variable_get('@auth')[:initial_credential].should == ''
41-
adaptor.connection.instance_variable_get('@auth')[:challenge_response].should_not be_nil
37+
adaptor = OmniAuth::LDAP::Adaptor.new({ host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["DIGEST-MD5"], bind_dn: 'bind_dn', password: 'password' })
38+
expect(adaptor.connection).to_not be_nil
39+
expect(adaptor.connection.host).to eq '192.168.1.145'
40+
expect(adaptor.connection.port).to eq 389
41+
expect(adaptor.connection.base).to eq 'dc=intridea, dc=com'
42+
expect(adaptor.connection.instance_variable_get('@auth')[:method]).to eq :sasl
43+
expect(adaptor.connection.instance_variable_get('@auth')[:mechanism]).to eq 'DIGEST-MD5'
44+
expect(adaptor.connection.instance_variable_get('@auth')[:initial_credential]).to eq ''
45+
expect(adaptor.connection.instance_variable_get('@auth')[:challenge_response]).to_not be_nil
4246
end
4347

4448
it 'should setup ldap connection with sasl-gss' do
4549
adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["GSS-SPNEGO"], bind_dn: 'bind_dn', password: 'password'})
46-
adaptor.connection.should_not == nil
47-
adaptor.connection.host.should == '192.168.1.145'
48-
adaptor.connection.port.should == 389
49-
adaptor.connection.base.should == 'dc=intridea, dc=com'
50-
adaptor.connection.instance_variable_get('@auth')[:method].should == :sasl
51-
adaptor.connection.instance_variable_get('@auth')[:mechanism].should == 'GSS-SPNEGO'
52-
adaptor.connection.instance_variable_get('@auth')[:initial_credential].should =~ /^NTLMSSP/
53-
adaptor.connection.instance_variable_get('@auth')[:challenge_response].should_not be_nil
50+
expect(adaptor.connection).to_not be_nil
51+
expect(adaptor.connection.host).to eq '192.168.1.145'
52+
expect(adaptor.connection.port).to eq 389
53+
expect(adaptor.connection.base).to eq 'dc=intridea, dc=com'
54+
expect(adaptor.connection.instance_variable_get('@auth')[:method]).to eq :sasl
55+
expect(adaptor.connection.instance_variable_get('@auth')[:mechanism]).to eq 'GSS-SPNEGO'
56+
expect(adaptor.connection.instance_variable_get('@auth')[:initial_credential]).to match /^NTLMSSP/
57+
expect(adaptor.connection.instance_variable_get('@auth')[:challenge_response]).to_not be_nil
5458
end
5559

5660
it 'should set the encryption method correctly' do
57-
adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'tls', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName'})
58-
adaptor.connection.instance_variable_get('@encryption').should include method: :start_tls
61+
adaptor = OmniAuth::LDAP::Adaptor.new({ host: "192.168.1.145", method: 'tls', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName' })
62+
expect(adaptor.connection.instance_variable_get('@encryption')).to include method: :start_tls
5963
end
6064
end
6165

6266
describe 'bind_as' do
63-
let(:args) { {:filter => Net::LDAP::Filter.eq('sAMAccountName', 'username'), :password => 'password', :size => 1} }
67+
let(:args) { { :filter => Net::LDAP::Filter.eq('sAMAccountName', 'username'), :password => 'password', :size => 1 } }
6468
let(:rs) { Struct.new(:dn).new('new dn') }
6569

6670
it 'should bind simple' do
67-
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'})
68-
adaptor.connection.should_receive(:open).and_yield(adaptor.connection)
69-
adaptor.connection.should_receive(:search).with(args).and_return([rs])
70-
adaptor.connection.should_receive(:bind).with({:username => 'new dn', :password => args[:password], :method => :simple}).and_return(true)
71-
adaptor.bind_as(args).should == rs
71+
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' })
72+
expect(adaptor.connection).to receive(:open).and_yield(adaptor.connection)
73+
expect(adaptor.connection).to receive(:search).with(args).and_return([rs])
74+
expect(adaptor.connection).to receive(:bind).with({ :username => 'new dn', :password => args[:password], :method => :simple }).and_return(true)
75+
expect(adaptor.bind_as(args)).to eq rs
7276
end
7377

7478
it 'should bind sasl' do
75-
adaptor = OmniAuth::LDAP::Adaptor.new({host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["GSS-SPNEGO"], bind_dn: 'bind_dn', password: 'password'})
76-
adaptor.connection.should_receive(:open).and_yield(adaptor.connection)
77-
adaptor.connection.should_receive(:search).with(args).and_return([rs])
78-
adaptor.connection.should_receive(:bind).and_return(true)
79-
adaptor.bind_as(args).should == rs
79+
adaptor = OmniAuth::LDAP::Adaptor.new({ host: "192.168.1.145", method: 'plain', base: 'dc=intridea, dc=com', port: 389, uid: 'sAMAccountName', try_sasl: true, sasl_mechanisms: ["GSS-SPNEGO"], bind_dn: 'bind_dn', password: 'password' })
80+
expect(adaptor.connection).to receive(:open).and_yield(adaptor.connection)
81+
expect(adaptor.connection).to receive(:search).with(args).and_return([rs])
82+
expect(adaptor.connection).to receive(:bind).and_return(true)
83+
expect(adaptor.bind_as(args)).to eq rs
8084
end
8185
end
8286
end

spec/omniauth/strategies/ldap_spec.rb

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,110 +24,112 @@ class MyLdapProvider < OmniAuth::Strategies::LDAP; end
2424
end
2525

2626
it 'should add a camelization for itself' do
27-
OmniAuth::Utils.camelize('ldap').should == 'LDAP'
27+
expect(OmniAuth::Utils.camelize('ldap')).to eq 'LDAP'
2828
end
2929

3030
describe '/auth/ldap' do
3131
before(:each){ get '/auth/ldap' }
3232

3333
it 'should display a form' do
34-
last_response.status.should == 200
35-
last_response.body.should be_include("<form")
34+
expect(last_response.status).to eq 200
35+
expect(last_response.body).to include("<form")
3636
end
3737

3838
it 'should have the callback as the action for the form' do
39-
last_response.body.should be_include("action='/auth/ldap/callback'")
39+
expect(last_response.body).to include("action='/auth/ldap/callback'")
4040
end
4141

4242
it 'should have a text field for each of the fields' do
43-
last_response.body.scan('<input').size.should == 2
43+
expect(last_response.body.scan('<input').size).to eq 2
4444
end
4545
it 'should have a label of the form title' do
46-
last_response.body.scan('MyLdap Form').size.should > 1
46+
expect(last_response.body.scan('MyLdap Form').size).to be > 1
4747
end
4848
end
4949

5050
describe 'post /auth/ldap/callback' do
5151
before(:each) do
5252
@adaptor = double(OmniAuth::LDAP::Adaptor, {:uid => 'ping'})
53-
@adaptor.stub(:filter)
54-
OmniAuth::LDAP::Adaptor.stub(:new).and_return(@adaptor)
53+
54+
allow(@adaptor).to receive(:filter)
55+
allow(OmniAuth::LDAP::Adaptor).to receive(:new) { @adaptor }
5556
end
5657

5758
context 'failure' do
5859
before(:each) do
59-
@adaptor.stub(:bind_as).and_return(false)
60+
allow(@adaptor).to receive(:bind_as) { false }
6061
end
6162

6263
context "when username is not preset" do
6364
it 'should redirect to error page' do
6465
post('/auth/ldap/callback', {})
6566

66-
last_response.should be_redirect
67-
last_response.headers['Location'].should =~ %r{missing_credentials}
67+
# expect(last_response).to be redirect
68+
expect(last_response).to be_redirect
69+
expect(last_response.headers['Location']).to match %r{missing_credentials}
6870
end
6971
end
7072

7173
context "when username is empty" do
7274
it 'should redirect to error page' do
73-
post('/auth/ldap/callback', {:username => ""})
75+
post('/auth/ldap/callback', { username: "" })
7476

75-
last_response.should be_redirect
76-
last_response.headers['Location'].should =~ %r{missing_credentials}
77+
expect(last_response).to be_redirect
78+
expect(last_response.headers['Location']).to match %r{missing_credentials}
7779
end
7880
end
7981

8082
context "when username is present" do
8183
context "and password is not preset" do
8284
it 'should redirect to error page' do
83-
post('/auth/ldap/callback', {:username => "ping"})
85+
post('/auth/ldap/callback', { username: "ping" })
8486

85-
last_response.should be_redirect
86-
last_response.headers['Location'].should =~ %r{missing_credentials}
87+
expect(last_response).to be_redirect
88+
expect(last_response.headers['Location']).to match %r{missing_credentials}
8789
end
8890
end
8991

9092
context "and password is empty" do
9193
it 'should redirect to error page' do
92-
post('/auth/ldap/callback', {:username => "ping", :password => ""})
94+
post('/auth/ldap/callback', { username: "ping", password: "" })
9395

94-
last_response.should be_redirect
95-
last_response.headers['Location'].should =~ %r{missing_credentials}
96+
expect(last_response).to be_redirect
97+
expect(last_response.headers['Location']).to match %r{missing_credentials}
9698
end
9799
end
98100
end
99101

100102
context "when username and password are present" do
101103
context "and bind on LDAP server failed" do
102104
it 'should redirect to error page' do
103-
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
105+
post('/auth/ldap/callback', { username: 'ping', password: 'password' })
104106

105-
last_response.should be_redirect
106-
last_response.headers['Location'].should =~ %r{invalid_credentials}
107+
expect(last_response).to be_redirect
108+
expect(last_response.headers['Location']).to match %r{invalid_credentials}
107109
end
108110
context 'and filter is set' do
109111
it 'should bind with filter' do
110-
@adaptor.stub(:filter).and_return('uid=%{username}')
111-
Net::LDAP::Filter.should_receive(:construct).with('uid=ping')
112-
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
112+
allow(@adaptor).to receive(:filter) { 'uid=%{username}' }
113+
expect(Net::LDAP::Filter).to receive(:construct).with('uid=ping')
114+
post('/auth/ldap/callback', { username: 'ping', password: 'password' })
113115

114-
last_response.should be_redirect
115-
last_response.headers['Location'].should =~ %r{invalid_credentials}
116+
expect(last_response).to be_redirect
117+
expect(last_response.headers['Location']).to match %r{invalid_credentials}
116118
end
117119
end
118120

119121
end
120122

121123
context "and communication with LDAP server caused an exception" do
122124
before :each do
123-
@adaptor.stub(:bind_as).and_throw(Exception.new('connection_error'))
125+
allow(@adaptor).to receive(:bind_as).and_throw(Exception.new('connection_error'))
124126
end
125127

126128
it 'should redirect to error page' do
127-
post('/auth/ldap/callback', {:username => "ping", :password => "password"})
129+
post('/auth/ldap/callback', { username: "ping", password: "password" })
128130

129-
last_response.should be_redirect
130-
last_response.headers['Location'].should =~ %r{ldap_error}
131+
expect(last_response).to be_redirect
132+
expect(last_response.headers['Location']).to match %r{ldap_error}
131133
end
132134
end
133135
end
@@ -137,8 +139,8 @@ class MyLdapProvider < OmniAuth::Strategies::LDAP; end
137139
let(:auth_hash){ last_request.env['omniauth.auth'] }
138140

139141
before(:each) do
140-
@adaptor.stub(:filter)
141-
@adaptor.stub(:bind_as).and_return(Net::LDAP::Entry.from_single_ldif_string(
142+
allow(@adaptor).to receive(:filter)
143+
allow(@adaptor).to receive(:bind_as) { Net::LDAP::Entry.from_single_ldif_string(
142144
%Q{dn: cn=ping, dc=intridea, dc=com
143145
mail: ping@intridea.com
144146
givenname: Ping
@@ -156,38 +158,42 @@ class MyLdapProvider < OmniAuth::Strategies::LDAP; end
156158
jpegphoto: http://www.intridea.com/ping.jpg
157159
description: omniauth-ldap
158160
}
159-
))
161+
)}
160162
end
161163

162164
it 'should not redirect to error page' do
163165
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
164-
last_response.should_not be_redirect
166+
expect(last_response).to_not be_redirect
165167
end
166168

167169
context 'and filter is set' do
168170
it 'should bind with filter' do
169-
@adaptor.stub(:filter).and_return('uid=%{username}')
170-
Net::LDAP::Filter.should_receive(:construct).with('uid=ping')
171-
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
171+
allow(@adaptor).to receive(:filter) { 'uid=%{username}' }
172+
expect(Net::LDAP::Filter).to receive(:construct).with('uid=ping')
173+
post('/auth/ldap/callback', { username: 'ping', password: 'password' })
172174

173-
last_response.should_not be_redirect
175+
expect(last_response).to_not be_redirect
174176
end
175177
end
176178

177179
it 'should map user info to Auth Hash' do
178-
post('/auth/ldap/callback', {:username => 'ping', :password => 'password'})
179-
auth_hash.uid.should == 'cn=ping, dc=intridea, dc=com'
180-
auth_hash.info.email.should == 'ping@intridea.com'
181-
auth_hash.info.first_name.should == 'Ping'
182-
auth_hash.info.last_name.should == 'Yu'
183-
auth_hash.info.phone.should == '555-555-5555'
184-
auth_hash.info.mobile.should == '444-444-4444'
185-
auth_hash.info.nickname.should == 'ping'
186-
auth_hash.info.title.should == 'dev'
187-
auth_hash.info.location.should == 'k street, Washington, DC, U.S.A 20001'
188-
auth_hash.info.url.should == 'www.intridea.com'
189-
auth_hash.info.image.should == 'http://www.intridea.com/ping.jpg'
190-
auth_hash.info.description.should == 'omniauth-ldap'
180+
post('/auth/ldap/callback', { username: 'ping', password: 'password' })
181+
182+
expect(auth_hash.uid).to eq 'cn=ping, dc=intridea, dc=com'
183+
184+
info = auth_hash.info
185+
186+
expect(info.email).to eq 'ping@intridea.com'
187+
expect(info.first_name).to eq 'Ping'
188+
expect(info.last_name).to eq 'Yu'
189+
expect(info.phone).to eq '555-555-5555'
190+
expect(info.mobile).to eq '444-444-4444'
191+
expect(info.nickname).to eq 'ping'
192+
expect(info.title).to eq 'dev'
193+
expect(info.location).to eq 'k street, Washington, DC, U.S.A 20001'
194+
expect(info.url).to eq 'www.intridea.com'
195+
expect(info.image).to eq 'http://www.intridea.com/ping.jpg'
196+
expect(info.description).to eq 'omniauth-ldap'
191197
end
192198
end
193199
end

0 commit comments

Comments
 (0)