class SparkApi::Authentication::ApiAuth

ApiAuth

Implementation the BaseAuth interface for API style authentication

Public Class Methods

new(client) click to toggle source
Calls superclass method
# File lib/spark_api/authentication/api_auth.rb, line 14
def initialize(client)
  super(client)
end

Public Instance Methods

authenticate() click to toggle source
# File lib/spark_api/authentication/api_auth.rb, line 18
def authenticate
  sig = sign("#{@client.api_secret}ApiKey#{@client.api_key}")
  SparkApi.logger.debug { "Authenticating to #{@client.endpoint}" }
  start_time = Time.now
  request_path = "#{SparkApi::Configuration::DEFAULT_SESSION_PATH}?ApiKey=#{@client.api_key}&ApiSig=#{sig}"
  resp = @client.connection(true).post request_path, ""
  request_time = Time.now - start_time
  SparkApi.logger.info { "[#{(request_time * 1000).to_i}ms] Api: POST #{request_path}" }
  SparkApi.logger.debug { "Authentication Response: #{resp.inspect}" }
  @session = Session.new(resp.body.results.first)
  SparkApi.logger.debug { "Authentication: #{@session.inspect}" }
  @session
end
build_param_string(param_hash) click to toggle source

Builds an ordered list of key value pairs and concatenates it all as one big string. Used specifically for signing a request.

# File lib/spark_api/authentication/api_auth.rb, line 39
def build_param_string(param_hash)
  return "" if param_hash.nil?
    sorted = param_hash.keys.sort do |a,b|
      a.to_s <=> b.to_s
    end
    params = ""
    sorted.each do |key|
      params += key.to_s + param_hash[key].to_s
    end
    params
end
logout() click to toggle source
# File lib/spark_api/authentication/api_auth.rb, line 32
def logout
  @client.delete("/session/#{@session.auth_token}") unless @session.nil?
  @session = nil
end
request(method, path, body, options) click to toggle source

Perform an HTTP request (no data)

# File lib/spark_api/authentication/api_auth.rb, line 64
def request(method, path, body, options)
  escaped_path = Addressable::URI.escape(path)
  connection = @client.connection
  connection.headers.merge!(options.delete(:override_headers) || {})
  request_opts = {
    :AuthToken => @session.auth_token
  }

  unless (@client.api_user.nil? || options[:ApiUser])
    request_opts.merge!(:ApiUser => "#{@client.api_user}")
  end

  request_opts.merge!(options)
  sig = sign_token(escaped_path, request_opts, body)
  request_path = "#{escaped_path}?#{build_url_parameters({"ApiSig"=>sig}.merge(request_opts))}"
  SparkApi.logger.debug { "Request: #{request_path}" }
  if body.nil?
    response = connection.send(method, request_path)
  else
    SparkApi.logger.debug { "Data: #{body}" }
    response = connection.send(method, request_path, body)
  end
  response
end
sign(sig) click to toggle source

Sign a request

# File lib/spark_api/authentication/api_auth.rb, line 52
def sign(sig)
  Digest::MD5.hexdigest(sig)
end
sign_token(path, params = {}, post_data="") click to toggle source

Sign a request with request data.

# File lib/spark_api/authentication/api_auth.rb, line 57
def sign_token(path, params = {}, post_data="")
  token_string = "#{@client.api_secret}ApiKey#{@client.api_key}ServicePath#{path}#{build_param_string(params)}#{post_data}"
  signed = sign(token_string)
  signed
end