add support for username authentication

This commit is contained in:
Silas 2022-02-22 00:43:16 -05:00
parent 2ac596b8c8
commit 66107c0523
Signed by: silentsilas
GPG Key ID: 4199EFB7DAA34349
5 changed files with 23 additions and 9 deletions

View File

@ -84,6 +84,8 @@ const AuthPage = (props: AuthPageProps) => {
} else { } else {
key = fragmentData[0]; key = fragmentData[0];
iv = fragmentData[1]; iv = fragmentData[1];
sessionStorage.setItem("link_key", key);
sessionStorage.setItem("link_iv", iv);
} }
if (key && iv) { if (key && iv) {

View File

@ -33,12 +33,21 @@ defmodule Entendu.UserFromAuth do
defp emails_from_auth(_auth), 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 defp basic_info(auth) do
%{ %{
id: auth.uid, id: auth.uid,
name: name_from_auth(auth), name: name_from_auth(auth),
avatar: avatar_from_auth(auth), avatar: avatar_from_auth(auth),
emails: emails_from_auth(auth) emails: emails_from_auth(auth),
username: username_from_auth(auth)
} }
end end
@ -58,8 +67,11 @@ defmodule Entendu.UserFromAuth do
end end
end end
def can_access?(recipient, emails) do def can_access?(recipient, %{emails: emails, username: username}),
emails do: email_matches?(recipient, emails) || username_matches?(recipient, username)
|> Enum.any?(&(&1["verified"] == true and &1["email"] == recipient))
end 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 end

View File

@ -29,7 +29,7 @@ defmodule EntenduWeb.AuthController do
with %{id: link_id, recipient: recipient} <- link, with %{id: link_id, recipient: recipient} <- link,
{:ok, user} <- UserFromAuth.find_or_create(auth), {: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 # TODO: send over encrypted data that the frontend can decrypt
conn conn

View File

@ -55,7 +55,7 @@ defmodule EntenduWeb.LinkController do
def text(conn, %{"id" => link_id}) do def text(conn, %{"id" => link_id}) do
with user = get_session(conn, :current_user), with user = get_session(conn, :current_user),
%Link{recipient: recipient} = link <- Links.get_link(link_id), %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}) path = EncryptedLink.url({link.text_content, link})
send_file(conn, 200, path) send_file(conn, 200, path)
end end
@ -64,7 +64,7 @@ defmodule EntenduWeb.LinkController do
def file(conn, %{"id" => link_id}) do def file(conn, %{"id" => link_id}) do
with user = get_session(conn, :current_user), with user = get_session(conn, :current_user),
%Link{recipient: recipient} = link <- Links.get_link(link_id), %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}) path = EncryptedLink.url({link.file_content, link})
send_file(conn, 200, path) send_file(conn, 200, path)
end end

View File

@ -25,7 +25,7 @@ defmodule EntenduWeb.Plugs.AuthorizeLink do
else else
with {:ok, user} <- get_user_from_path(conn), with {:ok, user} <- get_user_from_path(conn),
%Link{recipient: recipient} = link <- Links.get_link(link_id), %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 conn
|> assign(:link, link) |> assign(:link, link)
else else