diff --git a/handlers/nprofile.ts b/handlers/nprofile.ts index 36e4adb..33703c2 100644 --- a/handlers/nprofile.ts +++ b/handlers/nprofile.ts @@ -1,6 +1,7 @@ import { Context } from "@oak/oak"; import { nip19 } from "@nostr/tools"; import { log } from "../log.ts"; +import { lookupUsernameByPubkey } from "../ldap.ts"; import { fetchProfileEvent } from "../nostr.ts"; import { profilePageHtml } from "../html.ts" @@ -10,12 +11,10 @@ const nprofileHandler = async function (ctx: Context) { try { const r = nip19.decode(nprofile); - const profileEvent = await fetchProfileEvent(r.data.pubkey); + const username = await lookupUsernameByPubkey(r.data.pubkey); - if (profileEvent) { - const html = profilePageHtml(profileEvent); - - ctx.response.body = html; + if (username) { + ctx.response.redirect(`/@${username}`); } else { ctx.response.status = 404; ctx.response.body = "Not Found"; diff --git a/handlers/npub.ts b/handlers/npub.ts index d47a9b0..19dcd22 100644 --- a/handlers/npub.ts +++ b/handlers/npub.ts @@ -1,6 +1,7 @@ import { Context } from "@oak/oak"; import { nip19 } from "@nostr/tools"; import { log } from "../log.ts"; +import { lookupUsernameByPubkey } from "../ldap.ts"; import { fetchProfileEvent } from "../nostr.ts"; import { profilePageHtml } from "../html.ts" @@ -10,12 +11,10 @@ const npubHandler = async function (ctx: Context) { try { const r = nip19.decode(npub); - const profileEvent = await fetchProfileEvent(r.data); + const username = await lookupUsernameByPubkey(r.data); - if (profileEvent) { - const html = profilePageHtml(profileEvent); - - ctx.response.body = html; + if (username) { + ctx.response.redirect(`/@${username}`); } else { ctx.response.status = 404; ctx.response.body = "Not Found"; diff --git a/handlers/username.ts b/handlers/username.ts index ddccee8..33c7332 100644 --- a/handlers/username.ts +++ b/handlers/username.ts @@ -10,6 +10,12 @@ const usernameHandler = async function (ctx: Context) { const username = ctx.params.path.replace(/^@/, '');; const pubkey = await lookupPubkeyByUsername(username); + if (!pubkey) { + ctx.response.status = 404; + ctx.response.body = "Not Found"; + return; + } + try { const profileEvent = await fetchProfileEvent(pubkey); diff --git a/ldap.ts b/ldap.ts index e390316..6131339 100644 --- a/ldap.ts +++ b/ldap.ts @@ -14,7 +14,7 @@ const config = { const client = new Client({ url: config.url }); -export async function lookupPubkeyByUsername (username) { +export async function lookupPubkeyByUsername (username: string) { let pubkey; try { @@ -33,3 +33,23 @@ export async function lookupPubkeyByUsername (username) { 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; + } +}