Add basic webfinger.

This commit is contained in:
Roger Braun 2017-04-17 13:44:41 +02:00
commit ce6cc84a4a
8 changed files with 195 additions and 4 deletions

View 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

View 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)

View file

@ -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

View file

@ -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

42
lib/xml_builder.ex Normal file
View file

@ -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