Class: Soaspec::OAuth2
- Inherits:
-
Object
- Object
- Soaspec::OAuth2
- Defined in:
- lib/soaspec/o_auth2.rb
Overview
Handles working with OAuth2
Class Attribute Summary collapse
-
.token_url ⇒ Object
Default token url used across entire suite.
Instance Attribute Summary collapse
-
#params ⇒ Object
- Hash
-
OAuth parameters.
-
#retry_count ⇒ Object
Count of tries to obtain access token.
Instance Method Summary collapse
-
#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.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/soaspec/o_auth2.rb', line 18 def initialize(params_sent, api_username = nil) params = params_sent.transform_keys_to_symbols params[:token_url] ||= Soaspec::OAuth2.token_url raise '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
.token_url ⇒ Object
Default token url used across entire suite
8 9 10 |
# File 'lib/soaspec/o_auth2.rb', line 8 def token_url @token_url end |
Instance Attribute Details
#params ⇒ Object
- Hash
-
OAuth parameters
12 13 14 |
# File 'lib/soaspec/o_auth2.rb', line 12 def params @params end |
#retry_count ⇒ Object
Count of tries to obtain access token
14 15 16 |
# File 'lib/soaspec/o_auth2.rb', line 14 def retry_count @retry_count end |
Instance Method Details
#password ⇒ String
Returns Password to use in OAuth request.
56 57 58 |
# File 'lib/soaspec/o_auth2.rb', line 56 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
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/soaspec/o_auth2.rb', line 62 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.
47 48 49 50 51 52 53 |
# File 'lib/soaspec/o_auth2.rb', line 47 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/soaspec/o_auth2.rb', line 32 def response 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}"]) Soaspec::SpecLogger.info "oauth_params_used: #{params}" if Soaspec.debug_oauth? 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 |