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_handler) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 124

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_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_handler(options) ⇒ Object

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



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

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.new)
    else
      class_variable_set(:@@fallback_authentication_handler, DeviseFallbackHandler.new)
    end
  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 = {})
  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) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 143

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, :if, :unless)
end