38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
import ldap
|
|
|
|
class LdapQuery():
|
|
def __init__(self, servers, port, root):
|
|
self.servers = servers
|
|
self.root = "OU=Students,OU=People," + root
|
|
|
|
ldap.PORT = port
|
|
|
|
# cert path
|
|
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, "./ca_labs.wmi.amu.edu.pl.pem")
|
|
|
|
for i in range(len(self.servers)):
|
|
try:
|
|
self.ldap_handler = ldap.initialize(self.servers[i])
|
|
break
|
|
except:
|
|
print ("Server down")
|
|
|
|
self.ldap_handler.set_option(ldap.OPT_X_TLS_DEMAND, True)
|
|
self.ldap_handler.start_tls_s()
|
|
|
|
def search_user(self, login):
|
|
return self.ldap_handler.search_s(
|
|
self.root,
|
|
ldap.SCOPE_SUBTREE,
|
|
'cn=' + login,
|
|
['givenname', 'sn', 'mail'])
|
|
|
|
def authenticate(self, login=None, passwd=None):
|
|
try:
|
|
test=self.ldap_handler.simple_bind_s(login, passwd)
|
|
return True
|
|
except ldap.INVALID_CREDENTIALS:
|
|
print ("Invalid credentials")
|
|
return False
|
|
|