logo

pleroma

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

authentication_plug_test.exs (3736B)


  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. test "with an argon2 hash, it updates to a pkbdf2 hash", %{conn: conn} do
  58. user = insert(:user, password_hash: Argon2.hash_pwd_salt("123"))
  59. assert "$argon2" <> _ = user.password_hash
  60. conn =
  61. conn
  62. |> assign(:auth_user, user)
  63. |> assign(:auth_credentials, %{password: "123"})
  64. |> 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)
  68. user = User.get_by_id(user.id)
  69. assert "$pbkdf2" <> _ = user.password_hash
  70. end
  71. describe "checkpw/2" do
  72. test "check pbkdf2 hash" do
  73. hash =
  74. "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
  75. assert AuthenticationPlug.checkpw("test-password", hash)
  76. refute AuthenticationPlug.checkpw("test-password1", hash)
  77. end
  78. test "check bcrypt hash" do
  79. hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
  80. assert AuthenticationPlug.checkpw("password", hash)
  81. refute AuthenticationPlug.checkpw("password1", hash)
  82. end
  83. test "check argon2 hash" do
  84. hash =
  85. "$argon2id$v=19$m=65536,t=8,p=2$zEMMsTuK5KkL5AFWbX7jyQ$VyaQD7PF6e9btz0oH1YiAkWwIGZ7WNDZP8l+a/O171g"
  86. assert AuthenticationPlug.checkpw("password", hash)
  87. refute AuthenticationPlug.checkpw("password1", hash)
  88. end
  89. test "it returns false when hash invalid" do
  90. hash =
  91. "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  92. assert capture_log(fn ->
  93. refute AuthenticationPlug.checkpw("password", hash)
  94. end) =~ "[error] Password hash not recognized"
  95. end
  96. end
  97. end