Sha256: c4afb3918993dbb873af368eaa92a863bc25884762d81b3f469e48eacc84eb23

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

include All::Permissions::Accounts

DURATIONS = 'second|minute|hour|day|week|month|year'

card_accessor :expiration

view :raw do
  'Private data'
end

def validate! token
  error =
    case
    when !real?           then [:token_not_found, 'no token found']
    when expired?         then [:token_expired, 'expired token']
    when content != token then [:incorrect_token, 'token mismatch']
    end
  errors.add *error if error
end

def expired?
  !permanent? && updated_at <= term.ago
end

def permanent?
  term == 'permanent'
end

def used!
  Auth.as_bot { delete! } unless permanent?
end

def term
  @term ||=
    if expiration.present?
      term_from_string expiration
    else
      Card.config.token_expiry
    end
end

def term_from_string string
  string.strip!
  return 'permanent' if string == 'none'
  re_match = /^(\d+)[\.\s]*(#{DURATIONS})s?$/.match(string)
  number, unit = re_match.captures if re_match
  if unit
    number.to_i.send unit
  else
    raise Card::Oops, "illegal expiration value (eg '2 days')"
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
card-1.17.4 mod/05_standard/set/right/token.rb
card-1.17.3 mod/05_standard/set/right/token.rb
card-1.17.2 mod/05_standard/set/right/token.rb
card-1.17.1 mod/05_standard/set/right/token.rb
card-1.17.0 mod/05_standard/set/right/token.rb
card-1.16.15 mod/05_standard/set/right/token.rb
card-1.16.14 mod/05_standard/set/right/token.rb
card-1.16.13 mod/05_standard/set/right/token.rb