59 lines
1.2 KiB
TypeScript
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;
|
|
}
|
|
|
|
await client.unbind();
|
|
} catch (e) {
|
|
await client.unbind();
|
|
throw e;
|
|
}
|
|
|
|
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) {
|
|
await client.unbind();
|
|
throw e;
|
|
}
|
|
|
|
return username;
|
|
}
|