module SimpleTokenAuthentication::TokenAuthenticationHandler

Public Instance Methods

authenticate_entity_from_token!(entity) click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 30
def authenticate_entity_from_token!(entity)
  record = find_record_from_identifier(entity)

  if token_correct?(record, entity, token_comparator)
    perform_sign_in!(record, sign_in_handler)
    after_successful_token_authentication if respond_to?(:after_successful_token_authentication, true)
  end
end
fallback!(entity, fallback_handler) click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 39
def fallback!(entity, fallback_handler)
  fallback_handler.fallback!(self, entity)
end
find_record_from_identifier(entity) click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 56
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) click to toggle source

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

# File lib/simple_token_authentication/token_authentication_handler.rb, line 73
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) click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 48
def perform_sign_in!(record, sign_in_handler)
  # 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.
  sign_in_handler.sign_in self, record, store: SimpleTokenAuthentication.sign_in_token
end
sign_in_handler() click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 82
def sign_in_handler
  SignInHandler.instance
end
token_comparator() click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 78
def token_comparator
  TokenComparator.instance
end
token_correct?(record, entity, token_comparator) click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 43
def token_correct?(record, entity, token_comparator)
  record && token_comparator.compare(record.authentication_token,
                                     entity.get_token_from_params_or_headers(self))
end