Class: IBMCloudSdkCore::IamAuthenticator

Inherits:
Authenticator show all
Defined in:
lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb

Overview

Basic Authenticator

Constant Summary

Constants inherited from Authenticator

Authenticator::AUTH_TYPE_BASIC, Authenticator::AUTH_TYPE_BEARER_TOKEN, Authenticator::AUTH_TYPE_CP4D, Authenticator::AUTH_TYPE_IAM, Authenticator::AUTH_TYPE_NO_AUTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars) ⇒ IamAuthenticator

Returns a new instance of IamAuthenticator.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb', line 12

def initialize(vars)
  defaults = {
    url: nil,
    client_id: nil,
    client_secret: nil,
    disable_ssl_verification: nil
  }
  vars = defaults.merge(vars)
  @apikey = vars[:apikey]
  @url = vars[:url]
  @client_id = vars[:client_id]
  @client_secret = vars[:client_secret]
  @disable_ssl_verification = vars[:disable_ssl_verification]
  @authentication_type = AUTH_TYPE_IAM

  validate
  @token_manager = IAMTokenManager.new(
    apikey: @apikey,
    url: @url,
    client_id: @client_id,
    client_secret: @client_secret,
    disable_ssl_verification: @disable_ssl_verification
  )
end

Instance Attribute Details

#authentication_typeObject

Returns the value of attribute authentication_type.



11
12
13
# File 'lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb', line 11

def authentication_type
  @authentication_type
end

#client_idObject

Returns the value of attribute client_id.



11
12
13
# File 'lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb', line 11

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



11
12
13
# File 'lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb', line 11

def client_secret
  @client_secret
end

#disable_ssl_verificationObject

Returns the value of attribute disable_ssl_verification.



11
12
13
# File 'lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb', line 11

def disable_ssl_verification
  @disable_ssl_verification
end

Instance Method Details

#authenticate(headers) ⇒ Object



37
38
39
# File 'lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb', line 37

def authenticate(headers)
  headers["Authorization"] = "Bearer #{@token_manager.access_token}"
end

#validateObject

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
# File 'lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb', line 41

def validate
  # Adds the Authorization header, if possible
  raise ArgumentError.new("The apikey shouldn\'t be None.") if @apikey.nil?
  raise ArgumentError.new('The apikey shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your apikey') if check_bad_first_or_last_char(@apikey)

  # Both the client id and secret should be provided or neither should be provided.
  raise ArgumentError.new("Only one of 'client_id' or 'client_secret' were specified, but both parameters should be specified together.") if (@client_id.nil? && !@client_secret.nil?) || (!@client_id.nil? && @client_secret.nil?)
end