Database authenticator behaviour / Pleroma implementation refactoring.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 26 Feb 2019 12:27:01 +0000 (15:27 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 26 Feb 2019 12:27:01 +0000 (15:27 +0300)
lib/pleroma/web/auth/database_authenticator.ex [new file with mode: 0644]
lib/pleroma/web/auth/pleroma_database_authenticator.ex [moved from lib/pleroma/web/oauth/authenticator.ex with 81% similarity]
lib/pleroma/web/oauth.ex
lib/pleroma/web/oauth/authenticator_adapter.ex [deleted file]
lib/pleroma/web/oauth/oauth_controller.ex

diff --git a/lib/pleroma/web/auth/database_authenticator.ex b/lib/pleroma/web/auth/database_authenticator.ex
new file mode 100644 (file)
index 0000000..69024a4
--- /dev/null
@@ -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
similarity index 81%
rename from lib/pleroma/web/oauth/authenticator.ex
rename to lib/pleroma/web/auth/pleroma_database_authenticator.ex
index 86bbc41f07b8dac70da7b1976d6a408ebb3dcf9c..79a8dcfce4dcfe416d38ac4a646f9ac303ff9a2e 100644 (file)
@@ -1,8 +1,8 @@
-defmodule Pleroma.Web.Authenticator do
+defmodule Pleroma.Web.Auth.PleromaDatabaseAuthenticator do
   alias Pleroma.User
   alias Comeonin.Pbkdf2
 
-  @behaviour Pleroma.Web.AuthenticatorAdapter
+  @behaviour Pleroma.Web.Auth.DatabaseAuthenticator
 
   def get_user(%Plug.Conn{} = conn) do
     %{"authorization" => %{"name" => name, "password" => password}} = conn.params
index f3bac33c83409865ceca9b9dde7ef646be3e2a60..d2835a0ba579c8a5c94b9a79736f07f9b48dd58a 100644 (file)
@@ -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_adapter.ex b/lib/pleroma/web/oauth/authenticator_adapter.ex
deleted file mode 100644 (file)
index 282963b..0000000
+++ /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
index abe6fd2f239cbd71a93e10f5f1079c7022f96cfc..02c0babd24080f75bc936f0a48aa10b8c1b17c81 100644 (file)
@@ -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