commit 7b939d8cce4434ac276bd9842194572bb4f58f53
parent 55c26fc551c5e7b17255ea6d7f596ca0ed53a7f4
Author: Alberto Lerda <albertolerda97@gmail.com>
Date: Wed, 5 Oct 2022 08:48:14 +0200
Zenflows.{GQL.MW.Debug, Web.Router}: move auth flag in middleware
Diffstat:
3 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/src/zenflows/gql/mw/debug.ex b/src/zenflows/gql/mw/debug.ex
@@ -0,0 +1,38 @@
+# Zenflows is designed to implement the Valueflows vocabulary,
+# written and maintained by srfsh <info@dyne.org>.
+# Copyright (C) 2021-2022 Dyne.org foundation <foundation@dyne.org>.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+defmodule Zenflows.GQL.MW.Debug do
+@moduledoc """
+Absinthe middleware to verify GraphQL calls.
+"""
+
+@behaviour Absinthe.Middleware
+
+@impl true
+def call(res, _opts) do
+ if res.context[:authenticate_calls?] do
+ res
+ else
+ put_in(res.context[:authenticate_calls?], conf()[:authenticate_calls?])
+ end
+end
+
+@spec conf() :: Keyword.t()
+defp conf() do
+ Application.fetch_env!(:zenflows, Zenflows.GQL)
+end
+end
diff --git a/src/zenflows/gql/schema.ex b/src/zenflows/gql/schema.ex
@@ -169,19 +169,21 @@ def middleware(mw, field, %{identifier: id})
when id in ~w[query mutation subscription]a do
alias Absinthe.Type
- cond do
+ mw = cond do
# require nothing to be provided
Type.meta(field, :only_guest?) ->
- mw ++ [MW.Errors]
+ mw
# require the admin key to be provided
Type.meta(field, :only_admin?) ->
- [MW.Admin | mw] ++ [MW.Errors]
+ [MW.Admin | mw]
# require every call to be signed
true ->
- [MW.Sign | mw] ++ [MW.Errors]
+ [MW.Sign | mw]
end
+ [MW.Debug | mw] ++ [MW.Errors]
+
end
def middleware(mw, _, _), do: mw
diff --git a/src/zenflows/web/router.ex b/src/zenflows/web/router.ex
@@ -32,10 +32,7 @@ plug Plug.Parsers,
plug MW.GQLContext
plug :dispatch
-@init_opts [
- schema: Zenflows.GQL.Schema,
- context: %{authenticate_calls?: true},
-]
+@init_opts [schema: Zenflows.GQL.Schema]
forward "/api/file",
to: Zenflows.Web.File