Previously when building a plug pipeline you could use builder_opts/0
to “forward”
plug Guardian.Plug.Pipeline, builder_opts()
but now it’s depricated what’s the alternative?
I’ve tried the approach given here with copy_opts_to_assign: :builder_opts
but the builder_opts are nowhere to be found in assigns.
What I want to accomplish?
I have two authorization plugs, one checks if the project is owned by the organization and the 2nd one authorizes the user permissions. User permissions can be organization scoped or project scoped.
When permission is project scoped I want the request first go trough project authorization plug and then user authorizaiton, otherwise just do user authorization.
The request can be halted at any of those two auth plugs.
This approach works:
use Plug.Builder
plug :maybe_authorize_project
plug MyPlugs.AuthorizeUser, builder_opts()
@impl true
def init(opts) do
defaults = [permissions: ["organization.view"], project_authorization: false]
opts = Keyword.merge(defaults, opts)
if Enum.any?(opts[:permissions], fn permission ->
String.starts_with?(permission, "project.")
end) do
Keyword.put(opts, :project_authorization, true)
else
opts
end
end
@impl true
def call(conn, opts) do
Logger.info("Opts: #{inspect(opts)}")
conn
|> super(opts)
end
@authorize_project_opts PublicAPI.Plugs.AuthorizeProject.init([])
defp maybe_authorize_project(conn, opts) do
if opts[:project_authorization] do
conn
|> MyPlugs.AuthorizeProject.call(@authorize_project_opts)
else
conn
end
end
But since builder_opts/0
is depricated I’d like to know how to do it without it. It seems like a sane use case to forward opts like this.