c15b4bfb8198afd475f86c02198e814f0e2b29fd
[akkoma] / lib / pleroma / web / mongooseim / mongoose_im_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MongooseIM.MongooseIMController do
6 use Pleroma.Web, :controller
7
8 alias Comeonin.Pbkdf2
9 alias Pleroma.Plugs.RateLimiter
10 alias Pleroma.Repo
11 alias Pleroma.User
12
13 plug(RateLimiter, [name: :authentication] when action in [:user_exists, :check_password])
14 plug(RateLimiter, [name: :authentication, params: ["user"]] when action == :check_password)
15
16 def user_exists(conn, %{"user" => username}) do
17 with %User{} <- Repo.get_by(User, nickname: username, local: true) do
18 conn
19 |> json(true)
20 else
21 _ ->
22 conn
23 |> put_status(:not_found)
24 |> json(false)
25 end
26 end
27
28 def check_password(conn, %{"user" => username, "pass" => password}) do
29 user = Repo.get_by(User, nickname: username, local: true)
30
31 state = case user do
32 nil -> nil
33 _ -> User.account_status(user)
34 end
35
36 case state do
37 :deactivated ->
38 conn
39 |> put_status(:not_found)
40 |> json(false)
41
42 :confirmation_pending ->
43 conn
44 |> put_status(:not_found)
45 |> json(false)
46
47 _ ->
48 with %User{password_hash: password_hash} <-
49 user,
50 true <- Pbkdf2.checkpw(password, password_hash) do
51 conn
52 |> json(true)
53 else
54 false ->
55 conn
56 |> put_status(:forbidden)
57 |> json(false)
58
59 _ ->
60 conn
61 |> put_status(:not_found)
62 |> json(false)
63 end
64 end
65 end
66 end