Hello all,
trying out plugg + ecto instead of using full phoenix for api development and I’m getting:
09:11:22.225 [error] GenServer #PID<0.621.0> terminating
** (Plug.Conn.NotSentError) a response was neither set nor sent from the connection
(bandit 0.7.7) lib/bandit/pipeline.ex:121: Bandit.Pipeline.commit_response/2
(bandit 0.7.7) lib/bandit/http1/handler.ex:24: Bandit.HTTP1.Handler.handle_data/3
(bandit 0.7.7) lib/bandit/delegating_handler.ex:18: Bandit.DelegatingHandler.handle_data/3
(bandit 0.7.7) lib/thousand_island/handler.ex:332: Bandit.DelegatingHandler.handle_continue/2
(stdlib 3.15.2) gen_server.erl:695: :gen_server.try_dispatch/4
(stdlib 3.15.2) gen_server.erl:437: :gen_server.loop/7
(stdlib 3.15.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:continue, :handle_connection}
State: {%ThousandIsland.Socket{socket: #Port<0.17>, transport_module: ThousandIsland.Transports.TCP, read_timeout: 60000, span: %ThousandIsland.Telemetry{span_name: :connection, telemetry_span_context: #Reference<0.526008314.269221892.10052>, start_time: -576460747528887000, start_metadata: %{parent_telemetry_span_context: #Reference<0.526008314.269221892.9968>, remote_address: {127, 0, 0, 1}, remote_port: 57037, telemetry_span_context: #Reference<0.526008314.269221892.10052>}}}, %{handler_module: Bandit.InitialHandler, opts: %{http_1: [], http_2: [], websocket: []}, plug: {AuthexWeb.Endpoint, []}}}
in mi root router I have this line:
forward "/tokens", to: Routers.Token
And this is router I’m forwarding to:
defmodule AuthexWeb.Routers.Token do
use Web.Router
alias AuthexWeb.Plugs
alias AuthexWeb.Controllers
plug(Plugs.BasicClientAuthorization)
post "/tokens" do
IO.puts("one")
Controllers.Token.create(conn, conn.assigns.current_client, conn.body_params)
end
post "/" do
IO.puts("two")
Controllers.Token.create(conn, conn.assigns.current_client, conn.body_params)
end
post _ do
IO.puts("_")
Controllers.Token.create(conn, conn.assigns.current_client, conn.body_params)
end
post "*a" do
IO.puts("_")
Controllers.Token.create(conn, conn.assigns.current_client, conn.body_params)
end
match _ do
IO.puts("what?")
Controllers.Fallback.not_found(conn)
end
end
I have also debug log in Plugs.BasicClientAuthorization
which get printed every time, btw. this is how it looks like:
defmodule AuthexWeb.Plugs.BasicClientAuthorization do
use Web.Plug
alias Plug.BasicAuth
def init(opts), do: opts
def call(conn, _opts) do
case BasicAuth.parse_basic_auth(conn) do
{id, secret} ->
case Authex.get_client(id) do
nil ->
invalid_authorization(conn)
client ->
if Authex.client_secret_valid?(client, secret) do
assign(conn, :current_client, client)
else
invalid_authorization(conn)
end
end
_ ->
conn
|> BasicAuth.request_basic_auth()
|> halt()
end
|> IO.inspect(label: "#{inspect(__MODULE__)}")
end
defp invalid_authorization(conn) do
conn
|> send_resp(403, "{\"error\": \"invalid\"}")
|> halt()
end
end
Do you see anything wrong? Even some hints on debugging plug would be appreciated.
thank you