1 defmodule Pleroma.Web.WebFinger do
2 alias Pleroma.XmlBuilder
4 alias Pleroma.Web.OStatus
9 base_url = Pleroma.Web.base_url
11 :XRD, %{ xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0" },
13 :Link, %{ rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}" }
19 def webfinger(resource) do
20 host = Pleroma.Web.host
21 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
22 case Regex.named_captures(regex, resource) do
23 %{"username" => username} ->
24 user = User.get_cached_by_nickname(username)
25 {:ok, represent_user(user)}
30 def represent_user(user) do
32 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
34 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.host}"},
36 {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}},
37 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}}
43 # FIXME: Make this call the host-meta to find the actual address.
44 defp webfinger_address(domain) do
45 "//#{domain}/.well-known/webfinger"
48 defp webfinger_from_xml(doc) do
49 magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
50 "data:application/magic-public-key," <> magic_key = magic_key
51 topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc)
52 subject = XML.string_from_xpath("//Subject", doc)
53 salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
63 def finger(account, getter \\ &HTTPoison.get/3) do
64 domain = with [_name, domain] <- String.split(account, "@") do
67 URI.parse(account).host
69 address = webfinger_address(domain)
72 response = with {:ok, result} <- getter.("https:" <> address, ["Accept": "application/xrd+xml"], [params: [resource: account]]) do
75 getter.("http:" <> address, ["Accept": "application/xrd+xml"], [params: [resource: account]])
78 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response,
79 doc <- XML.parse_document(body),
80 {:ok, data} <- webfinger_from_xml(doc) do
84 Logger.debug("Couldn't finger #{account}.")
85 Logger.debug(inspect(e))