From 82124b0669cbc1b2319fa743a76721f7cf441976 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Mon, 4 Mar 2024 13:25:12 -0800 Subject: [PATCH] Updated basic auth parsing to ignore empty strings --- lib/pinchflat_web/router.ex | 6 +++++- test/pinchflat_web/routing_test.exs | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/pinchflat_web/router.ex b/lib/pinchflat_web/router.ex index 3326764..e68e799 100644 --- a/lib/pinchflat_web/router.ex +++ b/lib/pinchflat_web/router.ex @@ -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 diff --git a/test/pinchflat_web/routing_test.exs b/test/pinchflat_web/routing_test.exs index a7ee8a8..37c047d 100644 --- a/test/pinchflat_web/routing_test.exs +++ b/test/pinchflat_web/routing_test.exs @@ -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