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

Extended by:
Card::Set
Includes:
All::Permissions::Accounts
Defined in:
tmpsets/set/mod007-05_standard/right/token.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Card::Set

abstract_set?, all_set?, card_accessor, card_reader, card_writer, clean_empty_module_from_hash, clean_empty_modules, define_active_job, define_event_method, define_event_perform_later_method, define_on_format, ensure_set, event, extended, format, phase_method, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, view, write_tmp_file

Methods included from All::Permissions::Accounts

#permit

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'tmpsets/set/mod007-05_standard/right/token.rb', line 24

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

#permanent?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'tmpsets/set/mod007-05_standard/right/token.rb', line 28

def permanent?
  term == 'permanent'
end

#termObject



36
37
38
39
40
41
42
43
# File 'tmpsets/set/mod007-05_standard/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



45
46
47
48
49
50
51
52
53
54
55
# File 'tmpsets/set/mod007-05_standard/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
  if unit
    number.to_i.send unit
  else
    raise Card::Oops, "illegal expiration value (eg '2 days')"
  end
end

#used!Object



32
33
34
# File 'tmpsets/set/mod007-05_standard/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/mod007-05_standard/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