Class: IBMCloudSdkCore::CloudPakForDataAuthenticator

Inherits:
Authenticator
  • Object
show all
Defined in:
lib/ibm_cloud_sdk_core/authenticators/cp4d_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) ⇒ CloudPakForDataAuthenticator

Returns a new instance of CloudPakForDataAuthenticator.



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

def initialize(vars)
  defaults = {
    username: nil,
    password: nil,
    url: nil,
    disable_ssl_verification: false
  }
  vars = defaults.merge(vars)
  @username = vars[:username]
  @password = vars[:password]
  @url = vars[:url]
  @disable_ssl_verification = vars[:disable_ssl_verification]
  @authentication_type = AUTH_TYPE_CP4D

  validate
  @token_manager = CP4DTokenManager.new(
    url: @url,
    username: @username,
    password: @password,
    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/cp4d_authenticator.rb', line 11

def authentication_type
  @authentication_type
end

#disable_ssl_verificationObject

Returns the value of attribute disable_ssl_verification.



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

def disable_ssl_verification
  @disable_ssl_verification
end

Instance Method Details

#authenticate(headers) ⇒ Object

Adds the Authorization header, if possible



36
37
38
# File 'lib/ibm_cloud_sdk_core/authenticators/cp4d_authenticator.rb', line 36

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

#validateObject

Checks if all the inputs needed are present

Raises:

  • (ArgumentError)


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

def validate
  raise ArgumentError.new("The username or password shouldn\'t be None.") if @username.nil? || @password.nil?
  raise ArgumentError.new("The url shouldn\'t be None.") if @url.nil?
  raise ArgumentError.new('The username shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username') if check_bad_first_or_last_char(@username)
  raise ArgumentError.new('The password shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your password') if check_bad_first_or_last_char(@password)
  raise ArgumentError.new('The url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your url') if check_bad_first_or_last_char(@url)
end