From 9c457c1f685a15af5ad305aa3d3db0e55b36afed Mon Sep 17 00:00:00 2001 From: Carsten Date: Tue, 21 Apr 2026 22:48:36 +0200 Subject: [PATCH] feat(server): Server.Auth.verify_password/1 --- server/lib/server/auth.ex | 16 ++++++++++++++++ server/test/server/auth_test.exs | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 server/lib/server/auth.ex create mode 100644 server/test/server/auth_test.exs diff --git a/server/lib/server/auth.ex b/server/lib/server/auth.ex new file mode 100644 index 0000000..1f2a8e8 --- /dev/null +++ b/server/lib/server/auth.ex @@ -0,0 +1,16 @@ +defmodule Server.Auth do + @moduledoc "Single-user dashboard authentication." + + @spec verify_password(term()) :: :ok | :error + def verify_password(password) when is_binary(password) do + hash = Application.fetch_env!(:server, :dashboard_password_hash) + + if Argon2.verify_pass(password, hash) do + :ok + else + :error + end + end + + def verify_password(_), do: :error +end diff --git a/server/test/server/auth_test.exs b/server/test/server/auth_test.exs new file mode 100644 index 0000000..cc60c73 --- /dev/null +++ b/server/test/server/auth_test.exs @@ -0,0 +1,28 @@ +defmodule Server.AuthTest do + use ExUnit.Case, async: true + + alias Server.Auth + + setup do + hash = Argon2.hash_pwd_salt("testpass") + prev = Application.get_env(:server, :dashboard_password_hash) + Application.put_env(:server, :dashboard_password_hash, hash) + on_exit(fn -> Application.put_env(:server, :dashboard_password_hash, prev) end) + :ok + end + + describe "verify_password/1" do + test "returns :ok for correct password" do + assert Auth.verify_password("testpass") == :ok + end + + test "returns :error for wrong password" do + assert Auth.verify_password("wrong") == :error + end + + test "returns :error for non-binary input" do + assert Auth.verify_password(nil) == :error + assert Auth.verify_password(123) == :error + end + end +end