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

Validate audience when payload is a scalar and options is an array #183

Merged
merged 1 commit into from
Feb 2, 2017
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
16 changes: 2 additions & 14 deletions lib/jwt/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,11 @@ def initialize(payload, options)
def verify_aud
return unless (options_aud = extract_option(:aud))

if @payload['aud'].is_a?(Array)
verify_aud_array(@payload['aud'], options_aud)
else
if ([*@payload['aud']] & [*options_aud]).empty?
raise(
JWT::InvalidAudError,
"Invalid audience. Expected #{options_aud}, received #{@payload['aud'] || '<none>'}"
) unless @payload['aud'].to_s == options_aud.to_s
end
end

def verify_aud_array(audience, options_aud)
if options_aud.is_a?(Array)
options_aud.each do |aud|
raise(JWT::InvalidAudError, 'Invalid audience') unless audience.include?(aud.to_s)
end
else
raise(JWT::InvalidAudError, 'Invalid audience') unless audience.include?(options_aud.to_s)
)
end
end

Expand Down
18 changes: 17 additions & 1 deletion spec/jwt/verify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module JWT
let(:options) { { leeway: 0 } }

context '.verify_aud(payload, options)' do
let(:scalar_aud) { 'ruby-jwt-audience' }
let(:scalar_aud) { 'ruby-jwt-aud' }
let(:array_aud) { %w(ruby-jwt-aud test-aud ruby-ruby-ruby) }
let(:scalar_payload) { base_payload.merge('aud' => scalar_aud) }
let(:array_payload) { base_payload.merge('aud' => array_aud) }
Expand Down Expand Up @@ -47,6 +47,22 @@ module JWT
Verify.verify_aud(array_payload, options.merge('aud' => array_aud.first))
end

it 'must allow an array with any value matching any value in the options array' do
Verify.verify_aud(array_payload, options.merge(aud: array_aud))
end

it 'must allow an array with any value matching any value in the options array with a string options key' do
Verify.verify_aud(array_payload, options.merge("aud" => array_aud))
end

it 'must allow a singular audience payload matching any value in the options array' do
Verify.verify_aud(scalar_payload, options.merge(aud: array_aud))
end

it 'must allow a singular audience payload matching any value in the options array with a string options key' do
Verify.verify_aud(scalar_payload, options.merge("aud" => array_aud))
end

it 'should allow strings or symbols in options array' do
options['aud'] = [
'ruby-jwt-aud',
Expand Down