Class: SparkApi::Authentication::ApiAuth
- Defined in:
- lib/spark_api/authentication/api_auth.rb
Overview
ApiAuth
Implementation the BaseAuth interface for API style authentication
Instance Attribute Summary
Attributes inherited from BaseAuth
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#build_param_string(param_hash) ⇒ Object
Builds an ordered list of key value pairs and concatenates it all as one big string.
-
#initialize(client) ⇒ ApiAuth
constructor
A new instance of ApiAuth.
- #logout ⇒ Object
-
#request(method, path, body, options) ⇒ Object
Perform an HTTP request (no data).
-
#sign(sig) ⇒ Object
Sign a request.
-
#sign_token(path, params = {}, post_data = "") ⇒ Object
Sign a request with request data.
Methods inherited from BaseAuth
#authenticated?, #build_url_parameters
Constructor Details
#initialize(client) ⇒ ApiAuth
Returns a new instance of ApiAuth.
14 15 16 |
# File 'lib/spark_api/authentication/api_auth.rb', line 14 def initialize(client) super(client) end |
Instance Method Details
#authenticate ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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) ⇒ 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/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 ⇒ Object
32 33 34 35 |
# 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) ⇒ 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 84 85 86 87 |
# File 'lib/spark_api/authentication/api_auth.rb', line 64 def request(method, path, body, ) escaped_path = Addressable::URI.escape(path) connection = @client.connection connection.headers.merge!(.delete(:override_headers) || {}) request_opts = { :AuthToken => @session.auth_token } unless (@client.api_user.nil? || [:ApiUser]) request_opts.merge!(:ApiUser => "#{@client.api_user}") end request_opts.merge!() 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) ⇒ Object
Sign a request
52 53 54 |
# File 'lib/spark_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/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 |