Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.6KB

  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. test "with a crypt hash, it updates to a pkbdf2 hash", %{conn: conn} do
  56. user =
  57. insert(:user,
  58. password_hash:
  59. "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  60. )
  61. conn =
  62. conn
  63. |> assign(:auth_user, user)
  64. |> assign(:auth_credentials, %{password: "password"})
  65. |> AuthenticationPlug.call(%{})
  66. assert conn.assigns.user.id == conn.assigns.auth_user.id
  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. @tag :skip_on_mac
  79. test "check sha512-crypt hash" do
  80. hash =
  81. "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  82. assert AuthenticationPlug.checkpw("password", hash)
  83. end
  84. test "check bcrypt hash" do
  85. hash = "$2a$10$uyhC/R/zoE1ndwwCtMusK.TLVzkQ/Ugsbqp3uXI.CTTz0gBw.24jS"
  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 Pleroma.Plugs.AuthenticationPlug.checkpw("password", hash)
  94. end) =~ "[error] Password hash not recognized"
  95. end
  96. end
  97. end