Skip to content

Commit 345b2a2

Browse files
authored
Merge pull request #106 from omniauth/feat/timeout
2 parents c4d782d + 422ee0b commit 345b2a2

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

.rubocop_gradual.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"lib/omniauth-ldap/adaptor.rb:3212021924": [
3-
[65, 7, 413, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 105664470]
2+
"lib/omniauth-ldap/adaptor.rb:3925200886": [
3+
[68, 7, 413, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 105664470]
44
],
55
"spec/integration/middleware_spec.rb:4142891586": [
66
[3, 16, 39, "RSpec/DescribeClass: The first argument to describe should be the class or module being tested.", 638096201],
@@ -12,10 +12,10 @@
1212
[4, 3, 12, "RSpec/BeforeAfterAll: Beware of using `before(:all)` as it may cause state to leak between tests. If you are using `rspec-rails`, and `use_transactional_fixtures` is enabled, then records created in `before(:all)` are not automatically rolled back.", 86334566],
1313
[70, 16, 5, "RSpec/ExpectActual: Provide the actual value you are testing to `expect(...)`.", 237881235]
1414
],
15-
"spec/omniauth-ldap/adaptor_spec.rb:321639549": [
15+
"spec/omniauth-ldap/adaptor_spec.rb:1245381318": [
1616
[3, 1, 38, "RSpec/SpecFilePathFormat: Spec path should end with `omni_auth/ldap/adaptor*_spec.rb`.", 1973618936],
17-
[225, 9, 26, "RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.", 1924417310],
18-
[226, 9, 26, "RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.", 1924417310]
17+
[239, 9, 26, "RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.", 1924417310],
18+
[240, 9, 26, "RSpec/StubbedMock: Prefer `allow` over `expect` when configuring a response.", 1924417310]
1919
],
2020
"spec/omniauth/adaptor_spec.rb:1168013709": [
2121
[3, 1, 38, "RSpec/SpecFilePathFormat: Spec path should end with `omni_auth/ldap/adaptor*_spec.rb`.", 1973618936],

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ use OmniAuth::Strategies::LDAP,
6363
name_proc: proc { |name| name.gsub(/@.*$/, "") },
6464
bind_dn: "default_bind_dn",
6565
password: "password",
66+
# Optional timeouts (seconds)
67+
connect_timeout: 3,
68+
read_timeout: 7,
6669
tls_options: {
6770
ssl_version: "TLSv1_2",
6871
ciphers: ["AES-128-CBC", "AES-128-CBC-HMAC-SHA1", "AES-128-CBC-HMAC-SHA256"],
@@ -194,6 +197,8 @@ The following options are available for configuring the OmniAuth LDAP strategy:
194197
- `:password_policy` - When true, the strategy will request the LDAP Password Policy response control (OID `1.3.6.1.4.1.42.2.27.8.5.1`) during the user bind. If the server supports it, the adaptor exposes:
195198
- `adaptor.last_operation_result` — the last Net::LDAP operation result object.
196199
- `adaptor.last_password_policy_response` — the matching password policy response control (implementation-specific object). This can indicate conditions such as password expired, account locked, reset required, or grace logins remaining (per the draft RFC).
200+
- `:connect_timeout` - Maximum time in seconds to wait when establishing the TCP connection to the LDAP server. Forwarded to `Net::LDAP`.
201+
- `:read_timeout` - Maximum time in seconds to wait for reads during LDAP operations (search/bind). Forwarded to `Net::LDAP`.
197202

198203
Example enabling password policy:
199204

@@ -719,7 +724,7 @@ To join the community or get help 👇️ Join the Discord.
719724

720725
To say "thanks!" ☝️ Join the Discord or 👇️ send money.
721726

722-
[![Sponsor me on GitHub Sponsors][🖇sponsor-bottom-img]][🖇sponsor] 💌 [![Sponsor me on Liberapay][⛳liberapay-bottom-img]][⛳liberapay-img] 💌 [![Donate on PayPal][🖇paypal-bottom-img]][🖇paypal-img]
727+
[![Sponsor me on GitHub Sponsors][🖇sponsor-bottom-img]][🖇sponsor] 💌 [![Sponsor me on Liberapay][⛳liberapay-bottom-img]][⛳liberapay] 💌 [![Donate on PayPal][🖇paypal-bottom-img]][🖇paypal]
723728

724729
### Please give the project a star ⭐ ♥.
725730

lib/omniauth-ldap/adaptor.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class ConnectionError < StandardError; end
3232
:filter,
3333
:tls_options,
3434
:password_policy,
35+
# Timeouts
36+
:connect_timeout,
37+
:read_timeout,
3538

3639
# Deprecated
3740
:method,
@@ -89,6 +92,8 @@ def initialize(configuration = {})
8992
port: @port,
9093
encryption: encryption_options,
9194
}
95+
# Remove passing timeouts here to avoid issues on older net-ldap versions.
96+
# We'll set them after initialization if the connection responds to writers.
9297
@bind_method = if @try_sasl
9398
:sasl
9499
else
@@ -103,6 +108,21 @@ def initialize(configuration = {})
103108
}
104109
config[:auth] = @auth
105110
@connection = Net::LDAP.new(config)
111+
# Apply optional timeout settings if supported by the installed net-ldap version
112+
if !@connect_timeout.nil?
113+
if @connection.respond_to?(:connect_timeout=)
114+
@connection.connect_timeout = @connect_timeout
115+
else
116+
@connection.instance_variable_set(:@connect_timeout, @connect_timeout)
117+
end
118+
end
119+
if !@read_timeout.nil?
120+
if @connection.respond_to?(:read_timeout=)
121+
@connection.read_timeout = @read_timeout
122+
else
123+
@connection.instance_variable_set(:@read_timeout, @read_timeout)
124+
end
125+
end
106126
end
107127

108128
#:base => "dc=yourcompany, dc=com",

lib/omniauth/strategies/ldap.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class LDAP
4646
# standard Rack "HTTP_" variant automatically.
4747
option :header_auth, false
4848
option :header_name, "REMOTE_USER"
49+
# Optional timeouts (forwarded to Net::LDAP when supported)
50+
option :connect_timeout, nil
51+
option :read_timeout, nil
4952

5053
def request_phase
5154
# OmniAuth >= 2.0 expects the request phase to be POST-only for /auth/:provider.

spec/omniauth-ldap/adaptor_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,20 @@
195195
expect(adaptor.connection.instance_variable_get(:@encryption)).to include method: :start_tls
196196
end
197197
end
198+
199+
context "when timeouts are configured" do
200+
it "passes connect_timeout and read_timeout settings to Net::LDAP connection" do
201+
adaptor = described_class.new(host: "192.168.1.145", encryption: "plain", base: "dc=example,dc=com", port: 389, uid: "uid", connect_timeout: 3, read_timeout: 7)
202+
expect(adaptor.connection.instance_variable_get(:@connect_timeout)).to eq 3
203+
expect(adaptor.connection.instance_variable_get(:@read_timeout)).to eq 7
204+
end
205+
206+
it "omits timeout settings when not provided" do
207+
adaptor = described_class.new(host: "192.168.1.145", encryption: "plain", base: "dc=example,dc=com", port: 389, uid: "uid")
208+
expect(adaptor.connection.instance_variable_get(:@connect_timeout)).to be_nil
209+
expect(adaptor.connection.instance_variable_get(:@read_timeout)).to be_nil
210+
end
211+
end
198212
end
199213

200214
describe "bind_as" do

0 commit comments

Comments
 (0)