Forward args from custom plug to second plug

159 views Asked by At

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?

1

There are 1 answers

0
steventnorris On BEST ANSWER

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