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

Add unauthorized_entity to authenticate_for's default for called by callback #230

Open
wants to merge 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
### Fixed
- Add unauthorized_entity to authenticate_for's default for called by callback

## [2.1.1] - 2017-02-11
### Fixed
- Stop trying to retrieve user from empty payload when no token is given
Expand Down
14 changes: 9 additions & 5 deletions lib/knock/authenticable.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module Knock::Authenticable
def authenticate_for entity_class
def authenticate_for entity_class, by_callback: true
getter_name = "current_#{entity_class.to_s.parameterize.underscore}"
define_current_entity_getter(entity_class, getter_name)
public_send(getter_name)
if by_callback
unauthorized_entity unless public_send(getter_name)
else
public_send(getter_name)
end
end

private
Expand All @@ -15,7 +19,7 @@ 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)
unauthorized_entity unless authenticate_entity(entity_name)
when 'current'
authenticate_entity(entity_name)
else
Expand All @@ -26,11 +30,11 @@ def method_missing(method, *args)
def authenticate_entity(entity_name)
if token
entity_class = entity_name.camelize.constantize
send(:authenticate_for, entity_class)
send(:authenticate_for, entity_class, by_callback: false)
end
end

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index

private

def unauthorized_entity(entity)
def unauthorized_entity
head :not_found
end
end
14 changes: 11 additions & 3 deletions test/dummy/test/controllers/v1/test_namespaced_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@

module Knock
class TestNamespacedControllerTest < ActionDispatch::IntegrationTest

setup do
@user = V1::User.first
@token = Knock::AuthToken.new(payload: { sub: @user.id }).token
end

test "allow namespaced models" do
token = Knock::AuthToken.new(payload: { sub: @user.id }).token
get v1_test_namespaced_index_url, headers: {'Authorization': "Bearer #{token}"}
get v1_test_namespaced_index_url, headers: {'Authorization': "Bearer #{@token}"}
assert_response :ok
assert_equal @user, @controller.current_v1_user
end

test 'responds with unauthorized' do
get v1_test_namespaced_index_url
assert_response :unauthorized
end

test 'responds with unauthorized with invalid token in header' do
get v1_test_namespaced_index_url, headers: {'Authorization': 'Bearer invalid'}
assert_response :unauthorized
end
end
end