add support for username authentication
This commit is contained in:
@@ -33,12 +33,21 @@ defmodule Entendu.UserFromAuth do
|
||||
|
||||
defp emails_from_auth(_auth), do: []
|
||||
|
||||
defp username_from_auth(%Auth{info: %{nickname: username}}), do: username
|
||||
|
||||
defp username_from_auth(auth) do
|
||||
Logger.warn("#{auth.provider} needs to be configured for accessing their username!")
|
||||
IO.inspect(auth, label: "username_from_auth")
|
||||
nil
|
||||
end
|
||||
|
||||
defp basic_info(auth) do
|
||||
%{
|
||||
id: auth.uid,
|
||||
name: name_from_auth(auth),
|
||||
avatar: avatar_from_auth(auth),
|
||||
emails: emails_from_auth(auth)
|
||||
emails: emails_from_auth(auth),
|
||||
username: username_from_auth(auth)
|
||||
}
|
||||
end
|
||||
|
||||
@@ -58,8 +67,11 @@ defmodule Entendu.UserFromAuth do
|
||||
end
|
||||
end
|
||||
|
||||
def can_access?(recipient, emails) do
|
||||
emails
|
||||
|> Enum.any?(&(&1["verified"] == true and &1["email"] == recipient))
|
||||
end
|
||||
def can_access?(recipient, %{emails: emails, username: username}),
|
||||
do: email_matches?(recipient, emails) || username_matches?(recipient, username)
|
||||
|
||||
defp email_matches?(recipient, emails),
|
||||
do: emails |> Enum.any?(&(&1["verified"] == true and &1["email"] == recipient))
|
||||
|
||||
defp username_matches?(recipient, username), do: String.trim(username) === recipient
|
||||
end
|
||||
|
@@ -29,7 +29,7 @@ defmodule EntenduWeb.AuthController do
|
||||
|
||||
with %{id: link_id, recipient: recipient} <- link,
|
||||
{:ok, user} <- UserFromAuth.find_or_create(auth),
|
||||
true <- UserFromAuth.can_access?(recipient, user.emails) do
|
||||
true <- UserFromAuth.can_access?(recipient, user) do
|
||||
# TODO: send over encrypted data that the frontend can decrypt
|
||||
|
||||
conn
|
||||
|
@@ -55,7 +55,7 @@ defmodule EntenduWeb.LinkController do
|
||||
def text(conn, %{"id" => link_id}) do
|
||||
with user = get_session(conn, :current_user),
|
||||
%Link{recipient: recipient} = link <- Links.get_link(link_id),
|
||||
true <- UserFromAuth.can_access?(recipient, user.emails) do
|
||||
true <- UserFromAuth.can_access?(recipient, user) do
|
||||
path = EncryptedLink.url({link.text_content, link})
|
||||
send_file(conn, 200, path)
|
||||
end
|
||||
@@ -64,7 +64,7 @@ defmodule EntenduWeb.LinkController do
|
||||
def file(conn, %{"id" => link_id}) do
|
||||
with user = get_session(conn, :current_user),
|
||||
%Link{recipient: recipient} = link <- Links.get_link(link_id),
|
||||
true <- UserFromAuth.can_access?(recipient, user.emails) do
|
||||
true <- UserFromAuth.can_access?(recipient, user) do
|
||||
path = EncryptedLink.url({link.file_content, link})
|
||||
send_file(conn, 200, path)
|
||||
end
|
||||
|
@@ -25,7 +25,7 @@ defmodule EntenduWeb.Plugs.AuthorizeLink do
|
||||
else
|
||||
with {:ok, user} <- get_user_from_path(conn),
|
||||
%Link{recipient: recipient} = link <- Links.get_link(link_id),
|
||||
true <- UserFromAuth.can_access?(recipient, user.emails) do
|
||||
true <- UserFromAuth.can_access?(recipient, user) do
|
||||
conn
|
||||
|> assign(:link, link)
|
||||
else
|
||||
|
Reference in New Issue
Block a user