-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As prep for ActivityPub!
- Loading branch information
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule AsciinemaWeb.WebFingerController do | ||
use AsciinemaWeb, :new_controller | ||
alias Asciinema.Accounts | ||
|
||
def show(conn, %{"resource" => resource}) do | ||
resource = | ||
resource | ||
|> String.trim() | ||
|> String.downcase() | ||
|
||
with "acct:" <> acct <- resource, | ||
[username, domain] <- String.split(acct, "@"), | ||
^domain <- AsciinemaWeb.Endpoint.host(), | ||
{:username, user} when not is_nil(user) <- Accounts.lookup_user(username) do | ||
render(conn, :show, user: user, domain: domain) | ||
else | ||
_ -> {:error, :not_found} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule AsciinemaWeb.WebFingerJSON do | ||
use AsciinemaWeb, :json | ||
|
||
def show(%{user: %{username: username}, domain: domain}) do | ||
%{ | ||
subject: "acct:#{username}@#{domain}", | ||
aliases: [ | ||
url(~p"/~#{username}") | ||
], | ||
links: [ | ||
%{ | ||
rel: "http://webfinger.net/rel/profile-page", | ||
type: "text/html", | ||
href: url(~p"/~#{username}") | ||
} | ||
] | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Asciinema.WebFingerControllerTest do | ||
use AsciinemaWeb.ConnCase | ||
import Asciinema.Factory | ||
|
||
setup [:create_user] | ||
|
||
describe "show" do | ||
test "returns basic info", %{conn: conn} do | ||
conn = get(conn, ~p"/.well-known/webfinger?resource=acct:Pinky@localhost") | ||
|
||
assert json_response(conn, 200) == %{ | ||
"subject" => "acct:Pinky@localhost", | ||
"aliases" => [ | ||
url(~p"/~Pinky") | ||
], | ||
"links" => [ | ||
%{ | ||
"rel" => "http://webfinger.net/rel/profile-page", | ||
"type" => "text/html", | ||
"href" => url(~p"/~Pinky") | ||
} | ||
] | ||
} | ||
end | ||
|
||
test "does case-insensitive acct lookup", %{conn: conn} do | ||
conn = get(conn, ~p"/.well-known/webfinger?resource=acct:pinky@lOcaLhOst") | ||
|
||
assert %{"subject" => "acct:Pinky@localhost"} = json_response(conn, 200) | ||
end | ||
|
||
test "returns 404 when username not found", %{conn: conn} do | ||
conn = get(conn, ~p"/.well-known/webfinger?resource=acct:nope@localhost") | ||
|
||
assert json_response(conn, 404) | ||
end | ||
|
||
test "returns 404 when domain doesn't match", %{conn: conn} do | ||
conn = get(conn, ~p"/.well-known/webfinger?resource=acct:[email protected]") | ||
|
||
assert json_response(conn, 404) | ||
end | ||
end | ||
|
||
defp create_user(_) do | ||
%{user: insert(:user, username: "Pinky")} | ||
end | ||
end |