简体   繁体   中英

Forward args from custom plug to second plug

I have a custom plug that looks like this:

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

However, I need to be able to pass in an error handler. I currently do that like this:

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

I'd prefer to do something like this:

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

And have that pass into my plug like this, or equivalent:

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

How do I pass args from my custom plug to its internally called plugs?

The below will accomplish what I need. Using builder_opts() will pass through the options from one plug to the next. This is using Plug.Builder , which of course can be used outside of Guardian for other plugs as well.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM