From 48d84d76cd8013d09ab9fa6e9ff812346c65d76f Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Mon, 30 Dec 2024 11:46:29 -0800 Subject: [PATCH] Hooked up token_protected_route plug to database --- lib/pinchflat_web/plugs.ex | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/pinchflat_web/plugs.ex b/lib/pinchflat_web/plugs.ex index 5c78530..67b5b71 100644 --- a/lib/pinchflat_web/plugs.ex +++ b/lib/pinchflat_web/plugs.ex @@ -1,7 +1,8 @@ defmodule PinchflatWeb.Plugs do - use PinchflatWeb, :router - # TODO: doc and test + use PinchflatWeb, :router + alias Pinchflat.Settings + def maybe_basic_auth(conn, opts) do if Application.get_env(:pinchflat, :expose_feed_endpoints) do conn @@ -26,17 +27,24 @@ defmodule PinchflatWeb.Plugs do end def token_protected_route(%{query_params: %{"route_token" => route_token}} = conn, _opts) do - # TODO: make this match against the token in the database - conn + if Settings.get!(:route_token) == route_token do + conn + else + send_unauthorized(conn) + end end def token_protected_route(conn, _opts) do - conn - |> send_resp(:unauthorized, "Unauthorized") - |> halt() + send_unauthorized(conn) end defp credential_set?(credential) do credential && credential != "" end + + defp send_unauthorized(conn) do + conn + |> send_resp(:unauthorized, "Unauthorized") + |> halt() + end end