Refactor User.post_register_action/1 emails
[akkoma] / test / pleroma / web / plugs / authentication_plug_test.exs
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.Plugs.AuthenticationPlugTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 alias Pleroma.User
9 alias Pleroma.Web.Plugs.AuthenticationPlug
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
11 alias Pleroma.Web.Plugs.PlugHelper
12
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
15
16 setup %{conn: conn} do
17 user = %User{
18 id: 1,
19 name: "dude",
20 password_hash: Pbkdf2.hash_pwd_salt("guy")
21 }
22
23 conn =
24 conn
25 |> assign(:auth_user, user)
26
27 %{user: user, conn: conn}
28 end
29
30 test "it does nothing if a user is assigned", %{conn: conn} do
31 conn =
32 conn
33 |> assign(:user, %User{})
34
35 ret_conn =
36 conn
37 |> AuthenticationPlug.call(%{})
38
39 assert ret_conn == conn
40 end
41
42 test "with a correct password in the credentials, " <>
43 "it assigns the auth_user and marks OAuthScopesPlug as skipped",
44 %{conn: conn} do
45 conn =
46 conn
47 |> assign(:auth_credentials, %{password: "guy"})
48 |> AuthenticationPlug.call(%{})
49
50 assert conn.assigns.user == conn.assigns.auth_user
51 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
52 end
53
54 test "with a bcrypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
55 user = insert(:user, password_hash: Bcrypt.hash_pwd_salt("123"))
56 assert "$2" <> _ = user.password_hash
57
58 conn =
59 conn
60 |> assign(:auth_user, user)
61 |> assign(:auth_credentials, %{password: "123"})
62 |> AuthenticationPlug.call(%{})
63
64 assert conn.assigns.user.id == conn.assigns.auth_user.id
65 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
66
67 user = User.get_by_id(user.id)
68 assert "$pbkdf2" <> _ = user.password_hash
69 end
70
71 @tag :skip_on_mac
72 test "with a crypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
73 user =
74 insert(:user,
75 password_hash:
76 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
77 )
78
79 conn =
80 conn
81 |> assign(:auth_user, user)
82 |> assign(:auth_credentials, %{password: "password"})
83 |> AuthenticationPlug.call(%{})
84
85 assert conn.assigns.user.id == conn.assigns.auth_user.id
86 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
87
88 user = User.get_by_id(user.id)
89 assert "$pbkdf2" <> _ = user.password_hash
90 end
91
92 describe "checkpw/2" do
93 test "check pbkdf2 hash" do
94 hash =
95 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
96
97 assert AuthenticationPlug.checkpw("test-password", hash)
98 refute AuthenticationPlug.checkpw("test-password1", hash)
99 end
100
101 @tag :skip_on_mac
102 test "check sha512-crypt hash" do
103 hash =
104 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
105
106 assert AuthenticationPlug.checkpw("password", hash)
107 end
108
109 test "check bcrypt hash" do
110 hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
111
112 assert AuthenticationPlug.checkpw("password", hash)
113 refute AuthenticationPlug.checkpw("password1", hash)
114 end
115
116 test "it returns false when hash invalid" do
117 hash =
118 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
119
120 assert capture_log(fn ->
121 refute AuthenticationPlug.checkpw("password", hash)
122 end) =~ "[error] Password hash not recognized"
123 end
124 end
125 end