Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDAP dynamic Bind DN #3226

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions redash/authentication/ldap_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

try:
from ldap3 import Server, Connection, SIMPLE, ANONYMOUS, NTLM
from ldap3.core.exceptions import LDAPBindError
except ImportError:
if settings.LDAP_LOGIN_ENABLED:
logger.error("The ldap3 library was not found. This is required to use LDAP authentication (see requirements.txt).")
Expand Down Expand Up @@ -60,9 +61,19 @@ def login(org_slug=None):

def auth_ldap_user(username, password):
server = Server(settings.LDAP_HOST_URL, use_ssl=settings.LDAP_SSL)
conn = Connection(server, settings.LDAP_BIND_DN, password=settings.LDAP_BIND_DN_PASSWORD, authentication=settings.LDAP_AUTH_METHOD, auto_bind=True)
bind_dn = settings.LDAP_BIND_DN
bind_dn_password = settings.LDAP_BIND_DN_PASSWORD
if settings.LDAP_BIND_DN_DYNAMIC:
bind_dn = bind_dn % {"username": username}
bind_dn_password = password

try:
conn = Connection(server, bind_dn, password=bind_dn_password, authentication=settings.LDAP_AUTH_METHOD, auto_bind=True)
except LDAPBindError:
return None

conn.search(settings.LDAP_SEARCH_DN, settings.LDAP_SEARCH_TEMPLATE % {"username": username}, attributes=[settings.LDAP_DISPLAY_NAME_KEY, settings.LDAP_EMAIL_KEY])
conn.search(settings.LDAP_SEARCH_DN, settings.LDAP_SEARCH_TEMPLATE % {"username": username},
attributes=[settings.LDAP_DISPLAY_NAME_KEY, settings.LDAP_EMAIL_KEY])

if len(conn.entries) == 0:
return None
Expand Down
2 changes: 2 additions & 0 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def all_settings():
# For AD this should be "org\\user".
LDAP_BIND_DN = os.environ.get('REDASH_LDAP_BIND_DN', None)
LDAP_BIND_DN_PASSWORD = os.environ.get('REDASH_LDAP_BIND_DN_PASSWORD', '')
# Allows dynamic Bind DN with entered in form username and password
LDAP_BIND_DN_DYNAMIC = parse_boolean(os.environ.get('REDASH_LDAP_BIND_DN_DYNAMIC', 'false'))
# AD/LDAP email and display name keys
LDAP_DISPLAY_NAME_KEY = os.environ.get('REDASH_LDAP_DISPLAY_NAME_KEY', 'displayName')
LDAP_EMAIL_KEY = os.environ.get('REDASH_LDAP_EMAIL_KEY', "mail")
Expand Down