class Tinypass::AccessToken

Attributes

access_state[RW]
token_data[RW]

Public Class Methods

new(rid_or_token_data, expiration_in_seconds = nil, early_expiration_in_seconds = nil) click to toggle source
# File lib/tinypass/token/access_token.rb, line 5
def initialize(rid_or_token_data, expiration_in_seconds = nil, early_expiration_in_seconds = nil)
  if rid_or_token_data.kind_of?(TokenData)
    self.token_data = rid_or_token_data
    return
  end

  expiration_in_seconds ||= 0
  early_expiration_in_seconds ||= 0

  token_data = TokenData.new
  token_data[TokenData::RID] = rid_or_token_data.to_s
  token_data[TokenData::EX] = TokenData.convert_to_epoch_seconds(expiration_in_seconds)
  token_data[TokenData::EARLY_EX] = TokenData.convert_to_epoch_seconds(early_expiration_in_seconds)
  self.token_data = token_data
end

Public Instance Methods

access_granted?(client_ip = nil) click to toggle source
# File lib/tinypass/token/access_token.rb, line 80
def access_granted?(client_ip = nil)
  if expiration_in_seconds == -1
    # special case. RID_NOT_FOUND
    @access_state = AccessState::RID_NOT_FOUND if @access_state != AccessState::NO_TOKENS_FOUND
    return false
  end

  if Utils::valid_ip?(client_ip) && ips.any? && !ips.include?(client_ip)
    @access_state = AccessState::CLIENT_IP_DOES_NOT_MATCH_TOKEN
    return false
  end

  if metered?
    if trial_period_active?
      @access_state = AccessState::METERED_IN_TRIAL
      return true
    end

    if lockout_period_active?
      @access_state = AccessState::METERED_IN_LOCKOUT
    else
      @access_state = AccessState::METERED_TRIAL_DEAD
    end

    return false
  end

  if expired?
    @access_state = AccessState::EXPIRED
    return false
  end

  @access_state = AccessState::ACCESS_GRANTED
  true
end
access_id() click to toggle source
# File lib/tinypass/token/access_token.rb, line 25
def access_id
  token_data[TokenData::ACCESS_ID]
end
early_expiration_in_seconds() click to toggle source
# File lib/tinypass/token/access_token.rb, line 38
def early_expiration_in_seconds
  token_data.fetch(TokenData::EARLY_EX, 0)
end
Also aliased as: early_expiration_in_secs
early_expiration_in_secs()
expiration_in_seconds() click to toggle source
# File lib/tinypass/token/access_token.rb, line 33
def expiration_in_seconds
  token_data.fetch(TokenData::EX, 0)
end
Also aliased as: expiration_in_secs
expiration_in_secs()
expired?() click to toggle source
# File lib/tinypass/token/access_token.rb, line 138
def expired?
  expiration = early_expiration_in_seconds
  expiration = expiration_in_seconds if expiration == 0

  return false if expiration == 0

  expiration <= Time.now.to_i
end
ips() click to toggle source
# File lib/tinypass/token/access_token.rb, line 71
def ips
  token_data.fetch(TokenData::IPS, [])
end
lockout_end_time_secs() click to toggle source
# File lib/tinypass/token/access_token.rb, line 47
def lockout_end_time_secs
  token_data.fetch(TokenData::METER_LOCKOUT_ENDTIME, 0)
end
lockout_period_active?() click to toggle source
# File lib/tinypass/token/access_token.rb, line 131
def lockout_period_active?
  return false unless metered?
  return false if trial_period_active?

  return Time.now.to_i <= lockout_end_time_secs
end
meter_type() click to toggle source
# File lib/tinypass/token/access_token.rb, line 67
def meter_type
  token_data.fetch(TokenData::METER_TYPE, 0)
end
meter_view_based?() click to toggle source
# File lib/tinypass/token/access_token.rb, line 63
def meter_view_based?
  metered? && token_data[TokenData::METER_TRIAL_MAX_ACCESS_ATTEMPTS]
end
metered?() click to toggle source
# File lib/tinypass/token/access_token.rb, line 59
def metered?
  meter_type != 0
end
rid() click to toggle source
# File lib/tinypass/token/access_token.rb, line 21
def rid
  token_data.rid
end
trial_dead?() click to toggle source
# File lib/tinypass/token/access_token.rb, line 147
def trial_dead?
  !lockout_period_active? && !trial_period_active?
end
trial_end_time_secs() click to toggle source
# File lib/tinypass/token/access_token.rb, line 43
def trial_end_time_secs
  token_data.fetch(TokenData::METER_TRIAL_ENDTIME, 0)
end
trial_period_active?() click to toggle source
# File lib/tinypass/token/access_token.rb, line 116
def trial_period_active?
  return false unless metered?

  if meter_type == TokenData::METER_STRICT
    return Time.now.to_i <= trial_end_time_secs
  end

  if meter_view_based?
    return trial_view_count <= trial_view_limit && Time.now.to_i <= trial_end_time_secs
  end

  # unknown meter
  return trial_end_time_secs == 0 || Time.now.to_i <= trial_end_time_secs
end
trial_view_count() click to toggle source
# File lib/tinypass/token/access_token.rb, line 51
def trial_view_count
  token_data.fetch(TokenData::METER_TRIAL_ACCESS_ATTEMPTS, 0)
end
trial_view_limit() click to toggle source
# File lib/tinypass/token/access_token.rb, line 55
def trial_view_limit
  token_data.fetch(TokenData::METER_TRIAL_MAX_ACCESS_ATTEMPTS, 0)
end
uid() click to toggle source
# File lib/tinypass/token/access_token.rb, line 29
def uid
  token_data.fetch(TokenData::UID, 0)
end