Last active
October 15, 2018 12:08
-
-
Save geordi/2a0ba8609442618972cd17ed20e3242f to your computer and use it in GitHub Desktop.
LDAP VSB-TUO Python example
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
''' | |
LDAP example for VSB-TUO | |
You need python-ldap package: pip install python-ldap (tested on version 3.1.0) | |
''' | |
import getpass | |
import ldap | |
if __name__ == '__main__': | |
ldap.set_option(ldap.OPT_REFERRALS,0) | |
ldap.protocol_version = 3 | |
ldap_server='ldaps://ldap.vsb.cz' | |
username = input('username: ') | |
password= getpass.getpass('password: ') | |
# VSB specific user context | |
trailing_context = username[-1] | |
# the following is the user_dn format provided by the ldap server | |
user_dn = 'cn=' + username | |
# adjust this to your base dn for searching | |
base_dn = 'ou=USERS,o=VSB' | |
connect = ldap.initialize(ldap_server) | |
search_filter = 'cn=' + username | |
listing = connect.search_s(base_dn, ldap.SCOPE_SUBTREE, user_dn, []) | |
if len(listing) == 0: | |
print('User not found') | |
else: | |
print(listing[0][0]) | |
#connect.set_option(ldap.OPT_REFERRALS, 0) | |
try: | |
#if authentication successful, get the full user data | |
connect.simple_bind_s('cn={},ou={},ou=USERS,o=VSB'.format(username, trailing_context), password) | |
result = connect.search_s(base_dn, ldap.SCOPE_SUBTREE, search_filter) | |
# return all user data results | |
connect.unbind_s() | |
print(result) | |
except ldap.LDAPError as e: | |
print(e) | |
connect.unbind_s() | |
print('authentication error') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment