logo

pleroma

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

password_controller.ex (1468B)


  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.TwitterAPI.PasswordController do
  5. @moduledoc """
  6. The module contains functions for password reset.
  7. """
  8. use Pleroma.Web, :controller
  9. require Logger
  10. import Pleroma.Web.ControllerHelper, only: [json_response: 3]
  11. alias Pleroma.PasswordResetToken
  12. alias Pleroma.Repo
  13. alias Pleroma.User
  14. alias Pleroma.Web.TwitterAPI.TwitterAPI
  15. plug(Pleroma.Web.Plugs.RateLimiter, [name: :request] when action == :request)
  16. @doc "POST /auth/password"
  17. def request(conn, params) do
  18. nickname_or_email = params["email"] || params["nickname"]
  19. TwitterAPI.password_reset(nickname_or_email)
  20. json_response(conn, :no_content, "")
  21. end
  22. def reset(conn, %{"token" => token}) do
  23. with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
  24. false <- PasswordResetToken.expired?(token),
  25. %User{} = user <- User.get_cached_by_id(token.user_id) do
  26. render(conn, "reset.html", %{
  27. token: token,
  28. user: user
  29. })
  30. else
  31. _e -> render(conn, "invalid_token.html")
  32. end
  33. end
  34. def do_reset(conn, %{"data" => data}) do
  35. with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
  36. render(conn, "reset_success.html")
  37. else
  38. _e -> render(conn, "reset_failed.html")
  39. end
  40. end
  41. end