gql_context.ex (2042B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule Zenflows.Web.MW.GQLContext do 19 @moduledoc """ 20 Plug middleware to set Absinthe context. 21 22 Must be placed after `Plug.Parsers`. 23 """ 24 25 @behaviour Plug 26 27 alias Plug.Conn 28 29 @impl true 30 def init(opts), do: opts 31 32 @impl true 33 def call(conn, _opts) do 34 ctx = 35 %{} 36 |> set_admin(conn) 37 |> set_user_and_sign_and_body(conn) 38 39 Absinthe.Plug.put_options(conn, context: ctx) 40 end 41 42 # Set `admin` key from the headers. 43 @spec set_admin(map(), Conn.t()) :: map() 44 defp set_admin(ctx, conn) do 45 case Conn.get_req_header(conn, "zenflows-admin") do 46 [key | _] -> Map.put(ctx, :gql_admin, key) 47 _ -> ctx 48 end 49 end 50 51 # Set `user` and `sign` keys from the headers, and `body` key from 52 # `conn.assigs`, if only `admin` is not set. 53 @spec set_user_and_sign_and_body(map(), Conn.t()) :: map() 54 defp set_user_and_sign_and_body(%{gql_admin: _} = ctx, _conn), do: ctx 55 defp set_user_and_sign_and_body(ctx, conn) do 56 with [user | _] <- Conn.get_req_header(conn, "zenflows-user"), 57 [sign | _] <- Conn.get_req_header(conn, "zenflows-sign"), 58 {:ok, raw_body} <- Map.fetch(conn.assigns, :raw_body) do 59 ctx 60 |> Map.put(:gql_user, user) 61 |> Map.put(:gql_sign, sign) 62 |> Map.put(:gql_body, raw_body) 63 else _ -> 64 ctx 65 end 66 end 67 end