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) 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
32 def get_registration(%Plug.Conn{
33 assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}
35 registration = Registration.get_by_provider_uid(provider, uid)
43 |> Registration.changeset(%{
44 provider: to_string(provider),
47 "nickname" => info.nickname,
48 "email" => info.email,
50 "description" => info.description
57 def get_registration(%Plug.Conn{} = _conn), do: {:error, :missing_credentials}
59 def create_from_registration(
60 %Plug.Conn{params: %{"authorization" => registration_attrs}},
63 nickname = value([registration_attrs["nickname"], Registration.nickname(registration)])
64 email = value([registration_attrs["email"], Registration.email(registration)])
65 name = value([registration_attrs["name"], Registration.name(registration)]) || nickname
66 bio = value([registration_attrs["bio"], Registration.description(registration)])
68 random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
70 with {:ok, new_user} <-
71 User.register_changeset(
78 password: random_password,
79 password_confirmation: random_password
86 Registration.changeset(registration, %{user_id: new_user.id}) |> Repo.update() do
91 defp value(list), do: Enum.find(list, &(to_string(&1) != ""))
93 def handle_error(%Plug.Conn{} = _conn, error) do
97 def auth_template, do: nil
99 def oauth_consumer_template, do: nil