Skip to content
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: 2 additions & 2 deletions lib/jwt/claims/audience.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def initialize(expected_audience:)
#
# @param context [Object] the context containing the JWT payload.
# @param _args [Hash] additional arguments (not used).
# @raise [JWT::InvalidAudError] if the audience claim is invalid.
# @raise [JWT::InvalidAudError] if the audience claim is invalid or the payload is not a Hash.
# @return [nil]
def verify!(context:, **_args)
aud = context.payload['aud']
aud = context.payload['aud'] if context.payload.is_a?(Hash)
raise JWT::InvalidAudError, "Invalid audience. Expected #{expected_audience}, received #{aud || '<none>'}" if ([*aud] & [*expected_audience]).empty?
end

Expand Down
5 changes: 3 additions & 2 deletions lib/jwt/claims/issuer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ def initialize(issuers:)
#
# @param context [Object] the context containing the JWT payload.
# @param _args [Hash] additional arguments (not used).
# @raise [JWT::InvalidIssuerError] if the issuer claim is invalid.
# @raise [JWT::InvalidIssuerError] if the issuer claim is invalid or the payload is not a Hash.
# @return [nil]
def verify!(context:, **_args)
case (iss = context.payload['iss'])
iss = context.payload['iss'] if context.payload.is_a?(Hash)
case iss
when *issuers
nil
else
Expand Down
4 changes: 2 additions & 2 deletions lib/jwt/claims/jwt_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def initialize(validator:)
#
# @param context [Object] the context containing the JWT payload.
# @param _args [Hash] additional arguments (not used).
# @raise [JWT::InvalidJtiError] if the JWT ID claim is invalid or missing.
# @raise [JWT::InvalidJtiError] if the JWT ID claim is invalid or missing, or the payload is not a Hash.
# @return [nil]
def verify!(context:, **_args)
jti = context.payload['jti']
jti = context.payload['jti'] if context.payload.is_a?(Hash)
if validator.respond_to?(:call)
verified = validator.arity == 2 ? validator.call(jti, context.payload) : validator.call(jti)
raise(JWT::InvalidJtiError, 'Invalid jti') unless verified
Expand Down
4 changes: 2 additions & 2 deletions lib/jwt/claims/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def initialize(expected_subject:)
#
# @param context [Object] the context containing the JWT payload.
# @param _args [Hash] additional arguments (not used).
# @raise [JWT::InvalidSubError] if the subject claim is invalid.
# @raise [JWT::InvalidSubError] if the subject claim is invalid or the payload is not a Hash.
# @return [nil]
def verify!(context:, **_args)
sub = context.payload['sub']
sub = context.payload['sub'] if context.payload.is_a?(Hash)
raise(JWT::InvalidSubError, "Invalid subject. Expected #{expected_subject}, received #{sub || '<none>'}") unless sub.to_s == expected_subject
end

Expand Down
9 changes: 9 additions & 0 deletions spec/jwt/claims/audience_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,14 @@
subject
end
end

context 'when payload is not a Hash' do
let(:expected_audience) { scalar_aud }
let(:payload) { [scalar_aud] }

it 'raises JWT::InvalidAudError' do
expect { subject }.to raise_error(JWT::InvalidAudError, 'Invalid audience. Expected ruby-jwt-aud, received <none>')
end
end
end
end
11 changes: 11 additions & 0 deletions spec/jwt/claims/issuer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
end
end

context 'when payload is not a Hash' do
[['x'], 42, nil, 'beautyexperts_nbf_iat'].each do |non_hash_payload|
context "when payload is #{non_hash_payload.inspect}" do
let(:payload) { non_hash_payload }
it 'raises JWT::InvalidIssuerError' do
expect { verify! }.to raise_error(JWT::InvalidIssuerError, 'Invalid issuer. Expected ["ruby-jwt-gem"], received <none>')
end
end
end
end

context 'when expected issuer is an array that matches the payload' do
let(:expected_issuers) { ['first', issuer, 'third'] }
it 'passes validation' do
Expand Down
14 changes: 14 additions & 0 deletions spec/jwt/claims/jwt_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
end
end

context 'when payload is not a Hash' do
let(:payload) { [jti] }
it 'raises JWT::InvalidJtiError' do
expect { verify! }.to raise_error(JWT::InvalidJtiError, 'Missing jti')
end

context 'when a validator is given' do
let(:validator) { ->(jti, payload) { jti.nil? && payload == [jti] } }
it 'is called with a nil jti' do
expect { verify! }.to raise_error(JWT::InvalidJtiError, 'Invalid jti')
end
end
end

context 'when payload contains a jti that is an empty string' do
let(:jti) { '' }
it 'raises JWT::InvalidJtiError' do
Expand Down
34 changes: 34 additions & 0 deletions spec/jwt/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,40 @@
end
end

context 'when the payload is not a JSON object' do
let(:token) { JWT::Token.new(payload: payload).tap { |t| t.sign!(algorithm: 'HS256', key: secret) }.jwt }

[['x'], 42, nil, 'string'].each do |non_object_payload|
context "when the payload is #{non_object_payload.inspect}" do
let(:payload) { non_object_payload }

it 'decodes the payload when no claim needs a JSON object' do
expect(JWT.decode(token, secret, true, algorithm: 'HS256')).to eq([payload, { 'alg' => 'HS256' }])
end

it 'raises JWT::InvalidIssuerError when the issuer is verified' do
expect { JWT.decode(token, secret, true, algorithm: 'HS256', verify_iss: true, iss: 'me') }.to raise_error(JWT::InvalidIssuerError, 'Invalid issuer. Expected ["me"], received <none>')
end

it 'raises JWT::InvalidAudError when the audience is verified' do
expect { JWT.decode(token, secret, true, algorithm: 'HS256', verify_aud: true, aud: 'me') }.to raise_error(JWT::InvalidAudError, 'Invalid audience. Expected me, received <none>')
end

it 'raises JWT::InvalidSubError when the subject is verified' do
expect { JWT.decode(token, secret, true, algorithm: 'HS256', verify_sub: true, sub: 'me') }.to raise_error(JWT::InvalidSubError, 'Invalid subject. Expected me, received <none>')
end

it 'raises JWT::InvalidJtiError when the jti is verified' do
expect { JWT.decode(token, secret, true, algorithm: 'HS256', verify_jti: true) }.to raise_error(JWT::InvalidJtiError, 'Missing jti')
end

it 'raises JWT::MissingRequiredClaim when a claim is required' do
expect { JWT.decode(token, secret, true, algorithm: 'HS256', required_claims: ['exp']) }.to raise_error(JWT::MissingRequiredClaim, 'Missing required claim exp')
end
end
end
end

context 'claim verification order' do
let(:token) { JWT.encode({ nbf: Time.now.to_i + 100 }, 'secret') }

Expand Down
Loading