substr/ldap.ts

53 lines
1.0 KiB
TypeScript

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