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

Allow a list of algorithms for decode #241

Merged
merged 3 commits into from
Oct 5, 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
12 changes: 10 additions & 2 deletions lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def decode(jwt, key = nil, verify = true, custom_options = {}, &keyfinder)
def decode_verify_signature(key, header, payload, signature, signing_input, options, &keyfinder)
algo, key = signature_algorithm_and_key(header, payload, key, &keyfinder)

raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') unless options[:algorithm]
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless algo == options[:algorithm]
raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms(options).empty?
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless allowed_algorithms(options).include?(algo)

Signature.verify(algo, key, signing_input, signature)
end
Expand All @@ -58,4 +58,12 @@ def signature_algorithm_and_key(header, payload, key, &keyfinder)
end
[header['alg'], key]
end

def allowed_algorithms(options)
if options.key?(:algorithm)
[options[:algorithm]]
else
options[:algorithms] || []
end
end
end
2 changes: 1 addition & 1 deletion lib/jwt/default_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module DefaultOptions
verify_aud: false,
verify_sub: false,
leeway: 0,
algorithm: 'HS256'
algorithms: ['HS256']
}.freeze
end
end
18 changes: 15 additions & 3 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,27 @@

context 'Verify' do
context 'algorithm' do
it 'should raise JWT::IncorrectAlgorithm on missmatch' do
token = JWT.encode payload, data[:secret], 'HS512'
it 'should raise JWT::IncorrectAlgorithm on mismatch' do
token = JWT.encode payload, data[:secret], 'HS256'

expect do
JWT.decode token, data[:secret], true, algorithm: 'HS384'
end.to raise_error JWT::IncorrectAlgorithm

expect do
JWT.decode token, data[:secret], true, algorithm: 'HS512'
JWT.decode token, data[:secret], true, algorithm: 'HS256'
end.not_to raise_error
end

it 'should raise JWT::IncorrectAlgorithm when algorithms array does not contain algorithm' do
token = JWT.encode payload, data[:secret], 'HS512'

expect do
JWT.decode token, data[:secret], true, algorithms: ['HS384']
end.to raise_error JWT::IncorrectAlgorithm

expect do
JWT.decode token, data[:secret], true, algorithms: ['HS512', 'HS384']
end.not_to raise_error
end

Expand Down