58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import { load } from "@std/dotenv";
|
|
import { Client } from "ldapts";
|
|
import { log } from "./log.ts";
|
|
|
|
const dirname = new URL(".", import.meta.url).pathname;
|
|
await load({ envPath: `${dirname}/.env`, export: true });
|
|
|
|
const config = {
|
|
url: Deno.env.get("LDAP_URL"),
|
|
bindDN: Deno.env.get("LDAP_BIND_DN"),
|
|
password: Deno.env.get("LDAP_PASSWORD"),
|
|
searchDN: Deno.env.get("LDAP_SEARCH_DN"),
|
|
};
|
|
|
|
const client = new Client({ url: config.url });
|
|
|
|
export async function lookupPubkeyByUsername(username: string) {
|
|
let pubkey;
|
|
|
|
try {
|
|
await client.bind(config.bindDN, config.password);
|
|
|
|
const { searchEntries } = await client.search(config.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(config.bindDN, config.password);
|
|
|
|
const { searchEntries } = await client.search(config.searchDN, {
|
|
filter: `(nostrKey=${pubkey})`,
|
|
attributes: ["cn"],
|
|
});
|
|
|
|
username = searchEntries[0]?.cn;
|
|
} catch (ex) {
|
|
log(ex, "red");
|
|
} finally {
|
|
await client.unbind();
|
|
}
|
|
|
|
return username;
|
|
}
|