Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
This repository was archived by the owner on May 14, 2024. It is now read-only.

Sharing unescape DN code for helper method #968

@ansibleguy76

Description

@ansibleguy76

While implementing ldap authentication against AD with ldapjs, I have noticed the following :

When searching for a user with non-ascii characters, I noticed AD returns the DN encoded.

example :
Search for user "kürt"
returned :
CN=K\c3\bcrt, ... (\c3\bc => hex for ü)

I then need to send the DN again to ldap to authenticate with it. However AD required the non-decoded DN.
CN=Kürt ...

function unescapeLdapResult(ldapResult) {
  // Regular expression to match the escaped sequences
  const regex = /\\([0-9a-fA-F]{2})\\([0-9a-fA-F]{2})/g;

  // Replace each escaped sequence with its Unicode character
  return ldapResult.replace(regex, (match, p1, p2) => {
      // Convert the hex codes to a Buffer
      const bytes = Buffer.from([parseInt(p1, 16), parseInt(p2, 16)]);
      // Convert the Buffer to a UTF-8 String
      return bytes.toString('utf8');
  });
}

I have created this function to acquire this, it would nice to have it as a helper method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions