Class: Koinz::OAuth2Client::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/koinz/omniauth_client.rb

Class Method Summary collapse

Class Method Details

.oauth_call(access_token, host, uri, params = {}) ⇒ Object

TODO: Do we need to support Http-post? Example:

Koinz::OAuth2Client.Application.oauth_call(auth_token, MERCHANT_APP, 
                                          '/merchant', :name => 'name'

Parameters:

  • the (String)

    access token

  • HOST (String)

    to be contacted, eg. ‘merchant.koinz.com

  • uri (String)

    for invocation on remote host (ONLY JSON supported)

  • params (Hash) (defaults to: {})

    to be passed in the URL



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/koinz/omniauth_client.rb', line 36

def self.oauth_call(access_token, host, uri, params = {})
  # Prepare the entire URL
  request_uri = "#{host}/#{uri}?access_token=#{access_token}"

  # Add the params
  params.each_pair do |k,v|
    request_uri += "&#{k}=#{v}"
  end

  response = Net::HTTP.get_response(URI.parse(request_uri))

  # Raise an exception if response is not valid
  # A redirect implies token is not valid
  raise OAuth2::AccessDenied.new if ['302', '301', '401'].include?(response.code)

  result = ActiveSupport::JSON.decode(response.body)
  return result

  rescue Exception => e
    return { :error => e.message }
end

.two_legged_call(host, uri, params = {}) ⇒ Object

This is a method with which any application can make a 2-legged oauth call to the other apps. Its caters to token-expiry and exceptions. In case of error, it returns Json: { :error => <err-message> }



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/koinz/omniauth_client.rb', line 62

def self.two_legged_call(host, uri, params = {})
  result = {}
  3.times do |attempts|
    @@SECURE_TOKEN ||= secure_token
    result = oauth_call(@@SECURE_TOKEN.token, host, uri, params)

    if result.is_a?(Hash) and result[:error] && result[:error] =~ /OAuth2::AccessDenied/
      # OAuth2::AccessDenied (Received HTTP 401 during request.)
      # Token expired -- refresh and retry

      @@SECURE_TOKEN = secure_token
    else
      break
    end
  end
  return result
end