Database authenticator behaviour / Pleroma implementation refactoring.

This commit is contained in:
Ivan Tashkinov 2019-02-26 15:27:01 +03:00
parent afddce45b3
commit e82b70eb53
5 changed files with 19 additions and 20 deletions

View file

@ -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

View file

@ -1,8 +1,8 @@
defmodule Pleroma.Web.Authenticator do defmodule Pleroma.Web.Auth.PleromaDatabaseAuthenticator do
alias Pleroma.User alias Pleroma.User
alias Comeonin.Pbkdf2 alias Comeonin.Pbkdf2
@behaviour Pleroma.Web.AuthenticatorAdapter @behaviour Pleroma.Web.Auth.DatabaseAuthenticator
def get_user(%Plug.Conn{} = conn) do def get_user(%Plug.Conn{} = conn) do
%{"authorization" => %{"name" => name, "password" => password}} = conn.params %{"authorization" => %{"name" => name, "password" => password}} = conn.params

View file

@ -3,14 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.OAuth do 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 def parse_scopes(scopes, _default) when is_list(scopes) do
Enum.filter(scopes, &(&1 not in [nil, ""])) Enum.filter(scopes, &(&1 not in [nil, ""]))
end end

View file

@ -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

View file

@ -5,7 +5,7 @@
defmodule Pleroma.Web.OAuth.OAuthController do defmodule Pleroma.Web.OAuth.OAuthController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
alias Pleroma.Web.OAuth alias Pleroma.Web.Auth.DatabaseAuthenticator
alias Pleroma.Web.OAuth.Authorization alias Pleroma.Web.OAuth.Authorization
alias Pleroma.Web.OAuth.Token alias Pleroma.Web.OAuth.Token
alias Pleroma.Web.OAuth.App alias Pleroma.Web.OAuth.App
@ -45,7 +45,7 @@ def create_authorization(conn, %{
"redirect_uri" => redirect_uri "redirect_uri" => redirect_uri
} = auth_params } = auth_params
}) do }) 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), %App{} = app <- Repo.get_by(App, client_id: client_id),
true <- redirect_uri in String.split(app.redirect_uris), true <- redirect_uri in String.split(app.redirect_uris),
scopes <- oauth_scopes(auth_params, []), scopes <- oauth_scopes(auth_params, []),
@ -98,7 +98,7 @@ def create_authorization(conn, %{
|> authorize(auth_params) |> authorize(auth_params)
error -> error ->
OAuth.authenticator().handle_error(conn, error) DatabaseAuthenticator.handle_error(conn, error)
end end
end end