import { Client } from "ldapts"; import config from "./config.ts"; const { ldap, ldapEnabled } = config; let client: Client; if (ldapEnabled) { client = new Client({ url: ldap.url as string }); } export async function lookupPubkeyByUsername(username: string) { let pubkey; try { await client.bind(ldap.bindDN as string, ldap.password as string); const { searchEntries } = await client.search(ldap.searchDN as string, { filter: `(cn=${username})`, attributes: ["nostrKey"], }); pubkey = searchEntries[0]?.nostrKey as string; } catch (e) { console.error(e); } finally { await client.unbind(); } return pubkey; } export async function lookupUsernameByPubkey(pubkey: string) { let username; try { await client.bind(ldap.bindDN as string, ldap.password as string); const { searchEntries } = await client.search(ldap.searchDN as string, { filter: `(nostrKey=${pubkey})`, attributes: ["cn"], }); username = searchEntries[0]?.cn; } catch (e) { console.error(e); } finally { await client.unbind(); } return username; }