module Miasma::Contrib::Google::ApiCommon

Public Class Methods

included(klass) click to toggle source
# File lib/miasma/contrib/google.rb, line 132
def self.included(klass)
  klass.class_eval do
    attribute :google_service_account_email, String, :required => true
    attribute :google_service_account_private_key, String, :required => true
    attribute :google_auth_scope, String, :required => true, :multiple => true, :default => 'cloud-platform'
    attribute :google_auth_base, String, :default => 'https://www.googleapis.com/auth'
    attribute :google_assertion_target, String, :required => true, :default => 'https://www.googleapis.com/oauth2/v4/token'
    attribute :google_assertion_expiry, Integer, :required => true, :default => 120
    attribute :google_project, String, :required => true
    attribute :google_api_base_endpoint, String, :required => true, :default => 'https://www.googleapis.com'
  end

  klass.const_set(:TOKEN_GRANT_TYPE, 'urn:ietf:params:oauth:grant-type:jwt-bearer')
end

Public Instance Methods

access_token_expired?() click to toggle source

@return [TrueClass, FalseClass]

# File lib/miasma/contrib/google.rb, line 220
def access_token_expired?
  if(oauth_token_information[:expires_on])
    oauth_token_information[:expires_on] < Time.now
  else
    true
  end
end
client_access_token() click to toggle source

@return [String] auth token

# File lib/miasma/contrib/google.rb, line 214
def client_access_token
  request_client_token if access_token_expired?
  oauth_token_information[:access_token]
end
connect() click to toggle source

Setup for API connections

# File lib/miasma/contrib/google.rb, line 160
def connect
  @oauth_token_information = Smash.new
end
connection() click to toggle source

@return [HTTP] connection for requests (forces headers)

Calls superclass method
# File lib/miasma/contrib/google.rb, line 169
def connection
  super.headers(
    'Authorization' => "Bearer #{client_access_token}"
  )
end
endpoint() click to toggle source

@return [String]

# File lib/miasma/contrib/google.rb, line 148
def endpoint
  point = google_api_base_endpoint.dup
  if(self.class.const_defined?(:GOOGLE_SERVICE_PATH))
    point << "/#{self.class.const_get(:GOOGLE_SERVICE_PATH)}"
  end
  if(self.class.const_defined?(:GOOGLE_SERVICE_PROJECT) && self.class.const_get(:GOOGLE_SERVICE_PROJECT))
    point << "/projects/#{google_project}"
  end
  point
end
oauth_token_information() click to toggle source
# File lib/miasma/contrib/google.rb, line 164
def oauth_token_information
  @oauth_token_information
end
perform_request_retry(exception) click to toggle source

Define when request should be retried

@param exception [Exception] @return [TrueClass, FalseClass]

# File lib/miasma/contrib/google.rb, line 243
def perform_request_retry(exception)
  if(exception.is_a?(Error::ApiError::RequestError))
    exception.response.code >= 500
  else
    false
  end
end
request_client_token() click to toggle source

Request a new authentication token from the remote API

@return [Smash] token information - :access_token, :token_type, :expires_in, :expires_on

# File lib/miasma/contrib/google.rb, line 191
def request_client_token
  token_signer = signer
  result = HTTP.post(
    google_assertion_target,
    :form => {
      :grant_type => self.class.const_get(:TOKEN_GRANT_TYPE),
      :assertion => token_signer.generate
    }
  )
  unless(result.code == 200)
    raise Miasma::Error::ApiError.new(
      'Request for client authentication token failed',
      :response => result
    )
  end
  @oauth_token_information = MultiJson.load(result.body.to_s).to_smash
  @oauth_token_information[:expires_on] = Time.at(
    @oauth_token_information[:expires_in] + token_signer.claims[:iat].to_i
  )
  @oauth_token_information
end
retryable_allowed?(*_) click to toggle source

When in debug mode, do not retry requests

@return [TrueClass, FalseClass]

Calls superclass method
# File lib/miasma/contrib/google.rb, line 231
def retryable_allowed?(*_)
  if(ENV['DEBUG'])
    false
  else
    super
  end
end
signer() click to toggle source

@return [Contrib::Google::Signature::Jwt]

# File lib/miasma/contrib/google.rb, line 176
def signer
  Contrib::Google::Signature::Jwt.new(
    google_service_account_private_key,
    :iss => google_service_account_email,
    :scope => [google_auth_scope].flatten.compact.map{|scope|
      "#{google_auth_base}/#{scope}"
    },
    :aud => google_assertion_target,
    :exp => Time.now.to_i + google_assertion_expiry
  )
end