Class: ActiveRecord::TokenFor::TokenDefinition

Inherits:
Struct
  • Object
show all
Defined in:
lib/active_record/token_for.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



14
15
16
# File 'lib/active_record/token_for.rb', line 14

def block
  @block
end

#defining_classObject

Returns the value of attribute defining_class

Returns:

  • (Object)

    the current value of defining_class



14
15
16
# File 'lib/active_record/token_for.rb', line 14

def defining_class
  @defining_class
end

#expires_inObject

Returns the value of attribute expires_in

Returns:

  • (Object)

    the current value of expires_in



14
15
16
# File 'lib/active_record/token_for.rb', line 14

def expires_in
  @expires_in
end

#purposeObject

Returns the value of attribute purpose

Returns:

  • (Object)

    the current value of purpose



14
15
16
# File 'lib/active_record/token_for.rb', line 14

def purpose
  @purpose
end

Instance Method Details

#full_purposeObject



15
16
17
# File 'lib/active_record/token_for.rb', line 15

def full_purpose
  @full_purpose ||= [defining_class.name, purpose, expires_in].join("\n")
end

#generate_token(model) ⇒ Object



27
28
29
# File 'lib/active_record/token_for.rb', line 27

def generate_token(model)
  message_verifier.generate(payload_for(model), expires_in: expires_in, purpose: full_purpose)
end

#message_verifierObject



19
20
21
# File 'lib/active_record/token_for.rb', line 19

def message_verifier
  defining_class.generated_token_verifier
end

#payload_for(model) ⇒ Object



23
24
25
# File 'lib/active_record/token_for.rb', line 23

def payload_for(model)
  block ? [model.id, model.instance_eval(&block).as_json] : [model.id]
end

#resolve_token(token) ⇒ Object



31
32
33
34
35
# File 'lib/active_record/token_for.rb', line 31

def resolve_token(token)
  payload = message_verifier.verified(token, purpose: full_purpose)
  model = yield(payload[0]) if payload
  model if model && payload_for(model) == payload
end