From 9106048c6191b4b16037980655514d9b5e430023 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 13 Jan 2021 15:11:11 +0100 Subject: [PATCH] Password: Replace Pbkdf2 with Password. --- benchmarks/load_testing/users.ex | 2 +- lib/pleroma/mfa.ex | 2 +- lib/pleroma/user.ex | 2 +- lib/pleroma/web/plugs/authentication_plug.ex | 2 +- mix.exs | 1 - test/pleroma/mfa_test.exs | 4 ++-- test/pleroma/web/auth/basic_auth_test.exs | 2 +- .../web/auth/pleroma_authenticator_test.exs | 2 +- .../web/auth/totp_authenticator_test.exs | 2 +- .../web/mongoose_im_controller_test.exs | 4 ++-- .../web/o_auth/ldap_authorization_test.exs | 4 ++-- .../pleroma/web/o_auth/mfa_controller_test.exs | 4 ++-- .../web/o_auth/o_auth_controller_test.exs | 18 +++++++++--------- .../web/plugs/authentication_plug_test.exs | 2 +- .../twitter_api/password_controller_test.exs | 2 +- .../web/twitter_api/util_controller_test.exs | 2 +- test/support/builders/user_builder.ex | 2 +- test/support/factory.ex | 2 +- 18 files changed, 29 insertions(+), 30 deletions(-) diff --git a/benchmarks/load_testing/users.ex b/benchmarks/load_testing/users.ex index 34a904ac2..1815490a4 100644 --- a/benchmarks/load_testing/users.ex +++ b/benchmarks/load_testing/users.ex @@ -55,7 +55,7 @@ defp generate_user(i) do name: "Test テスト User #{i}", email: "user#{i}@example.com", nickname: "nick#{i}", - password_hash: Pbkdf2.hash_pwd_salt("test"), + password_hash: Pleroma.Password.hash_pwd_salt("test"), bio: "Tester Number #{i}", local: !remote } diff --git a/lib/pleroma/mfa.ex b/lib/pleroma/mfa.ex index f43e03a54..29488c876 100644 --- a/lib/pleroma/mfa.ex +++ b/lib/pleroma/mfa.ex @@ -71,7 +71,7 @@ def invalidate_backup_code(%User{} = user, hash_code) 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, &Pbkdf2.hash_pwd_salt/1), + hashed_codes <- Enum.map(codes, &Pleroma.Password.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/user.ex b/lib/pleroma/user.ex index cd0c64acc..04e6ffd51 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2187,7 +2187,7 @@ def get_ap_ids_by_nicknames(nicknames) do defp put_password_hash( %Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset ) do - change(changeset, password_hash: Pbkdf2.hash_pwd_salt(password)) + change(changeset, password_hash: Pleroma.Password.hash_pwd_salt(password)) end defp put_password_hash(changeset), do: changeset diff --git a/lib/pleroma/web/plugs/authentication_plug.ex b/lib/pleroma/web/plugs/authentication_plug.ex index c3e13858a..f7a2a3ab7 100644 --- a/lib/pleroma/web/plugs/authentication_plug.ex +++ b/lib/pleroma/web/plugs/authentication_plug.ex @@ -48,7 +48,7 @@ def checkpw(password, "$2" <> _ = password_hash) do end def checkpw(password, "$pbkdf2" <> _ = password_hash) do - Pbkdf2.verify_pass(password, password_hash) + Pleroma.Password.verify_pass(password, password_hash) end def checkpw(_password, _password_hash) do diff --git a/mix.exs b/mix.exs index f26a5391a..14448f12f 100644 --- a/mix.exs +++ b/mix.exs @@ -125,7 +125,6 @@ defp deps do {:postgrex, ">= 0.15.5"}, {:oban, "~> 2.1.0"}, {:gettext, "~> 0.18"}, - {:pbkdf2_elixir, "~> 1.2"}, {:bcrypt_elixir, "~> 2.2"}, {:trailing_format_plug, "~> 0.0.7"}, {:fast_sanitize, "~> 0.2.0"}, diff --git a/test/pleroma/mfa_test.exs b/test/pleroma/mfa_test.exs index 29e478892..db68fc1ca 100644 --- a/test/pleroma/mfa_test.exs +++ b/test/pleroma/mfa_test.exs @@ -30,8 +30,8 @@ test "returns backup codes" 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 Pbkdf2.verify_pass(code1, hash1) - assert Pbkdf2.verify_pass(code2, hash2) + assert Pleroma.Password.verify_pass(code1, hash1) + assert Pleroma.Password.verify_pass(code2, hash2) end end diff --git a/test/pleroma/web/auth/basic_auth_test.exs b/test/pleroma/web/auth/basic_auth_test.exs index f732c7e27..b74516dd6 100644 --- a/test/pleroma/web/auth/basic_auth_test.exs +++ b/test/pleroma/web/auth/basic_auth_test.exs @@ -11,7 +11,7 @@ test "with HTTP Basic Auth used, grants access to OAuth scope-restricted endpoin conn: conn } do user = insert(:user) - assert Pbkdf2.verify_pass("test", user.password_hash) + assert Pleroma.Password.verify_pass("test", user.password_hash) basic_auth_contents = (URI.encode_www_form(user.nickname) <> ":" <> URI.encode_www_form("test")) diff --git a/test/pleroma/web/auth/pleroma_authenticator_test.exs b/test/pleroma/web/auth/pleroma_authenticator_test.exs index 477cf26ed..ec63e2d41 100644 --- a/test/pleroma/web/auth/pleroma_authenticator_test.exs +++ b/test/pleroma/web/auth/pleroma_authenticator_test.exs @@ -11,7 +11,7 @@ defmodule Pleroma.Web.Auth.PleromaAuthenticatorTest do setup do password = "testpassword" name = "AgentSmith" - user = insert(:user, nickname: name, password_hash: Pbkdf2.hash_pwd_salt(password)) + user = insert(:user, nickname: name, password_hash: Pleroma.Password.hash_pwd_salt(password)) {:ok, [user: user, name: name, password: password]} end diff --git a/test/pleroma/web/auth/totp_authenticator_test.exs b/test/pleroma/web/auth/totp_authenticator_test.exs index 583612454..6d2646b61 100644 --- a/test/pleroma/web/auth/totp_authenticator_test.exs +++ b/test/pleroma/web/auth/totp_authenticator_test.exs @@ -34,7 +34,7 @@ test "checks backup codes" do hashed_codes = backup_codes - |> Enum.map(&Pbkdf2.hash_pwd_salt(&1)) + |> Enum.map(&Pleroma.Password.hash_pwd_salt(&1)) user = insert(:user, diff --git a/test/pleroma/web/mongoose_im_controller_test.exs b/test/pleroma/web/mongoose_im_controller_test.exs index 031db53c8..183a17a02 100644 --- a/test/pleroma/web/mongoose_im_controller_test.exs +++ b/test/pleroma/web/mongoose_im_controller_test.exs @@ -41,13 +41,13 @@ test "/user_exists", %{conn: conn} do end test "/check_password", %{conn: conn} do - user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt("cool")) + user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt("cool")) _deactivated_user = insert(:user, nickname: "konata", deactivated: true, - password_hash: Pbkdf2.hash_pwd_salt("cool") + password_hash: Pleroma.Password.hash_pwd_salt("cool") ) res = diff --git a/test/pleroma/web/o_auth/ldap_authorization_test.exs b/test/pleroma/web/o_auth/ldap_authorization_test.exs index 4a2e940fd..9ebd084a5 100644 --- a/test/pleroma/web/o_auth/ldap_authorization_test.exs +++ b/test/pleroma/web/o_auth/ldap_authorization_test.exs @@ -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: Pbkdf2.hash_pwd_salt(password)) + user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password)) app = insert(:oauth_app, scopes: ["read", "write"]) host = Pleroma.Config.get([:ldap, :host]) |> to_charlist @@ -101,7 +101,7 @@ test "creates a new user after successful LDAP authorization" do @tag @skip test "disallow authorization for wrong LDAP credentials" do password = "testpassword" - user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password)) + user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password)) app = insert(:oauth_app, scopes: ["read", "write"]) host = Pleroma.Config.get([:ldap, :host]) |> to_charlist diff --git a/test/pleroma/web/o_auth/mfa_controller_test.exs b/test/pleroma/web/o_auth/mfa_controller_test.exs index 9fc1e0ec2..dacf03b2b 100644 --- a/test/pleroma/web/o_auth/mfa_controller_test.exs +++ b/test/pleroma/web/o_auth/mfa_controller_test.exs @@ -20,7 +20,7 @@ defmodule Pleroma.Web.OAuth.MFAControllerTest do insert(:user, multi_factor_authentication_settings: %MFA.Settings{ enabled: true, - backup_codes: [Pbkdf2.hash_pwd_salt("test-code")], + backup_codes: [Pleroma.Password.hash_pwd_salt("test-code")], totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true} } ) @@ -246,7 +246,7 @@ test "returns access token with valid code", %{conn: conn, app: app} do hashed_codes = backup_codes - |> Enum.map(&Pbkdf2.hash_pwd_salt(&1)) + |> Enum.map(&Pleroma.Password.hash_pwd_salt(&1)) user = insert(:user, diff --git a/test/pleroma/web/o_auth/o_auth_controller_test.exs b/test/pleroma/web/o_auth/o_auth_controller_test.exs index f01fdf660..1c5438cc2 100644 --- a/test/pleroma/web/o_auth/o_auth_controller_test.exs +++ b/test/pleroma/web/o_auth/o_auth_controller_test.exs @@ -316,7 +316,7 @@ test "with valid params, POST /oauth/register?op=connect redirects to `redirect_ app: app, conn: conn } do - user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt("testpassword")) + user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt("testpassword")) registration = insert(:registration, user: nil) redirect_uri = OAuthController.default_redirect_uri(app) @@ -347,7 +347,7 @@ test "with unlisted `redirect_uri`, POST /oauth/register?op=connect results in H app: app, conn: conn } do - user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt("testpassword")) + user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt("testpassword")) registration = insert(:registration, user: nil) unlisted_redirect_uri = "http://cross-site-request.com" @@ -790,7 +790,7 @@ test "issues a token for an all-body request" 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: Pbkdf2.hash_pwd_salt(password)) + user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password)) app = insert(:oauth_app, scopes: ["read", "write"]) @@ -818,7 +818,7 @@ test "issues a mfa token for `password` grant_type, when MFA enabled" do user = insert(:user, - password_hash: Pbkdf2.hash_pwd_salt(password), + password_hash: Pleroma.Password.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 @@ test "rejects token exchange for valid credentials belonging to unconfirmed user password = "testpassword" {:ok, user} = - insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password)) + insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password)) |> User.confirmation_changeset(need_confirmation: true) |> User.update_and_set_cache() @@ -955,7 +955,7 @@ test "rejects token exchange for valid credentials belonging to deactivated user user = insert(:user, - password_hash: Pbkdf2.hash_pwd_salt(password), + password_hash: Pleroma.Password.hash_pwd_salt(password), deactivated: true ) @@ -983,7 +983,7 @@ test "rejects token exchange for user with password_reset_pending set to true" d user = insert(:user, - password_hash: Pbkdf2.hash_pwd_salt(password), + password_hash: Pleroma.Password.hash_pwd_salt(password), password_reset_pending: true ) @@ -1012,7 +1012,7 @@ test "rejects token exchange for user with confirmation_pending set to true" do user = insert(:user, - password_hash: Pbkdf2.hash_pwd_salt(password), + password_hash: Pleroma.Password.hash_pwd_salt(password), confirmation_pending: true ) @@ -1038,7 +1038,7 @@ test "rejects token exchange for user with confirmation_pending set to true" do test "rejects token exchange for valid credentials belonging to an unapproved user" do password = "testpassword" - user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password), approval_pending: true) + user = insert(:user, password_hash: Pleroma.Password.hash_pwd_salt(password), approval_pending: true) refute Pleroma.User.account_status(user) == :active diff --git a/test/pleroma/web/plugs/authentication_plug_test.exs b/test/pleroma/web/plugs/authentication_plug_test.exs index 9d66681ce..4a0ff6710 100644 --- a/test/pleroma/web/plugs/authentication_plug_test.exs +++ b/test/pleroma/web/plugs/authentication_plug_test.exs @@ -17,7 +17,7 @@ defmodule Pleroma.Web.Plugs.AuthenticationPlugTest do user = %User{ id: 1, name: "dude", - password_hash: Pbkdf2.hash_pwd_salt("guy") + password_hash: Pleroma.Password.hash_pwd_salt("guy") } conn = diff --git a/test/pleroma/web/twitter_api/password_controller_test.exs b/test/pleroma/web/twitter_api/password_controller_test.exs index a552af4c3..880f097cb 100644 --- a/test/pleroma/web/twitter_api/password_controller_test.exs +++ b/test/pleroma/web/twitter_api/password_controller_test.exs @@ -92,7 +92,7 @@ test "it returns HTTP 200", %{conn: conn} do assert response =~ "

Password changed!

" user = refresh_record(user) - assert Pbkdf2.verify_pass("test", user.password_hash) + assert Pleroma.Password.verify_pass("test", user.password_hash) assert Enum.empty?(Token.get_user_tokens(user)) end diff --git a/test/pleroma/web/twitter_api/util_controller_test.exs b/test/pleroma/web/twitter_api/util_controller_test.exs index 882122848..923be8fae 100644 --- a/test/pleroma/web/twitter_api/util_controller_test.exs +++ b/test/pleroma/web/twitter_api/util_controller_test.exs @@ -397,7 +397,7 @@ test "with proper permissions, valid password and matching new password and conf assert json_response(conn, 200) == %{"status" => "success"} fetched_user = User.get_cached_by_id(user.id) - assert Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true + assert Pleroma.Password.verify_pass("newpass", fetched_user.password_hash) == true end end diff --git a/test/support/builders/user_builder.ex b/test/support/builders/user_builder.ex index 0c687c029..27470498d 100644 --- a/test/support/builders/user_builder.ex +++ b/test/support/builders/user_builder.ex @@ -7,7 +7,7 @@ def build(data \\ %{}) do email: "test@example.org", name: "Test Name", nickname: "testname", - password_hash: Pbkdf2.hash_pwd_salt("test"), + password_hash: Pleroma.Password.hash_pwd_salt("test"), bio: "A tester.", ap_id: "some id", last_digest_emailed_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second), diff --git a/test/support/factory.ex b/test/support/factory.ex index bf7121901..53b1dfd09 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -29,7 +29,7 @@ def user_factory(attrs \\ %{}) do name: sequence(:name, &"Test テスト User #{&1}"), email: sequence(:email, &"user#{&1}@example.com"), nickname: sequence(:nickname, &"nick#{&1}"), - password_hash: Pbkdf2.hash_pwd_salt("test"), + password_hash: Pleroma.Password.hash_pwd_salt("test"), bio: sequence(:bio, &"Tester Number #{&1}"), is_discoverable: true, last_digest_emailed_at: NaiveDateTime.utc_now(),