diff options
author | Simponic <loganhunt@simponic.xyz> | 2022-12-19 16:27:34 -0700 |
---|---|---|
committer | Simponic <loganhunt@simponic.xyz> | 2022-12-19 16:27:34 -0700 |
commit | fe661a935ac887b11aec31ff049ace2db7ccbf84 (patch) | |
tree | 13fdbe8757d929671f7006c20181a4b8febc83ce /lib/chessh/schema/key.ex | |
parent | b4743f9efb685545cdd780cc9ba7a50e083dd8cf (diff) | |
download | chessh-fe661a935ac887b11aec31ff049ace2db7ccbf84.tar.gz chessh-fe661a935ac887b11aec31ff049ace2db7ccbf84.zip |
Add unsupported message for DSA keys, player and such
Diffstat (limited to 'lib/chessh/schema/key.ex')
-rw-r--r-- | lib/chessh/schema/key.ex | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/chessh/schema/key.ex b/lib/chessh/schema/key.ex new file mode 100644 index 0000000..f8c14cf --- /dev/null +++ b/lib/chessh/schema/key.ex @@ -0,0 +1,46 @@ +defmodule Chessh.Key do + use Ecto.Schema + import Ecto.Changeset + + schema "keys" do + field(:key, :string) + field(:name, :string) + + belongs_to(:player, Chessh.Player) + + timestamps() + end + + def changeset(key, attrs) do + key + |> cast(update_encode_key(attrs, :key), [:key]) + |> cast(attrs, [:name]) + |> validate_required([:key, :name]) + |> validate_format(:key, ~r/[\-\w\d]+ [^ ]+$/, message: "invalid ssh key") + |> validate_format(:key, ~r/^(?!ssh-dss).+/, message: "DSA keys are not supported") + end + + defp update_encode_key(attrs, field) do + if Map.has_key?(attrs, field) do + Map.update!(attrs, field, &encode_key/1) + else + attrs + end + end + + def encode_key(key) do + if is_tuple(key) do + case key do + {pub, [opts]} -> [{pub, [opts]}] + key -> [{key, [comment: '']}] + end + |> :ssh_file.encode(:openssh_key) + else + key + end + # Remove comment at end of key + |> String.replace(~r/ [^ ]+\@[^ ]+$/, "") + # Remove potential spaces / newline + |> String.trim() + end +end |