Skip to content

Commit

Permalink
Fix issuer claim tests
Browse files Browse the repository at this point in the history
  • Loading branch information
waiting-for-dev committed Dec 15, 2024
1 parent 787164d commit fcc8152
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/warden/jwt_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module Warden
module JWTAuth
extend Dry::Configurable

module_function

def symbolize_keys(hash)
hash.transform_keys(&:to_sym)
end
Expand All @@ -36,8 +38,6 @@ def constantize_values(hash)
end
end

module_function :constantize_values, :symbolize_keys, :upcase_first_items

# The secret used to encode the token
setting :secret

Expand Down
1 change: 1 addition & 0 deletions spec/support/shared_contexts/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
config.mappings = { user: Fixtures::UserRepo }
config.token_header = 'Authorization'
config.aud_header = 'TEST_AUD'
config.issuer = 'http://example.com'
end
end

Expand Down
33 changes: 14 additions & 19 deletions spec/warden/jwt_auth/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,25 @@
end
end

context 'when issuer is configured' do
let(:token) { Warden::JWTAuth::TokenEncoder.new.call({ issuer: issuer }) }
let(:env) { { 'HTTP_AUTHORIZATION' => "Bearer #{token}" } }
let(:issuer) { 'http://example.com' }
let(:strategy) { described_class.new(env, :user) }
context 'when issuer claim is configured and it matches the configured issuer' do
it 'returns true' do
token = Warden::JWTAuth::TokenEncoder.new.call({ 'iss' => Warden::JWTAuth.config.issuer })
env = { 'HTTP_AUTHORIZATION' => "Bearer #{token}" }

before do
Warden::JWTAuth.configure do |config|
config.issuer = issuer
end
end
strategy = described_class.new(env, :user)

context 'when the issuer claim matches the configured issuer' do
it 'returns true' do
expect(strategy).to be_valid
end
expect(strategy).to be_valid
end
end

context 'when the issuer claim does not match the configured issuer' do
let(:token) { Warden::JWTAuth::TokenEncoder.new.call({ 'iss' => 'http://example.org' }) }
context "when issuer claim is configured and it doesn't match the configured issuer" do
it 'returns false' do
token = Warden::JWTAuth::TokenEncoder.new.call({ 'iss' => Warden::JWTAuth.config.issuer + 'aaa' })
env = { 'HTTP_AUTHORIZATION' => "Bearer #{token}" }

it 'returns false' do
expect(strategy).not_to be_valid
end
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
end
end
end
Expand Down

0 comments on commit fcc8152

Please sign in to comment.