class IronBank::Authentications::Token

Get a bearer token to enable authenticated calls to Zuora through OAuth.

Constants

ONE_HOUR
TEN_MINUTES

Attributes

access_token[R]
base_url[R]
client_id[R]
client_secret[R]
expires_at[R]

Public Class Methods

new(client_id:, client_secret:, base_url:) click to toggle source
# File lib/iron_bank/authentications/token.rb, line 19
def initialize(client_id:, client_secret:, base_url:)
  @client_id     = client_id
  @client_secret = client_secret
  @base_url      = base_url
  fetch_token
end

Public Instance Methods

expired?() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 26
def expired?
  # Ten minutes as a margin of error in order to renew token in time
  expires_at < Time.now + TEN_MINUTES
end
header() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 31
def header
  { "Authorization" => "Bearer #{use}" }
end

Private Instance Methods

authenticate() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 53
def authenticate
  connection.post("/oauth/token", authentication_params).body
end
authentication_params() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 69
def authentication_params
  {
    client_id:     client_id,
    client_secret: client_secret,
    grant_type:    "client_credentials"
  }
end
connection() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 57
def connection
  @connection ||= Faraday.new(url: base_url) do |conn|
    IronBank.configuration.middlewares.each do |klass, options|
      conn.use klass, options
    end

    conn.request  :url_encoded
    conn.response :json
    conn.adapter  Faraday.default_adapter
  end
end
fetch_token() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 45
def fetch_token
  response      = authenticate || {}
  @access_token = response.fetch("access_token", nil)
  @expires_at   = Time.now + response.fetch("expires_in", ONE_HOUR).to_i
  validate_access_token
end
Also aliased as: refetch_token
refetch_token()
Alias for: fetch_token
use() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 40
def use
  refetch_token if expired?
  access_token
end
validate_access_token() click to toggle source
# File lib/iron_bank/authentications/token.rb, line 77
def validate_access_token
  raise InvalidAccessToken, access_token unless access_token
end