Class: CoreLibrary::AuthHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/utilities/auth_helper.rb

Overview

A utility class for authentication flow

Class Method Summary collapse

Class Method Details

.apply(auth_params, func) ⇒ Object

Applies callable to each entry of the hash.

Parameters:

  • auth_params (Hash)

    The auth parameters hash to apply against.

  • func (Callable)

    The callable function to apply for each entry of the provided hash.



45
46
47
# File 'lib/apimatic-core/utilities/auth_helper.rb', line 45

def self.apply(auth_params, func)
  auth_params.each { |key, value| func.call(key, value) }
end

.get_base64_encoded_value(*props, delimiter: ':') ⇒ String

Performs the Base64 encodes the provided parameters after joining the parameters with delimiter.

Parameters:

  • *props (String)

    The string properties which should participate in encoding.

  • delimiter (String|Optional) (defaults to: ':')

    The delimiter to use while joining the properties.

Returns:

  • (String)

    The encoded Base64 string.



10
11
12
13
14
15
# File 'lib/apimatic-core/utilities/auth_helper.rb', line 10

def self.get_base64_encoded_value(*props, delimiter: ':')
  return if props.any?(&:nil?)

  joined = props.join(delimiter)
  Base64.strict_encode64(joined)
end

.get_token_expiry(expires_in, current_timestamp) ⇒ Time

Calculates the expiry after adding the expires_in value to the current timestamp.

Parameters:

  • expires_in (int)

    The number of ticks after which the token would get expired.

  • current_timestamp (int)

    The current timestamp.

Returns:

  • (Time)

    The calculated expiry time of the token.



30
31
32
# File 'lib/apimatic-core/utilities/auth_helper.rb', line 30

def self.get_token_expiry(expires_in, current_timestamp)
  current_timestamp + expires_in
end

.token_expired?(token_expiry) ⇒ Boolean

Checks if OAuth token has expired.

Parameters:

  • token_expiry (int)

    The expiring of a token.

Returns:

  • (Boolean)

    true if token has expired, false otherwise.

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/apimatic-core/utilities/auth_helper.rb', line 20

def self.token_expired?(token_expiry)
  raise ArgumentError, 'Token expiry can not be nil.' if token_expiry.nil?

  token_expiry < Time.now.utc.to_i
end

.valid_auth?(auth_params) ⇒ Boolean

Checks whether the provided auth parameters does not contain any nil key/value.

Parameters:

  • auth_params (Hash)

    The auth parameters hash to check against.

Returns:

  • (Boolean)

    True if there is not any nil key/value in the given auth parameters.



37
38
39
40
# File 'lib/apimatic-core/utilities/auth_helper.rb', line 37

def self.valid_auth?(auth_params)
  !auth_params.nil? and !auth_params.empty? and
    auth_params.all? { |key, value| !(key.nil? or value.nil?) }
end