ueberauth / guardian

Elixir Authentication
MIT License
3.4k stars 381 forks source link

Using an environment variable as a secret key in elixir 1.14 #713

Closed MarkMekhaiel closed 1 year ago

MarkMekhaiel commented 1 year ago

Steps to Reproduce

Running Elixir 1.14, we can no longer call compile_env when using an environment variable as this value changes between compile time and runtime, and instead must use get_env inside of a function. However this is not possible with declarations made in the module body, outside of any functions. You cannot assign the env variable to a function, as no functions are defined when this config code is executed. And you can't use compile_env as this variable changes between compile time and runtime.

According to 1.14, we should be able to do the following, but Elixir seems to think we are calling compile_env from a function rather than the module body.

defmodule App.Auth.Bla do
  use Guardian,
    otp_app: :app,
    allowed_algos: ["HS512"],
    issuer: "app",
    secret_key: Application.compile_env(:app, __MODULE__)[:secret_key]
end

I assume this is down to how Guardian assigns the secret key?

Expected Result

Application compiles as expected

Actual Result

== Compilation error in file lib/app/auth/bla.ex ==
** (RuntimeError) Application.compile_env/3 cannot be called inside functions, only in the module body
    (elixir 1.14.0) lib/application.ex:532: Application."MACRO-compile_env"/4
    (elixir 1.14.0) expanding macro: Application.compile_env/2
    lib/app/auth/bla.ex:6: App.Auth.Bla.config/0
    (elixir 1.14.0) expanding macro: Kernel.|>/2
    lib/app/auth/bla.ex:2: App.Auth.Bla.config/
MarkMekhaiel commented 1 year ago

Moving config to config files instead of modules sorted the issue

Hanspagh commented 1 year ago

dublicate of #706