Class: ZuoraAPI::Oauth

Inherits:
Login
  • Object
show all
Defined in:
lib/zuora_api/logins/oauth.rb

Constant Summary

Constants inherited from Login

Login::CONNECTION_EXCEPTIONS, Login::CONNECTION_READ_EXCEPTIONS, Login::ENVIRONMENTS, Login::MIN_Endpoint, Login::REGIONS, Login::XML_SAVE_OPTIONS, Login::ZUORA_API_ERRORS, Login::ZUORA_SERVER_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Login

#current_error, #current_session, #entity_id, #environment, #errors, #hostname, #region, #status, #tenant_id, #tenant_name, #timeout_sleep, #url, #user_info, #wsdl_number, #zconnect_provider

Instance Method Summary collapse

Methods inherited from Login

#aqua_endpoint, #aqua_query, #checkJRStatus, #createJournalRun, #dateFormat, #describe_call, endpoints, environments, #errors_via_content_type, #fileURL, #getDataSourceExport, #getFileById, #get_catalog, #get_entity_id, #get_file, #get_full_nav, #get_identity, #get_oauth_client, #get_session, #get_soap_error_and_message, #query, #raise_errors, #raise_errors_helper, #refresh_nav, regions, #reporting_url, #reset_files, #rest_call, #rest_domain, #rest_endpoint, #set_nav, #soap_call, #update_create_tenant, #update_environment, #update_region, #update_zconnect_provider

Constructor Details

#initialize(oauth_client_id: nil, oauth_secret: nil, bearer_token: nil, oauth_session_expires_at: nil, **keyword_args) ⇒ Oauth

Returns a new instance of Oauth.



5
6
7
8
9
10
11
12
# File 'lib/zuora_api/logins/oauth.rb', line 5

def initialize(oauth_client_id: nil, oauth_secret: nil, bearer_token: nil, oauth_session_expires_at: nil, **keyword_args)
  self.oauth_client_id = oauth_client_id
  self.oauth_secret = oauth_secret
  self.bearer_token = bearer_token
  self.oauth_session_expires_at = oauth_session_expires_at
  raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Request Oauth Login but either 'Oauth Client Id' or 'Oauth Secret' were not passed") if self.bearer_token.blank? && (self.oauth_client_id.blank? || self.oauth_secret.blank?)
  super
end

Instance Attribute Details

#bearer_tokenObject

Returns the value of attribute bearer_token.



3
4
5
# File 'lib/zuora_api/logins/oauth.rb', line 3

def bearer_token
  @bearer_token
end

#oauth_client_idObject

Returns the value of attribute oauth_client_id.



3
4
5
# File 'lib/zuora_api/logins/oauth.rb', line 3

def oauth_client_id
  @oauth_client_id
end

#oauth_secretObject

Returns the value of attribute oauth_secret.



3
4
5
# File 'lib/zuora_api/logins/oauth.rb', line 3

def oauth_secret
  @oauth_secret
end

#oauth_session_expires_atObject

Returns the value of attribute oauth_session_expires_at.



3
4
5
# File 'lib/zuora_api/logins/oauth.rb', line 3

def oauth_session_expires_at
  @oauth_session_expires_at
end

#scope_entitiesObject

Returns the value of attribute scope_entities.



3
4
5
# File 'lib/zuora_api/logins/oauth.rb', line 3

def scope_entities
  @scope_entities
end

Instance Method Details

#get_active_bearer_tokenObject



27
28
29
30
# File 'lib/zuora_api/logins/oauth.rb', line 27

def get_active_bearer_token
  self.get_bearer_token if self.oauth_expired?
  return self.bearer_token
end

#get_bearer_token(zuora_track_id: nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/zuora_api/logins/oauth.rb', line 73

def get_bearer_token(zuora_track_id: nil)
  tries ||= 2
  raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Request Oauth Login but either 'Oauth Client Id' or 'Oauth Secret' were not passed") if self.oauth_client_id.blank? || self.oauth_secret.blank?

  headers = { "content-type" => "application/x-www-form-urlencoded" }
  headers['Zuora-Track-Id'] = zuora_track_id if zuora_track_id.present?

  output_json, response = self.rest_call(:method => :post, 
    :url => self.rest_endpoint.chomp('v1/').concat("oauth/token"), 
    :z_session => false,
    :session_type => :bearer,
    :headers => headers,
    :body => {"client_id"=> self.oauth_client_id, "client_secret"=>self.oauth_secret, "grant_type" =>"client_credentials"}
  )

  self.bearer_token = output_json["access_token"]
  self.scope_entities = output_json.fetch('scope', '').split(" ").map { |scope| scope.split('.').last.gsub('-', '') if scope.include?('entity.') }.compact.uniq
  self.oauth_session_expires_at = Time.now.to_i + output_json["expires_in"].to_i
  self.current_error = nil
  self.status = 'Active'

  return self.status

rescue ZuoraAPI::Exceptions::ZuoraAPIInternalServerError => ex
  raise ex if tries.zero?

  tries -= 1
  sleep(self.timeout_sleep)
  retry
rescue ZuoraAPI::Exceptions::ZuoraAPISessionError => ex
  self.bearer_token = nil
  self.oauth_session_expires_at = nil
  self.current_error = ex.message
  case ex.message
  when "Forbidden"
    self.current_error = "The user associated to OAuth credential set has been deactivated."
    self.status = 'Deactivated'
  else
    self.current_error = "Invalid login, please check client ID and Client Secret or URL endpoint"
    self.status = 'Invalid Login'
  end

  return self.status
rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition  => ex
  raise ex
rescue *(CONNECTION_EXCEPTIONS + CONNECTION_READ_EXCEPTIONS) => ex
  if !tries.zero?
    tries -= 1
    sleep(self.timeout_sleep)
    retry
  else
    if Rails.logger.class.to_s == "Ougai::Logger"
      Rails.logger.error("OAuthLogin - Timed out will retry after #{self.timeout_sleep} seconds", ex) 
    else
      Rails.logger.error("OAuthLogin - #{ex.class} Timed out will retry after #{self.timeout_sleep} seconds") 
    end
    self.current_error = "Invalid login, please check client ID and Client Secret or URL endpoint"
    self.status = 'Timeout'
    return self.status
  end
end

#get_z_session(debug: false, zuora_track_id: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zuora_api/logins/oauth.rb', line 32

def get_z_session(debug: false, zuora_track_id: nil)
  tries ||= 2
  headers = self.entity_id.present? ? {"Zuora-Entity-Ids" => self.entity_id } : {}
  headers['Zuora-Track-Id'] = zuora_track_id if zuora_track_id.present?
  output_json, response = self.rest_call(:url => self.rest_endpoint("connections"), :session_type => :bearer, :headers => headers)
  begin
    self.current_session = response.headers.to_h['set-cookie'][0].split(';')[0].split('=',2)[1].gsub('%3D', '=')
  rescue NoMethodError => ex 
    Rails.logger.fatal("Failure Parsing Cookie Headers", response.headers.to_s)
    raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Failure Parsing Cookie Headers")
  end 
rescue ZuoraAPI::Exceptions::ZuoraAPISessionError => ex
  if !tries.zero?
    tries -= 1
    Rails.logger.debug {"Session Invalid"}
    self.new_session(auth_type: :bearer)
    retry
  end
  raise ex if errors.include?(ex.class)
  return [output_json, response]

rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition  => ex
  raise ex if errors.include?(ex.class)
  return [output_json, response]

rescue *(CONNECTION_EXCEPTIONS + CONNECTION_READ_EXCEPTIONS) => ex
  if !tries.zero?
    tries -= 1
    sleep(self.timeout_sleep)
    retry
  end
  if Rails.logger.class.to_s == "Ougai::Logger"
    Rails.logger.error("OAuthLogin - Timed out", ex) 
  else
    Rails.logger.error("OAuthLogin - #{ex.class} Timed out") 
  end
  self.current_error = "Request timed out. Try again"
  self.status = 'Timeout'
  return self.status
end

#new_session(auth_type: nil, zuora_track_id: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zuora_api/logins/oauth.rb', line 14

def new_session(auth_type: nil, zuora_track_id: nil)
  if auth_type ==  :bearer
    get_bearer_token(zuora_track_id: zuora_track_id)
  elsif auth_type == :basic
    get_bearer_token(zuora_track_id: zuora_track_id) if self.oauth_expired?
    get_z_session(zuora_track_id: zuora_track_id) if self.status == 'Active'
  else
    get_bearer_token(zuora_track_id: zuora_track_id)
    get_z_session(zuora_track_id: zuora_track_id) if self.status == 'Active'
  end
  return self.status
end

#oauth_expired?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/zuora_api/logins/oauth.rb', line 135

def oauth_expired?
  return (self.oauth_session_expires_at.blank? || self.bearer_token.blank?) ? true : (self.oauth_session_expires_at.to_i < Time.now.to_i)
end