module Mrkt::Authentication

Public Instance Methods

add_authorization(req) click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 45
def add_authorization(req)
  req.headers[:authorization] = "Bearer #{@token}"
end
authenticate() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 30
def authenticate
  connection.get('/identity/oauth/token', authentication_params).tap do |response|
    data = response.body

    @token = data.fetch(:access_token)
    @token_type = data.fetch(:token_type)
    @valid_until = Time.now + data.fetch(:expires_in)
    @scope = data.fetch(:scope)
  end
end
authenticate!() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 3
def authenticate!
  return if authenticated?

  authenticate

  retry_authentication if !authenticated? && @retry_authentication

  raise Mrkt::Errors::AuthorizationError, 'Client not authenticated' unless authenticated?
end
authenticated?() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 13
def authenticated?
  @token && valid_token?
end
authentication_params() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 41
def authentication_params
  merge_params(required_authentication_params, optional_authentication_params)
end
retry_authentication() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 21
def retry_authentication
  @retry_authentication_count.times do
    sleep(@retry_authentication_wait_seconds) if @retry_authentication_wait_seconds.positive?
    authenticate

    break if authenticated?
  end
end
valid_token?() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 17
def valid_token?
  @valid_until && Time.now < @valid_until
end

Private Instance Methods

optional_authentication_params() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 51
def optional_authentication_params
  {
    partner_id: @partner_id
  }
end
required_authentication_params() click to toggle source
# File lib/mrkt/concerns/authentication.rb, line 57
def required_authentication_params
  {
    grant_type: 'client_credentials',
    client_id: @client_id,
    client_secret: @client_secret
  }
end