Merge branch 'features/ingestion-page' into 'develop'
[akkoma] / lib / pleroma / web / auth / helpers.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Auth.Helpers do
6 alias Pleroma.User
7
8 @doc "Gets user by nickname or email for auth."
9 @spec fetch_user(String.t()) :: User.t() | nil
10 def fetch_user(name) do
11 User.get_by_nickname_or_email(name)
12 end
13
14 # Gets name and password from conn
15 #
16 @spec fetch_credentials(Plug.Conn.t() | map()) ::
17 {:ok, {name :: any, password :: any}} | {:error, :invalid_credentials}
18 def fetch_credentials(%Plug.Conn{params: params} = _),
19 do: fetch_credentials(params)
20
21 def fetch_credentials(params) do
22 case params do
23 %{"authorization" => %{"name" => name, "password" => password}} ->
24 {:ok, {name, password}}
25
26 %{"grant_type" => "password", "username" => name, "password" => password} ->
27 {:ok, {name, password}}
28
29 _ ->
30 {:error, :invalid_credentials}
31 end
32 end
33 end