Skip to content

Commit 1a2aa1a

Browse files
committed
Extract algorithm checks to lower percieved complexity
1 parent a0897cf commit 1a2aa1a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/jwt/decode.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def decode_segments
2424
validate_segment_count!
2525
if @verify
2626
decode_crypto
27+
verify_algo
2728
verify_signature
2829
verify_claims
2930
end
@@ -33,14 +34,16 @@ def decode_segments
3334

3435
private
3536

37+
def verify_algo
38+
raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms.empty?
39+
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless options_includes_algo_in_header?
40+
end
41+
3642
def verify_signature
3743
@key = find_key(&@keyfinder) if @keyfinder
3844
@key = ::JWT::JWK::KeyFinder.new(jwks: @options[:jwks]).key_for(header['kid']) if @options[:jwks]
3945
@key = JWT::X5cKeyFinder.from(header['x5c'], @options.fetch(:root_certificates), @options[:crls]) if header['x5c']
4046

41-
raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms.empty?
42-
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless options_includes_algo_in_header?
43-
4447
Signature.verify(header['alg'], @key, signing_input, @signature)
4548
end
4649

0 commit comments

Comments
 (0)