logo

pleroma

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

mongoose_im_controller.ex (1336B)


  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.MongooseIM.MongooseIMController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Repo
  7. alias Pleroma.User
  8. alias Pleroma.Web.Plugs.AuthenticationPlug
  9. alias Pleroma.Web.Plugs.RateLimiter
  10. plug(RateLimiter, [name: :authentication] when action in [:user_exists, :check_password])
  11. plug(RateLimiter, [name: :authentication, params: ["user"]] when action == :check_password)
  12. def user_exists(conn, %{"user" => username}) do
  13. with %User{} <- Repo.get_by(User, nickname: username, local: true, is_active: true) do
  14. conn
  15. |> json(true)
  16. else
  17. _ ->
  18. conn
  19. |> put_status(:not_found)
  20. |> json(false)
  21. end
  22. end
  23. def check_password(conn, %{"user" => username, "pass" => password}) do
  24. with %User{password_hash: password_hash, is_active: true} <-
  25. Repo.get_by(User, nickname: username, local: true),
  26. true <- AuthenticationPlug.checkpw(password, password_hash) do
  27. conn
  28. |> json(true)
  29. else
  30. false ->
  31. conn
  32. |> put_status(:forbidden)
  33. |> json(false)
  34. _ ->
  35. conn
  36. |> put_status(:not_found)
  37. |> json(false)
  38. end
  39. end
  40. end