-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathldap_version_jihed.py
42 lines (32 loc) · 1.41 KB
/
ldap_version_jihed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import ldap
class LdapLogin():
def __init__(self, username="joe", password="joe"):
self.username = username
self.password = password
def login(self):
ldap_server = "ldap://192.168.2.115:389"
# the following is the user_dn format provided by the ldap server
user_dn = "cn="+self.username+",cn=users,dc=chatroom,dc=local"
base_dn = "cn=users,dc=chatroom,dc=local"
ldap_client = ldap.initialize(ldap_server)
search_filter = "cn="+self.username
try:
# if authentication successful, get the full user data
ldap_client.bind_s(user_dn, self.password)
result = ldap_client.search_s(
base_dn, ldap.SCOPE_SUBTREE, search_filter)
# return all user data results
ldap_client.unbind_s()
print(result)
except ldap.INVALID_CREDENTIALS:
ldap_client.unbind()
print("Wrong username or password..")
except ldap.SERVER_DOWN:
print("Server is down at the moment, please try again later!")
except ldap.LDAPError:
ldap_client.unbind_s()
print("authentication error!")
# TESTING CONNECTION, also IGNORE the ERRORS since there's NO ERROR, dunno why they are being thrown, the attributes DO EXIST in ldap module and I VERIFIED it!
# CONNECTION WILL ONLY WORK WHEN MY SERVER IS UP
log = LdapLogin()
log.login()