Last active
May 1, 2019 00:07
-
-
Save marcus-crane/3e96f5e683a5e27112a1388fb2ca5d3a to your computer and use it in GitHub Desktop.
An LDAP wrapper for talking to Active Directory
This file contains hidden or 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
from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, NTLM, SYNC | |
class LDAPClient: | |
def __init__(self, username, password, base, url): | |
self.username = username | |
self.password = password | |
self.base = base | |
self.url = url | |
self.connection = None | |
def get_server(self): | |
return Server(self.url, get_info=ALL) | |
def get_connection(self, server=None, client_strategy=SYNC, | |
auto_bind=True, authentication=NTLM): | |
if server is None: | |
server = self.get_server() | |
self.connection = Connection(server, user=self.username, | |
password=self.password, | |
authentication=authentication, | |
client_strategy=client_strategy, | |
auto_bind=auto_bind, | |
read_only=True) | |
def paged_search(self, filter): | |
if self.connection is None: | |
self.get_connection() | |
conn = self.connection | |
return conn.extend.standard.paged_search(self.base, | |
filter, | |
attributes=ALL_ATTRIBUTES, | |
generator=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment