Class: CARMA::Client::MuleSoftAuthTokenClient

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/carma/client/mule_soft_auth_token_client.rb

Defined Under Namespace

Classes: GetAuthTokenError

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.carma.mulesoft.auth'
GRANT_TYPE =
'client_credentials'
SCOPE =
'DTCWriteResource'

Instance Method Summary collapse

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

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

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

Instance Method Details

#auth_token_pathObject (private)



58
59
60
# File 'lib/carma/client/mule_soft_auth_token_client.rb', line 58

def auth_token_path
  config.settings.auth_token_path
end

#client_idObject (private)



50
51
52
# File 'lib/carma/client/mule_soft_auth_token_client.rb', line 50

def client_id
  config.settings.client_id
end

#client_secretObject (private)



54
55
56
# File 'lib/carma/client/mule_soft_auth_token_client.rb', line 54

def client_secret
  config.settings.client_secret
end

#new_bearer_tokenObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/carma/client/mule_soft_auth_token_client.rb', line 18

def new_bearer_token
  with_monitoring do
    response = perform(:post,
                       auth_token_path,
                       params,
                       token_headers,
                       { timeout: config.timeout })

    return JSON.parse(response.body)['access_token'] if response.status == 200

    raise GetAuthTokenError, "Response: #{response}"
  end
end

#paramsObject (private)



34
35
36
37
38
39
# File 'lib/carma/client/mule_soft_auth_token_client.rb', line 34

def params
  URI.encode_www_form({
                        grant_type: GRANT_TYPE,
                        scope: SCOPE
                      })
end

#token_headersObject (private)



41
42
43
44
45
46
47
48
# File 'lib/carma/client/mule_soft_auth_token_client.rb', line 41

def token_headers
  basic_auth = Base64.urlsafe_encode64("#{client_id}:#{client_secret}")

  {
    'Authorization' => "Basic #{basic_auth}",
    'Content-Type' => 'application/x-www-form-urlencoded'
  }
end