Updated basic auth parsing to ignore empty strings

This commit is contained in:
Kieran Eglin 2024-03-04 13:25:12 -08:00
parent 1b404086d5
commit 82124b0669
No known key found for this signature in database
GPG key ID: 193984967FCF432D
2 changed files with 14 additions and 1 deletions

View file

@ -54,10 +54,14 @@ defmodule PinchflatWeb.Router do
username = Application.get_env(:pinchflat, :basic_auth_username)
password = Application.get_env(:pinchflat, :basic_auth_password)
if username && password do
if credential_set?(username) && credential_set?(password) do
Plug.BasicAuth.basic_auth(conn, username: username, password: password, realm: "Pinchflat")
else
conn
end
end
defp credential_set?(credential) do
credential && credential != ""
end
end

View file

@ -44,5 +44,14 @@ defmodule PinchflatWeb.RoutingTest do
assert conn.status == 200
end
test "it treats empty strings as not being set when using basic auth", %{conn: conn} do
Application.put_env(:pinchflat, :basic_auth_username, "")
Application.put_env(:pinchflat, :basic_auth_password, "pass")
conn = get(conn, "/")
assert conn.status == 200
end
end
end