Skip to content

Commit

Permalink
Only support symbols for options names
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilioCristalli committed Feb 19, 2017
1 parent 300ed6c commit 36a7175
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 47 deletions.
20 changes: 8 additions & 12 deletions lib/jwt/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(payload, options)
end

def verify_aud
return unless (options_aud = extract_option(:aud))
return unless (options_aud = @options[:aud])
raise(JWT::InvalidAudError, "Invalid audience. Expected #{options_aud}, received #{@payload['aud'] || '<none>'}") if ([*@payload['aud']] & [*options_aud]).empty?
end

Expand All @@ -40,12 +40,12 @@ def verify_iat
end

def verify_iss
return unless (options_iss = extract_option(:iss))
return unless (options_iss = @options[:iss])
raise(JWT::InvalidIssuerError, "Invalid issuer. Expected #{options_iss}, received #{@payload['iss'] || '<none>'}") if @payload['iss'].to_s != options_iss.to_s
end

def verify_jti
options_verify_jti = extract_option(:verify_jti)
options_verify_jti = @options[:verify_jti]
if options_verify_jti.respond_to?(:call)
raise(JWT::InvalidJtiError, 'Invalid jti') unless options_verify_jti.call(@payload['jti'])
elsif @payload['jti'].to_s.strip.empty?
Expand All @@ -59,30 +59,26 @@ def verify_not_before
end

def verify_sub
return unless (options_sub = extract_option(:sub))
return unless (options_sub = @options[:sub])
raise(JWT::InvalidSubError, "Invalid subject. Expected #{options_sub}, received #{@payload['sub'] || '<none>'}") unless @payload['sub'].to_s == options_sub.to_s
end

private

def extract_option(key)
@options.values_at(key.to_sym, key.to_s).compact.first
end

def global_leeway
extract_option :leeway
@options[:leeway]
end

def exp_leeway
extract_option(:exp_leeway) || global_leeway
@options[:exp_leeway] || global_leeway
end

def iat_leeway
extract_option(:iat_leeway) || global_leeway
@options[:iat_leeway] || global_leeway
end

def nbf_leeway
extract_option(:nbf_leeway) || global_leeway
@options[:nbf_leeway] || global_leeway
end
end
end
35 changes: 0 additions & 35 deletions spec/jwt/verify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,21 @@ module JWT
end.to raise_error JWT::InvalidAudError
end

it 'must raise JWT::InvalidAudError when the singular audience does not match and the options aud key is a string' do
expect do
Verify.verify_aud(scalar_payload, options.merge('aud' => 'no-match'))
end.to raise_error JWT::InvalidAudError
end

it 'must allow a matching singular audience to pass' do
Verify.verify_aud(scalar_payload, options.merge(aud: scalar_aud))
end

it 'must allow a matching audence to pass when the options key is a string' do
Verify.verify_aud(scalar_payload, options.merge('aud' => scalar_aud))
end

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

it 'must allow an array with any value matching the one in the options with a string options key' do
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',
'test-aud',
'ruby-ruby-ruby',
:test
]

array_payload['aud'].push('test')

Verify.verify_aud(array_payload, options)
end
end

context '.verify_expiration(payload, options)' do
Expand Down

0 comments on commit 36a7175

Please sign in to comment.