Class: Soaspec::OAuth2

Inherits:
Object
  • Object
show all
Defined in:
lib/soaspec/o_auth2.rb

Overview

Handles working with OAuth2

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params_sent, api_username = nil) ⇒ OAuth2

Returns a new instance of OAuth2.

Parameters:

  • params_sent (Hash)

    Parameters to make OAuth request

  • api_username (String) (defaults to: nil)

    Username to use which can be set by Soaspec::ExchangeHandler

Raises:

  • (ArgumentError)


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 request_message
end

Class Attribute Details

.access_tokensObject



19
20
21
# File 'lib/soaspec/o_auth2.rb', line 19

def access_tokens
  @access_tokens
end

.refresh_tokenObject

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_urlObject

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

#paramsObject



23
24
25
# File 'lib/soaspec/o_auth2.rb', line 23

def params
  @params
end

#retry_countObject



25
26
27
# File 'lib/soaspec/o_auth2.rb', line 25

def retry_count
  @retry_count
end

Instance Method Details

#access_tokenString

Returns Existing or new access token, dependent on refresh_token attribute.

Returns:

  • (String)

    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

#passwordString

Returns Password to use in OAuth request.

Returns:

  • (String)

    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

#payloadHash

Payload to add to o-auth request dependent on params provided

Returns:

  • (Hash)

    Payload for retrieving OAuth access token



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_messageString

Returns String to represent OAuth for logging logs.

Returns:

  • (String)

    String to represent OAuth for logging logs



75
76
77
78
79
80
81
# File 'lib/soaspec/o_auth2.rb', line 75

def request_message
  if Soaspec.debug_oauth?
    "request_params: #{payload}"
  else
    params[:username] ? "User '#{params[:username]}'" : 'client_credentials'
  end
end

#responseHash

Returns Hash containing access token parameters.

Returns:

  • (Hash)

    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.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 Soaspec.debug_oauth?
  JSON.parse(response)
end