substr/ldap.ts

36 lines
869 B
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) {
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;
}
}