Pbkdf2: Use it everywhere.
authorLain Soykaf <lain@lain.com>
Thu, 14 Jan 2021 14:06:16 +0000 (15:06 +0100)
committerLain Soykaf <lain@lain.com>
Thu, 14 Jan 2021 14:06:16 +0000 (15:06 +0100)
21 files changed:
benchmarks/load_testing/users.ex
lib/pleroma/mfa.ex
lib/pleroma/password.ex [deleted file]
lib/pleroma/password/pbkdf2.ex
lib/pleroma/user.ex
lib/pleroma/web/plugs/authentication_plug.ex
test/pleroma/mfa_test.exs
test/pleroma/password/pbkdf2_test.exs
test/pleroma/password_test.exs [deleted file]
test/pleroma/web/auth/basic_auth_test.exs
test/pleroma/web/auth/pleroma_authenticator_test.exs
test/pleroma/web/auth/totp_authenticator_test.exs
test/pleroma/web/mongoose_im_controller_test.exs
test/pleroma/web/o_auth/ldap_authorization_test.exs
test/pleroma/web/o_auth/mfa_controller_test.exs
test/pleroma/web/o_auth/o_auth_controller_test.exs
test/pleroma/web/plugs/authentication_plug_test.exs
test/pleroma/web/twitter_api/password_controller_test.exs
test/pleroma/web/twitter_api/util_controller_test.exs
test/support/builders/user_builder.ex
test/support/factory.ex

index 1815490a4a3ca5f3a11f43fc9ebbddab193d7c7e..0a33cbfdbaf0831d97898dc7af93d013956569d6 100644 (file)
@@ -55,7 +55,7 @@ defmodule Pleroma.LoadTesting.Users do
       name: "Test テスト User #{i}",
       email: "user#{i}@example.com",
       nickname: "nick#{i}",
-      password_hash: Pleroma.Password.hash_pwd_salt("test"),
+      password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
       bio: "Tester Number #{i}",
       local: !remote
     }
index 29488c876c224317dc4fd7514039e46399de60cc..02dce7d4962ee1d2d033479304704cc251bb9226 100644 (file)
@@ -71,7 +71,7 @@ defmodule Pleroma.MFA do
   @spec generate_backup_codes(User.t()) :: {:ok, list(binary)} | {:error, String.t()}
   def generate_backup_codes(%User{} = user) do
     with codes <- BackupCodes.generate(),
-         hashed_codes <- Enum.map(codes, &Pleroma.Password.hash_pwd_salt/1),
+         hashed_codes <- Enum.map(codes, &Pleroma.Password.Pbkdf2.hash_pwd_salt/1),
          changeset <- Changeset.cast_backup_codes(user, hashed_codes),
          {:ok, _} <- User.update_and_set_cache(changeset) do
       {:ok, codes}
diff --git a/lib/pleroma/password.ex b/lib/pleroma/password.ex
deleted file mode 100644 (file)
index e962496..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.Password do
-  @moduledoc """
-  This module implements Pleroma.Password passwords in terms of Plug.Crypto.
-  """
-
-  alias Plug.Crypto.KeyGenerator
-
-  def decode64(str) do
-    str
-    |> String.replace(".", "+")
-    |> Base.decode64!(padding: false)
-  end
-
-  def encode64(bin) do
-    bin
-    |> Base.encode64(padding: false)
-    |> String.replace("+", ".")
-  end
-
-  def verify_pass(password, hash) do
-    ["pbkdf2-" <> digest, iterations, salt, hash] = String.split(hash, "$", trim: true)
-
-    salt = decode64(salt)
-
-    iterations = String.to_integer(iterations)
-
-    digest = String.to_atom(digest)
-
-    binary_hash =
-      KeyGenerator.generate(password, salt, digest: digest, iterations: iterations, length: 64)
-
-    encode64(binary_hash) == hash
-  end
-
-  def hash_pwd_salt(password, opts \\ []) do
-    salt =
-      Keyword.get_lazy(opts, :salt, fn ->
-        :crypto.strong_rand_bytes(16)
-      end)
-
-    digest = Keyword.get(opts, :digest, :sha512)
-
-    iterations =
-      Keyword.get(opts, :iterations, Pleroma.Config.get([:password, :iterations], 160_000))
-
-    binary_hash =
-      KeyGenerator.generate(password, salt, digest: digest, iterations: iterations, length: 64)
-
-    "$pbkdf2-#{digest}$#{iterations}$#{encode64(salt)}$#{encode64(binary_hash)}"
-  end
-end
index 747bc1d1d169fd33dbcf908aa2c43d11d10c598c..2fd5f4491858d08194db19cb29cccf20fcc2556a 100644 (file)
@@ -4,7 +4,7 @@
 
 defmodule Pleroma.Password.Pbkdf2 do
   @moduledoc """
-  This module implements Pleroma.Password.Pbkdf2 passwords in terms of Plug.Crypto.
+  This module implements Pbkdf2 passwords in terms of Plug.Crypto.
   """
 
   alias Plug.Crypto.KeyGenerator
index 04e6ffd51cec789d47923a68637ed54b810464bb..6a81adfd6a37f76cd73f95f39d332972f74f2174 100644 (file)
@@ -2187,7 +2187,7 @@ defmodule Pleroma.User do
   defp put_password_hash(
          %Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset
        ) do
-    change(changeset, password_hash: Pleroma.Password.hash_pwd_salt(password))
+    change(changeset, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password))
   end
 
   defp put_password_hash(changeset), do: changeset
index f7a2a3ab7f39a8bd9c908976e5502b27c7b6e14d..8d58169cf61ebf2e15ac7bbabc6b81998822cfd3 100644 (file)
@@ -48,7 +48,7 @@ defmodule Pleroma.Web.Plugs.AuthenticationPlug do
   end
 
   def checkpw(password, "$pbkdf2" <> _ = password_hash) do
-    Pleroma.Password.verify_pass(password, password_hash)
+    Pleroma.Password.Pbkdf2.verify_pass(password, password_hash)
   end
 
   def checkpw(_password, _password_hash) do
index db68fc1caa9530f2666cc8ba88e05ebeadabed14..76ba1a99d891f3bea92a4a47609d7905169f221a 100644 (file)
@@ -30,8 +30,8 @@ defmodule Pleroma.MFATest do
       {:ok, [code1, code2]} = MFA.generate_backup_codes(user)
       updated_user = refresh_record(user)
       [hash1, hash2] = updated_user.multi_factor_authentication_settings.backup_codes
-      assert Pleroma.Password.verify_pass(code1, hash1)
-      assert Pleroma.Password.verify_pass(code2, hash2)
+      assert Pleroma.Password.Pbkdf2.verify_pass(code1, hash1)
+      assert Pleroma.Password.Pbkdf2.verify_pass(code2, hash2)
     end
   end
 
index 4acbda939bedeed5bf93421a948ad2e1f080b4bb..e55348f9a92529eae9ee49d0e88c03e1a7bab958 100644 (file)
@@ -5,10 +5,10 @@
 defmodule Pleroma.Password.Pbkdf2Test do
   use Pleroma.DataCase, async: true
 
-  alias Pleroma.Password.Pbkdf2
+  alias Pleroma.Password.Pbkdf2, as: Password
 
   test "it generates the same hash as pbkd2_elixir" do
-    # hash = Pleroma.Password.Pbkdf2.hash_pwd_salt("password")
+    # hash = Pbkdf2.hash_pwd_salt("password")
     hash =
       "$pbkdf2-sha512$1$QJpEYw8iBKcnY.4Rm0eCVw$UBPeWQ91RxSv3snxsb/ZzMeG/2aa03c541bbo8vQudREGNta5t8jBQrd00fyJp8RjaqfvgdZxy2rhSwljyu21g"
 
@@ -19,14 +19,14 @@ defmodule Pleroma.Password.Pbkdf2Test do
   end
 
   @tag skip: "Works when Pbkd2 is present. Source: trust me bro"
-  test "Pleroma.Password.Pbkdf2 can verify passwords generated with it" do
-    hash = Password.hash_pwd_salt("password")
-
-    assert Pleroma.Password.Pbkdf2.verify_pass("password", hash)
+  test "Pbkdf2 can verify passwords generated with it" do
+    # Commented to prevent warnings.
+    # hash = Password.hash_pwd_salt("password")
+    # assert Pbkdf2.verify_pass("password", hash)
   end
 
   test "it verifies pbkdf2_elixir hashes" do
-    # hash = Pleroma.Password.Pbkdf2.hash_pwd_salt("password")
+    # hash = Pbkdf2.hash_pwd_salt("password")
     hash =
       "$pbkdf2-sha512$1$QJpEYw8iBKcnY.4Rm0eCVw$UBPeWQ91RxSv3snxsb/ZzMeG/2aa03c541bbo8vQudREGNta5t8jBQrd00fyJp8RjaqfvgdZxy2rhSwljyu21g"
 
diff --git a/test/pleroma/password_test.exs b/test/pleroma/password_test.exs
deleted file mode 100644 (file)
index 6ed0ca8..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.PasswordTest do
-  use Pleroma.DataCase, async: true
-
-  alias Pleroma.Password
-
-  test "it generates the same hash as pbkd2_elixir" do
-    # hash = Pleroma.Password.hash_pwd_salt("password")
-    hash =
-      "$pbkdf2-sha512$1$QJpEYw8iBKcnY.4Rm0eCVw$UBPeWQ91RxSv3snxsb/ZzMeG/2aa03c541bbo8vQudREGNta5t8jBQrd00fyJp8RjaqfvgdZxy2rhSwljyu21g"
-
-    # Use the same randomly generated salt
-    salt = Password.decode64("QJpEYw8iBKcnY.4Rm0eCVw")
-
-    assert hash == Password.hash_pwd_salt("password", salt: salt)
-  end
-
-  @tag skip: "Works when Pbkd2 is present. Source: trust me bro"
-  test "Pleroma.Password can verify passwords generated with it" do
-    hash = Password.hash_pwd_salt("password")
-
-    assert Pleroma.Password.verify_pass("password", hash)
-  end
-
-  test "it verifies pbkdf2_elixir hashes" do
-    # hash = Pleroma.Password.hash_pwd_salt("password")
-    hash =
-      "$pbkdf2-sha512$1$QJpEYw8iBKcnY.4Rm0eCVw$UBPeWQ91RxSv3snxsb/ZzMeG/2aa03c541bbo8vQudREGNta5t8jBQrd00fyJp8RjaqfvgdZxy2rhSwljyu21g"
-
-    assert Password.verify_pass("password", hash)
-  end
-end
index b74516dd647539581b24f0d0237f0a7592057b8f..2816aae4ceee1b167bb2e9cdd6946a44be1b7f18 100644 (file)
@@ -11,7 +11,7 @@ defmodule Pleroma.Web.Auth.BasicAuthTest do
     conn: conn
   } do
     user = insert(:user)
-    assert Pleroma.Password.verify_pass("test", user.password_hash)
+    assert Pleroma.Password.Pbkdf2.verify_pass("test", user.password_hash)
 
     basic_auth_contents =
       (URI.encode_www_form(user.nickname) <> ":" <> URI.encode_www_form("test"))
index ec63e2d41fba51732b35a420f33db3bdfc73492f..edaf9eecbe60619157a81dd349bfad526ca62a6e 100644 (file)
@@ -11,7 +11,7 @@ defmodule Pleroma.Web.Auth.PleromaAuthenticatorTest do
   setup do
     password = "testpassword"
     name = "AgentSmith"
-    user = insert(:user, nickname: name, password_hash: Pleroma.Password.hash_pwd_salt(password))
+    user = insert(:user, nickname: name, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password))
     {:ok, [user: user, name: name, password: password]}
   end
 
index 6d2646b617d1c10c3fba38d074e98e5af2e437a0..ac4209f2d92f16537b57ccc1c0f47738fa1ac46f 100644 (file)
@@ -34,7 +34,7 @@ defmodule Pleroma.Web.Auth.TOTPAuthenticatorTest do
 
     hashed_codes =
       backup_codes
-      |> Enum.map(&Pleroma.Password.hash_pwd_salt(&1))
+      |> Enum.map(&Pleroma.Password.Pbkdf2.hash_pwd_salt(&1))
 
     user =
       insert(:user,
index 183a17a02c452a4e2c9db4eb954a049192f11448..a7225d45c23da9108bd41aafb64a57427386b014 100644 (file)
@@ -41,13 +41,13 @@ defmodule Pleroma.Web.MongooseIMControllerTest do
   end
 
   test "/check_password", %{conn: conn} do
-    user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt("cool"))
+    user = insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("cool"))
 
     _deactivated_user =
       insert(:user,
         nickname: "konata",
         deactivated: true,
-        password_hash: Pleroma.Password.hash_pwd_salt("cool")
+        password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("cool")
       )
 
     res =
index 9ebd084a5bbd6b721d8a1f9312b9b3315e69493a..61b9ce6b76d7350155666ec04fa5052a6ae05750 100644 (file)
@@ -18,7 +18,7 @@ defmodule Pleroma.Web.OAuth.LDAPAuthorizationTest do
   @tag @skip
   test "authorizes the existing user using LDAP credentials" do
     password = "testpassword"
-    user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password))
+    user = insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password))
     app = insert(:oauth_app, scopes: ["read", "write"])
 
     host = Pleroma.Config.get([:ldap, :host]) |> to_charlist
@@ -101,7 +101,7 @@ defmodule Pleroma.Web.OAuth.LDAPAuthorizationTest do
   @tag @skip
   test "disallow authorization for wrong LDAP credentials" do
     password = "testpassword"
-    user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password))
+    user = insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password))
     app = insert(:oauth_app, scopes: ["read", "write"])
 
     host = Pleroma.Config.get([:ldap, :host]) |> to_charlist
index dacf03b2bdb9d3277a2651ec72dbb16802f65516..17bbde85b40a974ab45070afd08f6e3e2e4932f8 100644 (file)
@@ -20,7 +20,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do
       insert(:user,
         multi_factor_authentication_settings: %MFA.Settings{
           enabled: true,
-          backup_codes: [Pleroma.Password.hash_pwd_salt("test-code")],
+          backup_codes: [Pleroma.Password.Pbkdf2.hash_pwd_salt("test-code")],
           totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
         }
       )
@@ -246,7 +246,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do
 
       hashed_codes =
         backup_codes
-        |> Enum.map(&Pleroma.Password.hash_pwd_salt(&1))
+        |> Enum.map(&Pleroma.Password.Pbkdf2.hash_pwd_salt(&1))
 
       user =
         insert(:user,
index c6ee7b7e831a7fac9b16d91135aa2a3ad30b03cd..bf47afed820cf05a39562b9298784fe302b223a7 100644 (file)
@@ -316,7 +316,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
            app: app,
            conn: conn
          } do
-      user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt("testpassword"))
+      user = insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("testpassword"))
       registration = insert(:registration, user: nil)
       redirect_uri = OAuthController.default_redirect_uri(app)
 
@@ -347,7 +347,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
            app: app,
            conn: conn
          } do
-      user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt("testpassword"))
+      user = insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("testpassword"))
       registration = insert(:registration, user: nil)
       unlisted_redirect_uri = "http://cross-site-request.com"
 
@@ -790,7 +790,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
 
     test "issues a token for `password` grant_type with valid credentials, with full permissions by default" do
       password = "testpassword"
-      user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password))
+      user = insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password))
 
       app = insert(:oauth_app, scopes: ["read", "write"])
 
@@ -818,7 +818,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
 
       user =
         insert(:user,
-          password_hash: Pleroma.Password.hash_pwd_salt(password),
+          password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password),
           multi_factor_authentication_settings: %MFA.Settings{
             enabled: true,
             totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
@@ -927,7 +927,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
       password = "testpassword"
 
       {:ok, user} =
-        insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password))
+        insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password))
         |> User.confirmation_changeset(need_confirmation: true)
         |> User.update_and_set_cache()
 
@@ -955,7 +955,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
 
       user =
         insert(:user,
-          password_hash: Pleroma.Password.hash_pwd_salt(password),
+          password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password),
           deactivated: true
         )
 
@@ -983,7 +983,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
 
       user =
         insert(:user,
-          password_hash: Pleroma.Password.hash_pwd_salt(password),
+          password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password),
           password_reset_pending: true
         )
 
@@ -1012,7 +1012,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
 
       user =
         insert(:user,
-          password_hash: Pleroma.Password.hash_pwd_salt(password),
+          password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password),
           confirmation_pending: true
         )
 
@@ -1040,7 +1040,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
 
       user =
         insert(:user,
-          password_hash: Pleroma.Password.hash_pwd_salt(password),
+          password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password),
           approval_pending: true
         )
 
index 4a0ff671099f44210a9ae83bf52d5588cddcfcd7..118ab302a0c956ad1217f141a5c927e20cf3cf93 100644 (file)
@@ -17,7 +17,7 @@ defmodule Pleroma.Web.Plugs.AuthenticationPlugTest do
     user = %User{
       id: 1,
       name: "dude",
-      password_hash: Pleroma.Password.hash_pwd_salt("guy")
+      password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("guy")
     }
 
     conn =
index 880f097cb7ec23c4b13f8619bb345bcd1991b1dc..cf99e243418252239ae99b3414d23e2f59a8279f 100644 (file)
@@ -92,7 +92,7 @@ defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do
       assert response =~ "<h2>Password changed!</h2>"
 
       user = refresh_record(user)
-      assert Pleroma.Password.verify_pass("test", user.password_hash)
+      assert Pleroma.Password.Pbkdf2.verify_pass("test", user.password_hash)
       assert Enum.empty?(Token.get_user_tokens(user))
     end
 
index 923be8fae9e98e4bb934c625d0d7935123bc11d0..6d007ab66ff6d5da17782869d9a72d31e1f5c846 100644 (file)
@@ -397,7 +397,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
 
       assert json_response(conn, 200) == %{"status" => "success"}
       fetched_user = User.get_cached_by_id(user.id)
-      assert Pleroma.Password.verify_pass("newpass", fetched_user.password_hash) == true
+      assert Pleroma.Password.Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
     end
   end
 
index 27470498df2e1637faa8d020583edd7887a4fb69..6bccbb35aeea5485ea008f72a39364a08b7d6dfb 100644 (file)
@@ -7,7 +7,7 @@ defmodule Pleroma.Builders.UserBuilder do
       email: "test@example.org",
       name: "Test Name",
       nickname: "testname",
-      password_hash: Pleroma.Password.hash_pwd_salt("test"),
+      password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
       bio: "A tester.",
       ap_id: "some id",
       last_digest_emailed_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
index 53b1dfd09a04a70a8cf1047432adec3956822bb1..bf9592064ed7c63eda4d16e508c36f074f0b7e23 100644 (file)
@@ -29,7 +29,7 @@ defmodule Pleroma.Factory do
       name: sequence(:name, &"Test テスト User #{&1}"),
       email: sequence(:email, &"user#{&1}@example.com"),
       nickname: sequence(:nickname, &"nick#{&1}"),
-      password_hash: Pleroma.Password.hash_pwd_salt("test"),
+      password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
       bio: sequence(:bio, &"Tester Number #{&1}"),
       is_discoverable: true,
       last_digest_emailed_at: NaiveDateTime.utc_now(),