class IBM::Cloud::SDKHTTP::IAMToken

Used to authenticate with IAM.

Attributes

connection[R]
response[R]

Public Class Methods

new(api_key, connection, logger: nil) click to toggle source
# File lib/ibm/cloud/sdk_http/iam_token.rb, line 11
def initialize(api_key, connection, logger: nil)
  @api_key = api_key
  @logger = logger
  @connection = connection
  @response = nil
  @data = nil
end

Public Instance Methods

authorization_header() click to toggle source

Get a Bearer token string. Before returning check to see if token is expired. @return [String] The Bearer token header used in subsequent requests. @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.

# File lib/ibm/cloud/sdk_http/iam_token.rb, line 46
def authorization_header
  fetch if expired?
  "#{data.fetch(:token_type)} #{data.fetch(:access_token)}"
end
data() click to toggle source
# File lib/ibm/cloud/sdk_http/iam_token.rb, line 31
def data
  fetch unless @response
  @response.raise_for_status!.json
end
expired?() click to toggle source

Check to see if the access_token is expired. Fetch a new token if none exists. @return [IBM::Cloud::SDK::VPC::Response] Wrapped response to query. @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.

# File lib/ibm/cloud/sdk_http/iam_token.rb, line 39
def expired?
  data.fetch(:expiration, 0).to_i <= Time.now.to_i + 600
end
fetch() click to toggle source
# File lib/ibm/cloud/sdk_http/iam_token.rb, line 21
def fetch
  payload = {
    body: {
      grant_type: 'urn:ibm:params:oauth:grant-type:apikey',
      apikey: @api_key
    }
  }
  @response = @connection.request('post', 'https://iam.cloud.ibm.com/identity/token', payload)
end