(self)
| 32 | return LDAPConnection.__instance__ |
| 33 | |
| 34 | def __init__(self): |
| 35 | # Cache some settings |
| 36 | self.__LDAP_FILTER = ldap_setting("FILTER", "") |
| 37 | self.__LDAP_SEARCH_BASE = ldap_setting("SEARCH_BASE", "") |
| 38 | |
| 39 | # The time a cache entry is valid (in hours) |
| 40 | try: |
| 41 | self.__LDAP_CACHE_TTL = int(ldap_setting("CACHE_TTL", "")) |
| 42 | except ValueError: |
| 43 | logging.error("Invalid value for cache_ttl. Defaulting to 1 hour") |
| 44 | self.__LDAP_CACHE_TTL = 1 |
| 45 | |
| 46 | password = ldap_setting("BIND_PW", "") |
| 47 | if not password: |
| 48 | pw_file = ldap_setting("BIND_PW_FILE", "") |
| 49 | if pw_file: |
| 50 | with open(pw_file, "r") as f: |
| 51 | password = f.read().replace("\n", "") |
| 52 | |
| 53 | self.__ldap_connection = ldap.initialize(ldap_setting("SERVER", "")) |
| 54 | try: |
| 55 | self.__ldap_connection.simple_bind_s(ldap_setting("BIND_DN", ""), password) |
| 56 | except ldap.LDAPError as err: |
| 57 | logging.error(f"LDAP Error occurring during bind: {err.desc}") |
| 58 | |
| 59 | def __is_cache_valid(self, username): |
| 60 | """Returns True if the cache entry is still valid. Returns False otherwise.""" |
nothing calls this directly
no test coverage detected