将参数从自定义插件转发到第二个插件

Forward args from custom plug to second plug

我有一个看起来像这样的定制插头:

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 从我的自定义插件传递到其内部调用的插件?

下面将完成我需要的。使用 builder_opts() 会将选项从一个插头传递到下一个插头。这是使用 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