Merge branch 'develop' into dtluna/pleroma-refactor/1
[akkoma] / lib / pleroma / web / web_finger / web_finger.ex
1 defmodule Pleroma.Web.WebFinger do
2
3 alias Pleroma.{Repo, User, XmlBuilder}
4 alias Pleroma.Web
5 alias Pleroma.Web.{XML, Salmon, OStatus}
6 require Logger
7
8 def host_meta do
9 base_url = Web.base_url
10 {
11 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
12 {
13 :Link, %{rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}"}
14 }
15 }
16 |> XmlBuilder.to_doc
17 end
18
19 def webfinger(resource) do
20 host = Pleroma.Web.Endpoint.host
21 regex = ~r/(acct:)?(?<username>\w+)@#{host}/
22 with %{"username" => username} <- Regex.named_captures(regex, resource) do
23 user = User.get_by_nickname(username)
24 {:ok, represent_user(user)}
25 else _e ->
26 with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
27 {:ok, represent_user(user)}
28 else _e ->
29 {:error, "Couldn't find user"}
30 end
31 end
32 end
33
34 def represent_user(user) do
35 {:ok, user} = ensure_keys_present(user)
36 {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
37 magic_key = Salmon.encode_key(public)
38 {
39 :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
40 [
41 {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}"},
42 {:Alias, user.ap_id},
43 {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}},
44 {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
45 {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
46 {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}
47 ]
48 }
49 |> XmlBuilder.to_doc
50 end
51
52 # This seems a better fit in Salmon
53 def ensure_keys_present(user) do
54 info = user.info || %{}
55 if info["keys"] do
56 {:ok, user}
57 else
58 {:ok, pem} = Salmon.generate_rsa_pem
59 info = Map.put(info, "keys", pem)
60 Repo.update(Ecto.Changeset.change(user, info: info))
61 end
62 end
63
64 # FIXME: Make this call the host-meta to find the actual address.
65 defp webfinger_address(domain) do
66 "//#{domain}/.well-known/webfinger"
67 end
68
69 defp webfinger_from_xml(doc) do
70 magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
71 "data:application/magic-public-key," <> magic_key = magic_key
72 topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc)
73 subject = XML.string_from_xpath("//Subject", doc)
74 salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
75 data = %{
76 "magic_key" => magic_key,
77 "topic" => topic,
78 "subject" => subject,
79 "salmon" => salmon
80 }
81 {:ok, data}
82 end
83
84 def finger(account, getter \\ &HTTPoison.get/3) do
85 domain = with [_name, domain] <- String.split(account, "@") do
86 domain
87 else _e ->
88 URI.parse(account).host
89 end
90 address = webfinger_address(domain)
91
92 # try https first
93 response = with {:ok, result} <- getter.("https:" <> address, ["Accept": "application/xrd+xml"], [params: [resource: account]]) do
94 {:ok, result}
95 else _ ->
96 getter.("http:" <> address, ["Accept": "application/xrd+xml"], [params: [resource: account], follow_redirect: true])
97 end
98
99 with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response,
100 doc <- XML.parse_document(body),
101 {:ok, data} <- webfinger_from_xml(doc) do
102 {:ok, data}
103 else
104 e ->
105 Logger.debug("Couldn't finger #{account}.")
106 Logger.debug(inspect(e))
107 {:error, e}
108 end
109 end
110 end