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

#authenticate_entity_from_fallback!(entity, fallback_authentication_handler) ⇒ Object



37
38
39
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 37

def authenticate_entity_from_fallback!(entity, fallback_authentication_handler)
  fallback_authentication_handler.authenticate_entity!(self, entity)
end

#authenticate_entity_from_token!(entity) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 29

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

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

#find_record_from_identifier(entity) ⇒ Object



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

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.
  record = nil
  record = identifier_param_value && entity.model.where(entity.identifier => identifier_param_value).first
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



72
73
74
75
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 72

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



46
47
48
49
50
51
52
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 46

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

Private: Get one (always the same) object which behaves as a sign in handler



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

def 
  @@sign_in_handler ||= SignInHandler.new
end

#token_comparatorObject

Private: Get one (always the same) object which behaves as a token comprator



78
79
80
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 78

def token_comparator
  @@token_comparator ||= TokenComparator.new
end

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

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 41

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