Module: SimpleTokenAuthentication::TokenAuthenticationHandler

Extended by:
ActiveSupport::Concern
Defined in:
lib/simple_token_authentication/token_authentication_handler.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#after_successful_token_authenticationObject

This method is a hook and is meant to be overridden.

It is not expected to return anything special, only its side effects will be used.



34
35
36
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 34

def after_successful_token_authentication
  # intentionally left blank
end

#authenticate_entity_from_token!(entity) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 38

def authenticate_entity_from_token!(entity)
  record = find_record_from_identifier(entity)

  if token_correct?(record, entity, token_comparator)
    perform_sign_in!(record, )
    after_successful_token_authentication
  end
end

#fallback!(entity, fallback_handler) ⇒ Object



47
48
49
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 47

def fallback!(entity, fallback_handler)
  fallback_handler.fallback!(self, entity)
end

#find_record_from_identifier(entity) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 64

def find_record_from_identifier(entity)
  identifier_param_value = entity.get_identifier_from_params_or_headers(self).presence

  identifier_param_value = integrate_with_devise_case_insensitive_keys(identifier_param_value, entity)

  # The finder method should be compatible with all the model adapters,
  # namely ActiveRecord and Mongoid in all their supported versions.
  identifier_param_value && entity.model.find_for_authentication(entity.identifier => identifier_param_value)
end

#integrate_with_devise_case_insensitive_keys(identifier_value, entity) ⇒ Object

Private: Take benefit from Devise case-insensitive keys

See github.com/plataformatec/devise/blob/v3.4.1/lib/generators/templates/devise.rb#L45-L48

identifier_value - the original identifier_value String

Returns an identifier String value which case follows the Devise case-insensitive keys policy



81
82
83
84
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 81

def integrate_with_devise_case_insensitive_keys(identifier_value, entity)
  identifier_value.downcase! if identifier_value && Devise.case_insensitive_keys.include?(entity.identifier)
  identifier_value
end

#perform_sign_in!(record, sign_in_handler) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 56

def perform_sign_in!(record, )
  # Notice the store option defaults to false, so the record
  # identifier is not actually stored in the session and a token
  # is needed for every request. That behaviour can be configured
  # through the sign_in_token option.
  . self, record, store: SimpleTokenAuthentication.
end

#sign_in_handlerObject



90
91
92
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 90

def 
  SignInHandler.instance
end

#token_comparatorObject



86
87
88
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 86

def token_comparator
  TokenComparator.instance
end

#token_correct?(record, entity, token_comparator) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 51

def token_correct?(record, entity, token_comparator)
  record && token_comparator.compare(record.authentication_token,
                                     entity.get_token_from_params_or_headers(self))
end