Coder Social home page Coder Social logo

goth's Introduction

CI

Goth

Google + Auth = Goth

A simple library to generate and retrieve OAuth2 tokens for use with Google Cloud Service accounts.

Installation

Note: below are instructions for using Goth v1.3+. For more information on earlier versions of Goth, see v1.2.0 documentation on hexdocs.pm.

  1. Add :goth to your list of dependencies in mix.exs. To use the built-in, Hackney-based HTTP client adapter, add :hackney too:

    def deps do
      [
        {:goth, "~> 1.3-rc"},
        {:hackney, "~> 1.17"}
      ]
    end
  2. Add Goth to your supervision tree:

    defmodule MyApp.Application do
      use Application
    
      def start(_type, _args) do
        credentials = "GOOGLE_APPLICATION_CREDENTIALS_JSON" |> System.fetch_env!() |> Jason.decode!()
        source = {:service_account, credentials, []}
    
        children = [
          {Goth, name: MyApp.Goth, source: source}
        ]
    
        Supervisor.start_link(children, strategy: :one_for_one)
      end
    end
  3. Fetch the token:

    iex> {:ok, token} = Goth.fetch(MyApp.Goth)
    iex> token
    %Goth.Token{
      expires: 1453356568,
      token: "ya29.cALlJ4ICWRvMkYB-WsAR-CZnExE459PA7QPqKg5nei9y2T9-iqmbcgxq8XrTATNn_BPim",
      type: "Bearer",
      ...
    }

See Goth.start_link/1 for more information about possible configuration options.

Upgrading from Goth < 1.3

Earlier versions of Goth relied on global application environment configuration which is deprecated in favour of a more direct and explicit approach in Goth v1.3+. Previously, we were depending on the HTTPoison HTTP client, now we have an optional dependency on Hackney, so in order to use it, you need to explicitly include it in your dependencies too:

Change your mix.exs:

def deps do
 [
   {:goth, "~> 1.3-rc"},
   {:hackney, "~> 1.17"}
 ]
end

You might have code similar to this:

# config/config.exs
config :goth,
  json: {:system, "GCP_CREDENTIALS"}
# lib/myapp.ex
defmodule MyApp do
  def gcloud_authorization() do
    {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform.read-only")
    "#{token.type} #{token.token}"
  end
end

Replace it with:

defmodule MyApp.Application do
  @moduledoc false
  use Application

  def start(_type, _args) do
    credentials = "GCP_CREDENTIALS" |> System.fetch_env!() |> Jason.decode!()
    scopes = ["https://www.googleapis.com/auth/cloud-platform.read-only"]
    source = {:service_account, credentials, scopes: scopes}

    children = [
      {Goth, name: MyApp.Goth, source: source}
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end
end
# lib/myapp.ex
defmodule MyApp do
  def gcloud_authorization() do
    {:ok, token} = Goth.fetch(MyApp.Goth)
    "#{token.type} #{token.token}"
  end
end

For more information on earlier versions of Goth, see v1.2.0 documentation on hexdocs.pm.

goth's People

Contributors

amuino avatar apognu avatar arrel avatar dazuma avatar gonzooo avatar hassox avatar jayjun avatar jordanadams avatar leapingfrogs avatar lytedev avatar magnetised avatar mcrumm avatar nitinstp23 avatar p42ul avatar pacoguzman avatar paveltyk avatar peburrows avatar rjacobskind avatar rubas avatar sadraskol avatar sam701 avatar savtrip avatar sgerrand avatar stratus3d avatar tazjin avatar thefirstavenger avatar toraritte avatar tsubery avatar wojtekmach avatar yuyabee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.