2

I have a phoenix app that is making an OAuth call to github. I want to store my secret keys as environment variables so I can keep them out of version control.

I have created a file called .env where I define my private key:

export GITHUB_CLIENT_ID="891538_my_key_bf0055"

I attempt to obtain my private key in my config.exs file, the file responsible for configuring your application using System.Config.

config :ueberauth, Ueberauth.Strategy.Github.OAuth, client_id: System.get_env("GITHUB_CLIENT_ID"), client_secret: System.get_env("GITHUB_SECRET_ID")

To make a long story short, my controller is almost able to handshake with github for the request. When I make a request to github to authorize my app, http://localhost:4000/auth/github, I can almost make a request and I see a 404 page from github. I have noticed that the url has no client_id though!

My router to access the callback is

  scope "/auth", Discuss do
    pipe_through :browser # Use the default browser stack

    # make request to github, google, fb
    get "/:provider", AuthController, :request
    get "/:provider/callback", AuthController, :callback
  end

And what I get is URL with no value https://github.com/login/oauth/authorize?client_id=&redirect_uri=http%3A%2F%2Flocalhost%3A4000%2Fauth%2Fgithub%2Fcallback&response_type=code&scope=user%2Cpublic_repo`

If I don't use an environment variable in config.exs and instead use the string value, the request work as it should.

How do I use environment variables in Phoenix?

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Alex Fallenstedt
  • 2,040
  • 1
  • 18
  • 34

4 Answers4

3

If using Distillery releases, you may want to avoid using System.get_env/1 from inside the config.exs files, as it will store the value of the environment variable at build time, rather than runtime.

In the prod.exs configuration, you can use

config :ueberauth, Ueberauth.Strategy.Github.OAuth,
    client_id: "${GITHUB_CLIENT_ID}",
    client_secret: "${GITHUB_SECRET_ID}"

Then generate the release with REPLACE_OS_VARS=true environment variable set.

Distillery Docs

Mike Buhot
  • 4,790
  • 20
  • 31
  • Tonight I'll check out the Distillery docs and see how I can implement this to my project. If all goes well, I'll mark this as the correct answer. Thank you Mike! – Alex Fallenstedt Jun 13 '17 at 15:35
2

You shouldn't wrap the client_id string with double quotes. Write it as is :

export GITHUB_CLIENT_ID=891538_my_key_bf0055

Before launching your server or IEx, don't forget to source .env.

BachirC
  • 199
  • 1
  • 10
1

If you want your ENV vars to stay visible only in the process of your app you can put them in the .env file and execute your app with

env $(cat .env | grep -v ^# | xargs) iex -S mix phoenix.server

Of course, in production you might want to try some more sophisticated mechanism but the above works ok for simple/dev use case and it will let you know if your application is reading the ENV var correctly.

hisa_py
  • 929
  • 11
  • 16
  • 1
    This is what my research has been leading me too. I've found that in production, it would be best to store these variables in `prod.secret.exs`. If I were to store my keys in this file, then how can I access them? Currently, for development, this is how I am obtaining my keys. https://github.com/Fallenstedt/elixir-discuss/blob/master/config/config.exs#L35-L37 – Alex Fallenstedt Jun 14 '17 at 15:51
0

For development – when running by iex -S mix phx.server – you can set the variables in .iex.exs:

System.put_env(%{"GITHUB_CLIENT_ID" => "891538_my_key_bf0055",
                 "GITHUB_SECRET_ID" => "1234567890asdfghjkls"})
TNT
  • 3,392
  • 1
  • 24
  • 27