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_Endpoints, Login::REGIONS, Login::USER_AGENT, 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_header_type, #entity_id, #entity_identifier, #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, #entity_header, environments, #error_logger, #errors_via_content_type, #exception_args, #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, #insert_entity_header, #log, #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



29
30
31
32
# File 'lib/zuora_api/logins/oauth.rb', line 29

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zuora_api/logins/oauth.rb', line 53

def get_bearer_token(zuora_track_id: nil)
  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?
  headers['X-Amzn-Trace-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,
    timeout_retry: true,
    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
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zuora_api/logins/oauth.rb', line 34

def get_z_session(debug: false, zuora_track_id: nil)
  headers = self.entity_header
  headers['Zuora-Track-Id'] = zuora_track_id if zuora_track_id.present?
  headers['X-Amzn-Trace-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: {
        status: response.code,
        params: response.body.to_s,
        headers: response.headers.to_s,
      }
    })
    raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("Failure Parsing Cookie Headers", response)
  end 
end

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



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

def new_session(auth_type: nil, zuora_track_id: nil)
  super do
    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)
    else
      get_bearer_token(zuora_track_id: zuora_track_id) if self.oauth_expired?
      get_z_session(zuora_track_id: zuora_track_id) 
    end
    return self.status
  end
end

#oauth_expired?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/zuora_api/logins/oauth.rb', line 77

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