class IntuitOAuth::Utils

Public Class Methods

build_response_object(response) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# File lib/intuit-oauth/utils.rb, line 24
def self.scopes_to_string(scopes)
  scopes_str = scopes.join(' ')
  scopes_str.chomp(' ')
end