diff --git a/CHANGELOG.md b/CHANGELOG.md index bc6060c..f97a38a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ### next -* TODO: Replace this bullet point with an actual description of a change. +* Switched to Zeitwerk as autoloader (#17) ### 2.6.0 (3 January 2025) diff --git a/grape-jwt-authentication.gemspec b/grape-jwt-authentication.gemspec index abec391..b869c29 100644 --- a/grape-jwt-authentication.gemspec +++ b/grape-jwt-authentication.gemspec @@ -37,6 +37,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'grape', '>= 1.0', '< 3.0' spec.add_dependency 'httparty', '>= 0.21' spec.add_dependency 'jwt', '~> 2.6' - spec.add_dependency 'keyless', '~> 1.4' + spec.add_dependency 'keyless', '~> 1.6' spec.add_dependency 'recursive-open-struct', '~> 2.0' + spec.add_dependency 'zeitwerk', '~> 2.6' end diff --git a/lib/grape/jwt/authentication.rb b/lib/grape/jwt/authentication.rb index df42927..729cbdd 100644 --- a/lib/grape/jwt/authentication.rb +++ b/lib/grape/jwt/authentication.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'zeitwerk' require 'active_support' require 'active_support/concern' require 'active_support/configurable' @@ -10,10 +11,6 @@ require 'jwt' require 'keyless' require 'grape' -require 'grape/jwt/authentication/version' -require 'grape/jwt/authentication/configuration' -require 'grape/jwt/authentication/dependencies' -require 'grape/jwt/authentication/jwt_handler' module Grape module Jwt @@ -22,32 +19,44 @@ module Authentication extend ActiveSupport::Concern include Grape::DSL::API + # Setup a Zeitwerk autoloader instance and configure it + loader = Zeitwerk::Loader.for_gem_extension(Grape::Jwt) + + # Finish the auto loader configuration + loader.setup + + # Make sure to eager load all SDK constants + loader.eager_load + class << self attr_writer :configuration - end - # Retrieve the current configuration object. - # - # @return [Configuration] - def self.configuration - @configuration ||= Configuration.new - end + # Include top-level features + include Extensions::Dependencies - # Configure the concern by providing a block which takes - # care of this task. Example: - # - # Grape::Jwt::Authentication.configure do |conf| - # # conf.xyz = [..] - # end - def self.configure - yield(configuration) - configure_dependencies - end + # Retrieve the current configuration object. + # + # @return [Configuration] + def configuration + @configuration ||= Configuration.new + end - # Reset the current configuration with the default one. - def self.reset_configuration! - self.configuration = Configuration.new - configure_dependencies + # Configure the concern by providing a block which takes + # care of this task. Example: + # + # Grape::Jwt::Authentication.configure do |conf| + # # conf.xyz = [..] + # end + def configure + yield(configuration) + configure_dependencies + end + + # Reset the current configuration with the default one. + def reset_configuration! + self.configuration = Configuration.new + configure_dependencies + end end included do diff --git a/lib/grape/jwt/authentication/dependencies.rb b/lib/grape/jwt/authentication/dependencies.rb deleted file mode 100644 index 880c8f5..0000000 --- a/lib/grape/jwt/authentication/dependencies.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -module Grape - module Jwt - # The Grape JWT authentication concern. - module Authentication - # Specifies which configuration keys are shared between keyless - # and grape-jwt-authentication, so that we can easily pass through - # our configuration to keyless. - KEYLESS_CONFIGURATION = %i[ - authenticator rsa_public_key_url rsa_public_key_caching - rsa_public_key_expiration jwt_issuer jwt_beholder jwt_options - jwt_verification_key - ].freeze - - # (Re)configure our gem dependencies. We take care of setting up - # +Keyless+, which has been extracted from this gem. - def self.configure_dependencies - configure_keyless - end - - # Configure the +Keyless+ gem with our configuration. - def self.configure_keyless - configuration = Grape::Jwt::Authentication.configuration - - Keyless.configure do |keyless| - KEYLESS_CONFIGURATION.each do |option| - keyless.send("#{option}=", configuration.send(option)) - end - end - end - end - end -end diff --git a/lib/grape/jwt/authentication/extensions/dependencies.rb b/lib/grape/jwt/authentication/extensions/dependencies.rb new file mode 100644 index 0000000..f26d33f --- /dev/null +++ b/lib/grape/jwt/authentication/extensions/dependencies.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Grape + module Jwt + module Authentication + module Extensions + # Root-level handling of dependencies. + module Dependencies + # Specifies which configuration keys are shared between keyless + # and grape-jwt-authentication, so that we can easily pass through + # our configuration to keyless. + KEYLESS_CONFIGURATION = %i[ + authenticator rsa_public_key_url rsa_public_key_caching + rsa_public_key_expiration jwt_issuer jwt_beholder jwt_options + jwt_verification_key + ].freeze + + # (Re)configure our gem dependencies. We take care of setting up + # +Keyless+, which has been extracted from this gem. + def configure_dependencies + configure_keyless + end + + # Configure the +Keyless+ gem with our configuration. + def configure_keyless + configuration = Grape::Jwt::Authentication.configuration + + Keyless.configure do |keyless| + KEYLESS_CONFIGURATION.each do |option| + keyless.send("#{option}=", configuration.send(option)) + end + end + end + end + end + end + end +end