peburrows / goth

Elixir package for Oauth authentication via Google Cloud APIs
http://hexdocs.pm/goth
MIT License
289 stars 111 forks source link

allow for dynamic configuration via an init/1 Config callback #32

Closed peburrows closed 6 years ago

peburrows commented 6 years ago

To use:

config :goth, config_module: MyGothConfig

defmodule MyGothConfig
  use Goth.Config

  def init(config) do
    {:ok, Keyword.put(config, :json, load_json_from_somewhere())}
  end
end
rjacobskind commented 5 years ago

@peburrows When developing locally, I notice that this line always fails: https://github.com/peburrows/goth/blob/df400832bd614cb14a606ba42c4d5b9f15394072/lib/goth/config.ex#L113

So, my custom config module is never actually run. I haven't been able to get my goth config working properly because of this. Any possible fix in mind?

Edit: In further investigating, I discovered that Code.ensure_loaded(mod) (non-boolean response) returns {:error, :nofile}. My config module does exist though.

peburrows commented 5 years ago

Strange. What do your module and your config look like? If nothing jumps out as wrong in your module or the config, the simplest reproducible example would help me to debug this.

rjacobskind commented 5 years ago

I am storing the credentials as base64 and then decoding them into the json format.

This is what my config looks like:

config :goth, config_module: MyApp.GothConfig

And this is what my config module looks like:

defmodule MyApp.GothConfig do
  use Goth.Config

  def init(config) do
    credentials =
      (Application.get_env(:my_app, :credentials) ||
         System.get_env("GOOGLE_APPLICATION_CREDENTIALS"))
      |> Base.decode64()
      |> elem(1)

    {:ok, Keyword.put(config, :json, credentials)}
  end
end
peburrows commented 5 years ago

Hrm, that all looks fine. What does your mix.exs file look like, and are you able to reproduce this in a bare project with Goth as the only dependency?

rjacobskind commented 5 years ago

Upon further investigation, I found that the issue was caused by my config file living in the config directory, so it wasn't getting compiled. I fixed this and now Goth is able to find my module

peburrows commented 5 years ago

Awesome, glad you were able to figure it out!