Compare commits
3 Commits
7dc733630b
...
feat/ldap
Author | SHA1 | Date | |
---|---|---|---|
0b2cb22744 | |||
440927b2c1 | |||
167533a65f |
@ -172,6 +172,8 @@ if any(os.path.isfile(x) for x in config_locations):
|
||||
LDAP_FILTER = ldap.get("filter", "")
|
||||
LDAP_BIND_DN = ldap.get("bind_dn", "")
|
||||
LDAP_BIND_PW = ldap.get("bind_pw", "")
|
||||
LDAP_BIND_PW_FILE = ldap.get("bind_pw_file", "")
|
||||
LDAP_CACHE_TTL = ldap.get("cache_ttl", "")
|
||||
|
||||
# Configure EteBase to use LDAP
|
||||
ETEBASE_CREATE_USER_FUNC = "myauth.ldap.create_user"
|
||||
|
@ -36,9 +36,25 @@ class LDAPConnection:
|
||||
self.__LDAP_FILTER = ldap_setting("FILTER", "")
|
||||
self.__LDAP_SEARCH_BASE = ldap_setting("SEARCH_BASE", "")
|
||||
|
||||
# The time a cache entry is valid (in hours)
|
||||
try:
|
||||
self.__LDAP_CACHE_TTL = int(ldap_setting("CACHE_TTL", ""))
|
||||
except ValueError:
|
||||
logging.error("Invalid value for cache_ttl. Defaulting to 1 hour")
|
||||
self.__LDAP_CACHE_TTL = 1
|
||||
|
||||
password = ldap_setting("BIND_PW", "")
|
||||
if not password:
|
||||
pw_file = ldap_setting("BIND_PW_FILE", "")
|
||||
if pw_file:
|
||||
with open(pw_file, "r") as f:
|
||||
password = f.read().replace("\n", "")
|
||||
else:
|
||||
logging.error("No bind password specified")
|
||||
|
||||
self.__ldap_connection = ldap.initialize(ldap_setting("SERVER", ""))
|
||||
try:
|
||||
self.__ldap_connection.simple_bind_s(ldap_setting("BIND_DN", ""), ldap_setting("BIND_PW", ""))
|
||||
self.__ldap_connection.simple_bind_s(ldap_setting("BIND_DN", ""), password)
|
||||
except ldap.LDAPError as err:
|
||||
logging.error(f"LDAP Error occuring during bind: {err.desc}")
|
||||
|
||||
@ -75,7 +91,7 @@ class LDAPConnection:
|
||||
return False
|
||||
|
||||
if len(result) == 1:
|
||||
self.__user_cache[username] = timezone.now() + timezone.timedelta(hours=1)
|
||||
self.__user_cache[username] = timezone.now() + timezone.timedelta(hours=self.__LDAP_CACHE_TTL)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user