Class: FlexmlsApi::Authentication::ApiAuth

Inherits:
BaseAuth
  • Object
show all
Defined in:
lib/flexmls_api/authentication/api_auth.rb

Overview

ApiAuth

Implementation the BaseAuth interface for API style authentication

Instance Attribute Summary

Attributes inherited from BaseAuth

#session

Instance Method Summary collapse

Methods inherited from BaseAuth

#authenticated?, #build_url_parameters

Constructor Details

#initialize(client) ⇒ ApiAuth

Returns a new instance of ApiAuth.



14
15
16
# File 'lib/flexmls_api/authentication/api_auth.rb', line 14

def initialize(client)
  super(client)
end

Instance Method Details

#authenticateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flexmls_api/authentication/api_auth.rb', line 18

def authenticate
  sig = sign("#{@client.api_secret}ApiKey#{@client.api_key}")
  FlexmlsApi.logger.debug("Authenticating to #{@client.endpoint}")
  start_time = Time.now
  request_path = "/#{@client.version}/session?ApiKey=#{@client.api_key}&ApiSig=#{sig}"
  resp = @client.connection(true).post request_path, ""
  request_time = Time.now - start_time
  FlexmlsApi.logger.info("[#{(request_time * 1000).to_i}ms] Api: POST #{request_path}")
  FlexmlsApi.logger.debug("Authentication Response: #{resp.inspect}")
  @session = Session.new(resp.body.results.first)
  FlexmlsApi.logger.debug("Authentication: #{@session.inspect}")
  @session
end

#build_param_string(param_hash) ⇒ Object

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/flexmls_api/authentication/api_auth.rb', line 39

def build_param_string(param_hash)
  return "" if param_hash.nil?
    sorted = param_hash.sort do |a,b|
      a.to_s <=> b.to_s
    end
    params = ""
    sorted.each do |key,val|
      params += key.to_s + val.to_s
    end
    params
end

#logoutObject



32
33
34
35
# File 'lib/flexmls_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) ⇒ Object

Perform an HTTP request (no data)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/flexmls_api/authentication/api_auth.rb', line 64

def request(method, path, body, options)
  escaped_path = URI.escape(path)
  request_opts = {
    :AuthToken => @session.auth_token
  }
  unless @client.api_user.nil?
    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))}"
  FlexmlsApi.logger.debug("Request: #{request_path}")
  if body.nil?
    response = @client.connection.send(method, request_path)
  else
    FlexmlsApi.logger.debug("Data: #{body}")
    response = @client.connection.send(method, request_path, body)
  end
  response
end

#sign(sig) ⇒ Object

Sign a request



52
53
54
# File 'lib/flexmls_api/authentication/api_auth.rb', line 52

def sign(sig)
  Digest::MD5.hexdigest(sig)
end

#sign_token(path, params = {}, post_data = "") ⇒ Object

Sign a request with request data.



57
58
59
60
61
# File 'lib/flexmls_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