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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 131

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



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

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



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 119

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 = {}) ⇒ Object

Provide token authentication handling for a token authenticatable class

model - the token authenticatable Class

Returns nothing.



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

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/simple_token_authentication/token_authentication_handler.rb', line 150

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