Skip to content

Commit

Permalink
Add ldap search filter (mastodon#8151)
Browse files Browse the repository at this point in the history
  • Loading branch information
immae authored and Gargron committed Aug 15, 2018
1 parent aaac14b commit b0f4fe4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.production.sample
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ STREAMING_CLUSTER_NUM=1
# LDAP_BIND_DN=
# LDAP_PASSWORD=
# LDAP_UID=cn
# LDAP_SEARCH_FILTER="%{uid}=%{email}"

# PAM authentication (optional)
# PAM authentication uses for the email generation the "email" pam variable
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ module Devise
@@ldap_password = nil
mattr_accessor :ldap_tls_no_verify
@@ldap_tls_no_verify = false
mattr_accessor :ldap_search_filter
@@ldap_search_filter = nil

class Strategies::PamAuthenticatable
def valid?
Expand Down Expand Up @@ -362,5 +364,6 @@ def valid?
config.ldap_password = ENV.fetch('LDAP_PASSWORD')
config.ldap_uid = ENV.fetch('LDAP_UID', 'cn')
config.ldap_tls_no_verify = ENV['LDAP_TLS_NO_VERIFY'] == 'true'
config.ldap_search_filter = ENV.fetch('LDAP_SEARCH_FILTER', '%{uid}=%{email}')
end
end
3 changes: 2 additions & 1 deletion lib/devise/ldap_authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def authenticate!
connect_timeout: 10
)

if (user_info = ldap.bind_as(base: Devise.ldap_base, filter: "(#{Devise.ldap_uid}=#{email})", password: password))
filter = format(Devise.ldap_search_filter, uid: Devise.ldap_uid, email: email)
if (user_info = ldap.bind_as(base: Devise.ldap_base, filter: filter, password: password))
user = User.ldap_get_user(user_info.first)
success!(user)
else
Expand Down

0 comments on commit b0f4fe4

Please sign in to comment.