Class: Salesforce::Service

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/salesforce/service.rb

Direct Known Subclasses

Gibft::Service

Constant Summary collapse

SALESFORCE_HOST =
"https://#{Settings.salesforce.env == 'prod' ? 'login' : 'test'}.salesforce.com".freeze

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#with_monitoring

Methods inherited from Common::Client::Base

configuration, #raise_backend_exception

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger

Instance Method Details

#claim_setObject



20
21
22
23
24
25
26
27
# File 'lib/salesforce/service.rb', line 20

def claim_set
  {
    iss: self.class::CONSUMER_KEY,
    sub: self.class::SALESFORCE_USERNAME,
    aud: SALESFORCE_HOST,
    exp: Time.now.utc.to_i
  }
end

#get_clientObject



40
41
42
43
44
45
46
# File 'lib/salesforce/service.rb', line 40

def get_client
  Restforce.new(
    oauth_token: get_oauth_token,
    instance_url: self.class.module_parent::Configuration::SALESFORCE_INSTANCE_URL,
    api_version: '41.0'
  )
end

#get_oauth_tokenObject



33
34
35
36
37
38
# File 'lib/salesforce/service.rb', line 33

def get_oauth_token
  body = with_monitoring { request(:post, '', oauth_params).body }
  Raven.extra_context(oauth_response_body: body)

  body['access_token']
end

#jwt_bearer_tokenObject



16
17
18
# File 'lib/salesforce/service.rb', line 16

def jwt_bearer_token
  JWT.encode(claim_set, private_key, 'RS256')
end

#oauth_paramsObject



9
10
11
12
13
14
# File 'lib/salesforce/service.rb', line 9

def oauth_params
  {
    grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    assertion: jwt_bearer_token
  }
end

#private_keyObject



29
30
31
# File 'lib/salesforce/service.rb', line 29

def private_key
  OpenSSL::PKey::RSA.new(File.read(self.class::SIGNING_KEY_PATH))
end