Class: Foobara::Auth::VerifyToken

Inherits:
Command
  • Object
show all
Defined in:
foobara-auth-0.0.9/src/verify_token.rb

Defined Under Namespace

Classes: ExpiredTokenError, InactiveTokenError

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Instance Method Summary collapse

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#run_subcommand!, #subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#auto_detect_current_transactions, #commit_transaction, #open_transaction, #opened_transactions, #relevant_entity_classes, #rollback_transaction, #transactions

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Instance Attribute Details

#secretObject

Returns the value of attribute secret.



48
49
50
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 48

def secret
  @secret
end

#token_idObject

Returns the value of attribute token_id.



48
49
50
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 48

def token_id
  @token_id
end

#token_record_to_verify_againstObject



51
52
53
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 51

def token_record_to_verify_against
  @token_record_to_verify_against || token_record
end

#verifiedObject

Returns the value of attribute verified.



48
49
50
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 48

def verified
  @verified
end

Instance Method Details

#determine_token_id_and_hashed_secretObject



59
60
61
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 59

def determine_token_id_and_hashed_secret
  self.token_id, self.secret = token_string.split("_")
end

#executeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 34

def execute
  determine_token_id_and_hashed_secret

  unless token_record?
    load_token_record
  end

  validate_token_is_active
  validate_token_is_not_expired
  verify_hashed_secret_against_token_record

  verified_and_token_record
end

#load_token_recordObject



63
64
65
66
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 63

def load_token_record
  self.token_record_to_verify_against = Types::Token.load(token_id)
  # TODO: handle no record found...
end

#token_record?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 55

def token_record?
  !!token_record
end

#validate_token_is_activeObject



84
85
86
87
88
89
90
91
92
93
94
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 84

def validate_token_is_active
  unless token_record_to_verify_against.active?
    add_runtime_error(
      InactiveTokenError.new(
        context: {
          state: token_record_to_verify_against.state_machine.current_state
        }
      )
    )
  end
end

#validate_token_is_not_expiredObject



74
75
76
77
78
79
80
81
82
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 74

def validate_token_is_not_expired
  if token_record_to_verify_against.expires_at&.<(Time.now)
    Types::Token.transaction(mode: :open_nested) do
      token = Types::Token.load(token_record_to_verify_against.id)
      token.expire!
    end
    add_runtime_error(ExpiredTokenError)
  end
end

#verified_and_token_recordObject



96
97
98
99
100
101
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 96

def verified_and_token_record
  {
    verified:,
    token_record: token_record_to_verify_against
  }
end

#verify_hashed_secret_against_token_recordObject



68
69
70
71
72
# File 'foobara-auth-0.0.9/src/verify_token.rb', line 68

def verify_hashed_secret_against_token_record
  hashed_secret = token_record_to_verify_against.hashed_secret

  self.verified = run_subcommand!(VerifySecret, secret:, hashed_secret:)
end