Skip to content

Instantly share code, notes, and snippets.

@marcus-crane
Last active May 1, 2019 00:07
Show Gist options
  • Save marcus-crane/3e96f5e683a5e27112a1388fb2ca5d3a to your computer and use it in GitHub Desktop.
Save marcus-crane/3e96f5e683a5e27112a1388fb2ca5d3a to your computer and use it in GitHub Desktop.
An LDAP wrapper for talking to Active Directory
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