Guest User

Untitled

a guest
Apr 29th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import ldap
  2.  
  3. def check_credentials(username, password):
  4. """Verifies credentials for username and password.
  5. Returns None on success or a string describing the error on failure
  6. # Adapt to your needs
  7. """
  8. LDAP_SERVER = 'ldap://xxx'
  9. # fully qualified AD user name
  10. LDAP_USERNAME = '%s@xxx.xx' % username
  11. # your password
  12. LDAP_PASSWORD = password
  13. base_dn = 'DC=xxx,DC=xxx'
  14. ldap_filter = 'userPrincipalName=%s@xxx.xx' % username
  15. attrs = ['memberOf']
  16. try:
  17. # build a client
  18. ldap_client = ldap.initialize(LDAP_SERVER)
  19. # perform a synchronous bind
  20. ldap_client.set_option(ldap.OPT_REFERRALS,0)
  21. ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
  22. except ldap.INVALID_CREDENTIALS:
  23. ldap_client.unbind()
  24. return 'Wrong username ili password'
  25. except ldap.SERVER_DOWN:
  26. return 'AD server not awailable'
  27. # all is well
  28. # get all user groups and store it in cerrypy session for future use
  29. cherrypy.session[username] = str(ldap_client.search_s(base_dn,
  30. ldap.SCOPE_SUBTREE, ldap_filter, attrs)[0][1]['memberOf'])
  31. ldap_client.unbind()
  32. return None
Add Comment
Please, Sign In to add comment