substr/ldap.ts

59 lines
1.2 KiB
TypeScript

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"],
});
if (
searchEntries.length > 0 &&
typeof searchEntries[0].nostrKey === "string"
) {
pubkey = searchEntries[0].nostrKey;
}
} 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"],
});
if (searchEntries.length > 0) {
username = searchEntries[0].cn;
}
} catch (e) {
console.error(e);
} finally {
await client.unbind();
}
return username;
}