From 182e0b58daa44e33c1a8d26ffb3c9db5597ba99c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Miko=C5=82ajczak?= Date: Fri, 10 Nov 2023 00:53:17 +0100 Subject: [PATCH] Add initial docs page, improve adding domains MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Mikołajczak --- config/description.exs | 14 ++++++++- ...to_serve_multiple_domains_for_webfinger.md | 30 +++++++++++++++++++ lib/pleroma/domain.ex | 4 ++- .../controllers/domain_controller.ex | 12 ++++---- .../web/mastodon_api/views/instance_view.ex | 2 +- lib/pleroma/web/web_finger.ex | 2 +- .../20230618190919_create_domains.exs | 2 +- 7 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 docs/configuration/how_to_serve_multiple_domains_for_webfinger.md diff --git a/config/description.exs b/config/description.exs index d18649ae8a..4d80dd846f 100644 --- a/config/description.exs +++ b/config/description.exs @@ -1061,6 +1061,18 @@ suggestions: [ "en" ] + }, + %{ + key: :multitenancy, + type: :map, + description: "Multitenancy support", + children: [ + %{ + key: :enabled, + type: :boolean, + description: "Enables allowing multiple Webfinger domains" + }, + ] } ] }, @@ -3466,5 +3478,5 @@ ] } ] - } + }, ] diff --git a/docs/configuration/how_to_serve_multiple_domains_for_webfinger.md b/docs/configuration/how_to_serve_multiple_domains_for_webfinger.md new file mode 100644 index 0000000000..5a7a3733ff --- /dev/null +++ b/docs/configuration/how_to_serve_multiple_domains_for_webfinger.md @@ -0,0 +1,30 @@ +# How to serve multiple domains for Pleroma user identifiers + +It is possible to use multiple domains for WebFinger identifiers. If configured, users can select from the available domains during registration. Domains can be set by instance administrator and can be marked as either public (everyone can choose it) or private (only available when admin creates a user) + +## Configuring + +### Configuring Pleroma + +To enable using multiple domains, append the following to your `prod.secret.exs` or `dev.secret.exs`: +```elixir +config :pleroma, :instance, :multitenancy, enabled: true +``` + +Creating, updating and deleting domains is available from the admin API. + +### Configuring WebFinger domains + +If you recall how webfinger queries work, the first step is to query `https://example.org/.well-known/host-meta`, which will contain an URL template. + +Therefore, the easiest way to configure the additional domains is to redirect `/.well-known/host-meta` to the domain used by Pleroma. + +With nginx, it would be as simple as adding: + +```nginx +location = /.well-known/host-meta { + return 301 https://pleroma.example.org$request_uri; +} +``` + +in the additional domain's server block. diff --git a/lib/pleroma/domain.ex b/lib/pleroma/domain.ex index d708188eed..cf7efc350a 100644 --- a/lib/pleroma/domain.ex +++ b/lib/pleroma/domain.ex @@ -19,11 +19,13 @@ def changeset(%__MODULE__{} = domain, params \\ %{}) do domain |> cast(params, [:domain, :public]) |> validate_required([:domain]) + |> update_change(:domain, &String.downcase/1) + |> unique_constraint(:domain) end def update_changeset(%__MODULE__{} = domain, params \\ %{}) do domain - |> cast(params, [:domain]) + |> cast(params, [:public]) end def get(id), do: Repo.get(__MODULE__, id) diff --git a/lib/pleroma/web/admin_api/controllers/domain_controller.ex b/lib/pleroma/web/admin_api/controllers/domain_controller.ex index 46b0394601..cb2b7dd3f6 100644 --- a/lib/pleroma/web/admin_api/controllers/domain_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/domain_controller.ex @@ -37,11 +37,13 @@ def index(conn, _) do end def create(%{body_params: params} = conn, _) do - domain = - params - |> Domain.create() - - render(conn, "show.json", domain: domain) + with {:domain_not_used, true} <- + {:domain_not_used, params[:domain] !== Pleroma.Web.WebFinger.domain()}, + {:domain, domain} <- Domain.create(params) do + render(conn, "show.json", domain: domain) + else + {:domain_not_used, false} -> {:error, :invalid_domain} + end end def update(%{body_params: params} = conn, %{id: id}) do diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index 4ab4ccb74c..fa2b467eca 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -147,7 +147,7 @@ def fields_limits do end defp multitenancy do - enabled = Config.get([:multitenancy, :enabled]) + enabled = Config.get([:instance, :multitenancy, :enabled]) if enabled do domains = diff --git a/lib/pleroma/web/web_finger.ex b/lib/pleroma/web/web_finger.ex index 49c17e2c32..39e38d25cc 100644 --- a/lib/pleroma/web/web_finger.ex +++ b/lib/pleroma/web/web_finger.ex @@ -102,7 +102,7 @@ defp get_subject(%User{nickname: nickname}) do end end - defp domain do + def domain do Pleroma.Config.get([__MODULE__, :domain]) || Pleroma.Web.Endpoint.host() end diff --git a/priv/repo/migrations/20230618190919_create_domains.exs b/priv/repo/migrations/20230618190919_create_domains.exs index a306c80ee0..c97e073261 100644 --- a/priv/repo/migrations/20230618190919_create_domains.exs +++ b/priv/repo/migrations/20230618190919_create_domains.exs @@ -3,7 +3,7 @@ defmodule Pleroma.Repo.Migrations.CreateDomains do def change do create_if_not_exists table(:domains) do - add(:domain, :string) + add(:domain, :citext) add(:public, :boolean) timestamps()