2018-12-13 22:46:37 +01:00
|
|
|
import glob
|
2019-01-14 16:08:58 +01:00
|
|
|
import logging
|
2018-12-13 22:46:37 +01:00
|
|
|
import os
|
2019-01-16 14:09:30 +01:00
|
|
|
from typing import Any, Dict, List, Optional
|
2018-12-13 22:46:37 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
2018-12-30 01:33:11 +01:00
|
|
|
# See https://jackstromberg.com/2013/01/useraccountcontrol-attributeflag-values/
|
|
|
|
# for docs on what these values mean.
|
|
|
|
LDAP_USER_ACCOUNT_CONTROL_NORMAL = '512'
|
|
|
|
LDAP_USER_ACCOUNT_CONTROL_DISABLED = '514'
|
2018-12-13 23:58:26 +01:00
|
|
|
|
2018-12-13 22:46:37 +01:00
|
|
|
def generate_dev_ldap_dir(mode: str, num_users: int=8) -> Dict[str, Dict[str, Any]]:
|
|
|
|
mode = mode.lower()
|
|
|
|
names = []
|
|
|
|
for i in range(1, num_users+1):
|
|
|
|
names.append(('LDAP User %d' % (i,), 'ldapuser%d@zulip.com' % (i,)))
|
|
|
|
|
|
|
|
profile_images = [open(path, "rb").read() for path in
|
|
|
|
glob.glob(os.path.join(settings.STATIC_ROOT, "images/team/*"))]
|
|
|
|
ldap_dir = {}
|
|
|
|
for i, name in enumerate(names):
|
|
|
|
if mode == 'a':
|
|
|
|
email = name[1].lower()
|
|
|
|
email_username = email.split('@')[0]
|
|
|
|
ldap_dir['uid=' + email + ',ou=users,dc=zulip,dc=com'] = {
|
|
|
|
'cn': [name[0], ],
|
2019-01-16 08:36:27 +01:00
|
|
|
'userPassword': [email_username, ],
|
2018-12-13 23:58:26 +01:00
|
|
|
'thumbnailPhoto': [profile_images[i % len(profile_images)], ],
|
|
|
|
'userAccountControl': [LDAP_USER_ACCOUNT_CONTROL_NORMAL, ],
|
2018-12-13 22:46:37 +01:00
|
|
|
}
|
|
|
|
elif mode == 'b':
|
|
|
|
email = name[1].lower()
|
|
|
|
email_username = email.split('@')[0]
|
|
|
|
ldap_dir['uid=' + email_username + ',ou=users,dc=zulip,dc=com'] = {
|
|
|
|
'cn': [name[0], ],
|
2019-01-16 08:36:27 +01:00
|
|
|
'userPassword': [email_username, ],
|
2018-12-13 23:58:26 +01:00
|
|
|
'jpegPhoto': [profile_images[i % len(profile_images)], ],
|
2018-12-13 22:46:37 +01:00
|
|
|
}
|
|
|
|
elif mode == 'c':
|
|
|
|
email = name[1].lower()
|
|
|
|
email_username = email.split('@')[0]
|
|
|
|
ldap_dir['uid=' + email_username + ',ou=users,dc=zulip,dc=com'] = {
|
|
|
|
'cn': [name[0], ],
|
2019-01-29 14:14:24 +01:00
|
|
|
'userPassword': [email_username, ],
|
2018-12-13 22:46:37 +01:00
|
|
|
'email': email,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ldap_dir
|
2019-01-12 18:12:11 +01:00
|
|
|
|
2019-01-16 14:09:30 +01:00
|
|
|
def init_fakeldap(directory: Optional[Dict[str, Dict[str, List[str]]]]=None) -> None:
|
2019-01-12 18:12:11 +01:00
|
|
|
# We only use this in development. Importing mock inside
|
|
|
|
# this function is an import time optimization, which
|
|
|
|
# avoids the expensive import of the mock module (slow
|
|
|
|
# because its dependency pbr uses pkgresources, which is
|
|
|
|
# really slow to import.)
|
|
|
|
import mock
|
|
|
|
from fakeldap import MockLDAP
|
|
|
|
|
2019-01-14 16:08:58 +01:00
|
|
|
# Silent `django_auth_ldap` logger in dev mode to avoid
|
|
|
|
# spammy user not found log messages.
|
|
|
|
ldap_auth_logger = logging.getLogger('django_auth_ldap')
|
|
|
|
ldap_auth_logger.setLevel(logging.CRITICAL)
|
|
|
|
|
2019-01-14 18:52:25 +01:00
|
|
|
fakeldap_logger = logging.getLogger('fakeldap')
|
|
|
|
fakeldap_logger.setLevel(logging.CRITICAL)
|
|
|
|
|
2019-01-12 18:12:11 +01:00
|
|
|
ldap_patcher = mock.patch('django_auth_ldap.config.ldap.initialize')
|
|
|
|
mock_initialize = ldap_patcher.start()
|
|
|
|
mock_ldap = MockLDAP()
|
|
|
|
mock_initialize.return_value = mock_ldap
|
|
|
|
|
2019-01-16 14:09:30 +01:00
|
|
|
mock_ldap.directory = directory or generate_dev_ldap_dir(settings.FAKE_LDAP_MODE,
|
|
|
|
settings.FAKE_LDAP_NUM_USERS)
|