From f3995911d7877fb8c47031539d6d4345a9e0c854 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 1 Oct 2019 12:35:27 -0400 Subject: [PATCH] Add basic web finger support --- .../controllers/web_finger_controller.ex | 33 +++++++++++++++++++ lib/clacks_web/router.ex | 2 ++ 2 files changed, 35 insertions(+) create mode 100644 lib/clacks_web/controllers/web_finger_controller.ex diff --git a/lib/clacks_web/controllers/web_finger_controller.ex b/lib/clacks_web/controllers/web_finger_controller.ex new file mode 100644 index 0000000..ebd817a --- /dev/null +++ b/lib/clacks_web/controllers/web_finger_controller.ex @@ -0,0 +1,33 @@ +defmodule ClacksWeb.WebFingerController do + use ClacksWeb, :controller + alias Clacks.Actor + + @acct_regex ~r/(?:acct:)?([a-z0-9_]+)(?:@.+)?/i + + def get(conn, %{"resource" => resource}) do + with [_, nickname] <- Regex.run(@acct_regex, resource), + %Actor{local: true} = actor <- Actor.get_by_nickanme(nickname) do + url = Application.get_env(:clacks, ClacksWeb.Endpoint)[:url] + host = url[:host] + port = url[:port] + maybe_port = if port == URI.default_port(url[:scheme]), do: "", else: ":#{port}" + + conn + |> json(%{ + "subject" => "acct:#{actor.nickname}@#{host}#{maybe_port}", + "links" => [ + %{ + "rel" => "self", + "type" => "application/activity+json", + "href" => actor.ap_id + } + ] + }) + else + _ -> + conn + |> put_status(404) + |> json(%{error: "No such resource"}) + end + end +end diff --git a/lib/clacks_web/router.ex b/lib/clacks_web/router.ex index 61176c7..8a8846c 100644 --- a/lib/clacks_web/router.ex +++ b/lib/clacks_web/router.ex @@ -22,6 +22,8 @@ defmodule ClacksWeb.Router do get "/objects/:id", ObjectsController, :get get "/users/:nickname", ActorController, :get + + get "/.well-known/webfinger", WebFingerController, :get end # Other scopes may use custom stacks.