Skip to content

Commit

Permalink
Extract algorithm checks to lower percieved complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater committed Dec 27, 2021
1 parent f7c0b15 commit e2bfc24
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Metrics/AbcSize:
Max: 25

Metrics/ClassLength:
Max: 101
Max: 103

Metrics/ModuleLength:
Max: 100
Expand Down Expand Up @@ -95,4 +95,4 @@ Layout/HashAlignment:
EnforcedLastArgumentHashStyle: always_ignore

Style/TrivialAccessors:
AllowPredicates: true
AllowPredicates: true
5 changes: 4 additions & 1 deletion lib/jwt/decode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def decode_segments
validate_segment_count!
if @verify
decode_crypto
verify_algo
set_key
verify_signature
verify_claims
Expand All @@ -46,11 +47,13 @@ def verify_signature
raise(JWT::VerificationError, 'Signature verification failed')
end

def set_key
def verify_algo
raise(JWT::IncorrectAlgorithm, 'An algorithm must be specified') if allowed_algorithms.empty?
raise(JWT::IncorrectAlgorithm, 'Token is missing alg header') unless algorithm
raise(JWT::IncorrectAlgorithm, 'Expected a different algorithm') unless options_includes_algo_in_header?
end

def set_key
@key = find_key(&@keyfinder) if @keyfinder
@key = ::JWT::JWK::KeyFinder.new(jwks: @options[:jwks]).key_for(header['kid']) if @options[:jwks]
if (x5c_options = @options[:x5c])
Expand Down

0 comments on commit e2bfc24

Please sign in to comment.