From ce6cc84a4a9bfe1d47d00201ab31c241878f0ab9 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 17 Apr 2017 13:44:41 +0200 Subject: [PATCH] Add basic webfinger. --- config/config.exs | 4 ++ lib/pleroma/web/router.ex | 11 ++++ lib/pleroma/web/web.ex | 13 ++-- lib/pleroma/web/web_finger/web_finger.ex | 38 ++++++++++++ .../web/web_finger/web_finger_controller.ex | 21 +++++++ lib/xml_builder.ex | 42 +++++++++++++ test/web/web_finger/web_finger_test.exs | 11 ++++ test/xml_builder_test.exs | 59 +++++++++++++++++++ 8 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 lib/pleroma/web/web_finger/web_finger.ex create mode 100644 lib/pleroma/web/web_finger/web_finger_controller.ex create mode 100644 lib/xml_builder.ex create mode 100644 test/web/web_finger/web_finger_test.exs create mode 100644 test/xml_builder_test.exs diff --git a/config/config.exs b/config/config.exs index 2b041b10f..18a2490a4 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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" diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 0446f622b..99d1f69c2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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 diff --git a/lib/pleroma/web/web.ex b/lib/pleroma/web/web.ex index d03db2231..a81e3e6e1 100644 --- a/lib/pleroma/web/web.ex +++ b/lib/pleroma/web/web.ex @@ -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 index 000000000..258ff7671 --- /dev/null +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -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:(?\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 index 000000000..7c0fd3142 --- /dev/null +++ b/lib/pleroma/web/web_finger/web_finger_controller.ex @@ -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 index 000000000..ac1ac8a74 --- /dev/null +++ b/lib/xml_builder.ex @@ -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}" + 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: "" <> 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 index 000000000..8a3007ff9 --- /dev/null +++ b/test/web/web_finger/web_finger_test.exs @@ -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 index 000000000..f502a0f0e --- /dev/null +++ b/test/xml_builder_test.exs @@ -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 = "Some content" + + 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 = "Some content" + + assert XmlBuilder.to_doc(data) == expected_xml + end + + test "Works without attributes" do + data = { + :feed, + "Some content" + } + + expected_xml = "Some content" + + 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[brushpinhead] + + 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[] + assert XmlBuilder.to_xml(data) == expected_xml + end +end