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

Class Method 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

Options Hash (params_sent):

  • URL (token_url)

    to retrieve OAuth token from. @Note this can be set globally instead of here

  • Client (client_id)

    ID

  • Client (client_secret)

    Secret

  • Username (username)

    used in password grant

  • Password (password)

    used in password grant

  • Security (security_token)

    Token used in password grant

Raises:

  • (ArgumentError)


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_tokensObject



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_urlsObject

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_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


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).

Returns:

  • (Boolean)

    Whether to include request message describing OAuth (either full or simplified)



29
30
31
# File 'lib/soaspec/o_auth2.rb', line 29

def request_message=(value)
  @request_message = value
end

.token_urlObject

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

#paramsObject



43
44
45
# File 'lib/soaspec/o_auth2.rb', line 43

def params
  @params
end

#retry_countObject



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.

Returns:

  • (Boolean)

    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).

Returns:

  • (Boolean)

    Whether to include request message describing OAuth (either full or simplified)



37
38
39
# File 'lib/soaspec/o_auth2.rb', line 37

def request_message?
  @request_message
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



84
85
86
87
88
89
90
91
92
# File 'lib/soaspec/o_auth2.rb', line 84

def access_token
  Soaspec::SpecLogger.info request_message if self.class.request_message?
  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

Returns:

  • (Boolean)

    Whether to see params sent to & received from oauth URL



71
72
73
# File 'lib/soaspec/o_auth2.rb', line 71

def debug_oauth?
  self.class.debug_oauth?
end

#instance_urlString

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

Returns:

  • (String)

    Instance url



79
80
81
# File 'lib/soaspec/o_auth2.rb', line 79

def instance_url
  Soaspec::OAuth2.instance_urls[params] ||= response['instance_url']
end

#passwordString

Returns Password to use in OAuth request.

Returns:

  • (String)

    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

#payloadHash

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

Returns:

  • (Hash)

    Payload for retrieving OAuth access token



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_messageString

Returns String to represent OAuth for logging logs.

Returns:

  • (String)

    String to represent OAuth for logging logs



110
111
112
113
114
115
116
# File 'lib/soaspec/o_auth2.rb', line 110

def request_message
  if 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



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