From 947431e9aa1d4dbf85f9ccc270f1989737744ed4 Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 18 Apr 2018 13:13:57 +0300 Subject: [PATCH] MastoAPI and OAuth: allow login with either email or username. --- lib/pleroma/user.ex | 7 +++++++ lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 2 +- lib/pleroma/web/oauth/oauth_controller.ex | 2 +- .../web/templates/mastodon_api/mastodon/login.html.eex | 2 +- lib/pleroma/web/templates/o_auth/o_auth/show.html.eex | 2 +- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index e959fe677..c77fd6816 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -250,6 +250,13 @@ def get_by_nickname(nickname) do Repo.get_by(User, nickname: nickname) end + def get_by_nickname_or_email(nickname_or_email) do + case user = Repo.get_by(User, nickname: nickname_or_email) do + %User{} -> user + nil -> Repo.get_by(User, email: nickname_or_email) + end + end + def get_cached_user_info(user) do key = "user_info:#{user.id}" Cachex.get!(:user_cache, key, fallback: fn _ -> user_info(user) end) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 21a3660c8..10531ec15 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -693,7 +693,7 @@ defp get_or_make_app() do end def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do - with %User{} = user <- User.get_cached_by_nickname(name), + with %User{} = user <- User.get_by_nickname_or_email(name), true <- Pbkdf2.checkpw(password, user.password_hash), {:ok, app} <- get_or_make_app(), {:ok, auth} <- Authorization.create_authorization(app, user), diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex index 05f366611..6297b7bae 100644 --- a/lib/pleroma/web/oauth/oauth_controller.ex +++ b/lib/pleroma/web/oauth/oauth_controller.ex @@ -29,7 +29,7 @@ def create_authorization(conn, %{ "redirect_uri" => redirect_uri } = params }) do - with %User{} = user <- User.get_cached_by_nickname(name), + with %User{} = user <- User.get_by_nickname_or_email(name), true <- Pbkdf2.checkpw(password, user.password_hash), %App{} = app <- Repo.get_by(App, client_id: client_id), {:ok, auth} <- Authorization.create_authorization(app, user) do diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex index 2ef67b901..2bb54977e 100644 --- a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex @@ -3,7 +3,7 @@

<%= @error %>

<% end %> <%= form_for @conn, mastodon_api_path(@conn, :login), [as: "authorization"], fn f -> %> -<%= text_input f, :name, placeholder: "Username" %> +<%= text_input f, :name, placeholder: "Username or email" %>
<%= password_input f, :password, placeholder: "Password" %>
diff --git a/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex b/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex index a7fa7523b..de2241ec9 100644 --- a/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex +++ b/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex @@ -2,7 +2,7 @@

OAuth Authorization

<%= form_for @conn, o_auth_path(@conn, :authorize), [as: "authorization"], fn f -> %> -<%= label f, :name, "Name" %> +<%= label f, :name, "Name or email" %> <%= text_input f, :name %>
<%= label f, :password, "Password" %>