gpt4 book ai didi

elixir - 在 Elixir Phoenix Absinthe GraphIQL 客户端中实现身份验证?

转载 作者:行者123 更新时间:2023-12-01 23:25:16 24 4
gpt4 key购买 nike

我在 Absinthe 中使用内置的 GraphiQL 界面。如下:

  pipeline :browser do
plug RemoteIp, headers: ~w[x-forwarded-for], proxies: ~w[]
plug :accepts, ["html", "json"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end

scope "/graphiql" do
pipe_through :browser # Use the default browser stack

forward "/", Absinthe.Plug.GraphiQL,
schema: ApiWeb.Schema,
default_headers: {__MODULE__, :graphiql_headers},
context: %{pubsub: ApiWeb.Endpoint}
end

def graphiql_headers(conn) do
%{
"X-CSRF-Token" => Plug.CSRFProtection.get_csrf_token(),
}
end

我需要最终用户插入 Authentication: Bearer <JWT>在界面中,然后需要为 sub: header 解开它,其中包含我的用户 ID,我需要在解析器中使用它。

用户可以配置自定义 header ,这没问题。如果他随后执行 GraphSQL 查询,该接口(interface)将向/graphiql 端点发出 POST。正是在这个点上,我想调用一些插件来检查 JWT 并检索用户信息。

我以为我可以使用 default_headers 选项,但这似乎只在 GET 请求期间调用。

我似乎需要不同的管道来 GET 和 POST 到/graphiql 端点,我该如何实现呢?我一定做错了什么......

请注意,如果我对 GET 和 POST 使用相同的管道,则仅在访问浏览器中的端点时就会检查 JWT,这是我不希望的。

最佳答案

是的,实际上我做了以下事情:

  pipeline :authenticate_on_post_only do
plug ApiWeb.Plugs.Authenticate, post_only: true
end

scope "/graphiql" do
pipe_through [:browser, :authenticate_on_post_only]

forward "/", Absinthe.Plug.GraphiQL,
schema: ApiWeb.GraphQL,
socket: ApiWeb.GraphQLSocket
end

结合:

defmodule ApiWeb.Plugs.Authenticate do
use Plug.Builder
alias ApiWeb.Helpers.JWT

plug Joken.Plug, verify: &JWT.verify/0, on_error: &JWT.error/2
plug ApiWeb.Plugs.Subject
plug Backend.Plug.Session

def call(%Plug.Conn{method: "POST"} = conn, opts) do
conn = super(conn, opts) # calls the above plugs
put_private(conn, :absinthe, %{context: conn}) # for absinthe (GraphQL), for resolvers to re-use
end
def call(conn, opts) do
if opts[:post_only] do
conn
else
super(conn, opts) # calls the above plugs
end
end
end

当然,您可以使用您自己的任何身份验证插件,而不是我列出的那些。

我在同一模块中还有一个 REST API,我使用它的方式如下:

  scope "/v1", ApiWeb do
pipe_through :api

<my resources here>
done

API 管道定义为:

  pipeline :api do
plug :put_resp_content_type, "application/json"
plug :accepts, ["json"]
plug ApiWeb.Plugs.Authenticate
end

它将对任何类型的 HTTP 请求进行身份验证。

关于elixir - 在 Elixir Phoenix Absinthe GraphIQL 客户端中实现身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48303254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com