Module: Knock::Authenticable

Defined in:
lib/knock/authenticable.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/knock/authenticable.rb', line 22

def method_missing(method, *args)
  prefix, entity_name = method.to_s.split('_', 2)
  case prefix
  when 'authenticate'
    head(:unauthorized) unless authenticate_entity(entity_name)
  when 'current'
    authenticate_entity(entity_name)
  else
    super
  end
end

Instance Method Details

#authenticateObject



2
3
4
5
# File 'lib/knock/authenticable.rb', line 2

def authenticate
  warn "[DEPRECATION]: `authenticate` is deprecated. Please use `authenticate_user` instead."
  head(:unauthorized) unless authenticate_for(User)
end

#authenticate_for(entity_class) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/knock/authenticable.rb', line 7

def authenticate_for entity_class
  token = params[:token] || token_from_request_headers
  return nil if token.nil?

  begin
    @entity = Knock::AuthToken.new(token: token).entity_for(entity_class)
    define_current_entity_getter(entity_class)
    @entity
  rescue
    nil
  end
end