diff --git a/lib/jwt/deprecations.rb b/lib/jwt/deprecations.rb index b77540a1..9f516a2d 100644 --- a/lib/jwt/deprecations.rb +++ b/lib/jwt/deprecations.rb @@ -6,6 +6,8 @@ module Deprecations class << self def context yield.tap { emit_warnings } + ensure + Thread.current[:jwt_warning_store] = nil end def warning(message, only_if_valid: false) @@ -30,8 +32,6 @@ def emit_warnings return if Thread.current[:jwt_warning_store].nil? Thread.current[:jwt_warning_store].each { |warning| warn(warning) } - - Thread.current[:jwt_warning_store] = nil end private diff --git a/spec/jwt/jwt_spec.rb b/spec/jwt/jwt_spec.rb index da1c01cd..5dcd05b5 100644 --- a/spec/jwt/jwt_spec.rb +++ b/spec/jwt/jwt_spec.rb @@ -957,9 +957,23 @@ def valid_alg?(alg) end end - context 'when valid token is invalid strict base64' do + context 'when valid token is invalid strict base64 and decoded with the correct key' do it 'does outputs deprecation warning' do - expect { JWT.decode("#{JWT.encode('a', 'b')} ", 'b') }.to output(/DEPRECATION/).to_stderr + expect { JWT.decode("#{JWT.encode('payload', 'key')} ", 'key') }.to output(/DEPRECATION/).to_stderr + end + end + + context 'when valid token is invalid strict base64 and decoded with the incorrect key' do + it 'does not output deprecation warning, even when decoded with the correct key' do + token = JWT.encode('payload', 'key') + expect { + begin + JWT.decode("#{token} ", 'incorrect') + rescue JWT::VerificationError + nil + end + JWT.decode(token, 'key') + }.not_to output(/DEPRECATION/).to_stderr end end end