class IntegrationApi::AuthConfiguration

Attributes

application_json_value[RW]
auth_uri[RW]
authorization[RW]
base_url[RW]
bearer[RW]
client_credentials[RW]
client_token[RW]
client_token_auth_uri[RW]
config[RW]
grant_type_key[RW]
password[RW]
username[RW]

Public Class Methods

default() click to toggle source
# File lib/integration_api/auth_configuration.rb, line 48
def self.default
  @@default ||= AuthConfiguration.new
end
new() { |self| ... } click to toggle source
Calls superclass method
# File lib/integration_api/auth_configuration.rb, line 32
def initialize
  super
  @authorization = 'Authorization'
  @client_token = 'Client-Token'
  @grant_type_key = 'grant_type'
  @client_credentials = 'client_credentials'
  @password = 'password'
  @username = 'username'
  @application_json_value = 'application/json'
  @auth_uri = '/authorization/v1/oauth/token'
  @client_token_auth_uri = '/authorization/v1/client-token'
  @bearer = 'Bearer '
  @config = Configuration.default
  yield(self) if block_given?
end

Public Instance Methods

auth_url(host) click to toggle source
# File lib/integration_api/auth_configuration.rb, line 60
def auth_url(host)
  url = "#{scheme}://#{[host, auth_uri].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
  URI.encode(url)
end
client_token_auth_url(host) click to toggle source
# File lib/integration_api/auth_configuration.rb, line 65
def client_token_auth_url(host)
  url = "#{scheme}://#{[host, client_token_auth_uri].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
  URI.encode(url)
end
configure() { |self| ... } click to toggle source
# File lib/integration_api/auth_configuration.rb, line 52
def configure
  yield(self) if block_given?
end
create_client_credential(client_id, client_secret) click to toggle source
# File lib/integration_api/auth_configuration.rb, line 102
def create_client_credential(client_id, client_secret)
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  # Request Params
  params = {}
  params[@grant_type_key] = @client_credentials
  # Header parameters
  header_params = {}
  header_params['Accept'] = '*/*'
  header_params['Content-Type'] = 'application/json'
  header_params[@authorization] = basic_cred
  response = Typhoeus::Request.new(
      auth_url(@config.host),
      :method => :post,
      :headers => header_params,
      :params => params
  ).run

  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), response.body
    end
  end
  @config.access_token = body['access_token']
end
create_client_token_credential(client_id, client_secret, client_token) click to toggle source
# File lib/integration_api/auth_configuration.rb, line 70
def create_client_token_credential(client_id, client_secret, client_token)
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  header_params = {}
  header_params[@authorization] = basic_cred;
  header_params[@client_token] = @bearer + client_token;
  response = Typhoeus::Request.new(
      client_token_auth_url(@config.host),
      :method => :post,
      :headers => header_params,
      :params => nil
  ).run

  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), response.body
    end
  end
  @config.access_token = body['access_token']
end
create_password_credential(client_id, client_secret, username, password) click to toggle source
# File lib/integration_api/auth_configuration.rb, line 143
def create_password_credential(client_id, client_secret, username, password)
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  # Request Params
  params = {}
  params[@grant_type_key] = @password
  params[@username] = username
  params[@password] = password
  # header parameters
  header_params = {}
  header_params['Accept'] = '*/*'
  header_params['Content-Type'] = 'application/json'
  header_params[@authorization] = basic_cred
  response = Typhoeus::Request.new(
      auth_url(@config.host),
      :method => :post,
      :headers => header_params,
      :params => params
  ).run
  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), body
    end
  end
  @config.access_token = body['access_token']
end
set_access_token(token) click to toggle source
# File lib/integration_api/auth_configuration.rb, line 139
def set_access_token(token)
  @config.access_token = token
end
set_base_url(base_url) click to toggle source
# File lib/integration_api/auth_configuration.rb, line 56
def set_base_url(base_url)
  @config.host = base_url
end