Class: ZuoraAPI::Basic

Inherits:
Login
  • Object
show all
Defined in:
lib/zuora_api_oauth_alpha/logins/basic.rb

Constant Summary

Constants inherited from Login

Login::ENVIRONMENTS, Login::REGIONS

Instance Attribute Summary collapse

Attributes inherited from Login

#authentication_type, #current_error, #current_session, #entity_id, #environment, #errors, #password, #region, #status, #tenant_id, #tenant_name, #timeout_sleep, #url, #user_info, #username, #wsdl_number

Instance Method Summary collapse

Methods inherited from Login

#aqua_endpoint, #aqua_query, #checkJRStatus, #createJournalRun, #dateFormat, #describe_call, endpoints, environments, #fileURL, #getDataSourceExport, #getFileById, #get_catalog, #get_file, #get_session, #query, #raise_errors, regions, #rest_call, #rest_endpoint, #soap_call, #update_create_tenant, #update_environment

Constructor Details

#initialize(**keyword_args) ⇒ Basic



5
6
7
# File 'lib/zuora_api_oauth_alpha/logins/basic.rb', line 5

def initialize(**keyword_args)
  super
end

Instance Attribute Details

#z_session_tokenObject

Returns the value of attribute z_session_token.



3
4
5
# File 'lib/zuora_api_oauth_alpha/logins/basic.rb', line 3

def z_session_token
  @z_session_token
end

Instance Method Details

#authentication_prefix(auth_type = :basic) ⇒ Object



17
18
19
20
# File 'lib/zuora_api_oauth_alpha/logins/basic.rb', line 17

def authentication_prefix(auth_type = :basic)
  return "ZSession "
  raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Basic Login, does not support Authentication of Type: #{auth_type}")
end

#check_session(auth_type = :basic) ⇒ Object



22
23
24
# File 'lib/zuora_api_oauth_alpha/logins/basic.rb', line 22

def check_session(auth_type = :basic)
  return @z_session_token.blank?
end

#new_session(auth_type = :basic) ⇒ Object



26
27
28
29
30
31
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
72
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
134
135
136
137
# File 'lib/zuora_api_oauth_alpha/logins/basic.rb', line 26

def new_session(auth_type = :basic)
  raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Basic Login, does not support Authentication of Type: #{auth_type}") if auth_type != :basic
  begin 
    tries ||= 2
    request = Nokogiri::XML::Builder.new do |xml|
      xml['SOAP-ENV'].Envelope('xmlns:SOAP-ENV' =>"http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:api' => "http://api.zuora.com/" ) do
        if (self.password.blank? && !@z_session_token.blank?)
          Rails.logger.debug("Method [Session]")
          xml['SOAP-ENV'].Header do
            xml['api'].SessionHeader do
              xml['api'].session @z_session_token
            end
          end
          xml['SOAP-ENV'].Body do
            xml['api'].getUserInfo
          end
        else
          xml['SOAP-ENV'].Header
          xml['SOAP-ENV'].Body do
            xml['api']. do
              xml['api'].username self.username
              xml['api'].password self.password
              xml['api'].entityId self.entity_id if !self.entity_id.blank?
            end
          end
        end
      end
    end
        
    input_xml = Nokogiri::XML(request.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip)
    input_xml.xpath('//ns1:session', 'ns1' =>'http://api.zuora.com/').children.remove
    Rails.logger.debug('Connect') {"Request SOAP XML: #{input_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip}"}# if debug
    
    @response_query = HTTParty.post(self.url,:body => request.to_xml, :headers => {'Content-Type' => "text/xml; charset=utf-8"}, :timeout => 10)
    @output_xml = Nokogiri::XML(@response_query.body)
    Rails.logger.debug('Connect') {"Response SOAP XML: #{@output_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip}"}# if debug

    if !@response_query.success?
      @z_session_token = nil
      if @output_xml.namespaces.size > 0  && @output_xml.xpath('//soapenv:Fault').size > 0
        self.current_error = @output_xml.xpath('//fns:FaultMessage', 'fns' =>'http://fault.api.zuora.com/').text
        if self.current_error.include?('deactivated')
          self.status = 'Deactivated'
          self.current_error = 'Deactivated user login, please check with Zuora tenant administrator'
          self.errors[:username] = self.current_error
        elsif self.current_error.include?('inactive')
          self.status = 'Inactive'
          self.current_error = 'Inactive user login, please check with Zuora tenant administrator'
          self.errors[:username] = self.current_error
        elsif self.current_error.include?("invalid username or password") || self.current_error.include?("Invalid login. User name and password do not match.")
          self.status = 'Invalid Login'
          self.current_error = 'Invalid login, please check username and password or URL endpoint'
          self.errors[:username] = self.current_error
          self.errors[:password] = self.current_error
        elsif self.current_error.include?('unsupported version')
          self.status = 'Unsupported API Version'
          self.current_error = 'Unsupported API version, please verify URL endpoint'
          self.errors[:url] = self.current_error
        elsif self.current_error.include?('invalid api version')
          self.status = 'Invalid API Version'
          self.current_error = 'Invalid API version, please verify URL endpoint'
          self.errors[:url] = self.current_error
        elsif self.current_error.include?('invalid session')
          self.status = 'Invalid Session'
          self.current_error = 'Session invalid, please update session and verify URL endpoint'
          self.errors[:session] = self.current_error
        elsif self.current_error.include?('Your IP address')
          self.status = 'Restricted IP'
          self.current_error = 'IP restricted, contact Zuora tenant administrator and remove IP restriction'
          self.errors[:base] = self.current_error
        elsif self.current_error.include?('This account has been locked')
          self.status = 'Locked'
          self.current_error = 'Locked user login, please wait or navigate to Zuora to unlock user'
          self.errors[:username] = self.current_error
        else
          self.status = 'Unknown'
          self.current_error =  @output_xml.xpath('//faultstring').text if self.current_error.blank?
          self.errors[:base] = self.current_error
        end
      else
        self.current_error = "Code = #{@response_query.code} Message = #{@response_query.body.to_s}"
        self.status = 'No Service'
      end
    else
      #If Session only is used for Gem TODO Depercate
      if (self.password.blank? && @z_session_token.present?)
        @z_session_token = @z_session_token
        self.username        = @output_xml.xpath('//ns1:Username', 'ns1' =>'http://api.zuora.com/').text if self.username.blank?
        
      #Username & password combo
      elsif (self.password.present? && self.username.present?)
        retrieved_session = @output_xml.xpath('//ns1:Session', 'ns1' =>'http://api.zuora.com/').text
        raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("No session found for api call.") if retrieved_session.blank?
        @z_session_token = retrieved_session
      end
      
      self.current_error     = nil
      self.status            = 'Active'
      return @z_session_token
    end
  rescue Net::ReadTimeout, Net::OpenTimeout, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex
    if !(tries -= 1).zero?
      Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"}
      sleep(self.timeout_sleep)
      retry
    else
      self.current_error = "Request timed out. Try again"
      self.status = 'Timeout'
      return self.status
    end
  end
end