Merge branch 'feauture/webfinger' into 'develop'
[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 Logger
8
9 def host_meta do
10 base_url = Web.base_url
11 {
12 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
13 {
14 :Link, %{rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}"}
15 }
16 }
17 |> XmlBuilder.to_doc
18 end
19
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)}
26 else _e ->
27 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
28 {:ok, represent_user(user)}
29 else _e ->
30 {:error, "Couldn't find user"}
31 end
32 end
33 end
34
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)
39 {
40 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
41 [
42 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}"},
43 {:Alias, user.ap_id},
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 ]
49 }
50 |> XmlBuilder.to_doc
51 end
52
53 # This seems a better fit in Salmon
54 def ensure_keys_present(user) do
55 info = user.info || %{}
56 if info["keys"] do
57 {:ok, user}
58 else
59 {:ok, pem} = Salmon.generate_rsa_pem
60 info = Map.put(info, "keys", pem)
61 Repo.update(Ecto.Changeset.change(user, info: info))
62 end
63 end
64
65 # FIXME: Make this call the host-meta to find the actual address.
66 defp webfinger_address(domain) do
67 "//#{domain}/.well-known/webfinger"
68 end
69
70 defp webfinger_from_xml(doc) do
71 magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
72 "data:application/magic-public-key," <> magic_key = magic_key
73 topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc)
74 subject = XML.string_from_xpath("//Subject", doc)
75 salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
76 data = %{
77 "magic_key" => magic_key,
78 "topic" => topic,
79 "subject" => subject,
80 "salmon" => salmon
81 }
82 {:ok, data}
83 end
84
85 def get_template_from_xml(body) do
86 xpath = "//Link[@rel='lrdd' and @type='application/xrd+xml']/@template"
87 with doc when doc != :error <- XML.parse_document(body),
88 template when template != nil <- XML.string_from_xpath(xpath, doc) do
89 {:ok, template}
90 end
91 end
92
93 def find_lrdd_template(domain) do
94 with {:ok, %{status_code: status_code, body: body}} <- @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do
95 get_template_from_xml(body)
96 else
97 e -> {:error, "Can't find lrdd template: #{inspect(e)}"}
98 end
99 end
100
101 def finger(account) do
102 domain = with [_name, domain] <- String.split(account, "@") do
103 domain
104 else _e ->
105 URI.parse(account).host
106 end
107
108 {:ok, template} = find_lrdd_template(domain)
109
110 address = String.replace(template, "{uri}", URI.encode(account))
111
112 response = @httpoison.get(address, ["Accept": "application/xrd+xml"])
113
114 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response,
115 doc when doc != :error<- XML.parse_document(body),
116 {:ok, data} <- webfinger_from_xml(doc) do
117 {:ok, data}
118 else
119 e ->
120 Logger.debug(fn -> "Couldn't finger #{account}." end)
121 Logger.debug(fn -> inspect(e) end)
122 {:error, e}
123 end
124 end
125 end