Skip to content

Commit

Permalink
Don't raise an error without configuration, use default config instead
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed Jan 30, 2023
1 parent ee8547d commit 1e0a796
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 97 deletions.
76 changes: 71 additions & 5 deletions lib/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,77 @@ module SecretStoring
autoload :BCrypt, "doorkeeper/secret_storing/bcrypt"
end

def self.authenticate(request, methods = Doorkeeper.config.access_token_methods)
OAuth::Token.authenticate(request, *methods)
end
class << self
attr_reader :orm_adapter

def configure(&block)
@config = Config::Builder.new(&block).build
setup
@config
end

# @return [Doorkeeper::Config] configuration instance
#
def configuration
@config || configure
end

def configured?
!@config.nil?
end

alias config configuration

def setup
setup_orm_adapter
run_orm_hooks
config.clear_cache!

# Deprecated, will be removed soon
unless configuration.orm == :active_record
setup_orm_models
setup_application_owner
end
end

def setup_orm_adapter
@orm_adapter = "doorkeeper/orm/#{configuration.orm}".classify.constantize
rescue NameError => e
raise e, "ORM adapter not found (#{configuration.orm})", <<-ERROR_MSG.strip_heredoc
[DOORKEEPER] ORM adapter not found (#{configuration.orm}), or there was an error
trying to load it.
def self.gem_version
::Gem::Version.new(::Doorkeeper::VERSION::STRING)
You probably need to add the related gem for this adapter to work with
doorkeeper.
ERROR_MSG
end

def run_orm_hooks
if @orm_adapter.respond_to?(:run_hooks)
@orm_adapter.run_hooks
else
::Kernel.warn <<~MSG.strip_heredoc
[DOORKEEPER] ORM "#{configuration.orm}" should move all it's setup logic under `#run_hooks` method for
the #{@orm_adapter.name}. Later versions of Doorkeeper will no longer support `setup_orm_models` and
`setup_application_owner` API.
MSG
end
end

def setup_orm_models
@orm_adapter.initialize_models!
end

def setup_application_owner
@orm_adapter.initialize_application_owner!
end

def authenticate(request, methods = Doorkeeper.config.access_token_methods)
OAuth::Token.authenticate(request, *methods)
end

def gem_version
::Gem::Version.new(::Doorkeeper::VERSION::STRING)
end
end
end
76 changes: 0 additions & 76 deletions lib/doorkeeper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,11 @@
require "doorkeeper/config/validations"

module Doorkeeper
# Defines a MissingConfiguration error for a missing Doorkeeper configuration
#
class MissingConfiguration < StandardError
def initialize
super("Configuration for doorkeeper missing. Do you have doorkeeper initializer?")
end
end

# Doorkeeper option DSL could be reused in extensions to build their own
# configurations. To use the Option DSL gems need to define `builder_class` method
# that returns configuration Builder class. This exception raises when they don't
# define it.
#
class MissingConfigurationBuilderClass < StandardError; end

class << self
attr_reader :orm_adapter

def configure(&block)
@config = Config::Builder.new(&block).build
setup
@config
end

# @return [Doorkeeper::Config] configuration instance
#
def configuration
@config || (raise MissingConfiguration)
end

def configured?
!@config.nil?
end

alias config configuration

def setup
setup_orm_adapter
run_orm_hooks
config.clear_cache!

# Deprecated, will be removed soon
unless configuration.orm == :active_record
setup_orm_models
setup_application_owner
end
end

def setup_orm_adapter
@orm_adapter = "doorkeeper/orm/#{configuration.orm}".classify.constantize
rescue NameError => e
raise e, "ORM adapter not found (#{configuration.orm})", <<-ERROR_MSG.strip_heredoc
[DOORKEEPER] ORM adapter not found (#{configuration.orm}), or there was an error
trying to load it.
You probably need to add the related gem for this adapter to work with
doorkeeper.
ERROR_MSG
end

def run_orm_hooks
if @orm_adapter.respond_to?(:run_hooks)
@orm_adapter.run_hooks
else
::Kernel.warn <<~MSG.strip_heredoc
[DOORKEEPER] ORM "#{configuration.orm}" should move all it's setup logic under `#run_hooks` method for
the #{@orm_adapter.name}. Later versions of Doorkeeper will no longer support `setup_orm_models` and
`setup_application_owner` API.
MSG
end
end

def setup_orm_models
@orm_adapter.initialize_models!
end

def setup_application_owner
@orm_adapter.initialize_application_owner!
end
end

class Config
# Default Doorkeeper configuration builder
class Builder < AbstractBuilder
Expand Down
2 changes: 1 addition & 1 deletion lib/doorkeeper/config/abstract_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AbstractBuilder
#
def initialize(config = Config.new, &block)
@config = config
instance_eval(&block)
instance_eval(&block) if block_given?
end

# Builds and validates configuration.
Expand Down
15 changes: 0 additions & 15 deletions spec/lib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,21 +501,6 @@ class ApplicationWithOwner < ActiveRecord::Base
end
end

it "raises an exception when configuration is not set" do
old_config = Doorkeeper.config
Doorkeeper.module_eval do
@config = nil
end

expect do
Doorkeeper.config
end.to raise_error Doorkeeper::MissingConfiguration

Doorkeeper.module_eval do
@config = old_config
end
end

describe "access_token_generator" do
it "is 'Doorkeeper::OAuth::Helpers::UniqueToken' by default" do
expect(Doorkeeper.configuration.access_token_generator).to(
Expand Down

0 comments on commit 1e0a796

Please sign in to comment.