module Knocknock::Authenticatable
Public Instance Methods
authenticate_for(resource_class)
click to toggle source
# File lib/knocknock/authenticatable.rb, line 3 def authenticate_for resource_class token = token_from_request_headers return head(:unauthorized) if token.nil? begin @resource = Knocknock::AuthToken.new(token: token).resource(resource_class) return head(:unauthorized) unless @resource define_current_resource_getter(resource_class) rescue return head(:unauthorized) end end
Private Instance Methods
constant_from_parts(parts)
click to toggle source
Not rescuing from NameError on purpose. If trying to use `authenticate_user` but no `User` constant exists, it makes more sense to raise NameError than NoMethodError.
# File lib/knocknock/authenticatable.rb, line 44 def constant_from_parts parts parts.map(&:capitalize).join.constantize end
define_current_resource_getter(resource_class)
click to toggle source
# File lib/knocknock/authenticatable.rb, line 34 def define_current_resource_getter resource_class underscored_resource = resource_class.to_s.underscore self.class.send(:define_method, "current_#{underscored_resource}") do @resource end end
method_missing(method, *args)
click to toggle source
Calls superclass method
# File lib/knocknock/authenticatable.rb, line 18 def method_missing(method, *args) prefix, *parts = method.to_s.split('_') if prefix == 'authenticate' resource_class = constant_from_parts(parts) send(:authenticate_for, resource_class) else super end end
token_from_request_headers()
click to toggle source
# File lib/knocknock/authenticatable.rb, line 28 def token_from_request_headers unless request.headers['Authorization'].nil? request.headers['Authorization'].split.last end end