Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Moving rails dependency as a dev one #159

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion knock.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]

s.add_dependency "rails", ">= 4.2"
s.add_dependency "jwt", "~> 1.5"
s.add_dependency "bcrypt", "~> 3.1"

s.add_development_dependency "rails", ">= 4.2"
s.add_development_dependency "sqlite3", "~> 1.3"
s.add_development_dependency "timecop", "~> 0.8.0"
end
2 changes: 2 additions & 0 deletions lib/generators/knock/install_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rails/generators'

module Knock
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates", __FILE__)
Expand Down
92 changes: 47 additions & 45 deletions lib/knock/authenticable.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
module Knock::Authenticable
def authenticate_for entity_class
getter_name = "current_#{entity_class.to_s.parameterize.underscore}"
define_current_entity_getter(entity_class, getter_name)
public_send(getter_name)
end
module Knock
class Authenticable
def authenticate_for entity_class
getter_name = "current_#{entity_class.to_s.parameterize.underscore}"
define_current_entity_getter(entity_class, getter_name)
public_send(getter_name)
end

private
private

def token
params[:token] || token_from_request_headers
end
def token
params[:token] || token_from_request_headers
end

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

def authenticate_entity(entity_name)
if token
entity_class = entity_name.camelize.constantize
send(:authenticate_for, entity_class)
def authenticate_entity(entity_name)
if token
entity_class = entity_name.camelize.constantize
send(:authenticate_for, entity_class)
end
end
end

def unauthorized_entity(entity_name)
head(:unauthorized)
end
def unauthorized_entity(entity_name)
head(:unauthorized)
end

def token_from_request_headers
unless request.headers['Authorization'].nil?
request.headers['Authorization'].split.last
def token_from_request_headers
unless request.headers['Authorization'].nil?
request.headers['Authorization'].split.last
end
end
end

def define_current_entity_getter entity_class, getter_name
unless self.respond_to?(getter_name)
memoization_var_name = "@_#{getter_name}"
self.class.send(:define_method, getter_name) do
unless instance_variable_defined?(memoization_var_name)
current =
begin
Knock::AuthToken.new(token: token).entity_for(entity_class)
rescue Knock.not_found_exception_class, JWT::DecodeError
nil
end
instance_variable_set(memoization_var_name, current)
def define_current_entity_getter entity_class, getter_name
unless self.respond_to?(getter_name)
memoization_var_name = "@_#{getter_name}"
self.class.send(:define_method, getter_name) do
unless instance_variable_defined?(memoization_var_name)
current =
begin
Knock::AuthToken.new(token: token).entity_for(entity_class)
rescue Knock.not_found_exception_class, JWT::DecodeError
nil
end
instance_variable_set(memoization_var_name, current)
end
instance_variable_get(memoization_var_name)
end
instance_variable_get(memoization_var_name)
end
end
end
Expand Down