Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authorization requires compile time settings #51

Open
teamon opened this issue Feb 14, 2017 · 5 comments
Open

Authorization requires compile time settings #51

teamon opened this issue Feb 14, 2017 · 5 comments

Comments

@teamon
Copy link
Contributor

teamon commented Feb 14, 2017

Since Endpoint is all macros it checks :authorization config during verk_web compile time. Since compiling dependency happens only once, changing config after dependencies have been compiled has no effect. I think the reasonable solution would be to have a plug that checks the config on init. BasicAuth does exactly that, but because of https://github.com/edgurgel/verk_web/blob/master/lib/verk_web/endpoint.ex#L39 it is not even loaded.

@teamon
Copy link
Contributor Author

teamon commented Feb 14, 2017

It probably could be something simple like this:

defmodule VerkBasicAuth do
  @behaviour Plug

  def init(_opts) do
    if Application.get_env(:verk_web, :authorization) do
      BasicAuth.init(use_config: {:verk_web, :authorization})
    else
      :no_auth
    end
  end

  def call(conn, :no_auth), do: conn
  def call(%{request_path: "/verk" <> _} = conn, opts), do: BasicAuth.call(conn, opts)
  def call(conn, _), do: conn
end

@teamon
Copy link
Contributor Author

teamon commented Feb 14, 2017

Ahh, actually this won't work, as stated in Plug docs init might be called during compilation.

@tlvenn
Copy link

tlvenn commented Apr 11, 2017

I have submitted a PR in Verk to address this kind of issue by leveraging Confex. Happy to submit a PR here to do the same.

@edgurgel
Copy link
Owner

@tlvenn, I don't think this issue is related to environment variables versus application configuration. I may be completely wrong here but the issue here is that the plug is added at compile time.

@tlvenn
Copy link

tlvenn commented Apr 11, 2017

Good catch @edgurgel. With Phoenix 1.3, you can leverage load_from_system_env to solve this problem:

@doc """
  Dynamically loads configuration from the system environment
  on startup.

  It receives the endpoint configuration from the config files
  and must return the updated configuration.
  """
  def load_from_system_env(config) do
    config = Confex.process_env(config)

    unless config[:secret_key_base] do
      raise "Set SECRET_KEY environment variable!"
    end

    {:ok, config}
  end

https://github.com/Nebo15/pkcs7_verify.api/blob/master/lib/pkcs7_verify_api/web/endpoint.ex#L30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants