Module: SimpleTokenAuthentication::TokenAuthenticationHandler::ClassMethods

Defined in:
lib/simple_token_authentication/token_authentication_handler.rb

Instance Method Summary collapse

Instance Method Details

#define_token_authentication_helpers_for(entity, fallback_authentication_handler) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 120

def define_token_authentication_helpers_for(entity, fallback_authentication_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)
        authenticate_entity_from_fallback!(_entity, fallback_authentication_handler)
      end.call(entity)
    end
  end
end

#entities_managerObject

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



103
104
105
106
107
108
109
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 103

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_authentication_handlerObject

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



112
113
114
115
116
117
118
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 112

def fallback_authentication_handler
  if class_variable_defined?(:@@fallback_authentication_handler)
    class_variable_get(:@@fallback_authentication_handler)
  else
    class_variable_set(:@@fallback_authentication_handler, FallbackAuthenticationHandler.new)
  end
end

#handle_token_authentication_for(model, options = {}) ⇒ Object

Provide token authentication handling for a token authenticatable class

model - the token authenticatable Class

Returns nothing.



94
95
96
97
98
99
100
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 94

def handle_token_authentication_for(model, options = {})
  name = options[:name] || model.name
  entity = entities_manager.find_or_create_entity(model, name)
  options = SimpleTokenAuthentication.parse_options(options)
  define_token_authentication_helpers_for(entity, fallback_authentication_handler)
  set_token_authentication_hooks(entity, options)
end

#set_token_authentication_hooks(entity, options) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 139

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
  before_filter authenticate_method, options.slice(:only, :except)
end