Class: IBMCloudSdkCore::BearerTokenAuthenticator

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

Returns a new instance of BearerTokenAuthenticator.



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

def initialize(vars)
  defaults = {
    bearer_token: nil
  }
  vars = defaults.merge(vars)
  @bearer_token = vars[:bearer_token]
  @authentication_type = AUTH_TYPE_BEARER_TOKEN
  validate
end

Instance Attribute Details

#authentication_typeObject

Returns the value of attribute authentication_type.



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

def authentication_type
  @authentication_type
end

Instance Method Details

#authenticate(headers) ⇒ Object

Adds the Authorization header, if possible



22
23
24
# File 'lib/ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.rb', line 22

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

#validateObject

Checks if all the inputs needed are present

Raises:

  • (ArgumentError)


27
28
29
# File 'lib/ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.rb', line 27

def validate
  raise ArgumentError.new("The bearer token shouldn\'t be None.") if @bearer_token.nil?
end