Skip to content

Commit

Permalink
✨ Convert LDAP username mastodon#12021
Browse files Browse the repository at this point in the history
Signed-off-by: mathieu.brunot <mathieu.brunot@monogramm.io>
  • Loading branch information
madmath03 committed Nov 24, 2019
1 parent 00b8704 commit f2c4f8e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env.nanobox
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ SMTP_FROM_ADDRESS=notifications@${APP_NAME}.nanoapp.io
# LDAP_BIND_DN=
# LDAP_PASSWORD=
# LDAP_UID=cn
# tootsuite/mastodon#12021: Convert LDAP username to valid format
# LDAP_UID_CONVERSION_ENABLED=true
# LDAP_UID_CONVERSION_SEARCH=.,-
# LDAP_UID_CONVERSION_REPLACE=_

# PAM authentication (optional)
# PAM authentication uses for the email generation the "email" pam variable
Expand Down
4 changes: 4 additions & 0 deletions .env.production.sample
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ STREAMING_CLUSTER_NUM=1
# LDAP_PASSWORD=
# LDAP_UID=cn
# LDAP_SEARCH_FILTER=%{uid}=%{email}
# tootsuite/mastodon#12021: Convert LDAP username to valid format
# LDAP_UID_CONVERSION_ENABLED=true
# LDAP_UID_CONVERSION_SEARCH=.,-
# LDAP_UID_CONVERSION_REPLACE=_

# PAM authentication (optional)
# PAM authentication uses for the email generation the "email" pam variable
Expand Down
13 changes: 11 additions & 2 deletions app/models/concerns/ldap_authenticable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ def authenticate_with_ldap(params = {})
end

def ldap_get_user(attributes = {})
resource = joins(:account).find_by(accounts: { username: attributes[Devise.ldap_uid.to_sym].first })
safe_username = attributes[Devise.ldap_uid.to_sym].first
# tootsuite/mastodon#12021: Convert LDAP username to valid format
if Devise.ldap_uid_conversion_enabled?
keys = Regexp.union(Devise.ldap_uid_conversion_search.to_sym.chars)
replacement = Devise.ldap_uid_conversion_replace.to_sym

safe_username = safe_username.gsub(keys, replacement)
end

resource = joins(:account).find_by(accounts: { username: safe_username })

if resource.blank?
resource = new(email: attributes[:mail].first, agreement: true, account_attributes: { username: attributes[Devise.ldap_uid.to_sym].first }, admin: false, external: true, confirmed_at: Time.now.utc)
resource = new(email: attributes[:mail].first, agreement: true, account_attributes: { username: safe_username }, admin: false, external: true, confirmed_at: Time.now.utc)
resource.save!
end

Expand Down
11 changes: 11 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ module Devise
@@ldap_tls_no_verify = false
mattr_accessor :ldap_search_filter
@@ldap_search_filter = nil
# tootsuite/mastodon#12021: Convert LDAP username to valid format
mattr_accessor :ldap_uid_conversion_enabled
@@ldap_uid_conversion_enabled = false
mattr_accessor :ldap_uid_conversion_search
@@ldap_uid_conversion_search = nil
mattr_accessor :ldap_uid_conversion_replace
@@ldap_uid_conversion_replace = nil

class Strategies::PamAuthenticatable
def valid?
Expand Down Expand Up @@ -365,5 +372,9 @@ def valid?
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}')
# tootsuite/mastodon#12021: Convert LDAP username to valid format
config.ldap_uid_conversion_enabled = ENV['LDAP_UID_CONVERSION_ENABLED'] == 'true'
config.ldap_uid_conversion_search = ENV.fetch('LDAP_UID_CONVERSION_SEARCH', '.,- ')
config.ldap_uid_conversion_replace = ENV.fetch('LDAP_UID_CONVERSION_REPLACE', '_')
end
end

0 comments on commit f2c4f8e

Please sign in to comment.