1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Auth.PleromaAuthenticator do
7 alias Pleroma.Registration
11 @behaviour Pleroma.Web.Auth.Authenticator
13 def get_user(%Plug.Conn{} = _conn, params) do
16 %{"authorization" => %{"name" => name, "password" => password}} ->
19 %{"grant_type" => "password", "username" => name, "password" => password} ->
23 with {_, %User{} = user} <- {:user, User.get_by_nickname_or_email(name)},
24 {_, true} <- {:checkpw, Pbkdf2.checkpw(password, user.password_hash)} do
33 %Plug.Conn{assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}},
36 registration = Registration.get_by_provider_uid(provider, uid)
43 Registration.changeset(%Registration{}, %{
44 provider: to_string(provider),
47 "nickname" => info.nickname,
48 "email" => info.email,
50 "description" => info.description
57 def get_registration(%Plug.Conn{} = _conn, _params), do: {:error, :missing_credentials}
59 def create_from_registration(_conn, params, registration) do
60 nickname = value([params["nickname"], Registration.nickname(registration)])
61 email = value([params["email"], Registration.email(registration)])
62 name = value([params["name"], Registration.name(registration)]) || nickname
63 bio = value([params["bio"], Registration.description(registration)])
65 random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
67 with {:ok, new_user} <-
68 User.register_changeset(
75 password: random_password,
76 password_confirmation: random_password
83 Registration.changeset(registration, %{user_id: new_user.id}) |> Repo.update() do
88 defp value(list), do: Enum.find(list, &(to_string(&1) != ""))
90 def handle_error(%Plug.Conn{} = _conn, error) do
94 def auth_template, do: nil
96 def oauth_consumer_template, do: nil