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.



47
48
49
# File 'lib/apimatic-core/utilities/auth_helper.rb', line 47

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.



32
33
34
# File 'lib/apimatic-core/utilities/auth_helper.rb', line 32

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

.token_expired?(token_expiry, clock_skew_time = nil) ⇒ Boolean

Checks if OAuth token has expired.

Parameters:

  • token_expiry (int)

    The expiring of a token.

  • clock_skew_time (int|Optional) (defaults to: nil)

    A buffer to the calculated expiry.

Returns:

  • (Boolean)

    true if token has expired, false otherwise.

Raises:

  • (ArgumentError)


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

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

  token_expiry -= clock_skew_time unless clock_skew_time.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.



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

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