logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

authentication_plug.ex (1644B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Plugs.AuthenticationPlug do
  5. @moduledoc "Password authentication plug."
  6. alias Pleroma.Helpers.AuthHelper
  7. alias Pleroma.User
  8. import Plug.Conn
  9. require Logger
  10. def init(options), do: options
  11. def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
  12. def call(
  13. %{
  14. assigns: %{
  15. auth_user: %{password_hash: password_hash} = auth_user,
  16. auth_credentials: %{password: password}
  17. }
  18. } = conn,
  19. _
  20. ) do
  21. if checkpw(password, password_hash) do
  22. {:ok, auth_user} = maybe_update_password(auth_user, password)
  23. conn
  24. |> assign(:user, auth_user)
  25. |> AuthHelper.skip_oauth()
  26. else
  27. conn
  28. end
  29. end
  30. def call(conn, _), do: conn
  31. def checkpw(password, "$2" <> _ = password_hash) do
  32. # Handle bcrypt passwords for Mastodon migration
  33. Bcrypt.verify_pass(password, password_hash)
  34. end
  35. def checkpw(password, "$pbkdf2" <> _ = password_hash) do
  36. Pleroma.Password.Pbkdf2.verify_pass(password, password_hash)
  37. end
  38. def checkpw(_password, _password_hash) do
  39. Logger.error("Password hash not recognized")
  40. false
  41. end
  42. def maybe_update_password(%User{password_hash: "$2" <> _} = user, password) do
  43. do_update_password(user, password)
  44. end
  45. def maybe_update_password(user, _), do: {:ok, user}
  46. defp do_update_password(user, password) do
  47. User.reset_password(user, %{password: password, password_confirmation: password})
  48. end
  49. end