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
-
.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.
-
.token_url ⇒ Object
Default token url used across entire suite.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#access_token ⇒ String
Existing or new access token, dependent on refresh_token attribute.
-
#initialize(params_sent, api_username = nil) ⇒ OAuth2
constructor
A new instance of OAuth2.
-
#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.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/soaspec/o_auth2.rb', line 35 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] Soaspec::SpecLogger.info end |
Class Attribute Details
.access_tokens ⇒ Object
19 20 21 |
# File 'lib/soaspec/o_auth2.rb', line 19 def access_tokens @access_tokens 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
17 18 19 |
# File 'lib/soaspec/o_auth2.rb', line 17 def refresh_token @refresh_token end |
.token_url ⇒ Object
Default token url used across entire suite
12 13 14 |
# File 'lib/soaspec/o_auth2.rb', line 12 def token_url @token_url end |
Instance Attribute Details
#params ⇒ Object
23 24 25 |
# File 'lib/soaspec/o_auth2.rb', line 23 def params @params end |
#retry_count ⇒ Object
25 26 27 |
# File 'lib/soaspec/o_auth2.rb', line 25 def retry_count @retry_count end |
Instance Method Details
#access_token ⇒ String
Returns Existing or new access token, dependent on refresh_token attribute.
50 51 52 53 54 55 56 57 |
# File 'lib/soaspec/o_auth2.rb', line 50 def access_token case Soaspec::OAuth2.refresh_token when :once Soaspec::OAuth2.access_tokens[params] ||= response['access_token'] else # Default is :always response['access_token'] end end |
#password ⇒ String
Returns Password to use in OAuth request.
84 85 86 |
# File 'lib/soaspec/o_auth2.rb', line 84 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
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/soaspec/o_auth2.rb', line 90 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.
75 76 77 78 79 80 81 |
# File 'lib/soaspec/o_auth2.rb', line 75 def if Soaspec.debug_oauth? "request_params: #{payload}" else params[:username] ? "User '#{params[:username]}'" : 'client_credentials' end end |
#response ⇒ Hash
Returns Hash containing access token parameters.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/soaspec/o_auth2.rb', line 60 def response Soaspec::SpecLogger.info "using oauth_params: #{params}" if Soaspec.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.}", "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 Soaspec.debug_oauth? JSON.parse(response) end |