Add basic webfinger.
authorRoger Braun <roger@rogerbraun.net>
Mon, 17 Apr 2017 11:44:41 +0000 (13:44 +0200)
committerRoger Braun <roger@rogerbraun.net>
Mon, 17 Apr 2017 11:44:41 +0000 (13:44 +0200)
config/config.exs
lib/pleroma/web/router.ex
lib/pleroma/web/web.ex
lib/pleroma/web/web_finger/web_finger.ex [new file with mode: 0644]
lib/pleroma/web/web_finger/web_finger_controller.ex [new file with mode: 0644]
lib/xml_builder.ex [new file with mode: 0644]
test/web/web_finger/web_finger_test.exs [new file with mode: 0644]
test/xml_builder_test.exs [new file with mode: 0644]

index 2b041b10f3a7539032e05e4c036db5a77ec681e4..18a2490a468d2b06fcbf2ed17fe541ca2831d58d 100644 (file)
@@ -26,6 +26,10 @@ config :logger, :console,
   format: "$time $metadata[$level] $message\n",
   metadata: [:request_id]
 
+config :mime, :types, %{
+  "application/xrd+xml" => ["xrd+xml"]
+}
+
 # Import environment specific config. This must remain at the bottom
 # of this file so it overrides the configuration defined above.
 import_config "#{Mix.env}.exs"
index 0446f622b6ce346f568b954b6a272c2649a110bc..99d1f69c269273bb581fef248e395536ed56b9fa 100644 (file)
@@ -19,6 +19,10 @@ defmodule Pleroma.Web.Router do
     plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Pleroma.Web.Router.user_fetcher/1}
   end
 
+  pipeline :well_known do
+    plug :accepts, ["xml", "xrd+xml"]
+  end
+
   scope "/api", Pleroma.Web do
     pipe_through :api
 
@@ -49,4 +53,11 @@ defmodule Pleroma.Web.Router do
     post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet
     post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar
   end
+
+  scope "/.well-known", Pleroma.Web do
+    pipe_through :well_known
+
+    get "/host-meta", WebFinger.WebFingerController, :host_meta
+    get "/webfinger", WebFinger.WebFingerController, :webfinger
+  end
 end
index d03db22313df1bf78d6bd3f0ac81b39d60081dbd..a81e3e6e19bab963922d31accb53629d3b082ec4 100644 (file)
@@ -61,12 +61,17 @@ defmodule Pleroma.Web do
     apply(__MODULE__, which, [])
   end
 
+  def host do
+    settings = Application.get_env(:pleroma, Pleroma.Web.Endpoint)
+    settings
+    |> Keyword.fetch!(:url)
+    |> Keyword.fetch!(:host)
+  end
+
   def base_url do
     settings = Application.get_env(:pleroma, Pleroma.Web.Endpoint)
-    host =
-      settings
-      |> Keyword.fetch!(:url)
-      |> Keyword.fetch!(:host)
+
+    host = host()
 
     protocol = settings |> Keyword.fetch!(:protocol)
 
diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex
new file mode 100644 (file)
index 0000000..258ff76
--- /dev/null
@@ -0,0 +1,38 @@
+defmodule Pleroma.Web.WebFinger do
+  alias Pleroma.XmlBuilder
+  alias Pleroma.User
+
+  def host_meta() do
+    base_url  = Pleroma.Web.base_url
+    {
+      :XRD, %{ xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0" },
+      {
+        :Link, %{ rel: "lrdd", type: "application/xrd+xml", template: "#{base_url}/.well-known/webfinger?resource={uri}"  }
+      }
+    }
+    |> XmlBuilder.to_doc
+  end
+
+  def webfinger(resource) do
+    host = Pleroma.Web.host
+    regex = ~r/acct:(?<username>\w+)@#{host}/
+    case Regex.named_captures(regex, resource) do
+      %{"username" => username} ->
+        user = User.get_cached_by_nickname(username)
+        {:ok, represent_user(user)}
+      _ -> nil
+    end
+  end
+
+  def represent_user(user) do
+    {
+      :XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
+      [
+        {:Subject, "acct:#{user.nickname}@#{Pleroma.Web.host}"},
+        {:Alias, user.ap_id},
+        {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: "#{user.ap_id}.atom"}}
+      ]
+    }
+    |> XmlBuilder.to_doc
+  end
+end
diff --git a/lib/pleroma/web/web_finger/web_finger_controller.ex b/lib/pleroma/web/web_finger/web_finger_controller.ex
new file mode 100644 (file)
index 0000000..7c0fd31
--- /dev/null
@@ -0,0 +1,21 @@
+defmodule Pleroma.Web.WebFinger.WebFingerController do
+  use Pleroma.Web, :controller
+
+  alias Pleroma.Web.WebFinger
+
+  def host_meta(conn, _params) do
+    xml = WebFinger.host_meta
+
+    conn
+    |> put_resp_content_type("application/xrd+xml")
+    |> send_resp(200, xml)
+  end
+
+  def webfinger(conn, %{"resource" => resource}) do
+    {:ok, response} = Pleroma.Web.WebFinger.webfinger(resource)
+
+    conn
+    |> put_resp_content_type("application/xrd+xml")
+    |> send_resp(200, response)
+  end
+end
diff --git a/lib/xml_builder.ex b/lib/xml_builder.ex
new file mode 100644 (file)
index 0000000..ac1ac8a
--- /dev/null
@@ -0,0 +1,42 @@
+defmodule Pleroma.XmlBuilder do
+  def to_xml({tag, attributes, content}) do
+    open_tag = make_open_tag(tag, attributes)
+
+    content_xml = to_xml(content)
+
+    "<#{open_tag}>#{content_xml}</#{tag}>"
+  end
+
+  def to_xml({tag, %{} = attributes}) do
+    open_tag = make_open_tag(tag, attributes)
+
+    "<#{open_tag} />"
+  end
+
+  def to_xml({tag, content}), do: to_xml({tag, %{}, content})
+
+  def to_xml(content) when is_binary(content) do
+    to_string(content)
+  end
+
+  def to_xml(content) when is_list(content) do
+    for element <- content do
+      to_xml(element)
+    end
+    |> Enum.join
+  end
+
+  def to_xml(%NaiveDateTime{} = time) do
+    NaiveDateTime.to_iso8601(time)
+  end
+
+  def to_doc(content), do: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" <> to_xml(content)
+
+  defp make_open_tag(tag, attributes) do
+    attributes_string = for {attribute, value} <- attributes do
+      "#{attribute}=\"#{value}\""
+    end |> Enum.join(" ")
+
+    Enum.join([tag, attributes_string], " ") |> String.strip
+  end
+end
diff --git a/test/web/web_finger/web_finger_test.exs b/test/web/web_finger/web_finger_test.exs
new file mode 100644 (file)
index 0000000..8a3007f
--- /dev/null
@@ -0,0 +1,11 @@
+defmodule Pleroma.Web.WebFingerTest do
+  use Pleroma.DataCase
+
+  describe "host meta" do
+    test "returns a link to the xml lrdd" do
+      host_info = Pleroma.Web.WebFinger.host_meta
+
+      assert String.contains?(host_info, Pleroma.Web.base_url)
+    end
+  end
+end
diff --git a/test/xml_builder_test.exs b/test/xml_builder_test.exs
new file mode 100644 (file)
index 0000000..f502a0f
--- /dev/null
@@ -0,0 +1,59 @@
+defmodule Pleroma.XmlBuilderTest do
+  use Pleroma.DataCase
+  alias Pleroma.XmlBuilder
+
+  test "Build a basic xml string from a tuple" do
+    data = { :feed, %{ xmlns: "http://www.w3.org/2005/Atom"}, "Some content" }
+
+    expected_xml = "<feed xmlns=\"http://www.w3.org/2005/Atom\">Some content</feed>"
+
+    assert XmlBuilder.to_xml(data) == expected_xml
+  end
+
+  test "returns a complete document" do
+    data = { :feed, %{ xmlns: "http://www.w3.org/2005/Atom"}, "Some content" }
+
+    expected_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><feed xmlns=\"http://www.w3.org/2005/Atom\">Some content</feed>"
+
+    assert XmlBuilder.to_doc(data) == expected_xml
+  end
+
+  test "Works without attributes" do
+    data = {
+      :feed,
+      "Some content"
+    }
+
+    expected_xml = "<feed>Some content</feed>"
+
+    assert XmlBuilder.to_xml(data) == expected_xml
+  end
+
+  test "It works with nested tuples" do
+    data = {
+      :feed,
+      [
+        {:guy, "brush"},
+        {:lament, %{ configuration: "puzzle" }, "pinhead" }
+      ]
+    }
+
+    expected_xml = ~s[<feed><guy>brush</guy><lament configuration="puzzle">pinhead</lament></feed>]
+
+    assert XmlBuilder.to_xml(data) == expected_xml
+  end
+
+  test "Represents NaiveDateTime as iso8601" do
+    assert XmlBuilder.to_xml(~N[2000-01-01 13:13:33]) == "2000-01-01T13:13:33"
+  end
+
+  test "Uses self-closing tags when no content is giving" do
+    data = {
+      :link,
+      %{ rel: "self" }
+    }
+
+    expected_xml = ~s[<link rel="self" />]
+    assert XmlBuilder.to_xml(data) == expected_xml
+  end
+end