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 import Pleroma.Web.Auth.Authenticator,
12 only: [fetch_credentials: 1, fetch_user: 1]
14 @behaviour Pleroma.Web.Auth.Authenticator
16 def get_user(%Plug.Conn{} = conn) do
17 with {:ok, {name, password}} <- fetch_credentials(conn),
18 {_, %User{} = user} <- {:user, fetch_user(name)},
19 {_, true} <- {:checkpw, Pbkdf2.checkpw(password, user.password_hash)} do
27 def get_registration(%Plug.Conn{
28 assigns: %{ueberauth_auth: %{provider: provider, uid: uid} = auth}
30 registration = Registration.get_by_provider_uid(provider, uid)
38 |> Registration.changeset(%{
39 provider: to_string(provider),
42 "nickname" => info.nickname,
43 "email" => info.email,
45 "description" => info.description
52 def get_registration(%Plug.Conn{} = _conn), do: {:error, :missing_credentials}
54 def create_from_registration(
55 %Plug.Conn{params: %{"authorization" => registration_attrs}},
58 nickname = value([registration_attrs["nickname"], Registration.nickname(registration)])
59 email = value([registration_attrs["email"], Registration.email(registration)])
60 name = value([registration_attrs["name"], Registration.name(registration)]) || nickname
61 bio = value([registration_attrs["bio"], Registration.description(registration)])
63 random_password = :crypto.strong_rand_bytes(64) |> Base.encode64()
65 with {:ok, new_user} <-
66 User.register_changeset(
73 password: random_password,
74 password_confirmation: random_password
77 need_confirmation: false
81 Registration.changeset(registration, %{user_id: new_user.id}) |> Repo.update() do
86 defp value(list), do: Enum.find(list, &(to_string(&1) != ""))
88 def handle_error(%Plug.Conn{} = _conn, error) do
92 def auth_template, do: nil
94 def oauth_consumer_template, do: nil