Database authenticator behaviour / Pleroma implementation refactoring.
This commit is contained in:
parent
afddce45b3
commit
e82b70eb53
5 changed files with 19 additions and 20 deletions
14
lib/pleroma/web/auth/database_authenticator.ex
Normal file
14
lib/pleroma/web/auth/database_authenticator.ex
Normal 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
|
||||
22
lib/pleroma/web/auth/pleroma_database_authenticator.ex
Normal file
22
lib/pleroma/web/auth/pleroma_database_authenticator.ex
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue