Created
December 15, 2022 22:02
-
-
Save blzzua/5152caa2c4ee9971e38a189120fb594e to your computer and use it in GitHub Desktop.
active directory ldap auth python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ldap | |
login, password = 'login', 'password' | |
ctx = {} | |
ctx['action'] = 'initializing LDAP connection' | |
ldap_obj = ldap.initialize('ldap://active.directory.local') | |
ldap_obj.protocol_version=ldap.VERSION3 | |
ldap_obj.set_option(ldap.OPT_REFERRALS, 0) # ???? | |
ldap_obj.bind_s('ldap_bind_user', 'ldap_bind_password', ldap.AUTH_SIMPLE) | |
searchfilter = '(sAMAccountName=%(username)s)' % {'username': login} | |
results = ldap_obj.search_s('ou=SubtreeName,dc=domain,dc=local', ldap.SCOPE_SUBTREE, searchfilter, ['objectclass'], 1) | |
nres = len(results) | |
if nres < 1: | |
print('login not found') | |
else: | |
try: | |
user_entry = results[0] | |
ldap_dn = user_entry[0] | |
ldap_obj.bind_s(who=ldap_dn, cred=password, method=ldap.AUTH_SIMPLE) | |
print("SUCCESS", ldap_obj) | |
except ldap.INVALID_CREDENTIALS: | |
print("INVALID_CREDENTIALS") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment