Skip to content

Commit 8a578b6

Browse files
Patrick SandifordPatrick Sandiford
Patrick Sandiford
authored and
Patrick Sandiford
committed
Added LDAP integration
1 parent b513f99 commit 8a578b6

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

project/settings.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
'level': LOGGING_LEVEL,
5555
'propagate': True,
5656
},
57+
'django_python3_ldap': {
58+
'handlers': ['console'],
59+
'level': LOGGING_LEVEL,
60+
'propagate': True,
61+
},
5762
},
5863
}
5964

@@ -67,6 +72,7 @@
6772
'django.contrib.messages',
6873
'django.contrib.staticfiles',
6974
'debug_toolbar',
75+
'django_python3_ldap',
7076
'jquery',
7177
'bootstrap3',
7278
'swift_browser',
@@ -170,3 +176,85 @@
170176
INTERNAL_IPS = ['127.0.0.1']
171177

172178
LOGIN_REDIRECT_URL = 'containers'
179+
180+
AUTHENTICATION_BACKENDS = [
181+
'django_python3_ldap.auth.LDAPBackend',
182+
'django.contrib.auth.backends.ModelBackend',
183+
]
184+
185+
# The URL of the LDAP server.
186+
LDAP_AUTH_URL = os.environ.get('LDAP_AUTH_URL',
187+
'ldap://localhost:389')
188+
189+
# Initiate TLS on connection.
190+
LDAP_AUTH_USE_TLS = os.environ.get('LDAP_AUTH_USE_TLS',
191+
False)
192+
193+
# The LDAP search base for looking up users.
194+
LDAP_AUTH_SEARCH_BASE = os.environ.get('LDAP_AUTH_SEARCH_BASE',
195+
'ou=people,dc=example,dc=com')
196+
197+
# The LDAP class that represents a user.
198+
LDAP_AUTH_OBJECT_CLASS = os.environ.get('LDAP_AUTH_OBJECT_CLASS',
199+
'inetOrgPerson')
200+
201+
# User model fields mapped to the LDAP
202+
# attributes that represent them.
203+
LDAP_AUTH_USER_FIELDS = {
204+
"username": os.environ.get('LDAP_AUTH_USERNAME_ATTR', 'uid'),
205+
"first_name": os.environ.get('LDAP_AUTH_FIRSTNAME_ATTR', 'givenName'),
206+
"last_name": os.environ.get('LDAP_AUTH_LASTNAME_ATTR', 'sn'),
207+
"email": os.environ.get('LDAP_AUTH_EMAIL_ATTR', 'mail'),
208+
}
209+
210+
# A tuple of django model fields used to uniquely identify a user.
211+
LDAP_AUTH_USER_LOOKUP_FIELDS = os.environ.get('LDAP_AUTH_USER_LOOKUP_FIELDS',
212+
("username",))
213+
214+
# Path to a callable that takes a dict of {model_field_name: value},
215+
# returning a dict of clean model data.
216+
# Use this to customize how data loaded from LDAP is saved to the User model.
217+
LDAP_AUTH_CLEAN_USER_DATA = os.environ.get('LDAP_AUTH_CLEAN_USER_DATA',
218+
"django_python3_ldap.utils.clean_user_data")
219+
220+
# Path to a callable that takes a user model and a dict of {ldap_field_name: [value]},
221+
# and saves any additional user relationships based on the LDAP data.
222+
# Use this to customize how data loaded from LDAP is saved to User model relations.
223+
# For customizing non-related User model fields, use LDAP_AUTH_CLEAN_USER_DATA.
224+
LDAP_AUTH_SYNC_USER_RELATIONS = os.environ.get('LDAP_AUTH_SYNC_USER_RELATIONS',
225+
"django_python3_ldap.utils.sync_user_relations")
226+
227+
# Path to a callable that takes a dict of {ldap_field_name: value},
228+
# returning a list of [ldap_search_filter]. The search filters will then be AND'd
229+
# together when creating the final search filter.
230+
LDAP_AUTH_FORMAT_SEARCH_FILTERS = os.environ.get('LDAP_AUTH_FORMAT_SEARCH_FILTERS',
231+
"django_python3_ldap.utils.format_search_filters")
232+
233+
# Path to a callable that takes a dict of {model_field_name: value}, and returns
234+
# a string of the username to bind to the LDAP server.
235+
# Use this to support different types of LDAP server.
236+
LDAP_AUTH_FORMAT_USERNAME = os.environ.get('LDAP_AUTH_FORMAT_USERNAME',
237+
'django_python3_ldap.utils.format_username_openldap')
238+
239+
# Sets the login domain for Active Directory users.
240+
LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = os.environ.get('LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN',
241+
None)
242+
243+
# The LDAP username and password of a user for querying the LDAP database for user
244+
# details. If None, then the authenticated user will be used for querying, and
245+
# the `ldap_sync_users` command will perform an anonymous query.
246+
LDAP_AUTH_CONNECTION_USERNAME = os.environ.get('LDAP_AUTH_CONNECTION_USERNAME',
247+
None)
248+
LDAP_AUTH_CONNECTION_PASSWORD = os.environ.get('LDAP_AUTH_CONNECTION_PASSWORD',
249+
None)
250+
251+
# Set connection/receive timeouts (in seconds) on the underlying `ldap3` library.
252+
LDAP_AUTH_CONNECT_TIMEOUT = os.environ.get('LDAP_AUTH_CONNECT_TIMEOUT',
253+
None)
254+
LDAP_AUTH_RECEIVE_TIMEOUT = os.environ.get('LDAP_AUTH_RECEIVE_TIMEOUT',
255+
None)
256+
257+
# Users must be a member of this group to access
258+
LDAP_GROUP_MEMBER = os.environ.get('LDAP_GROUP_MEMBER',
259+
None)
260+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ whitenoise==3.3.1
66
python-swiftclient==3.4.0
77
django-bootstrap3==9.1.0
88
django-static-jquery3==3.2.1
9+
django-python3-ldap==0.11.1

0 commit comments

Comments
 (0)