Skip to content

Commit

Permalink
feature: ldap dn handle utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
SeDemal committed Apr 30, 2024
1 parent 94675d5 commit 85bbe67
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/utils/auth/ldap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ const ldapSearch = async <
reject('error: ' + err.message);
});
res.on('searchEntry', (entry) => {
let userDn;
try {
//dn is the only attribute returned with special characters formatted in UTF-8 (Bad for any letters with an accent)
//Regex replaces any backslash followed by 2 hex characters with a percentage unless said backslash is preceded by another backslash.
//That can then be processed by decodeURIComponent which will turn back characters to normal.
userDn = decodeURIComponent(
entry.pojo.objectName.replace(/(?<!\\)\\([0-9a-fA-F]{2})/g, '%$1')
)
} catch { reject(new Error ('Cannot resolve distinguishedName for the user')) }
results.push(
entry.pojo.attributes.reduce<Record<string, string | string[]>>(
(obj, attr) => {
Expand All @@ -71,7 +80,10 @@ const ldapSearch = async <
: attr.values[0];
return obj;
},
{ dn: entry.pojo.objectName }
{
// Assume userDn since there's a reject if not set
dn: userDn!,
}
) as SearchResult<Attributes, ArrayAttributes>
);
});
Expand Down

0 comments on commit 85bbe67

Please sign in to comment.