It seems that the way configuration files in phoenix are loaded and compiled pose a problem when using third-party modules in config.exs
or dev.exs/prod.exs/test.exs
.
Example: To set up Guardian for JWT authentication I am trying to use the JOSE.JWK
module for JWK creation / loading in my config.exs
. I can use the module alright in the console with iex -S mix phoenix.server
. It is of course installed as a dependency. The error I'm getting is
** (Mix.Config.LoadError) could not load config config/config.exs
** (UndefinedFunctionError) undefined function JOSE.JWK.from_file/2 (module JOSE.JWK is not available)
This is the code in my config.exs
# Configure Guardian for JWT Authentication
config :guardian, Guardian,
allowed_algos: ["HS512"], # optional
verify_module: Guardian.JWT, # optional
issuer: "MyApp",
ttl: { 30, :days },
verify_issuer: true, # optional
secret_key: System.get_env("GUARDIAN_KEY_PASSPHRASE") |> JOSE.JWK.from_file(System.get_env("GUARDIAN_KEY_FILE")),
serializer: MyApp.GuardianSerializer
It works when I wrap the call to JOSE.JWK.from_file/2
in an anonymous function. But of course the value of Guardian.config(:secret_key) is then the anonymous function itself and not its return value:
# Configure Guardian for JWT Authentication
config :guardian, Guardian,
allowed_algos: ["HS512"], # optional
verify_module: Guardian.JWT, # optional
issuer: "MyApp",
ttl: { 30, :days },
verify_issuer: true, # optional
secret_key: fn -> System.get_env("GUARDIAN_KEY_PASSPHRASE") |> JOSE.JWK.from_file(System.get_env("GUARDIAN_KEY_FILE")) end,
serializer: MyApp.GuardianSerializer
This is ok in this example since Guardian accepts a function for this config value. But I can imagine other situations where this could be a problem.
Is this limitation on purpose? Am I missing something? Is there a way around this?