这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@rzane
Copy link
Contributor

@rzane rzane commented Jan 18, 2018

Resolves #3.

Copy link
Contributor

@danielberkompas danielberkompas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me so far! I'm reading the NIST guidelines this afternoon; I'll comment I find anything more we should add.

@password_blacklist File.cwd!()
|> Path.join("password_blacklist.txt")
|> File.read!()
|> String.split("\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should probably go in priv, and I think you can read it this way:

@password_blacklist "priv/password_blacklist.txt"
                    |> File.stream!
                    |> Stream.map(&String.trim/1)
                    |> Enum.into([])

It's slightly more efficient.

@danielberkompas
Copy link
Contributor

This regular expression can be used to exclude repeated characters like "aaaaa": (.)\1{2}

To detect sequences, it looks like we'll need to write code. Elixir binary pattern matching, here we come!

@rzane
Copy link
Contributor Author

rzane commented Jan 19, 2018

Binary matching could definitely work here, but I think this might be simpler:

  @consecutive_alphanum "abcdefghijklmnopqrstuvwxyz01234567890"

  defp consecutive?(value, size) do
    blacklist = chunk_letters(@consecutive_alphanum, size)
    value |> chunk_letters(size) |> Enum.any?(&(&1 in blacklist))
  end

  defp chunk_letters(value, size) do
    value |> String.to_charlist |> Enum.chunk_every(size, 1, :discard)
  end

@danielberkompas
Copy link
Contributor

Yeah, I wrote a function using pattern matching but yours is better.

We can use your function to exclude passwords with >= 3 consecutive characters.

def validate_consecutive(changeset, field, opts \\ []) do
  value = get_change(changeset, field)
  max = opts[:max] || 3
  msg = opts[:message] || "contains more than %{max} consecutive characters"
  
  if value && consecutive?(value, max) do
    add_error(changeset, field, msg, [validation: :consecutive, max: max])
  else
    changeset
  end 
end

@@ -0,0 +1,34 @@
defmodule Authority.Ecto.Password do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps this should be marked @moduledoc false?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with that.

@rzane rzane force-pushed the validate-secure-password branch from 0d1eedb to a7c51b1 Compare January 19, 2018 01:56
@rzane rzane changed the title WIP: Validate length, blacklist and confirmation for passwords validate_secure_password Jan 19, 2018
@danielberkompas danielberkompas merged commit 0aa182b into infinitered:master Jan 19, 2018
@danielberkompas
Copy link
Contributor

We add the @moduledoc false tag when we do #6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants