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

Instance Method Summary collapse

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #pattern_and_modules_from_path, #process_base_modules, #register_set, #tmp_file_template, #write_tmp_file

Methods included from Helpers

#abstract_set?, #all_set?, #shortname

Methods included from AdvancedApi

#attachment, #ensure_set, #stage_method

Methods included from Format

#all_set_format_mod!, #define_on_format, #format, #register_set_format, #view

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer

Methods included from Event

#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:



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