diff options
author | Simponic <loganhunt@simponic.xyz> | 2022-12-19 20:45:01 -0700 |
---|---|---|
committer | Simponic <loganhunt@simponic.xyz> | 2022-12-19 20:45:01 -0700 |
commit | 110eb0b1990d5d5ee77f9368a9b7653cfdc07131 (patch) | |
tree | bfe5744255cdfbdc9412d148236a2bf5f8aad7c1 /lib | |
parent | fe661a935ac887b11aec31ff049ace2db7ccbf84 (diff) | |
download | chessh-110eb0b1990d5d5ee77f9368a9b7653cfdc07131.tar.gz chessh-110eb0b1990d5d5ee77f9368a9b7653cfdc07131.zip |
Implement public key and add tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chessh/auth/keys.ex | 17 | ||||
-rw-r--r-- | lib/chessh/auth/password.ex | 4 | ||||
-rw-r--r-- | lib/chessh/schema/key.ex | 1 | ||||
-rw-r--r-- | lib/chessh/schema/player.ex | 2 |
4 files changed, 17 insertions, 7 deletions
diff --git a/lib/chessh/auth/keys.ex b/lib/chessh/auth/keys.ex index d85f4a4..a948fdf 100644 --- a/lib/chessh/auth/keys.ex +++ b/lib/chessh/auth/keys.ex @@ -1,10 +1,19 @@ defmodule Chessh.Auth.KeyAuthenticator do + alias Chessh.Key + alias Chessh.Repo use Sshd.PublicKeyAuthenticator require Logger + import Ecto.Query - def authenticate(username, public_key, _opts) do - Logger.debug("#{inspect(username)}") - Logger.debug("#{inspect(public_key)}") - true + def authenticate(username, public_key) do + !!Repo.one( + from(k in Key, + join: p in assoc(k, :player), + where: k.key == ^Key.encode_key(public_key), + where: p.username == ^String.Chars.to_string(username) + ) + ) end + + def authenticate(username, public_key, _opts), do: authenticate(username, public_key) end diff --git a/lib/chessh/auth/password.ex b/lib/chessh/auth/password.ex index a6fa73d..48a4d62 100644 --- a/lib/chessh/auth/password.ex +++ b/lib/chessh/auth/password.ex @@ -2,11 +2,11 @@ defmodule Chessh.Auth.PasswordAuthenticator do alias Chessh.Player alias Chessh.Repo use Sshd.PasswordAuthenticator + require Logger def authenticate(username, password) do case Repo.get_by(Player, username: String.Chars.to_string(username)) do - nil -> false - x -> Player.valid_password?(x, password) + x -> Player.valid_password?(x, String.Chars.to_string(password)) end end end diff --git a/lib/chessh/schema/key.ex b/lib/chessh/schema/key.ex index f8c14cf..765c83b 100644 --- a/lib/chessh/schema/key.ex +++ b/lib/chessh/schema/key.ex @@ -32,6 +32,7 @@ defmodule Chessh.Key do if is_tuple(key) do case key do {pub, [opts]} -> [{pub, [opts]}] + {pub, []} -> [{pub, [comment: '']}] key -> [{key, [comment: '']}] end |> :ssh_file.encode(:openssh_key) diff --git a/lib/chessh/schema/player.ex b/lib/chessh/schema/player.ex index d04ed3e..5b7b904 100644 --- a/lib/chessh/schema/player.ex +++ b/lib/chessh/schema/player.ex @@ -49,7 +49,7 @@ defmodule Chessh.Player do defp validate_username(changeset) do changeset |> validate_required([:username]) - |> validate_length(:username, min: 2, max: 12) + |> validate_length(:username, min: 2, max: 16) |> validate_format(:username, ~r/^[a-zA-Z0-9_\-]*$/, message: "only letters, numbers, underscores, and hyphens allowed" ) |