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, ): Promise { let pubkey: string | undefined; 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"], }); if ( searchEntries.length > 0 && typeof searchEntries[0].nostrKey === "string" ) { pubkey = searchEntries[0].nostrKey.toString(); } await client.unbind(); } catch (e) { await client.unbind(); throw e; } return pubkey; } export async function lookupUsernameByPubkey( pubkey: string, ): Promise { let username: string | undefined; 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"], }); if (searchEntries.length > 0) { username = searchEntries[0].cn.toString(); } await client.unbind(); } catch (e) { await client.unbind(); throw e; } return username; }