summaryrefslogtreecommitdiff
path: root/lib/chessh/auth/keys.ex
blob: f0e1c7891d9a3203a544ae97f78c3d892268eda8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
defmodule Chessh.Auth.KeyAuthenticator do
  alias Chessh.{Key, Repo, Player}
  import Ecto.Query

  def authenticate(player = %Player{}, public_key) do
    !!Repo.one(
      from(k in Key,
        where: k.key == ^Key.encode_key(public_key),
        where: k.player_id == ^player.id
      )
    )
  end

  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