Advertisement
Guest User

Untitled

a guest
Apr 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. from ldap3 import Server, Connection, ALL_ATTRIBUTES, MOCK_SYNC
  2.  
  3. REAL_SERVER = 'ldap.jumpcloud.com'
  4. BASE_DN = 'ou=Users,o=5ca4e308c67f9237e6aa027b,dc=jumpcloud,dc=com'
  5. BIND_USER = 'LDAPSearch'
  6. BIND_PASSWORD = 'LDAPSearch'
  7.  
  8. server = Server(REAL_SERVER)
  9.  
  10. connection = Connection(server, 'uid=%s,%s' % (BIND_USER, BASE_DN), BIND_PASSWORD, auto_bind=True)
  11.  
  12. def get_groups(USER, PASS):
  13. groups = []
  14.  
  15. auth_connection = Connection(server, 'uid=%s,%s' % (USER, BASE_DN), PASS, auto_bind=True)
  16.  
  17. if connection.search(BASE_DN, '(|(&(objectClass=*)(member=uid=%s,%s)))' % (USER, BASE_DN), attributes=ALL_ATTRIBUTES):
  18. groups = [entry.cn for entry in connection.entries]
  19.  
  20. auth_connection.unbind()
  21. return groups
  22.  
  23. def isAdmin(USER, PASS):
  24. for group in get_groups(USER, PASS):
  25. if group == "Administrators":
  26. return True
  27. return False
  28.  
  29. def isCoworker(USER, PASS):
  30. for group in get_groups(USER, PASS):
  31. if group == "Coworkers":
  32. return True
  33. return False
  34.  
  35. def isStudent(USER, PASS):
  36. for group in get_groups(USER, PASS):
  37. if group == "Students":
  38. return True
  39. return False
  40.  
  41. print(isStudent('student1', 'student1')) # geeft true
  42. print(isAdmin('student1', 'student1')) # geeft false
  43. try:
  44. print(isAdmin('student1', 'wrongpass')) # raises LDAPBindError (fout password)
  45. except:
  46. print('ERROR: Could not log in')
  47. print(isCoworker('administrator1', 'administrator1') | isAdmin('administrator1', 'administrator1')) # geeft true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement