added web domain to use in webfinger

This commit is contained in:
Maksim Pechnikov 2020-02-03 21:31:59 +03:00
parent c27d1d65bf
commit 0ffad24e2d
5 changed files with 148 additions and 54 deletions

View File

@ -3,8 +3,9 @@
# We don't run a server during test. If one is required, # We don't run a server during test. If one is required,
# you can enable the server option below. # you can enable the server option below.
config :pleroma, Pleroma.Web.Endpoint, config :pleroma, Pleroma.Web.Endpoint,
web_endpoint: nil,
http: [port: 4001], http: [port: 4001],
url: [port: 4001], url: [host: "localhost", port: 4001],
server: true server: true
# Disable captha for tests # Disable captha for tests

View File

@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web
alias Pleroma.Web.ActivityPub.Relay alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
@ -267,7 +268,7 @@ def gather_webfinger_links(%User{} = user) do
}, },
%{ %{
"rel" => "http://ostatus.org/schema/1.0/subscribe", "rel" => "http://ostatus.org/schema/1.0/subscribe",
"template" => "#{Pleroma.Web.base_url()}/ostatus_subscribe?acct={uri}" "template" => Web.web_url(%{path: "/ostatus_subscribe", query: "acct={uri}"})
} }
] ]
end end

View File

@ -20,6 +20,8 @@ defmodule Pleroma.Web do
below. below.
""" """
alias Pleroma.Web.Endpoint
def controller do def controller do
quote do quote do
use Phoenix.Controller, namespace: Pleroma.Web use Phoenix.Controller, namespace: Pleroma.Web
@ -103,7 +105,26 @@ defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, []) apply(__MODULE__, which, [])
end end
def base_url do def base_url, do: Endpoint.url()
Pleroma.Web.Endpoint.url()
def web_url(map \\ %{}) do
Pleroma.Web.web_endpoint()
|> URI.parse()
|> Map.merge(map)
|> URI.to_string()
end
def web_endpoint do
Pleroma.Config.get([Endpoint, :web_endpoint], Endpoint.url())
end
def web_host do
Pleroma.Web.web_endpoint()
|> URI.parse()
|> Map.get(:host)
end
def domains do
Enum.uniq([Pleroma.Web.web_host(), Pleroma.Web.Endpoint.host()])
end end
end end

View File

@ -13,8 +13,6 @@ defmodule Pleroma.Web.WebFinger do
require Logger require Logger
def host_meta do def host_meta do
base_url = Web.base_url()
{ {
:XRD, :XRD,
%{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"}, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
@ -23,7 +21,7 @@ def host_meta do
%{ %{
rel: "lrdd", rel: "lrdd",
type: "application/xrd+xml", type: "application/xrd+xml",
template: "#{base_url}/.well-known/webfinger?resource={uri}" template: Web.web_url(%{path: "/.well-known/webfinger", query: "resource={uri}"})
} }
} }
} }
@ -31,8 +29,8 @@ def host_meta do
end end
def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
host = Pleroma.Web.Endpoint.host() hosts = Enum.join(Pleroma.Web.domains(), "|")
regex = ~r/(acct:)?(?<username>[a-z0-9A-Z_\.-]+)@#{host}/ regex = ~r/(acct:)?(?<username>[a-z0-9A-Z_\.-]+)@(#{hosts})/
with %{"username" => username} <- Regex.named_captures(regex, resource), with %{"username" => username} <- Regex.named_captures(regex, resource),
%User{} = user <- User.get_cached_by_nickname(username) do %User{} = user <- User.get_cached_by_nickname(username) do
@ -62,7 +60,7 @@ def represent_user(user, "JSON") do
{:ok, user} = User.ensure_keys_present(user) {:ok, user} = User.ensure_keys_present(user)
%{ %{
"subject" => "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}", "subject" => acct_uri(user),
"aliases" => [user.ap_id], "aliases" => [user.ap_id],
"links" => gather_links(user) "links" => gather_links(user)
} }
@ -72,20 +70,22 @@ def represent_user(user, "XML") do
{:ok, user} = User.ensure_keys_present(user) {:ok, user} = User.ensure_keys_present(user)
links = links =
gather_links(user) user
|> gather_links()
|> Enum.map(fn link -> {:Link, link} end) |> Enum.map(fn link -> {:Link, link} end)
{ {
:XRD, :XRD,
%{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"}, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
[ [{:Subject, acct_uri(user)}, {:Alias, user.ap_id}] ++ links
{:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host()}"},
{:Alias, user.ap_id}
] ++ links
} }
|> XmlBuilder.to_doc() |> XmlBuilder.to_doc()
end end
defp acct_uri(%User{nickname: nickname}) do
"acct:#{nickname}@#{Pleroma.Web.web_host()}"
end
defp get_magic_key("data:application/magic-public-key," <> magic_key) do defp get_magic_key("data:application/magic-public-key," <> magic_key) do
{:ok, magic_key} {:ok, magic_key}
end end

View File

@ -8,17 +8,34 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
import ExUnit.CaptureLog import ExUnit.CaptureLog
import Pleroma.Factory import Pleroma.Factory
import Tesla.Mock import Tesla.Mock
import SweetXml
setup do setup do
mock(fn env -> apply(HttpRequestMock, :request, [env]) end) mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
:ok :ok
end end
clear_config([Pleroma.Web.Endpoint, :web_endpoint])
clear_config_all([:instance, :federating]) do clear_config_all([:instance, :federating]) do
Pleroma.Config.put([:instance, :federating], true) Pleroma.Config.put([:instance, :federating], true)
end end
test "GET host-meta" do describe "GET /.well-known/host-meta" do
test "host-meta for set subdomain" do
Pleroma.Config.put([Pleroma.Web.Endpoint, :web_endpoint], "http://pleroma.localhost")
response =
build_conn()
|> get("/.well-known/host-meta")
assert response.status == 200
assert response.resp_body ==
~s(<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="http://pleroma.localhost/.well-known/webfinger?resource={uri}" type="application/xrd+xml" /></XRD>)
end
test "host-meta for domain" do
response = response =
build_conn() build_conn()
|> get("/.well-known/host-meta") |> get("/.well-known/host-meta")
@ -27,11 +44,13 @@ test "GET host-meta" do
assert response.resp_body == assert response.resp_body ==
~s(<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="#{ ~s(<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="#{
Pleroma.Web.base_url() Pleroma.Web.web_url()
}/.well-known/webfinger?resource={uri}" type="application/xrd+xml" /></XRD>) }/.well-known/webfinger?resource={uri}" type="application/xrd+xml" /></XRD>)
end end
end
test "Webfinger JRD" do describe "Webfinger JRD" do
test "main domain" do
user = insert(:user) user = insert(:user)
response = response =
@ -42,6 +61,39 @@ test "Webfinger JRD" do
assert json_response(response, 200)["subject"] == "acct:#{user.nickname}@localhost" assert json_response(response, 200)["subject"] == "acct:#{user.nickname}@localhost"
end end
test "for subdomain" do
Pleroma.Config.put([Pleroma.Web.Endpoint, :web_endpoint], "http://pleroma.localhost")
user = insert(:user)
response =
build_conn()
|> put_req_header("accept", "application/jrd+json")
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@pleroma.localhost")
|> json_response(200)
assert response["subject"] == "acct:#{user.nickname}@pleroma.localhost"
assert response["aliases"] == [user.ap_id]
assert response["links"] == [
%{
"href" => user.ap_id,
"rel" => "http://webfinger.net/rel/profile-page",
"type" => "text/html"
},
%{"href" => user.ap_id, "rel" => "self", "type" => "application/activity+json"},
%{
"href" => user.ap_id,
"rel" => "self",
"type" =>
"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
},
%{
"rel" => "http://ostatus.org/schema/1.0/subscribe",
"template" => "http://pleroma.localhost/ostatus_subscribe?acct={uri}"
}
]
end
test "it returns 404 when user isn't found (JSON)" do test "it returns 404 when user isn't found (JSON)" do
result = result =
build_conn() build_conn()
@ -51,16 +103,34 @@ test "it returns 404 when user isn't found (JSON)" do
assert result == "Couldn't find user" assert result == "Couldn't find user"
end end
end
test "Webfinger XML" do describe "webfinger XML" do
test "for subdomain" do
Pleroma.Config.put([Pleroma.Web.Endpoint, :web_endpoint], "http://pleroma.localhost")
user = insert(:user)
response =
build_conn()
|> put_req_header("accept", "application/xrd+xml")
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@pleroma.localhost")
|> response(200)
|> parse()
assert xpath(response, ~x"//Subject/text()"s) == "acct:#{user.nickname}@pleroma.localhost"
end
test "for main domain" do
user = insert(:user) user = insert(:user)
response = response =
build_conn() build_conn()
|> put_req_header("accept", "application/xrd+xml") |> put_req_header("accept", "application/xrd+xml")
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost") |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
|> response(200)
|> parse()
assert response(response, 200) assert xpath(response, ~x"//Subject/text()"s) == "acct:#{user.nickname}@localhost"
end end
test "it returns 404 when user isn't found (XML)" do test "it returns 404 when user isn't found (XML)" do
@ -72,6 +142,7 @@ test "it returns 404 when user isn't found (XML)" do
assert result == "Couldn't find user" assert result == "Couldn't find user"
end end
end
test "Sends a 404 when invalid format" do test "Sends a 404 when invalid format" do
user = insert(:user) user = insert(:user)