1 defmodule Pleroma.Web.WebFinger do
2 @httpoison Application.get_env(:pleroma, :httpoison)
4 alias Pleroma.{Repo, User, XmlBuilder}
6 alias Pleroma.Web.{XML, Salmon, OStatus}
10 base_url = Web.base_url
12 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
14 :Link, %{rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}"}
20 def webfinger(resource) do
21 host = Pleroma.Web.Endpoint.host
22 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
23 with %{"username" => username} <- Regex.named_captures(regex, resource) do
24 user = User.get_by_nickname(username)
25 {:ok, represent_user(user)}
27 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
28 {:ok, represent_user(user)}
30 {:error, "Couldn't find user"}
35 def represent_user(user) do
36 {:ok, user} = ensure_keys_present(user)
37 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
38 magic_key = Salmon.encode_key(public)
40 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
42 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}"},
44 {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}},
45 {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
46 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
47 {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
48 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
49 {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
55 # This seems a better fit in Salmon
56 def ensure_keys_present(user) do
57 info = user.info || %{}
61 {:ok, pem} = Salmon.generate_rsa_pem
62 info = Map.put(info, "keys", pem)
63 Repo.update(Ecto.Changeset.change(user, info: info))
67 defp webfinger_from_xml(doc) do
68 magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
69 "data:application/magic-public-key," <> magic_key = magic_key
70 topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc)
71 subject = XML.string_from_xpath("//Subject", doc)
72 salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
73 subscribe_address = XML.string_from_xpath(~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}, doc)
74 ap_id = XML.string_from_xpath(~s{//Link[@rel="self" and @type="application/activity+json"]/@href}, doc)
76 "magic_key" => magic_key,
80 "subscribe_address" => subscribe_address,
86 def get_template_from_xml(body) do
87 xpath = "//Link[@rel='lrdd' and @type='application/xrd+xml']/@template"
88 with doc when doc != :error <- XML.parse_document(body),
89 template when template != nil <- XML.string_from_xpath(xpath, doc) do
94 def find_lrdd_template(domain) do
95 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
96 get_template_from_xml(body)
99 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
100 get_template_from_xml(body)
102 e -> {:error, "Can't find lrdd template: #{inspect(e)}"}
107 def finger(account) do
108 domain = with [_name, domain] <- String.split(account, "@") do
111 URI.parse(account).host
114 with {:ok, template} <- find_lrdd_template(domain),
115 address <- String.replace(template, "{uri}", URI.encode(account)),
116 response <- @httpoison.get(address, ["Accept": "application/xrd+xml"]),
117 {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response,
118 doc when doc != :error<- XML.parse_document(body),
119 {:ok, data} <- webfinger_from_xml(doc) do
123 Logger.debug(fn -> "Couldn't finger #{account}." end)
124 Logger.debug(fn -> inspect(e) end)