Class: Foobara::Auth::VerifyToken

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

Defined Under Namespace

Classes: ExpiredTokenError, InactiveTokenError, TokenDoesNotExistError

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, #raw_result

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

#relevant_entity_classes

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type

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.



53
54
55
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 53

def secret
  @secret
end

#token_idObject

Returns the value of attribute token_id.



53
54
55
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 53

def token_id
  @token_id
end

#token_record_to_verify_againstObject



56
57
58
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 56

def token_record_to_verify_against
  @token_record_to_verify_against || token_record
end

#verifiedObject

Returns the value of attribute verified.



53
54
55
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 53

def verified
  @verified
end

Instance Method Details

#determine_token_id_and_hashed_secretObject



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

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

#executeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 39

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



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

def load_token_record
  self.token_record_to_verify_against = Types::Token.load(token_id)
rescue Entity::NotFoundError
  add_runtime_error(TokenDoesNotExistError)
end

#token_record?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 60

def token_record?
  !!token_record
end

#validate_token_is_activeObject



90
91
92
93
94
95
96
97
98
99
100
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 90

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



80
81
82
83
84
85
86
87
88
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 80

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



102
103
104
105
106
107
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 102

def verified_and_token_record
  {
    verified:,
    token_record: token_record_to_verify_against
  }
end

#verify_hashed_secret_against_token_recordObject



74
75
76
77
78
# File 'foobara-auth-0.0.13/src/verify_token.rb', line 74

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