logo

pleroma

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

authentication_plug_test.exs (2866B)


  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.Plugs.AuthenticationPlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.User
  7. alias Pleroma.Web.Plugs.AuthenticationPlug
  8. alias Pleroma.Web.Plugs.OAuthScopesPlug
  9. alias Pleroma.Web.Plugs.PlugHelper
  10. import ExUnit.CaptureLog
  11. import Pleroma.Factory
  12. setup %{conn: conn} do
  13. user = %User{
  14. id: 1,
  15. name: "dude",
  16. password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("guy")
  17. }
  18. conn =
  19. conn
  20. |> assign(:auth_user, user)
  21. %{user: user, conn: conn}
  22. end
  23. test "it does nothing if a user is assigned", %{conn: conn} do
  24. conn =
  25. conn
  26. |> assign(:user, %User{})
  27. ret_conn =
  28. conn
  29. |> AuthenticationPlug.call(%{})
  30. assert ret_conn == conn
  31. end
  32. test "with a correct password in the credentials, " <>
  33. "it assigns the auth_user and marks OAuthScopesPlug as skipped",
  34. %{conn: conn} do
  35. conn =
  36. conn
  37. |> assign(:auth_credentials, %{password: "guy"})
  38. |> AuthenticationPlug.call(%{})
  39. assert conn.assigns.user == conn.assigns.auth_user
  40. assert conn.assigns.token == nil
  41. assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  42. end
  43. test "with a bcrypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
  44. user = insert(:user, password_hash: Bcrypt.hash_pwd_salt("123"))
  45. assert "$2" <> _ = user.password_hash
  46. conn =
  47. conn
  48. |> assign(:auth_user, user)
  49. |> assign(:auth_credentials, %{password: "123"})
  50. |> AuthenticationPlug.call(%{})
  51. assert conn.assigns.user.id == conn.assigns.auth_user.id
  52. assert conn.assigns.token == nil
  53. assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  54. user = User.get_by_id(user.id)
  55. assert "$pbkdf2" <> _ = user.password_hash
  56. end
  57. describe "checkpw/2" do
  58. test "check pbkdf2 hash" do
  59. hash =
  60. "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
  61. assert AuthenticationPlug.checkpw("test-password", hash)
  62. refute AuthenticationPlug.checkpw("test-password1", hash)
  63. end
  64. test "check bcrypt hash" do
  65. hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
  66. assert AuthenticationPlug.checkpw("password", hash)
  67. refute AuthenticationPlug.checkpw("password1", hash)
  68. end
  69. test "it returns false when hash invalid" do
  70. hash =
  71. "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  72. assert capture_log(fn ->
  73. refute AuthenticationPlug.checkpw("password", hash)
  74. end) =~ "[error] Password hash not recognized"
  75. end
  76. end
  77. end