logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

authentication_plug_test.exs (3672B)


  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. defmodule Pleroma.Plugs.AuthenticationPlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.Plugs.AuthenticationPlug
  7. alias Pleroma.Plugs.OAuthScopesPlug
  8. alias Pleroma.Plugs.PlugHelper
  9. alias Pleroma.User
  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: 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 PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  41. end
  42. test "with a bcrypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
  43. user = insert(:user, password_hash: Bcrypt.hash_pwd_salt("123"))
  44. assert "$2" <> _ = user.password_hash
  45. conn =
  46. conn
  47. |> assign(:auth_user, user)
  48. |> assign(:auth_credentials, %{password: "123"})
  49. |> AuthenticationPlug.call(%{})
  50. assert conn.assigns.user.id == conn.assigns.auth_user.id
  51. assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  52. user = User.get_by_id(user.id)
  53. assert "$pbkdf2" <> _ = user.password_hash
  54. end
  55. @tag :skip_on_mac
  56. test "with a crypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
  57. user =
  58. insert(:user,
  59. password_hash:
  60. "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  61. )
  62. conn =
  63. conn
  64. |> assign(:auth_user, user)
  65. |> assign(:auth_credentials, %{password: "password"})
  66. |> AuthenticationPlug.call(%{})
  67. assert conn.assigns.user.id == conn.assigns.auth_user.id
  68. assert PlugHelper.plug_skipped?(conn, OAuthScopesPlug)
  69. user = User.get_by_id(user.id)
  70. assert "$pbkdf2" <> _ = user.password_hash
  71. end
  72. describe "checkpw/2" do
  73. test "check pbkdf2 hash" do
  74. hash =
  75. "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
  76. assert AuthenticationPlug.checkpw("test-password", hash)
  77. refute AuthenticationPlug.checkpw("test-password1", hash)
  78. end
  79. @tag :skip_on_mac
  80. test "check sha512-crypt hash" do
  81. hash =
  82. "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  83. assert AuthenticationPlug.checkpw("password", hash)
  84. end
  85. test "check bcrypt hash" do
  86. hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
  87. assert AuthenticationPlug.checkpw("password", hash)
  88. refute AuthenticationPlug.checkpw("password1", hash)
  89. end
  90. test "it returns false when hash invalid" do
  91. hash =
  92. "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  93. assert capture_log(fn ->
  94. refute Pleroma.Plugs.AuthenticationPlug.checkpw("password", hash)
  95. end) =~ "[error] Password hash not recognized"
  96. end
  97. end
  98. end