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.Plugs.AuthenticationPlugTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Plugs.AuthenticationPlug
9 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.Plugs.PlugHelper
13 import ExUnit.CaptureLog
15 setup %{conn: conn} do
19 password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
24 |> assign(:auth_user, user)
26 %{user: user, conn: conn}
29 test "it does nothing if a user is assigned", %{conn: conn} do
32 |> assign(:user, %User{})
36 |> AuthenticationPlug.call(%{})
38 assert ret_conn == conn
41 test "with a correct password in the credentials, " <>
42 "it assigns the auth_user and marks OAuthScopesPlug as skipped",
46 |> assign(:auth_credentials, %{password: "guy"})
47 |> AuthenticationPlug.call(%{})
49 assert conn.assigns.user == conn.assigns.auth_user
50 assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
53 test "with a wrong password in the credentials, it does nothing", %{conn: conn} do
56 |> assign(:auth_credentials, %{password: "wrong"})
60 |> AuthenticationPlug.call(%{})
62 assert conn == ret_conn
65 describe "checkpw/2" do
66 test "check pbkdf2 hash" do
68 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
70 assert AuthenticationPlug.checkpw("test-password", hash)
71 refute AuthenticationPlug.checkpw("test-password1", hash)
75 test "check sha512-crypt hash" do
77 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
79 assert AuthenticationPlug.checkpw("password", hash)
82 test "it returns false when hash invalid" do
84 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
86 assert capture_log(fn ->
87 refute Pleroma.Plugs.AuthenticationPlug.checkpw("password", hash)
88 end) =~ "[error] Password hash not recognized"