2019-02-26 07:26:54 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-28 05:12:41 -06:00
|
|
|
defmodule Pleroma.Web.Auth.Authenticator do
|
2019-03-20 02:35:31 -05:00
|
|
|
alias Pleroma.Registration
|
2019-04-01 01:45:44 -05:00
|
|
|
alias Pleroma.User
|
2019-02-26 06:27:01 -06:00
|
|
|
|
2019-02-28 04:00:54 -06:00
|
|
|
def implementation do
|
|
|
|
Pleroma.Config.get(
|
2019-02-28 05:12:41 -06:00
|
|
|
Pleroma.Web.Auth.Authenticator,
|
|
|
|
Pleroma.Web.Auth.PleromaAuthenticator
|
2019-02-28 04:00:54 -06:00
|
|
|
)
|
|
|
|
end
|
2019-02-26 06:27:01 -06:00
|
|
|
|
2019-04-10 13:40:38 -05:00
|
|
|
@callback get_user(Plug.Conn.t()) :: {:ok, User.t()} | {:error, any()}
|
|
|
|
def get_user(plug), do: implementation().get_user(plug)
|
2019-03-15 09:08:03 -05:00
|
|
|
|
2019-04-10 13:40:38 -05:00
|
|
|
@callback create_from_registration(Plug.Conn.t(), Registration.t()) ::
|
2019-03-15 09:08:03 -05:00
|
|
|
{:ok, User.t()} | {:error, any()}
|
2019-04-10 13:40:38 -05:00
|
|
|
def create_from_registration(plug, registration),
|
|
|
|
do: implementation().create_from_registration(plug, registration)
|
2019-03-20 02:35:31 -05:00
|
|
|
|
2019-04-10 13:40:38 -05:00
|
|
|
@callback get_registration(Plug.Conn.t()) ::
|
2019-03-20 02:35:31 -05:00
|
|
|
{:ok, Registration.t()} | {:error, any()}
|
2019-04-10 13:40:38 -05:00
|
|
|
def get_registration(plug), do: implementation().get_registration(plug)
|
2019-02-26 06:27:01 -06:00
|
|
|
|
|
|
|
@callback handle_error(Plug.Conn.t(), any()) :: any()
|
2019-04-10 13:40:38 -05:00
|
|
|
def handle_error(plug, error),
|
|
|
|
do: implementation().handle_error(plug, error)
|
2019-02-28 04:58:58 -06:00
|
|
|
|
|
|
|
@callback auth_template() :: String.t() | nil
|
|
|
|
def auth_template do
|
2019-04-07 03:08:37 -05:00
|
|
|
# Note: `config :pleroma, :auth_template, "..."` support is deprecated
|
|
|
|
implementation().auth_template() ||
|
|
|
|
Pleroma.Config.get([:auth, :auth_template], Pleroma.Config.get(:auth_template)) ||
|
|
|
|
"show.html"
|
2019-02-28 04:58:58 -06:00
|
|
|
end
|
2019-03-27 07:39:35 -05:00
|
|
|
|
|
|
|
@callback oauth_consumer_template() :: String.t() | nil
|
|
|
|
def oauth_consumer_template do
|
|
|
|
implementation().oauth_consumer_template() ||
|
2019-04-07 03:08:37 -05:00
|
|
|
Pleroma.Config.get([:auth, :oauth_consumer_template], "consumer.html")
|
2019-03-27 07:39:35 -05:00
|
|
|
end
|
2019-05-01 08:28:04 -05:00
|
|
|
|
|
|
|
@doc "Gets user by nickname or email for auth."
|
|
|
|
@spec fetch_user(String.t()) :: User.t() | nil
|
|
|
|
def fetch_user(name) do
|
|
|
|
User.get_by_nickname_or_email(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Gets name and password from conn
|
|
|
|
#
|
|
|
|
@spec fetch_credentials(Plug.Conn.t() | map()) ::
|
|
|
|
{:ok, {name :: any, password :: any}} | {:error, :invalid_credentials}
|
|
|
|
def fetch_credentials(%Plug.Conn{params: params} = _),
|
|
|
|
do: fetch_credentials(params)
|
|
|
|
|
|
|
|
def fetch_credentials(params) do
|
|
|
|
case params do
|
|
|
|
%{"authorization" => %{"name" => name, "password" => password}} ->
|
|
|
|
{:ok, {name, password}}
|
|
|
|
|
|
|
|
%{"grant_type" => "password", "username" => name, "password" => password} ->
|
|
|
|
{:ok, {name, password}}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, :invalid_credentials}
|
|
|
|
end
|
|
|
|
end
|
2019-02-26 06:27:01 -06:00
|
|
|
end
|