Class: Foobara::Auth::AuthenticateWithApiKey

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

Defined Under Namespace

Classes: ApiKeyDoesNotExistError, InvalidApiKeyError

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

#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

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

#api_key_idObject

Returns the value of attribute api_key_id.



37
38
39
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 37

def api_key_id
  @api_key_id
end

#api_key_recordObject

Returns the value of attribute api_key_record.



37
38
39
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 37

def api_key_record
  @api_key_record
end

#api_key_secretObject

Returns the value of attribute api_key_secret.



37
38
39
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 37

def api_key_secret
  @api_key_secret
end

#expires_atObject

Returns the value of attribute expires_at.



37
38
39
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 37

def expires_at
  @expires_at
end

#userObject

Returns the value of attribute user.



37
38
39
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 37

def user
  @user
end

Instance Method Details

#determine_api_key_id_and_secretObject



39
40
41
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 39

def determine_api_key_id_and_secret
  self.api_key_id, self.api_key_secret = api_key.split("_")
end

#executeObject



27
28
29
30
31
32
33
34
35
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 27

def execute
  determine_api_key_id_and_secret
  load_api_key_record
  verify_api_key

  load_user

  user_and_credential
end

#load_api_key_recordObject



43
44
45
46
47
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 43

def load_api_key_record
  self.api_key_record = Types::Token.load(api_key_id)
rescue Foobara::Entity::NotFoundError
  add_runtime_error(ApiKeyDoesNotExistError)
end

#load_userObject



57
58
59
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 57

def load_user
  self.user ||= Types::User.that_owns(api_key_record, "api_keys")
end

#user_and_credentialObject



61
62
63
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 61

def user_and_credential
  [user, api_key_record]
end

#verify_api_keyObject



49
50
51
52
53
54
55
# File 'foobara-auth-0.0.13/src/authenticate_with_api_key.rb', line 49

def verify_api_key
  valid = run_subcommand!(VerifyToken, token_string: api_key)

  unless valid[:verified]
    add_runtime_error(InvalidApiKeyError)
  end
end