Class: ActiveRecord::TokenFor::TokenDefinition

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

#as_json

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



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

def block
  @block
end

#defining_classObject

Returns the value of attribute defining_class

Returns:

  • (Object)

    the current value of defining_class



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

def defining_class
  @defining_class
end

#expires_inObject

Returns the value of attribute expires_in

Returns:

  • (Object)

    the current value of expires_in



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

def expires_in
  @expires_in
end

#purposeObject

Returns the value of attribute purpose

Returns:

  • (Object)

    the current value of purpose



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

def purpose
  @purpose
end

Instance Method Details

#full_purposeObject



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

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

#generate_token(model) ⇒ Object



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

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

#message_verifierObject



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

def message_verifier
  defining_class.generated_token_verifier
end

#payload_for(model) ⇒ Object



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

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

#resolve_token(token) ⇒ Object



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

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