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}
11 base_url = Web.base_url
13 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
15 :Link, %{rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}"}
21 def webfinger(resource, "JSON") do
22 host = Pleroma.Web.Endpoint.host
23 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
24 with %{"username" => username} <- Regex.named_captures(regex, resource) do
25 user = User.get_by_nickname(username)
26 {:ok, represent_user(user, "JSON")}
28 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
29 {:ok, represent_user(user, "JSON")}
31 {:error, "Couldn't find user"}
36 def webfinger(resource, "XML") do
37 host = Pleroma.Web.Endpoint.host
38 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
39 with %{"username" => username} <- Regex.named_captures(regex, resource) do
40 user = User.get_by_nickname(username)
41 {:ok, represent_user(user, "XML")}
43 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
44 {:ok, represent_user(user, "XML")}
46 {:error, "Couldn't find user"}
51 def represent_user(user, "JSON") do
52 {:ok, user} = ensure_keys_present(user)
53 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
54 magic_key = Salmon.encode_key(public)
56 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}",
57 "aliases" => [user.ap_id],
59 %{"rel" => "http://schemas.google.com/g/2010#updates-from", "type" => "application/atom+xml", "href" => OStatus.feed_path(user)},
60 %{"rel" => "http://webfinger.net/rel/profile-page", "type" => "text/html", "href" => user.ap_id},
61 %{"rel" => "salmon", "href" => OStatus.salmon_path(user)},
62 %{"rel" => "magic-public-key", "href" => "data:application/magic-public-key,#{magic_key}"},
63 %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},
64 %{"rel" => "http://ostatus.org/schema/1.0/subscribe", "template" => OStatus.remote_follow_path()}
69 def represent_user(user, "XML") do
70 {:ok, user} = ensure_keys_present(user)
71 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
72 magic_key = Salmon.encode_key(public)
74 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
76 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}"},
78 {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}},
79 {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
80 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
81 {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}},
82 {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}},
83 {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}}
89 # This seems a better fit in Salmon
90 def ensure_keys_present(user) do
91 info = user.info || %{}
95 {:ok, pem} = Salmon.generate_rsa_pem
96 info = Map.put(info, "keys", pem)
97 Ecto.Changeset.change(user, info: info)
98 |> User.update_and_set_cache()
102 defp webfinger_from_xml(doc) do
103 magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
104 "data:application/magic-public-key," <> magic_key = magic_key
105 topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc)
106 subject = XML.string_from_xpath("//Subject", doc)
107 salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
108 subscribe_address = XML.string_from_xpath(~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}, doc)
109 ap_id = XML.string_from_xpath(~s{//Link[@rel="self" and @type="application/activity+json"]/@href}, doc)
111 "magic_key" => magic_key,
113 "subject" => subject,
115 "subscribe_address" => subscribe_address,
121 defp webfinger_from_json(doc) do
122 data = Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn (link, data) ->
123 case {link["type"], link["rel"]} do
124 {"application/activity+json", "self"} ->
125 Map.put(data, "ap_id", link["href"])
126 {_, "magic-public-key"} ->
127 "data:application/magic-public-key," <> magic_key = link["href"]
128 Map.put(data, "magic_key", magic_key)
129 {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} ->
130 Map.put(data, "topic", link["href"])
132 Map.put(data, "salmon", link["href"])
133 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
134 Map.put(data, "subscribe_address", link["template"])
136 Logger.debug("Unhandled type: #{inspect(link["type"])}")
143 def get_template_from_xml(body) do
144 xpath = "//Link[@rel='lrdd' and @type='application/xrd+xml']/@template"
145 with doc when doc != :error <- XML.parse_document(body),
146 template when template != nil <- XML.string_from_xpath(xpath, doc) do
151 def find_lrdd_template(domain) do
152 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
153 get_template_from_xml(body)
156 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
157 get_template_from_xml(body)
159 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
164 def finger(account) do
165 account = String.trim_leading(account, "@")
166 domain = with [_name, domain] <- String.split(account, "@") do
169 URI.parse(account).host
172 case find_lrdd_template(domain) do
174 address = String.replace(template, "{uri}", URI.encode(account))
176 address = "http://#{domain}/.well-known/webfinger?resource=acct:#{account}"
179 with response <- @httpoison.get(address, ["Accept": "application/xrd+xml,application/jrd+json"], follow_redirect: true),
180 {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response do
181 doc = XML.parse_document(body)
183 webfinger_from_xml(doc)
185 {:ok, doc} = Jason.decode(body)
186 webfinger_from_json(doc)
190 Logger.debug(fn -> "Couldn't finger #{account}" end)
191 Logger.debug(fn -> inspect(e) end)