Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent strategy from running when the current path matches a dispatc… #60

Merged
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
12 changes: 11 additions & 1 deletion lib/warden/jwt_auth/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ module JWTAuth
# Warden strategy to authenticate an user through a JWT token in the
# `Authorization` request header
class Strategy < Warden::Strategies::Base
include JWTAuth::Import['dispatch_requests']

def valid?
token_exists? && issuer_claim_valid?
token_exists? && issuer_claim_valid? && !path_is_dispatch_request_path?
end

def store?
Expand All @@ -25,6 +27,14 @@ def authenticate!

private

def path_is_dispatch_request_path?
current_path = EnvHelper.path_info(env)
request_method = EnvHelper.request_method(env)
dispatch_requests.any? do |tuple|
request_method == tuple.first && current_path.match(tuple.last)
end
end

def issuer_claim_valid?
configured_issuer = Warden::JWTAuth.config.issuer
return true if configured_issuer.nil?
Expand Down
37 changes: 36 additions & 1 deletion spec/warden/jwt_auth/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,28 @@
describe '#valid?' do
context 'when Authorization header is valid' do
it 'returns true' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123' }
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/users', 'REQUEST_METHOD' => 'GET' }
strategy = described_class.new(env, :user)

expect(strategy).to be_valid
end

it 'returns false when the current path / method matches a dispatch request path / method' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/sign_in', 'REQUEST_METHOD' => 'POST' }
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
end

it 'returns true when the current path matches a dispatch request, but the method does not' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/sign_in', 'REQUEST_METHOD' => 'GET' }
strategy = described_class.new(env, :user)

expect(strategy).to be_valid
end

it 'returns true when the current path does not match a dispatch request path' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/users', 'REQUEST_METHOD' => 'POST' }
strategy = described_class.new(env, :user)

expect(strategy).to be_valid
Expand All @@ -29,6 +50,20 @@

expect(strategy).not_to be_valid
end

it 'returns false when the current path matches a dispatch request path' do
env = { 'PATH_INFO' => '/sign_in', 'REQUEST_METHOD' => 'POST' }
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
end

it 'returns true when the current path does not match a dispatch request path' do
env = { 'PATH_INFO' => '/users', 'REQUEST_METHOD' => 'GET' }
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
end
end

context "when issuer is configured" do
Expand Down
Loading