Database authenticator behaviour / Pleroma implementation refactoring.

This commit is contained in:
Ivan Tashkinov 2019-02-26 15:27:01 +03:00
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

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