lib/jwt/jwa.rb in jwt-2.8.2 vs lib/jwt/jwa.rb in jwt-2.9.0

- old
+ new

@@ -6,57 +6,40 @@ require 'rbnacl' rescue LoadError raise if defined?(RbNaCl) end -require_relative 'jwa/hmac' -require_relative 'jwa/eddsa' +require_relative 'jwa/signing_algorithm' require_relative 'jwa/ecdsa' -require_relative 'jwa/rsa' -require_relative 'jwa/ps' +require_relative 'jwa/hmac' require_relative 'jwa/none' +require_relative 'jwa/ps' +require_relative 'jwa/rsa' require_relative 'jwa/unsupported' require_relative 'jwa/wrapper' +if JWT.rbnacl? + require_relative 'jwa/eddsa' +end + +if JWT.rbnacl_6_or_greater? + require_relative 'jwa/hmac_rbnacl' +elsif JWT.rbnacl? + require_relative 'jwa/hmac_rbnacl_fixed' +end + module JWT module JWA - ALGOS = [Hmac, Ecdsa, Rsa, Eddsa, Ps, None, Unsupported].tap do |l| - if ::JWT.rbnacl_6_or_greater? - require_relative 'jwa/hmac_rbnacl' - l << Algos::HmacRbNaCl - elsif ::JWT.rbnacl? - require_relative 'jwa/hmac_rbnacl_fixed' - l << Algos::HmacRbNaClFixed - end - end.freeze - class << self - def find(algorithm) - indexed[algorithm&.downcase] - end + def resolve(algorithm) + return find(algorithm) if algorithm.is_a?(String) || algorithm.is_a?(Symbol) - def create(algorithm) - return algorithm if JWA.implementation?(algorithm) - - Wrapper.new(*find(algorithm)) - end - - def implementation?(algorithm) - (algorithm.respond_to?(:valid_alg?) && algorithm.respond_to?(:verify)) || - (algorithm.respond_to?(:alg) && algorithm.respond_to?(:sign)) - end - - private - - def indexed - @indexed ||= begin - fallback = [nil, Unsupported] - ALGOS.each_with_object(Hash.new(fallback)) do |cls, hash| - cls.const_get(:SUPPORTED).each do |alg| - hash[alg.downcase] = [alg, cls] - end - end + unless algorithm.is_a?(SigningAlgorithm) + Deprecations.warning('Custom algorithms are required to include JWT::JWA::SigningAlgorithm') + return Wrapper.new(algorithm) end + + algorithm end end end end