Class: Soaspec::OAuth2
- Inherits:
-
Object
- Object
- Soaspec::OAuth2
- Defined in:
- lib/soaspec/o_auth2.rb
Overview
Handles working with OAuth2
Class Attribute Summary collapse
- .access_tokens ⇒ Object
-
.debug_oauth ⇒ Object
writeonly
Specify whether to see params sent to and retrieved from oauth.
-
.instance_urls ⇒ Object
List of URLs to that define the instance of an application.
-
.refresh_token ⇒ Object
Values are: * :always - (Default) Request token from token url every time it is needed * :once - Request token once for the entire execution of the suite.
-
.request_message ⇒ Boolean
writeonly
Whether to include request message describing OAuth (either full or simplified).
-
.token_url ⇒ Object
Default token url used across entire suite.
Instance Attribute Summary collapse
Class Method Summary collapse
-
.debug_oauth? ⇒ Boolean
Whether to see params sent to & received from oauth URL.
-
.request_message? ⇒ Boolean
Whether to include request message describing OAuth (either full or simplified).
Instance Method Summary collapse
-
#access_token ⇒ String
Existing or new access token, dependent on refresh_token attribute.
-
#debug_oauth? ⇒ Boolean
Retrieve whether to debug oauth parameters based on global settings.
-
#initialize(params_sent, api_username = nil) ⇒ OAuth2
constructor
A new instance of OAuth2.
-
#instance_url ⇒ String
Retrieve instance_url according to access token response.
-
#password ⇒ String
Password to use in OAuth request.
-
#payload ⇒ Hash
Payload to add to o-auth request dependent on params provided.
-
#request_message ⇒ String
String to represent OAuth for logging logs.
-
#response ⇒ Hash
Hash containing access token parameters.
Constructor Details
#initialize(params_sent, api_username = nil) ⇒ OAuth2
Returns a new instance of OAuth2.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/soaspec/o_auth2.rb', line 55 def initialize(params_sent, api_username = nil) self.retry_count = 0 # No initial tries at getting access token params = params_sent.transform_keys_to_symbols params[:token_url] ||= Soaspec::OAuth2.token_url raise ArgumentError, 'client_id and client_secret not set' unless params[:client_id] && params[:client_secret] raise ArgumentError, 'token_url mandatory' unless params[:token_url] self.params = params params[:username] = api_username || ERB.new(params[:username]).result(binding) if params[:username] params[:security_token] = ERB.new(params[:security_token]).result(binding) if params[:security_token] params[:token_url] = ERB.new(params[:token_url]).result(binding) if params[:token_url] params[:password] = ERB.new(params[:password]).result(binding) if params[:password] end |
Class Attribute Details
.access_tokens ⇒ Object
23 24 25 |
# File 'lib/soaspec/o_auth2.rb', line 23 def access_tokens @access_tokens end |
.debug_oauth=(value) ⇒ Object (writeonly)
Specify whether to see params sent to and retrieved from oauth. This will put password in log file, only recommended for debugging
27 28 29 |
# File 'lib/soaspec/o_auth2.rb', line 27 def debug_oauth=(value) @debug_oauth = value end |
.instance_urls ⇒ Object
List of URLs to that define the instance of an application
25 26 27 |
# File 'lib/soaspec/o_auth2.rb', line 25 def instance_urls @instance_urls end |
.refresh_token ⇒ Object
Values are:
* :always - (Default) Request token from token url every time it is needed
* :once - Request token once for the entire execution of the suite
21 22 23 |
# File 'lib/soaspec/o_auth2.rb', line 21 def refresh_token @refresh_token end |
.request_message=(value) ⇒ Boolean (writeonly)
Returns Whether to include request message describing OAuth (either full or simplified).
29 30 31 |
# File 'lib/soaspec/o_auth2.rb', line 29 def (value) = value end |
.token_url ⇒ Object
Default token url used across entire suite
16 17 18 |
# File 'lib/soaspec/o_auth2.rb', line 16 def token_url @token_url end |
Instance Attribute Details
#params ⇒ Object
43 44 45 |
# File 'lib/soaspec/o_auth2.rb', line 43 def params @params end |
#retry_count ⇒ Object
45 46 47 |
# File 'lib/soaspec/o_auth2.rb', line 45 def retry_count @retry_count end |
Class Method Details
.debug_oauth? ⇒ Boolean
Returns Whether to see params sent to & received from oauth URL.
32 33 34 |
# File 'lib/soaspec/o_auth2.rb', line 32 def debug_oauth? @debug_oauth || false end |
.request_message? ⇒ Boolean
Returns Whether to include request message describing OAuth (either full or simplified).
37 38 39 |
# File 'lib/soaspec/o_auth2.rb', line 37 def end |
Instance Method Details
#access_token ⇒ String
Returns Existing or new access token, dependent on refresh_token attribute.
84 85 86 87 88 89 90 91 92 |
# File 'lib/soaspec/o_auth2.rb', line 84 def access_token Soaspec::SpecLogger.info if self.class. case Soaspec::OAuth2.refresh_token when :once Soaspec::OAuth2.access_tokens[params] ||= response['access_token'] else # Default is :always response['access_token'] end end |
#debug_oauth? ⇒ Boolean
Retrieve whether to debug oauth parameters based on global settings
71 72 73 |
# File 'lib/soaspec/o_auth2.rb', line 71 def debug_oauth? self.class.debug_oauth? end |
#instance_url ⇒ String
Retrieve instance_url according to access token response. Some applications have a different instance It’s assumed this will be constant for a set of oauth parameters
79 80 81 |
# File 'lib/soaspec/o_auth2.rb', line 79 def instance_url Soaspec::OAuth2.instance_urls[params] ||= response['instance_url'] end |
#password ⇒ String
Returns Password to use in OAuth request.
119 120 121 |
# File 'lib/soaspec/o_auth2.rb', line 119 def password params[:security_token] ? (params[:password] + params[:security_token]) : params[:password] end |
#payload ⇒ Hash
Payload to add to o-auth request dependent on params provided
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/soaspec/o_auth2.rb', line 125 def payload payload = { client_id: params[:client_id], client_secret: params[:client_secret] } payload.merge(if params[:password] && params[:username] { grant_type: 'password', username: params[:username], password: password, multipart: true } else { grant_type: 'client_credentials' } end) end |
#request_message ⇒ String
Returns String to represent OAuth for logging logs.
110 111 112 113 114 115 116 |
# File 'lib/soaspec/o_auth2.rb', line 110 def if debug_oauth? "request_params: #{payload}" else params[:username] ? "User '#{params[:username]}'" : 'client_credentials' end end |
#response ⇒ Hash
Returns Hash containing access token parameters.
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/soaspec/o_auth2.rb', line 95 def response Soaspec::SpecLogger.info "using oauth_params: #{params}" if debug_oauth? response = RestClient.post(params[:token_url], payload, cache_control: 'no_cache', verify_ssl: false) rescue RestClient::Exception => error Soaspec::SpecLogger.info(["oauth_error: #{error.message}", "oauth_response: #{error.response}"]) self.retry_count += 1 sleep 0.1 # Wait if a bit before retying obtaining access token retry if retry_count < 3 raise error else Soaspec::SpecLogger.info(["response_headers: #{response.headers}", "response_body: #{response.body}"]) if debug_oauth? JSON.parse(response) end |