From e82b70eb530293c3dfe8597c4100320fba96e479 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Tue, 26 Feb 2019 15:27:01 +0300 Subject: Database authenticator behaviour / Pleroma implementation refactoring. --- lib/pleroma/web/auth/database_authenticator.ex | 14 ++++++++++++++ .../web/auth/pleroma_database_authenticator.ex | 22 ++++++++++++++++++++++ lib/pleroma/web/oauth.ex | 8 -------- lib/pleroma/web/oauth/authenticator.ex | 22 ---------------------- lib/pleroma/web/oauth/authenticator_adapter.ex | 7 ------- lib/pleroma/web/oauth/oauth_controller.ex | 6 +++--- 6 files changed, 39 insertions(+), 40 deletions(-) create mode 100644 lib/pleroma/web/auth/database_authenticator.ex create mode 100644 lib/pleroma/web/auth/pleroma_database_authenticator.ex delete mode 100644 lib/pleroma/web/oauth/authenticator.ex delete mode 100644 lib/pleroma/web/oauth/authenticator_adapter.ex (limited to 'lib') diff --git a/lib/pleroma/web/auth/database_authenticator.ex b/lib/pleroma/web/auth/database_authenticator.ex new file mode 100644 index 000000000..69024a4ba --- /dev/null +++ b/lib/pleroma/web/auth/database_authenticator.ex @@ -0,0 +1,14 @@ +defmodule Pleroma.Web.Auth.DatabaseAuthenticator do + alias Pleroma.User + + @implementation Pleroma.Config.get( + Pleroma.Web.Auth.DatabaseAuthenticator, + Pleroma.Web.Auth.PleromaDatabaseAuthenticator + ) + + @callback get_user(Plug.Conn.t()) :: {:ok, User.t()} | {:error, any()} + defdelegate get_user(plug), to: @implementation + + @callback handle_error(Plug.Conn.t(), any()) :: any() + defdelegate handle_error(plug, error), to: @implementation +end diff --git a/lib/pleroma/web/auth/pleroma_database_authenticator.ex b/lib/pleroma/web/auth/pleroma_database_authenticator.ex new file mode 100644 index 000000000..79a8dcfce --- /dev/null +++ b/lib/pleroma/web/auth/pleroma_database_authenticator.ex @@ -0,0 +1,22 @@ +defmodule Pleroma.Web.Auth.PleromaDatabaseAuthenticator do + alias Pleroma.User + alias Comeonin.Pbkdf2 + + @behaviour Pleroma.Web.Auth.DatabaseAuthenticator + + def get_user(%Plug.Conn{} = conn) do + %{"authorization" => %{"name" => name, "password" => password}} = conn.params + + with {_, %User{} = user} <- {:user, User.get_by_nickname_or_email(name)}, + {_, true} <- {:checkpw, Pbkdf2.checkpw(password, user.password_hash)} do + {:ok, user} + else + error -> + {:error, error} + end + end + + def handle_error(%Plug.Conn{} = _conn, error) do + error + end +end diff --git a/lib/pleroma/web/oauth.ex b/lib/pleroma/web/oauth.ex index f3bac33c8..d2835a0ba 100644 --- a/lib/pleroma/web/oauth.ex +++ b/lib/pleroma/web/oauth.ex @@ -3,14 +3,6 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.OAuth do - @authenticator Application.get_env( - :pleroma, - Pleroma.Web.AuthenticatorAdapter, - Pleroma.Web.Authenticator - ) - - def authenticator, do: @authenticator - def parse_scopes(scopes, _default) when is_list(scopes) do Enum.filter(scopes, &(&1 not in [nil, ""])) end diff --git a/lib/pleroma/web/oauth/authenticator.ex b/lib/pleroma/web/oauth/authenticator.ex deleted file mode 100644 index 86bbc41f0..000000000 --- a/lib/pleroma/web/oauth/authenticator.ex +++ /dev/null @@ -1,22 +0,0 @@ -defmodule Pleroma.Web.Authenticator do - alias Pleroma.User - alias Comeonin.Pbkdf2 - - @behaviour Pleroma.Web.AuthenticatorAdapter - - def get_user(%Plug.Conn{} = conn) do - %{"authorization" => %{"name" => name, "password" => password}} = conn.params - - with {_, %User{} = user} <- {:user, User.get_by_nickname_or_email(name)}, - {_, true} <- {:checkpw, Pbkdf2.checkpw(password, user.password_hash)} do - {:ok, user} - else - error -> - {:error, error} - end - end - - def handle_error(%Plug.Conn{} = _conn, error) do - error - end -end diff --git a/lib/pleroma/web/oauth/authenticator_adapter.ex b/lib/pleroma/web/oauth/authenticator_adapter.ex deleted file mode 100644 index 282963b1c..000000000 --- a/lib/pleroma/web/oauth/authenticator_adapter.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Pleroma.Web.AuthenticatorAdapter do - alias Pleroma.User - - @callback get_user(Plug.Conn.t()) :: {:ok, User.t()} | {:error, any()} - - @callback handle_error(Plug.Conn.t(), any()) :: any() -end diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex index abe6fd2f2..02c0babd2 100644 --- a/lib/pleroma/web/oauth/oauth_controller.ex +++ b/lib/pleroma/web/oauth/oauth_controller.ex @@ -5,7 +5,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do use Pleroma.Web, :controller - alias Pleroma.Web.OAuth + alias Pleroma.Web.Auth.DatabaseAuthenticator alias Pleroma.Web.OAuth.Authorization alias Pleroma.Web.OAuth.Token alias Pleroma.Web.OAuth.App @@ -45,7 +45,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do "redirect_uri" => redirect_uri } = auth_params }) do - with {_, {:ok, %User{} = user}} <- {:get_user, OAuth.authenticator().get_user(conn)}, + with {_, {:ok, %User{} = user}} <- {:get_user, DatabaseAuthenticator.get_user(conn)}, %App{} = app <- Repo.get_by(App, client_id: client_id), true <- redirect_uri in String.split(app.redirect_uris), scopes <- oauth_scopes(auth_params, []), @@ -98,7 +98,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do |> authorize(auth_params) error -> - OAuth.authenticator().handle_error(conn, error) + DatabaseAuthenticator.handle_error(conn, error) end end -- cgit v1.2.3