Class: IBMCloudSdkCore::BasicAuthenticator

Inherits:
Authenticator show all
Defined in:
lib/ibm_cloud_sdk_core/authenticators/basic_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) ⇒ BasicAuthenticator

Returns a new instance of BasicAuthenticator.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ibm_cloud_sdk_core/authenticators/basic_authenticator.rb', line 11

def initialize(vars)
  defaults = {
    username: nil,
    password: nil
  }
  vars = defaults.merge(vars)
  @username = vars[:username]
  @password = vars[:password]
  @authentication_type = AUTH_TYPE_BASIC
  validate
end

Instance Attribute Details

#authentication_typeObject

Returns the value of attribute authentication_type.



10
11
12
# File 'lib/ibm_cloud_sdk_core/authenticators/basic_authenticator.rb', line 10

def authentication_type
  @authentication_type
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/ibm_cloud_sdk_core/authenticators/basic_authenticator.rb', line 10

def password
  @password
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/ibm_cloud_sdk_core/authenticators/basic_authenticator.rb', line 10

def username
  @username
end

Instance Method Details

#authenticate(headers) ⇒ Object

Adds the Authorization header, if possible



24
25
26
27
# File 'lib/ibm_cloud_sdk_core/authenticators/basic_authenticator.rb', line 24

def authenticate(headers)
  base64_authentication = Base64.strict_encode64("#{@username}:#{@password}")
  headers["Authorization"] = "Basic #{base64_authentication}"
end

#validateObject

Checks if all the inputs needed are present

Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/ibm_cloud_sdk_core/authenticators/basic_authenticator.rb', line 30

def validate
  raise ArgumentError.new("The username and password shouldn\'t be None.") if @username.nil? || @password.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)
end