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_token!(entity) ⇒ Object



30
31
32
33
34
35
36
# 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, )
  end
end

#fallback!(entity, fallback_handler) ⇒ Object



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

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

#find_record_from_identifier(entity) ⇒ Object



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

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



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



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

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



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

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