1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.AuthenticationPlugTest do
6 use Pleroma.Web.ConnCase, async: true
9 alias Pleroma.Web.Plugs.AuthenticationPlug
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
11 alias Pleroma.Web.Plugs.PlugHelper
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
16 setup %{conn: conn} do
20 password_hash: Pbkdf2.hash_pwd_salt("guy")
25 |> assign(:auth_user, user)
27 %{user: user, conn: conn}
30 test "it does nothing if a user is assigned", %{conn: conn} do
33 |> assign(:user, %User{})
37 |> AuthenticationPlug.call(%{})
39 assert ret_conn == conn
42 test "with a correct password in the credentials, " <>
43 "it assigns the auth_user and marks OAuthScopesPlug as skipped",
47 |> assign(:auth_credentials, %{password: "guy"})
48 |> AuthenticationPlug.call(%{})
50 assert conn.assigns.user == conn.assigns.auth_user
51 assert conn.assigns.token == nil
52 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
55 test "with a bcrypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
56 user = insert(:user, password_hash: Bcrypt.hash_pwd_salt("123"))
57 assert "$2" <> _ = user.password_hash
61 |> assign(:auth_user, user)
62 |> assign(:auth_credentials, %{password: "123"})
63 |> AuthenticationPlug.call(%{})
65 assert conn.assigns.user.id == conn.assigns.auth_user.id
66 assert conn.assigns.token == nil
67 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
69 user = User.get_by_id(user.id)
70 assert "$pbkdf2" <> _ = user.password_hash
74 test "with a crypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
78 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
83 |> assign(:auth_user, user)
84 |> assign(:auth_credentials, %{password: "password"})
85 |> AuthenticationPlug.call(%{})
87 assert conn.assigns.user.id == conn.assigns.auth_user.id
88 assert conn.assigns.token == nil
89 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
91 user = User.get_by_id(user.id)
92 assert "$pbkdf2" <> _ = user.password_hash
95 describe "checkpw/2" do
96 test "check pbkdf2 hash" do
98 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
100 assert AuthenticationPlug.checkpw("test-password", hash)
101 refute AuthenticationPlug.checkpw("test-password1", hash)
105 test "check sha512-crypt hash" do
107 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
109 assert AuthenticationPlug.checkpw("password", hash)
112 test "check bcrypt hash" do
113 hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
115 assert AuthenticationPlug.checkpw("password", hash)
116 refute AuthenticationPlug.checkpw("password1", hash)
119 test "it returns false when hash invalid" do
121 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
123 assert capture_log(fn ->
124 refute AuthenticationPlug.checkpw("password", hash)
125 end) =~ "[error] Password hash not recognized"