我正在使用Guardian设置JWT身份验证。建议使用JWK(Json Web密钥)对JWT进行加密/签名。要生成所说的JWK,我使用JOSE.JWK。
由于我的密钥文件可能会存储在源代码管理中,因此我希望对其进行密码保护。密码通过System.get_env
(访问环境变量)传递到elixir应用程序中。
我可以生成文件,也可以使用密码读回。但是它在我的配置文件中失败config/dev.exs
。错误是
**(Mix.Config.LoadError)无法加载config config / dev.exs **(CompileError)config / dev.exs:2:模块JOSE.JWK未加载且找不到
我的dev.exs:
use Mix.Config
# 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
在控制台中,一切正常。所以我想当配置文件被解析时,模块还没有被加载。有办法解决吗?
编辑:它似乎与何时加载模块以及如何以及何时评估和编译配置文件有关。JSON.JWK
在匿名函数中包装对的调用,例如:
use Mix.Config
# 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
仅Guardian的master分支支持config.exs文件中的功能。发行版本(0.10.x)不支持功能。
该问题正在此处跟踪:https : //github.com/ueberauth/guardian/issues/136。您可以在此处查看新代码:https : //github.com/ueberauth/guardian/blob/master/lib/guardian.ex#L292-296,尤其是第294行
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句