class Redox::Authentication

Constants

AUTH_ENDPOINT
BASE_ENDPOINT
REFRESH_ENDPOINT

Attributes

token_expiry_padding[RW]
response[RW]

Public Class Methods

new() click to toggle source
# File lib/redox/authentication.rb, line 16
def initialize
  @response = nil
end

Public Instance Methods

access_header() click to toggle source
# File lib/redox/authentication.rb, line 67
def access_header
  return {
    'Authorization' => "Bearer #{self.access_token}",
  }
end
access_token() click to toggle source
# File lib/redox/authentication.rb, line 47
def access_token
  return @response['accessToken'] if @response
end
authenticate() click to toggle source
# File lib/redox/authentication.rb, line 20
def authenticate
  if (self.expires?)
    if (self.refresh_token)
      request = {
        body: { apiKey: Redox.configuration.api_key, refreshToken: self.refresh_token },
        endpoint: REFRESH_ENDPOINT
      }
    else
      request = {
        body: { apiKey: Redox.configuration.api_key, secret: Redox.configuration.secret },
        endpoint: AUTH_ENDPOINT
      }
    end

    response = self.request(**request, auth: false)

    if (false == response.ok?)
      @response = nil
      raise RedoxException.from_response(response, msg: 'Authentication')
    else
      @response = response
    end
  end

  return self
end
expire!() click to toggle source
# File lib/redox/authentication.rb, line 73
def expire!
  @response = nil
end
expires?(seconds_from_now = Authentication.token_expiry_padding) click to toggle source
# File lib/redox/authentication.rb, line 59
def expires?(seconds_from_now = Authentication.token_expiry_padding)
  if (self.expiry)
    return DateTime.strptime(self.expiry, Models::Meta::FROM_DATETIME_FORMAT).to_time.utc <= (Time.now + seconds_from_now).utc
  else
    return true
  end
end
expiry() click to toggle source
# File lib/redox/authentication.rb, line 51
def expiry
  return @response['expires'] if @response
end
refresh_token() click to toggle source
# File lib/redox/authentication.rb, line 55
def refresh_token
  return @response['refreshToken'] if @response
end