Skip to content

Commit

Permalink
Bring back Ruby 2.5 compatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Jun 9, 2023
1 parent 56ca3f3 commit 7f8887d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
11 changes: 11 additions & 0 deletions lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def decode(jwt, key = nil, verify = true, options = {}, &keyfinder) # rubocop:di
verification_key: key,
verify: verify,
keyfinder: keyfinder,
allowed_algorithms: normalizde_algorithm_option(options),
**configuration.decode.to_h.merge(options)).decode_segments
end

# Order is very important - first check for string keys, next for symbols
ALGORITHM_KEYS = ['algorithm',
:algorithm,
'algorithms',
:algorithms].freeze
def normalizde_algorithm_option(options)
ALGORITHM_KEYS.map { |alg_key| options.delete(alg_key) }.compact.first ||
configuration.decode.algorithms
end
end
25 changes: 5 additions & 20 deletions lib/jwt/default_decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

module JWT
class DefaultDecoder
def self.define_decoder(options) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
def self.define_decoder(options)
JWT.define do
allowed_algorithms(*Array(options['algorithm'] || options[:algorithm] || options['algorithms'] || options[:algorithms]))
allowed_algorithms(*options[:allowed_algorithms])

if options[:verification_key]
verification_key(options[:verification_key])
Expand Down Expand Up @@ -35,10 +35,9 @@ def initialize(token:, verify:, **options)
raise(JWT::DecodeError, 'Nil JSON web token') unless token

@options = options
@verify = verify

decoder = self.class.define_decoder(options)
@verify = verify

decoder = self.class.define_decoder(options)
@decode_context = decoder.decode(token: token)
end

Expand Down Expand Up @@ -77,26 +76,12 @@ def allowed_and_valid_algorithms
@allowed_and_valid_algorithms ||= allowed_algorithms.select { |alg| alg.valid_alg?(alg_in_header) }
end

# Order is very important - first check for string keys, next for symbols
ALGORITHM_KEYS = ['algorithm',
:algorithm,
'algorithms',
:algorithms].freeze

def given_algorithms
ALGORITHM_KEYS.each do |alg_key|
alg = @options[alg_key]
return Array(alg) if alg
end
[]
end

def allowed_algorithms
@allowed_algorithms ||= resolve_allowed_algorithms
end

def resolve_allowed_algorithms
given_algorithms.map do |alg|
Array(@options[:allowed_algorithms]).map do |alg|
if JWA.implementation?(alg)
alg
else
Expand Down

0 comments on commit 7f8887d

Please sign in to comment.