Module: Card::Set::Right::Token

Extended by:
Card::Set
Includes:
All::Permissions::Accounts
Defined in:
tmpsets/set/mod017-account/right/token.rb

Constant Summary collapse

DURATIONS =
"second|minute|hour|day|week|month|year".freeze

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from AdvancedApi

#attachment, #ensure_set, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Methods included from All::Permissions::Accounts

#permit

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'tmpsets/set/mod017-account/right/token.rb', line 24

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

#permanent?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'tmpsets/set/mod017-account/right/token.rb', line 28

def permanent?
  term == "permanent"
end

#termObject



36
37
38
39
40
41
42
43
# File 'tmpsets/set/mod017-account/right/token.rb', line 36

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

#term_from_string(string) ⇒ Object

Raises:

  • (Card::Error::Oops)


45
46
47
48
49
50
51
52
53
# File 'tmpsets/set/mod017-account/right/token.rb', line 45

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

  raise Card::Error::Oops, "illegal expiration value (eg '2 days')" unless unit
  number.to_i.send unit
end

#used!Object



32
33
34
# File 'tmpsets/set/mod017-account/right/token.rb', line 32

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

#validate!(token) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'tmpsets/set/mod017-account/right/token.rb', line 14

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