Class: IntuitOAuth::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/intuit-oauth/utils.rb

Class Method Summary collapse

Class Method Details

.build_response_object(response) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/intuit-oauth/utils.rb', line 64

def self.build_response_object(response)
  url = response.request.last_uri.to_s
  if response.code != 200
    raise OAuth2ClientException.new(response)
  elsif url['openid_sandbox_configuration'] || url['openid_configuration'] || url['openid_connect/userinfo']
    response
  else
    IntuitOAuth::ClientResponse.new(response)
  end
end

.format_string_delimiter(params, delimiter, with_quotes = false) ⇒ Object



57
58
59
60
61
62
# File 'lib/intuit-oauth/utils.rb', line 57

def self.format_string_delimiter(params, delimiter, with_quotes=false)
  if with_quotes
    return params.map { |k, v| "#{k}=\"#{v}\"" }.join(delimiter)
  end
  params.map { |k, v| "#{k}=#{v}" }.join(delimiter)
end

.generate_random_string(length = 20) ⇒ Object



34
35
36
# File 'lib/intuit-oauth/utils.rb', line 34

def self.generate_random_string(length=20)
  Array.new(length){[*'A'..'Z', *'0'..'9', *'a'..'z'].sample}.join
end

.get_auth_header(client_id, client_secret) ⇒ Object



29
30
31
32
# File 'lib/intuit-oauth/utils.rb', line 29

def self.get_auth_header(client_id, client_secret)
  encoded = Base64.strict_encode64("#{client_id}:#{client_secret}")
  "Basic #{encoded}"
end

.get_oauth1_header(method, uri, oauth1_tokens, uri_params = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/intuit-oauth/utils.rb', line 38

def self.get_oauth1_header(method, uri, oauth1_tokens, uri_params={})
  params = {
    oauth_consumer_key: oauth1_tokens[:consumer_key],
    oauth_token: oauth1_tokens[:access_token],
    oauth_signature_method: 'HMAC-SHA1',
    oauth_timestamp: Time.now.getutc.to_i.to_s,
    oauth_nonce: generate_random_string(7),
    oauth_version: '1.0'
  }

  all_params = params.merge(uri_params).sort.to_h
  base_string = "#{method.upcase}&#{CGI.escape(uri)}&#{CGI.escape(all_params.to_param)}"
  key = "#{oauth1_tokens[:consumer_secret]}&#{oauth1_tokens[:access_secret]}"

  signature = CGI.escape(Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', key, base_string).to_s))
  params[:oauth_signature] = signature
  "OAuth #{format_string_delimiter(params, ',', true)}"
end

.scopes_to_string(scopes) ⇒ Object



24
25
26
27
# File 'lib/intuit-oauth/utils.rb', line 24

def self.scopes_to_string(scopes)
  scopes_str = scopes.join(' ')
  scopes_str.chomp(' ')
end