Pyhton ldap3 NTLM unable to return json.loads data

26 Views Asked by At

Trying to add an AD user account to a AD group using python with ldap3 using the following script:

# Import necessary modules and libraries
import requests
from flask import json
from ldap3 import Server, Connection, ALL_ATTRIBUTES, SUBTREE, NTLM
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups

# Test API data
testuser = r"TS\testuser"

# Define LDAP server details
Server_ip = '192.168.2.3'

# Define bind user credentials
#BIND_Username = 'CN=Automation,CN=Users,DC=testnetwerk,DC=com'
BIND_Username = 'TESTNETWERK\\Automation'
BIND_Password = 'Welkom123!'

# Define LDAP paths
Base_DN = "dc=testnetwerk,dc=com"
Filter = "(sAMAccountName={0}*)"  # LDAP filter to search for users based on sAMAccountName
Group_DN = "CN=testgroup,CN=Users,DC=testnetwerk,DC=com"  # DN of the group to which users will be added

# Function to create an LDAP Server object
def server_ldap():
    return Server(Server_ip)


# Function to establish connection to LDAP server
def connect_ldap():
    server = server_ldap()
#    return Connection(server, user=BIND_Username, password=BIND_Password, auto_bind=True)
    return Connection(server, user=BIND_Username, password=BIND_Password, authentication=NTLM)

# Function to search for a user in LDAP directory based on sAMAccountName
def find_user(username):
    with connect_ldap() as c:
        print("Connected to LDAP server")
        # Perform LDAP search operation
        c.search(search_base=Base_DN, search_filter=Filter.format(username[3:]), search_scope=SUBTREE,
                 attributes=ALL_ATTRIBUTES, get_operational_attributes=True)
    # Return search results in JSON format
        print(json.loads(c.response_to_json()))
    return json.loads(c.response_to_json())


# Function to add the found user to the specified LDAP group
def add_user_to_group(username):
    # Retrieve the DN (Distinguished Name) of the user from search results
    user = find_user(username)["entries"][0]["dn"]
    print(user)
    # Add user to the specified group
    ad_add_members_to_groups(connect_ldap(), user, Group_DN)
    # Return confirmation message
    return "Added " + user + " to the group!"



print(find_user(testuser))
try:
    # Attempt to add test user to the group and print confirmation
    print(add_user_to_group(testuser))
except Exception as e:
    # Print error message if an exception occurs
    print("ai ai ai")
    print(e)


However printing out the value that should be returned using print(json.loads(c.response_to_json())) it responds, when returning it it does not and gives me the following error: TypeError: the JSON object must be str, bytes or bytearray, not NoneType

Uncommenting #BIND_Username = 'CN=Automation,CN=Users,DC=testnetwerk,DC=com' and # return Connection(server, user=BIND_Username, password=BIND_Password, auto_bind=True)

and commenting the other it works.

Response from the print when the return does not work:

{'entries': [{'attributes': {'accountExpires': '9999-12-31 23:59:59.999999+00:00', 'badPasswordTime': '1601-01-01 00:00:00+00:00', 'badPwdCount': 0, 'cn': 'Test User', 'codePage': 0, 'countryCode': 0, 'dSCorePropagationData': ['1601-01-01 00:00:00+00:00'], 'displayName': 'Test User', 'distinguishedName': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com', 'givenName': 'Test', 'instanceType': 4, 'lastLogoff': '1601-01-01 00:00:00+00:00', 'lastLogon': '1601-01-01 00:00:00+00:00', 'logonCount': 0, 'name': 'Test User', 'objectCategory': 'CN=Person,CN=Schema,CN=Configuration,DC=testnetwerk,DC=com', 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], 'objectGUID': '{bdfd2aa0-2fcf-46df-9417-2396360fb83f}', 'objectSid': 'S-1-5-21-813124840-2969614714-1384511549-1106', 'primaryGroupID': 513, 'pwdLastSet': '2024-03-29 12:50:26.547474+00:00', 'sAMAccountName': 'testuser', 'sAMAccountType': 805306368, 'sn': 'User', 'uSNChanged': 12835, 'uSNCreated': 12830, 'userAccountControl': 66048, 'userPrincipalName': '[email protected]', 'whenChanged': '2024-03-29 12:50:26+00:00', 'whenCreated': '2024-03-29 12:50:26+00:00'}, 'dn': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com'}]}

Response from the print when it does work:

{'entries': [{'attributes': {'accountExpires': '9999-12-31 23:59:59.999999+00:00', 'badPasswordTime': '1601-01-01 00:00:00+00:00', 'badPwdCount': 0, 'cn': 'Test User', 'codePage': 0, 'countryCode': 0, 'dSCorePropagationData': ['1601-01-01 00:00:00+00:00'], 'displayName': 'Test User', 'distinguishedName': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com', 'givenName': 'Test', 'instanceType': 4, 'lastLogoff': '1601-01-01 00:00:00+00:00', 'lastLogon': '1601-01-01 00:00:00+00:00', 'logonCount': 0, 'name': 'Test User', 'objectCategory': 'CN=Person,CN=Schema,CN=Configuration,DC=testnetwerk,DC=com', 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], 'objectGUID': '{bdfd2aa0-2fcf-46df-9417-2396360fb83f}', 'objectSid': 'S-1-5-21-813124840-2969614714-1384511549-1106', 'primaryGroupID': 513, 'pwdLastSet': '2024-03-29 12:50:26.547474+00:00', 'sAMAccountName': 'testuser', 'sAMAccountType': 805306368, 'sn': 'User', 'uSNChanged': 12835, 'uSNCreated': 12830, 'userAccountControl': 66048, 'userPrincipalName': '[email protected]', 'whenChanged': '2024-03-29 12:50:26+00:00', 'whenCreated': '2024-03-29 12:50:26+00:00'}, 'dn': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com'}]}

and the returned response when calling print(find_user(testuser))

{'entries': [{'attributes': {'accountExpires': '9999-12-31 23:59:59.999999+00:00', 'badPasswordTime': '1601-01-01 00:00:00+00:00', 'badPwdCount': 0, 'cn': 'Test User', 'codePage': 0, 'countryCode': 0, 'dSCorePropagationData': ['1601-01-01 00:00:00+00:00'], 'displayName': 'Test User', 'distinguishedName': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com', 'givenName': 'Test', 'instanceType': 4, 'lastLogoff': '1601-01-01 00:00:00+00:00', 'lastLogon': '1601-01-01 00:00:00+00:00', 'logonCount': 0, 'name': 'Test User', 'objectCategory': 'CN=Person,CN=Schema,CN=Configuration,DC=testnetwerk,DC=com', 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], 'objectGUID': '{bdfd2aa0-2fcf-46df-9417-2396360fb83f}', 'objectSid': 'S-1-5-21-813124840-2969614714-1384511549-1106', 'primaryGroupID': 513, 'pwdLastSet': '2024-03-29 12:50:26.547474+00:00', 'sAMAccountName': 'testuser', 'sAMAccountType': 805306368, 'sn': 'User', 'uSNChanged': 12835, 'uSNCreated': 12830, 'userAccountControl': 66048, 'userPrincipalName': '[email protected]', 'whenChanged': '2024-03-29 12:50:26+00:00', 'whenCreated': '2024-03-29 12:50:26+00:00'}, 'dn': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com'}]}

Any ideas?

0

There are 0 best solutions below