web_finger.ex 4.75 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Web.WebFinger do
lain's avatar
lain committed
2
  @httpoison Application.get_env(:pleroma, :httpoison)
3
4
5

  alias Pleroma.{Repo, User, XmlBuilder}
  alias Pleroma.Web
lain's avatar
lain committed
6
  alias Pleroma.Web.{XML, Salmon, OStatus}
lain's avatar
lain committed
7
  require Logger
lain's avatar
lain committed
8

9
10
  def host_meta do
    base_url  = Web.base_url
lain's avatar
lain committed
11
    {
12
      :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
lain's avatar
lain committed
13
      {
14
        :Link, %{rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}"}
lain's avatar
lain committed
15
16
17
18
19
20
      }
    }
    |> XmlBuilder.to_doc
  end

  def webfinger(resource) do
21
    host = Pleroma.Web.Endpoint.host
lain's avatar
lain committed
22
    regex = ~r/(acct:)?(?<username>\w+)@#{host}/
lain's avatar
lain committed
23
24
25
26
27
    with %{"username" => username} <- Regex.named_captures(regex, resource) do
      user = User.get_by_nickname(username)
      {:ok, represent_user(user)}
    else _e ->
      with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
lain's avatar
lain committed
28
        {:ok, represent_user(user)}
lain's avatar
lain committed
29
30
31
      else _e ->
        {:error, "Couldn't find user"}
      end
lain's avatar
lain committed
32
33
34
35
    end
  end

  def represent_user(user) do
lain's avatar
lain committed
36
37
38
    {:ok, user} = ensure_keys_present(user)
    {:ok, _private, public} = Salmon.keys_from_pem(user.info["keys"])
    magic_key = Salmon.encode_key(public)
lain's avatar
lain committed
39
40
41
    {
      :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
      [
42
        {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}"},
lain's avatar
lain committed
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)}},
lain's avatar
lain committed
45
        {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
lain's avatar
lain committed
46
        {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}},
lain's avatar
lain committed
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()}}
lain's avatar
lain committed
50
51
52
53
      ]
    }
    |> XmlBuilder.to_doc
  end
lain's avatar
lain committed
54

lain's avatar
lain committed
55
  # This seems a better fit in Salmon
lain's avatar
lain committed
56
57
58
59
60
61
62
  def ensure_keys_present(user) do
    info = user.info || %{}
    if info["keys"] do
      {:ok, user}
    else
      {:ok, pem} = Salmon.generate_rsa_pem
      info = Map.put(info, "keys", pem)
lain's avatar
lain committed
63
64
      Ecto.Changeset.change(user, info: info)
      |> User.update_and_set_cache()
lain's avatar
lain committed
65
66
67
    end
  end

lain's avatar
lain committed
68
69
70
71
72
73
  defp webfinger_from_xml(doc) do
    magic_key = XML.string_from_xpath(~s{//Link[@rel="magic-public-key"]/@href}, doc)
    "data:application/magic-public-key," <> magic_key = magic_key
    topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc)
    subject = XML.string_from_xpath("//Subject", doc)
    salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
eal's avatar
eal committed
74
    subscribe_address = XML.string_from_xpath(~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}, doc)
lain's avatar
lain committed
75
    ap_id = XML.string_from_xpath(~s{//Link[@rel="self" and @type="application/activity+json"]/@href}, doc)
lain's avatar
lain committed
76
    data = %{
77
78
79
      "magic_key" => magic_key,
      "topic" => topic,
      "subject" => subject,
eal's avatar
eal committed
80
      "salmon" => salmon,
lain's avatar
lain committed
81
82
      "subscribe_address" => subscribe_address,
      "ap_id" => ap_id
lain's avatar
lain committed
83
84
85
86
    }
    {:ok, data}
  end

lain's avatar
lain committed
87
88
89
90
91
92
93
94
95
  def get_template_from_xml(body) do
    xpath = "//Link[@rel='lrdd' and @type='application/xrd+xml']/@template"
    with doc when doc != :error <- XML.parse_document(body),
      template when template != nil <- XML.string_from_xpath(xpath, doc) do
      {:ok, template}
    end
  end

  def find_lrdd_template(domain) do
96
    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
lain's avatar
lain committed
97
98
      get_template_from_xml(body)
    else
Thog's avatar
Thog committed
99
      _ ->
Roger Braun's avatar
Roger Braun committed
100
101
102
103
104
        with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do
          get_template_from_xml(body)
        else
          e -> {:error, "Can't find lrdd template: #{inspect(e)}"}
        end
lain's avatar
lain committed
105
106
107
108
    end
  end

  def finger(account) do
lain's avatar
lain committed
109
    account = String.trim_leading(account, "@")
lain's avatar
lain committed
110
111
112
113
114
115
    domain = with [_name, domain] <- String.split(account, "@") do
               domain
             else _e ->
               URI.parse(account).host
             end

lain's avatar
lain committed
116
117
    with {:ok, template} <- find_lrdd_template(domain),
         address <- String.replace(template, "{uri}", URI.encode(account)),
118
         response <- @httpoison.get(address, ["Accept": "application/xrd+xml"]),
lain's avatar
lain committed
119
         {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response,
lain's avatar
lain committed
120
         doc when doc != :error<- XML.parse_document(body),
lain's avatar
lain committed
121
122
123
124
         {:ok, data} <- webfinger_from_xml(doc) do
      {:ok, data}
    else
      e ->
feld's avatar
feld committed
125
        Logger.debug(fn -> "Couldn't finger #{account}" end)
lain's avatar
lain committed
126
        Logger.debug(fn -> inspect(e) end)
lain's avatar
lain committed
127
128
129
        {:error, e}
    end
  end
lain's avatar
lain committed
130
end