Class: ZuoraAPI::Oauth
Constant Summary
Constants inherited from Login
Login::CONNECTION_EXCEPTIONS, Login::CONNECTION_READ_EXCEPTIONS, Login::ENVIRONMENTS, Login::MIN_Endpoint, Login::REGIONS, Login::XML_SAVE_OPTIONS
Instance Attribute Summary collapse
-
#bearer_token ⇒ Object
Returns the value of attribute bearer_token.
-
#oauth_client_id ⇒ Object
Returns the value of attribute oauth_client_id.
-
#oauth_secret ⇒ Object
Returns the value of attribute oauth_secret.
-
#oauth_session_expires_at ⇒ Object
Returns the value of attribute oauth_session_expires_at.
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
- #get_active_bearer_token ⇒ Object
- #get_bearer_token ⇒ Object
- #get_z_session(debug: false) ⇒ Object
-
#initialize(oauth_client_id: nil, oauth_secret: nil, bearer_token: nil, oauth_session_expires_at: nil, **keyword_args) ⇒ Oauth
constructor
A new instance of Oauth.
- #new_session(raise_errors: false, auth_type: nil) ⇒ Object
- #oauth_expired? ⇒ Boolean
Methods inherited from Login
#aqua_endpoint, #aqua_query, #checkJRStatus, #createJournalRun, #dateFormat, #describe_call, endpoints, environments, #fileURL, #getDataSourceExport, #getFileById, #get_catalog, #get_entity_id, #get_file, #get_full_nav, #get_identity, #get_oauth_client, #get_session, #get_zconnect_accesstoken, #is_prod_env, #query, #raise_errors, #refresh_nav, regions, #reporting_url, #rest_call, #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 if !bearer_token.blank? self.oauth_session_expires_at = oauth_session_expires_at if !oauth_session_expires_at.blank? raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Request Oauth Login but either 'Oauth Client Id' or 'Oauth Secret' were not passed") if @oauth_client_id.blank? || @oauth_secret.blank? super end |
Instance Attribute Details
#bearer_token ⇒ Object
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_id ⇒ Object
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_secret ⇒ Object
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_at ⇒ Object
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 |
Instance Method Details
#get_active_bearer_token ⇒ Object
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 ⇒ Object
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 |
# File 'lib/zuora_api/logins/oauth.rb', line 67 def get_bearer_token tries ||= 2 output_json, response = self.rest_call(:method => :post, :url => self.rest_endpoint.chomp('v1/').concat("oauth/token"), :z_session => false, :session_type => :bearer, :headers => {"content-type" => "application/x-www-form-urlencoded"}, :body => {"client_id"=> self.oauth_client_id, "client_secret"=>self.oauth_secret, "grant_type" =>"client_credentials"} ) self.bearer_token = output_json["access_token"] 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::ZuoraAPISessionError => ex self.bearer_token = nil self.oauth_session_expires_at = nil self.current_error = "Invalid login, please check client ID and Client Secret or URL endpoint" self.status = 'Invalid Login' return self.status rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition => ex if errors.include?(ex.class) raise ex else return [output_json, response] end rescue *(CONNECTION_EXCEPTIONS).concat(CONNECTION_READ_EXCEPTIONS) => 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 = "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) ⇒ 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 |
# File 'lib/zuora_api/logins/oauth.rb', line 32 def get_z_session(debug: false) tries ||= 2 headers = self.entity_id.present? ? {"Zuora-Entity-Ids" => self.entity_id } : {} output_json, response = self.rest_call(:url => self.rest_endpoint("connections"), :session_type => :bearer, :headers => headers) self.current_session = response.headers.to_h['set-cookie'][0].split(';')[0].split('=',2)[1].gsub('%3D', '=') rescue ZuoraAPI::Exceptions::ZuoraAPISessionError => ex if !(tries -= 1).zero? Rails.logger.debug {"Session Invalid"} self.new_session(auth_type: :bearer) retry else if errors.include?(ex.class) raise ex else return [output_json, response] end end rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition => ex if errors.include?(ex.class) raise ex else return [output_json, response] end rescue *(CONNECTION_EXCEPTIONS).concat(CONNECTION_READ_EXCEPTIONS) => 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 |
#new_session(raise_errors: false, auth_type: 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(raise_errors: false, auth_type: nil) if auth_type == :bearer get_bearer_token() elsif auth_type == :basic get_bearer_token() if self.oauth_expired? get_z_session() else get_bearer_token() get_z_session() end return self.status end |
#oauth_expired? ⇒ Boolean
107 108 109 |
# File 'lib/zuora_api/logins/oauth.rb', line 107 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 |