More Jason changes.
[akkoma] / lib / pleroma / web / web_finger / web_finger.ex
1 defmodule Pleroma.Web.WebFinger do
2 @httpoison Application.get_env(:pleroma, :httpoison)
3
4 alias Pleroma.{Repo, User, XmlBuilder}
5 alias Pleroma.Web
6 alias Pleroma.Web.{XML, Salmon, OStatus}
7 require Jason
8 require Logger
9
10 def host_meta do
11 base_url = Web.base_url
12 {
13 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
14 {
15 :Link, %{rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}"}
16 }
17 }
18 |> XmlBuilder.to_doc
19 end
20
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")}
27 else _e ->
28 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
29 {:ok, represent_user(user, "JSON")}
30 else _e ->
31 {:error, "Couldn't find user"}
32 end
33 end
34 end
35
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")}
42 else _e ->
43 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
44 {:ok, represent_user(user, "XML")}
45 else _e ->
46 {:error, "Couldn't find user"}
47 end
48 end
49 end
50
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)
55 %{
56 "subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}",
57 "aliases" => [user.ap_id],
58 "links" => [
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()}
65 ]
66 }
67 end
68
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)
73 {
74 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
75 [
76 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}"},
77 {:Alias, user.ap_id},
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()}}
84 ]
85 }
86 |> XmlBuilder.to_doc
87 end
88
89 # This seems a better fit in Salmon
90 def ensure_keys_present(user) do
91 info = user.info || %{}
92 if info["keys"] do
93 {:ok, user}
94 else
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()
99 end
100 end
101
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)
110 data = %{
111 "magic_key" => magic_key,
112 "topic" => topic,
113 "subject" => subject,
114 "salmon" => salmon,
115 "subscribe_address" => subscribe_address,
116 "ap_id" => ap_id
117 }
118 {:ok, data}
119 end
120
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"])
131 {_, "salmon"} ->
132 Map.put(data, "salmon", link["href"])
133 {_, "http://ostatus.org/schema/1.0/subscribe"} ->
134 Map.put(data, "subscribe_address", link["template"])
135 _ ->
136 Logger.debug("Unhandled type: #{inspect(link["type"])}")
137 data
138 end
139 end)
140 {:ok, data}
141 end
142
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
147 {:ok, template}
148 end
149 end
150
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)
154 else
155 _ ->
156 with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
157 get_template_from_xml(body)
158 else
159 e -> {:error, "Can't find LRDD template: #{inspect(e)}"}
160 end
161 end
162 end
163
164 def finger(account) do
165 account = String.trim_leading(account, "@")
166 domain = with [_name, domain] <- String.split(account, "@") do
167 domain
168 else _e ->
169 URI.parse(account).host
170 end
171
172 case find_lrdd_template(domain) do
173 {:ok, template} ->
174 address = String.replace(template, "{uri}", URI.encode(account))
175 _ ->
176 address = "http://#{domain}/.well-known/webfinger?resource=acct:#{account}"
177 end
178
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)
182 if doc != :error do
183 webfinger_from_xml(doc)
184 else
185 {:ok, doc} = Jason.decode(body)
186 webfinger_from_json(doc)
187 end
188 else
189 e ->
190 Logger.debug(fn -> "Couldn't finger #{account}" end)
191 Logger.debug(fn -> inspect(e) end)
192 {:error, e}
193 end
194 end
195 end