ueberauth / guardian

Elixir Authentication
MIT License
3.43k stars 382 forks source link

Accept runtime environment tuple for secret_key #498

Closed williamweckl closed 6 years ago

williamweckl commented 6 years ago

Hi guys, I'm using distillery and trying to use the convention tuple to set environment variable secret_key and noticed I can't do it.

Error I'm getting:

FunctionClauseError: no function clause matching in :jose_jwk.from/1

Stack trace:

1
File "src/jose_jwk.erl" line 134 in :jose_jwk.from/1 (jose) [args]
2
File "src/jose_jwt.erl" line 182 in :jose_jwt.sign/3 (jose)
3
File "lib/guardian/token/jwt.ex" line 168 in Guardian.Token.Jwt.create_token/3 (guardian)
4
File "lib/guardian.ex" line 754 in Guardian.returning_tuple/1 (guardian)
5
File "lib/guardian.ex" line 581 in Guardian.encode_and_sign/4 (guardian)

My config at prod.exs:

config :accounts, Accounts.Guardian,
  secret_key: {:system, "GUARDIAN_SECRET"}

If I use System.get_env("GUARDIAN_SECRET") it works. Using guardian 1.0.1 with elixir 1.6.4

yordis commented 6 years ago

@williamweckl What is the issue of doing System.get_env("GUARDIAN_SECRET") ?

williamweckl commented 6 years ago

When I do it at config it will compile as a string, so if I build my application in a different environment that I run it the configuration value will be different...

Better explained at: https://hexdocs.pm/distillery/runtime-configuration.html

yordis commented 6 years ago

I didn't finish to read by so far

From the posted link

NOTE: It is no longer recommended to use the {:system, "VAR"} convention, as it has inconsitent use in the community, and instead we’d rather push people towards an approach which works every time, rather than only sometimes.

So why are you trying to use that way?

yordis commented 6 years ago

From Distillery official documentation this method is not recommended.

williamweckl commented 6 years ago

@yordis

Answering your question I'm using it because almost all my other libraries accepts this format, even phoenix uses it... If I can't use it at least I need a way to set it at runtime...

yordis commented 6 years ago

@williamweckl could be this what you are looking for?

secret_key: {MyModule, :function_name, [:some, :args]}

From the readme: https://github.com/ueberauth/guardian#more-advanced-secrets

williamweckl commented 6 years ago

@yordis yes! this will work for me! thank you!

pcriv commented 5 years ago

Having this same issue, even tho the CHANGELOG says this is supported https://github.com/ueberauth/guardian/blob/master/CHANGELOG.md#v-0140