Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ldap3 import Server, Connection, ALL_ATTRIBUTES, MOCK_SYNC
- REAL_SERVER = 'ldap.jumpcloud.com'
- BASE_DN = 'ou=Users,o=5ca4e308c67f9237e6aa027b,dc=jumpcloud,dc=com'
- BIND_USER = 'LDAPSearch'
- BIND_PASSWORD = 'LDAPSearch'
- server = Server(REAL_SERVER)
- connection = Connection(server, 'uid=%s,%s' % (BIND_USER, BASE_DN), BIND_PASSWORD, auto_bind=True)
- def get_groups(USER, PASS):
- groups = []
- auth_connection = Connection(server, 'uid=%s,%s' % (USER, BASE_DN), PASS, auto_bind=True)
- if connection.search(BASE_DN, '(|(&(objectClass=*)(member=uid=%s,%s)))' % (USER, BASE_DN), attributes=ALL_ATTRIBUTES):
- groups = [entry.cn for entry in connection.entries]
- auth_connection.unbind()
- return groups
- def isAdmin(USER, PASS):
- for group in get_groups(USER, PASS):
- if group == "Administrators":
- return True
- return False
- def isCoworker(USER, PASS):
- for group in get_groups(USER, PASS):
- if group == "Coworkers":
- return True
- return False
- def isStudent(USER, PASS):
- for group in get_groups(USER, PASS):
- if group == "Students":
- return True
- return False
- print(isStudent('student1', 'student1')) # geeft true
- print(isAdmin('student1', 'student1')) # geeft false
- try:
- print(isAdmin('student1', 'wrongpass')) # raises LDAPBindError (fout password)
- except:
- print('ERROR: Could not log in')
- print(isCoworker('administrator1', 'administrator1') | isAdmin('administrator1', 'administrator1')) # geeft true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement