-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove dependance between ldapmanager and config
- Loading branch information
1 parent
a276bfb
commit 7f36d89
Showing
3 changed files
with
46 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,68 @@ | ||
import logging | ||
|
||
try: | ||
import ldap3 | ||
except ModuleNotFoundError: | ||
ldap3 = None | ||
|
||
from ..config import get_current_config | ||
import ldap3 | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LdapManager: | ||
@staticmethod | ||
def try_auth(user, password): | ||
config = get_current_config().LDAP | ||
entrie = LdapManager.search_user(user, config["admin_filter"]) | ||
if entrie: | ||
logger.debug("{0} is admin".format(user)) | ||
admin = True | ||
else: | ||
entrie = LdapManager.search_user(user, config["user_filter"]) | ||
|
||
def __init__(self, ldap_server, base_dn, user_filter, admin_filter, bind_dn, bind_password, username_attr, email_attr): | ||
self.ldap_server=ldap_server | ||
self.base_dn=base_dn | ||
self.user_filter=user_filter | ||
self.admin_filter=admin_filter | ||
self.bind_dn=bind_dn | ||
self.bind_password=bind_password | ||
self.username_attr=username_attr | ||
self.email_attr=email_attr | ||
if not self.ldap_server: | ||
raise ValueError("No LDAP configured") | ||
self.server = ldap3.Server(self.ldap_server, get_info="ALL") | ||
|
||
def try_auth(self,user, password): | ||
admin= False | ||
if self.admin_filter: | ||
entrie = self.search_user(user, self.admin_filter) | ||
if entrie: | ||
admin = False | ||
else: | ||
logger.debug("{0} is admin".format(user)) | ||
admin = True | ||
if not admin: | ||
entrie = self.search_user(user, self.user_filter) | ||
if not entrie: | ||
return False | ||
server = ldap3.Server(config["ldap_server"], get_info="ALL") | ||
try: | ||
with ldap3.Connection( | ||
server, entrie.entry_dn, password, read_only=True | ||
self.server, entrie.entry_dn, password, read_only=True | ||
) as conn: | ||
return { | ||
"uid": entrie[config["username_attr"]], | ||
"mail": entrie[config["email_attr"]], | ||
"uid": entrie[self.username_attr], | ||
"mail": entrie[self.email_attr], | ||
"admin": admin, | ||
} | ||
except ldap3.core.exceptions.LDAPBindError: | ||
logger.warning("wrong password for user {0}".format(user)) | ||
return False | ||
|
||
@staticmethod | ||
def search_user(user, filter): | ||
if not ldap3: | ||
logger.warning("module 'ldap2' is not installed") | ||
return False | ||
config = get_current_config().LDAP | ||
if not config["ldap_server"]: | ||
logger.info("No LDAP configured") | ||
return False | ||
server = ldap3.Server(config["ldap_server"], get_info="ALL") | ||
def search_user(self,user, filter): | ||
|
||
try: | ||
with ldap3.Connection( | ||
server, config["bind_dn"], config["bind_password"], read_only=True | ||
self.server, self.bind_dn, self.bind_password, read_only=True | ||
) as conn: | ||
conn.search( | ||
config["base_dn"], | ||
self.base_dn, | ||
filter, | ||
attributes=[config["email_attr"], config["username_attr"]], | ||
attributes=[self.email_attr, self.username_attr], | ||
) | ||
entries = conn.entries | ||
except ldap3.core.exceptions.LDAPBindError: | ||
logger.warning("wrong can't bind LDAP with {-1}".format(config["bind_dn"])) | ||
logger.warning( | ||
"wrong can't bind LDAP with {0}".format(self.bind_dn)) | ||
|
||
for entrie in entries: | ||
if entrie[config["username_attr"]] == user: | ||
if entrie[self.username_attr] == user: | ||
return entrie | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters