Skip to content

Commit

Permalink
Validate bytes only afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 27, 2021
1 parent 1a71939 commit eefca9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
27 changes: 21 additions & 6 deletions installer/templates/phx_gettext/en/LC_MESSAGES/errors.po
Expand Up @@ -50,13 +50,23 @@ msgid "are still associated with this entry"
msgstr ""

## From Ecto.Changeset.validate_length/3
msgid "should have %{count} item(s)"
msgid_plural "should have %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should be %{count} character(s)"
msgid_plural "should be %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have %{count} item(s)"
msgid_plural "should have %{count} item(s)"
msgid "should be %{count} byte(s)"
msgid_plural "should be %{count} byte(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at least %{count} item(s)"
msgid_plural "should have at least %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

Expand All @@ -65,8 +75,13 @@ msgid_plural "should be at least %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at least %{count} item(s)"
msgid_plural "should have at least %{count} item(s)"
msgid "should be at least %{count} byte(s)"
msgid_plural "should be at least %{count} byte(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at most %{count} item(s)"
msgid_plural "should have at most %{count} item(s)"
msgstr[0] ""
msgstr[1] ""

Expand All @@ -75,8 +90,8 @@ msgid_plural "should be at most %{count} character(s)"
msgstr[0] ""
msgstr[1] ""

msgid "should have at most %{count} item(s)"
msgid_plural "should have at most %{count} item(s)"
msgid "should be at most %{count} byte(s)"
msgid_plural "should be at most %{count} byte(s)"
msgstr[0] ""
msgstr[1] ""

Expand Down
5 changes: 4 additions & 1 deletion lib/mix/tasks/phx.gen.auth/hashing_library.ex
@@ -1,10 +1,11 @@
defmodule Mix.Tasks.Phx.Gen.Auth.HashingLibrary do
@moduledoc false

defstruct [:module, :mix_dependency, :test_config]
defstruct [:name, :module, :mix_dependency, :test_config]

def build("bcrypt") do
lib = %__MODULE__{
name: :bcrypt,
module: Bcrypt,
mix_dependency: ~s|{:bcrypt_elixir, "~> 2.0\"}|,
test_config: """
Expand All @@ -17,6 +18,7 @@ defmodule Mix.Tasks.Phx.Gen.Auth.HashingLibrary do

def build("pbkdf2") do
lib = %__MODULE__{
name: :pbkdf2,
module: Pbkdf2,
mix_dependency: ~s|{:pbkdf2_elixir, "~> 1.0\"}|,
test_config: """
Expand All @@ -29,6 +31,7 @@ defmodule Mix.Tasks.Phx.Gen.Auth.HashingLibrary do

def build("argon2") do
lib = %__MODULE__{
name: :argon2,
module: Argon2,
mix_dependency: ~s|{:argon2_elixir, "~> 2.0\"}|,
test_config: """
Expand Down
5 changes: 3 additions & 2 deletions priv/templates/phx.gen.auth/schema.ex
Expand Up @@ -48,8 +48,9 @@ defmodule <%= inspect schema.module %> do
defp validate_password(changeset, opts) do
changeset
|> validate_required([:password])
# If using Bcrypt, note it does not support passwords more than 72 bytes long
|> validate_length(:password, min: 12, max: 72, count: :bytes)
|> validate_length(:password, min: 12, max: 72)<%= if hashing_library.name == :bcrypt do %>
# If using Bcrypt, then further validate it is at most 72 bytes long
|> validate_length(:password, max: 72, count: :bytes)<% end %>
# |> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character")
# |> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character")
# |> validate_format(:password, ~r/[!?@#$%^&*_0-9]/, message: "at least one digit or punctuation character")
Expand Down

0 comments on commit eefca9b

Please sign in to comment.