module SimpleTokenAuthentication::TokenAuthenticationHandler::ClassMethods

Public Instance Methods

define_token_authentication_helpers_for(entity, fallback_handler) click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 123
def define_token_authentication_helpers_for(entity, fallback_handler)

  method_name = "authenticate_#{entity.name_underscore}_from_token"
  method_name_bang = method_name + '!'

  class_eval do
    define_method method_name.to_sym do
      lambda { |_entity| authenticate_entity_from_token!(_entity) }.call(entity)
    end

    define_method method_name_bang.to_sym do
      lambda do |_entity|
        authenticate_entity_from_token!(_entity)
        fallback!(_entity, fallback_handler)
      end.call(entity)
    end
  end
end
entities_manager() click to toggle source

Private: Get one (always the same) object which behaves as an entities manager

# File lib/simple_token_authentication/token_authentication_handler.rb, line 102
def entities_manager
  if class_variable_defined?(:@@entities_manager)
    class_variable_get(:@@entities_manager)
  else
    class_variable_set(:@@entities_manager, EntitiesManager.new)
  end
end
fallback_handler(options) click to toggle source

Private: Get one (always the same) object which behaves as a fallback authentication handler

# File lib/simple_token_authentication/token_authentication_handler.rb, line 111
def fallback_handler(options)
  if class_variable_defined?(:@@fallback_authentication_handler)
    class_variable_get(:@@fallback_authentication_handler)
  else
    if options[:fallback] == :exception
      class_variable_set(:@@fallback_authentication_handler, ExceptionFallbackHandler.instance)
    else
      class_variable_set(:@@fallback_authentication_handler, DeviseFallbackHandler.instance)
    end
  end
end
handle_token_authentication_for(model, options = {}) click to toggle source

Provide token authentication handling for a token authenticatable class

model - the token authenticatable Class

Returns nothing.

# File lib/simple_token_authentication/token_authentication_handler.rb, line 93
def handle_token_authentication_for(model, options = {})
  model_alias = options[:as] || options['as']
  entity = entities_manager.find_or_create_entity(model, model_alias)
  options = SimpleTokenAuthentication.parse_options(options)
  define_token_authentication_helpers_for(entity, fallback_handler(options))
  set_token_authentication_hooks(entity, options)
end
set_token_authentication_hooks(entity, options) click to toggle source
# File lib/simple_token_authentication/token_authentication_handler.rb, line 142
def set_token_authentication_hooks(entity, options)
  authenticate_method = unless options[:fallback] == :none
    :"authenticate_#{entity.name_underscore}_from_token!"
  else
    :"authenticate_#{entity.name_underscore}_from_token"
  end

  if respond_to?(:before_action)
    # See https://github.com/rails/rails/commit/9d62e04838f01f5589fa50b0baa480d60c815e2c
    before_action authenticate_method, options.slice(:only, :except, :if, :unless)
  else
    before_filter authenticate_method, options.slice(:only, :except, :if, :unless)
  end
end