0

我有一个看起来像这样的自定义插件:

defmodule Db.Auth.GuardianPipeline do
  use Guardian.Plug.Pipeline,
    otp_app: :db,
    module: Db.Auth.Guardian

  plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
  plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
  plug Guardian.Plug.LoadResource, allow_blank: true

  plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}, key: :impersonated_user
  plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}, key: :impersonated_user
  plug Guardian.Plug.LoadResource, allow_blank: true, key: :impersonated_user

end

但是,我需要能够传入错误处理程序。我目前这样做:

pipeline :authenticate do
    plug(Guardian.Plug.Pipeline, error_handler: Web.Auth.ErrorHandler)
    plug(Db.Auth.GuardianPipeline)
  end

我宁愿做这样的事情:

pipeline :authenticate do
    plug(Db.Auth.GuardianPipeline, error_handler: Web.Auth.ErrorHandler)
  end

并像这样或等效地把它传递到我的插件中:

defmodule Db.Auth.GuardianPipeline do
  use Guardian.Plug.Pipeline,
    otp_app: :db,
    module: Db.Auth.Guardian,
    error_handler: args["error_handler"],

  plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
  plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
  plug Guardian.Plug.LoadResource, allow_blank: true

  plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}, key: :impersonated_user
  plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}, key: :impersonated_user
  plug Guardian.Plug.LoadResource, allow_blank: true, key: :impersonated_user

end

如何将 args 从我的自定义插件传递到其内部调用的插件?

4

1 回答 1

0

下面将完成我需要的。使用builder_opts()将通过选项从一个插头传递到下一个。这是 using Plug.Builder,当然也可以在 Guardian 之外用于其他插件。

defmodule Db.Auth.AuthPipeline do
  use Guardian.Plug.Pipeline, otp_app: :db, module: Db.Auth.Guardian

  plug Guardian.Plug.Pipeline, builder_opts()
  plug Guardian.Plug.VerifySession, claims: %{"typ" => "access"}
  plug Guardian.Plug.VerifyHeader, claims: %{"typ" => "access"}
  plug Guardian.Plug.LoadResource, allow_blank: true

end
于 2018-12-02T23:12:49.707 回答