Skip to content

Commit 6cc5911

Browse files
authored
Merge pull request #715 from pow-auth/deprecate-password-hash-methods
Deprecate `password_hash_methods`
2 parents 16151ae + 59f04e0 commit 6cc5911

File tree

7 files changed

+36
-14
lines changed

7 files changed

+36
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v1.0.35 (TBA)
4+
5+
### Deprecations
6+
7+
* [`Pow.Ecto.Schema.Changeset`] Deprecated `:password_hash_methods` in favor of `:password_hash_verify`
8+
39
## v1.0.34 (2023-09-18)
410

511
**Note:** This release contains an important security fix. It is recommended to update immediately if you are using the `Pow.Store.Backend.MnesiaCache`.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ You can change the password hashing function easily. For example, this is how yo
404404
defmodule MyApp.Users.User do
405405
use Ecto.Schema
406406
use Pow.Ecto.Schema,
407-
password_hash_methods: {&Argon2.hash_pwd_salt/1,
408-
&Argon2.verify_pass/2}
407+
password_hash_verify: {&Argon2.hash_pwd_salt/1,
408+
&Argon2.verify_pass/2}
409409

410410
# ...
411411
end

guides/production_checklist.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ You can easily change the password hashing method in Pow. Here's how you can use
6565
defmodule MyApp.Users.User do
6666
use Ecto.Schema
6767
use Pow.Ecto.Schema,
68-
password_hash_methods: {&Argon2.hash_pwd_salt/1,
69-
&Argon2.verify_pass/2}
68+
password_hash_verify: {&Argon2.hash_pwd_salt/1,
69+
&Argon2.verify_pass/2}
7070

7171
# ...
7272
end

lib/pow/ecto/schema.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule Pow.Ecto.Schema do
3535
use Ecto.Schema
3636
use Pow.Ecto.Schema,
3737
user_id_field: :email,
38-
password_hash_methods: {&Pow.Ecto.Schema.Password.pbkdf2_hash/1,
38+
password_hash_verify: {&Pow.Ecto.Schema.Password.pbkdf2_hash/1,
3939
&Pow.Ecto.Schema.Password.pbkdf2_verify/2},
4040
password_min_length: 8,
4141
password_max_length: 4096

lib/pow/ecto/schema/changeset.ex

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule Pow.Ecto.Schema.Changeset do
1212
1313
* `:password_min_length` - minimum password length, defaults to 8
1414
* `:password_max_length` - maximum password length, defaults to 4096
15-
* `:password_hash_methods` - the password hash and verify functions to use,
15+
* `:password_hash_verify` - the password hash and verify functions to use,
1616
defaults to:
1717
1818
{&Pow.Ecto.Schema.Password.pbkdf2_hash/1,
@@ -72,7 +72,7 @@ defmodule Pow.Ecto.Schema.Changeset do
7272
@doc """
7373
Validates the password field.
7474
75-
A password hash is generated by using `:password_hash_methods` in the
75+
A password hash is generated by using `:password_hash_verify` in the
7676
configuration. The password is always required if the password hash is `nil`,
7777
and it's required to be between `:password_min_length` to
7878
`:password_max_length` characters long.
@@ -206,11 +206,11 @@ defmodule Pow.Ecto.Schema.Changeset do
206206
@doc """
207207
Verifies a password in a struct.
208208
209-
The password will be verified by using the `:password_hash_methods` in the
209+
The password will be verified by using the `:password_hash_verify` in the
210210
configuration.
211211
212212
To prevent timing attacks, a blank password will be passed to the hash method
213-
in the `:password_hash_methods` configuration option if the `:password_hash`
213+
in the `:password_hash_verify` configuration option if the `:password_hash`
214214
is `nil`.
215215
"""
216216
@spec verify_password(Ecto.Schema.t(), binary(), Config.t()) :: boolean()
@@ -277,7 +277,23 @@ defmodule Pow.Ecto.Schema.Changeset do
277277
end
278278

279279
defp get_password_hash_functions(config) do
280-
Config.get(config, :password_hash_methods, {&Password.pbkdf2_hash/1, &Password.pbkdf2_verify/2})
280+
case Config.get(config, :password_hash_verify) do
281+
nil -> fallback_get_password_hash(config) || {&Password.pbkdf2_hash/1, &Password.pbkdf2_verify/2}
282+
functions -> functions
283+
end
284+
end
285+
286+
# TODO: Remove by 1.1.0
287+
defp fallback_get_password_hash(config) do
288+
case Config.get(config, :password_hash_methods) do
289+
nil ->
290+
nil
291+
292+
functions ->
293+
IO.warn("use of `:password_hash_methods` config value is deprecated, use `:password_hash_verify` instead")
294+
295+
functions
296+
end
281297
end
282298

283299
defp get_email_validator(config) do

test/pow/ecto/context_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Pow.Ecto.ContextTest do
55
defmodule TimingAttackUser do
66
@moduledoc false
77
use Ecto.Schema
8-
use Pow.Ecto.Schema, password_hash_methods: {&__MODULE__.send_hash_password/1, &__MODULE__.send_verify_password/2}
8+
use Pow.Ecto.Schema, password_hash_verify: {&__MODULE__.send_hash_password/1, &__MODULE__.send_verify_password/2}
99

1010
@ecto_derive_inspect_for_redacted_fields false
1111

test/pow/ecto/schema/changeset_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ defmodule Pow.Ecto.Schema.ChangesetTest do
177177
refute changeset.valid?
178178
refute changeset.errors[:password_hash]
179179

180-
config = [password_hash_methods: {fn _ -> nil end, & &1}]
180+
config = [password_hash_verify: {fn _ -> nil end, & &1}]
181181
changeset = Changeset.password_changeset(%User{}, @valid_params, config)
182182

183183
refute changeset.valid?
@@ -213,7 +213,7 @@ defmodule Pow.Ecto.Schema.ChangesetTest do
213213
test "can use custom password hash functions" do
214214
password_hash = &(&1 <> "123")
215215
password_verify = &(&1 == &2 <> "123")
216-
config = [password_hash_methods: {password_hash, password_verify}]
216+
config = [password_hash_verify: {password_hash, password_verify}]
217217

218218
changeset = Changeset.password_changeset(%User{}, @valid_params, config)
219219

@@ -263,7 +263,7 @@ defmodule Pow.Ecto.Schema.ChangesetTest do
263263

264264
test "prevents timing attacks" do
265265
config = [
266-
password_hash_methods: {
266+
password_hash_verify: {
267267
fn password ->
268268
send(self(), {:password_hash, password})
269269

0 commit comments

Comments
 (0)