peburrows / goth

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

Look for ADC credentials at ~/.config/gcloud/application_default_credentials.json #77

Closed eamontaaffe closed 3 years ago

eamontaaffe commented 3 years ago

When you run the command gcloud auth application-default-credentials login the credentials are deposited at ~/.config/gcloud/application_default_credentials.json.

This behavior doesn't seem to be documented in the Application Default Credentials documentation. However, many of the google supported libraries seem to identify credentials at this location (such as the python api).

It would be great if this library would also support his credentials location as part of the ADC lookup procedure.

eamontaaffe commented 3 years ago

https://github.com/peburrows/goth/blob/7eb2068331a98036d7779b524895e8f9495d73a7/lib/goth/config.ex#L131-L136

It looks like we could just update this function slightly.

eamontaaffe commented 3 years ago

Maybe like this. Ill wrap this up into a PR later today.

 defp fetch_creds_filename do
   default_path = Path.expand("~/.config/gcloud/application_default_credentials.json")
   case {System.fetch_env("GOOGLE_APPLICATION_CREDENTIALS"), File.regular?(default_path)} do
     {{:ok, filename}, _} -> {:ok, filename}     
     {_, true} -> {:ok, default_path}
   end 
 end

 defp from_creds_file(_config) do 
   case fetch_creds_filename do
     {:ok, filename} -> filename |> File.read!() |> decode_json()
     {:error, _} -> nil
   end 
 end 
wojtekmach commented 3 years ago

See https://github.com/peburrows/goth/pull/79#issuecomment-785341947.

bbhoss commented 3 years ago

All of the Google auth clients support ADC, is there no way to get Goth to without a full redesign?

wojtekmach commented 3 years ago

We hope the redesigned API is more flexible so adding ADC support will hopefully not require a 'full redesign'. Feel free to send a PR with ADC support, it could even be the default :source.

bbhoss commented 3 years ago

Apologies, it wasn't clear that the redesign you write about above was already done as and released as an RC. I will give adding ADC support a shot!

wojtekmach commented 3 years ago

No worries, thanks for looking into it!