REFACTOR: Initialize auth providers after `plugin.activate!`
Also added some helpful functionality for plugin developers: - Raises RuntimeException if the auth provider has been registered too late - Logs use of deprecated parameters
This commit is contained in:
parent
488fba3c5f
commit
4e010382cc
|
@ -8,11 +8,18 @@ class Auth::AuthProvider
|
||||||
def self.auth_attributes
|
def self.auth_attributes
|
||||||
[:pretty_name, :title, :message, :frame_width, :frame_height, :authenticator,
|
[:pretty_name, :title, :message, :frame_width, :frame_height, :authenticator,
|
||||||
:pretty_name_setting, :title_setting, :enabled_setting, :full_screen_login, :full_screen_login_setting,
|
:pretty_name_setting, :title_setting, :enabled_setting, :full_screen_login, :full_screen_login_setting,
|
||||||
:custom_url]
|
:custom_url, :background_color]
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor(*auth_attributes)
|
attr_accessor(*auth_attributes)
|
||||||
|
|
||||||
|
def enabled_setting=(val)
|
||||||
|
Discourse.deprecate("enabled_setting is deprecated. Please define authenticator.enabled? instead")
|
||||||
|
@enabled_setting = val
|
||||||
|
end
|
||||||
|
|
||||||
|
def background_color=(val) Discourse.deprecate("background_color is no longer functional. Please define authenticator.enabled? instead") end;
|
||||||
|
|
||||||
def name
|
def name
|
||||||
authenticator.name
|
authenticator.name
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,6 +7,8 @@ class Middleware::OmniauthBypassMiddleware
|
||||||
def initialize(app, options = {})
|
def initialize(app, options = {})
|
||||||
@app = app
|
@app = app
|
||||||
|
|
||||||
|
Discourse.plugins.each(&:notify_before_auth)
|
||||||
|
|
||||||
# if you need to test this and are having ssl issues see:
|
# if you need to test this and are having ssl issues see:
|
||||||
# http://stackoverflow.com/questions/6756460/openssl-error-using-omniauth-specified-ssl-path-but-didnt-work
|
# http://stackoverflow.com/questions/6756460/openssl-error-using-omniauth-specified-ssl-path-but-didnt-work
|
||||||
# OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE if Rails.env.development?
|
# OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE if Rails.env.development?
|
||||||
|
|
|
@ -25,8 +25,8 @@ class Plugin::Instance
|
||||||
|
|
||||||
# Memoized array readers
|
# Memoized array readers
|
||||||
[:assets,
|
[:assets,
|
||||||
:auth_providers,
|
|
||||||
:color_schemes,
|
:color_schemes,
|
||||||
|
:before_auth_initializers,
|
||||||
:initializers,
|
:initializers,
|
||||||
:javascripts,
|
:javascripts,
|
||||||
:locales,
|
:locales,
|
||||||
|
@ -287,6 +287,11 @@ class Plugin::Instance
|
||||||
initializers << block
|
initializers << block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def before_auth(&block)
|
||||||
|
raise "Auth providers must be registered before omniauth middleware. after_initialize is too late!" if @before_auth_complete
|
||||||
|
before_auth_initializers << block
|
||||||
|
end
|
||||||
|
|
||||||
# A proxy to `DiscourseEvent.on` which does nothing if the plugin is disabled
|
# A proxy to `DiscourseEvent.on` which does nothing if the plugin is disabled
|
||||||
def on(event_name, &block)
|
def on(event_name, &block)
|
||||||
DiscourseEvent.on(event_name) do |*args|
|
DiscourseEvent.on(event_name) do |*args|
|
||||||
|
@ -313,6 +318,13 @@ class Plugin::Instance
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def notify_before_auth
|
||||||
|
before_auth_initializers.each do |callback|
|
||||||
|
callback.call(self)
|
||||||
|
end
|
||||||
|
@before_auth_complete = true
|
||||||
|
end
|
||||||
|
|
||||||
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
|
||||||
def register_category_custom_field_type(name, type)
|
def register_category_custom_field_type(name, type)
|
||||||
reloadable_patch do |plugin|
|
reloadable_patch do |plugin|
|
||||||
|
@ -458,7 +470,6 @@ class Plugin::Instance
|
||||||
register_assets! unless assets.blank?
|
register_assets! unless assets.blank?
|
||||||
register_locales!
|
register_locales!
|
||||||
register_service_workers!
|
register_service_workers!
|
||||||
register_auth_providers!
|
|
||||||
|
|
||||||
seed_data.each do |key, value|
|
seed_data.each do |key, value|
|
||||||
DiscoursePluginRegistry.register_seed_data(key, value)
|
DiscoursePluginRegistry.register_seed_data(key, value)
|
||||||
|
@ -496,13 +507,13 @@ class Plugin::Instance
|
||||||
end
|
end
|
||||||
|
|
||||||
def auth_provider(opts)
|
def auth_provider(opts)
|
||||||
|
before_auth do
|
||||||
provider = Auth::AuthProvider.new
|
provider = Auth::AuthProvider.new
|
||||||
|
|
||||||
Auth::AuthProvider.auth_attributes.each do |sym|
|
Auth::AuthProvider.auth_attributes.each do |sym|
|
||||||
provider.send "#{sym}=", opts.delete(sym)
|
provider.send "#{sym}=", opts.delete(sym)
|
||||||
end
|
end
|
||||||
|
|
||||||
after_initialize do
|
|
||||||
begin
|
begin
|
||||||
provider.authenticator.enabled?
|
provider.authenticator.enabled?
|
||||||
rescue NotImplementedError
|
rescue NotImplementedError
|
||||||
|
@ -513,9 +524,9 @@ class Plugin::Instance
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
auth_providers << provider
|
DiscoursePluginRegistry.register_auth_provider(provider)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# shotgun approach to gem loading, in future we need to hack bundler
|
# shotgun approach to gem loading, in future we need to hack bundler
|
||||||
|
@ -594,12 +605,6 @@ class Plugin::Instance
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_auth_providers!
|
|
||||||
auth_providers.each do |auth_provider|
|
|
||||||
DiscoursePluginRegistry.register_auth_provider(auth_provider)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def register_locales!
|
def register_locales!
|
||||||
root_path = File.dirname(@path)
|
root_path = File.dirname(@path)
|
||||||
|
|
||||||
|
|
|
@ -133,16 +133,18 @@ describe Plugin::Instance do
|
||||||
# No enabled_site_setting
|
# No enabled_site_setting
|
||||||
authenticator = Auth::Authenticator.new
|
authenticator = Auth::Authenticator.new
|
||||||
plugin.auth_provider(authenticator: authenticator)
|
plugin.auth_provider(authenticator: authenticator)
|
||||||
plugin.notify_after_initialize
|
plugin.notify_before_auth
|
||||||
expect(authenticator.enabled?).to eq(true)
|
expect(authenticator.enabled?).to eq(true)
|
||||||
|
|
||||||
# With enabled site setting
|
# With enabled site setting
|
||||||
|
plugin = Plugin::Instance.new
|
||||||
authenticator = Auth::Authenticator.new
|
authenticator = Auth::Authenticator.new
|
||||||
plugin.auth_provider(enabled_setting: 'ubuntu_login_enabled', authenticator: authenticator)
|
plugin.auth_provider(enabled_setting: 'ubuntu_login_enabled', authenticator: authenticator)
|
||||||
plugin.notify_after_initialize
|
plugin.notify_before_auth
|
||||||
expect(authenticator.enabled?).to eq(false)
|
expect(authenticator.enabled?).to eq(false)
|
||||||
|
|
||||||
# Defines own method
|
# Defines own method
|
||||||
|
plugin = Plugin::Instance.new
|
||||||
SiteSetting.stubs(:ubuntu_login_enabled).returns(true)
|
SiteSetting.stubs(:ubuntu_login_enabled).returns(true)
|
||||||
authenticator = Class.new(Auth::Authenticator) do
|
authenticator = Class.new(Auth::Authenticator) do
|
||||||
def enabled?
|
def enabled?
|
||||||
|
@ -150,7 +152,7 @@ describe Plugin::Instance do
|
||||||
end
|
end
|
||||||
end.new
|
end.new
|
||||||
plugin.auth_provider(enabled_setting: 'ubuntu_login_enabled', authenticator: authenticator)
|
plugin.auth_provider(enabled_setting: 'ubuntu_login_enabled', authenticator: authenticator)
|
||||||
plugin.notify_after_initialize
|
plugin.notify_before_auth
|
||||||
expect(authenticator.enabled?).to eq(false)
|
expect(authenticator.enabled?).to eq(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -182,11 +184,11 @@ describe Plugin::Instance do
|
||||||
plugin = Plugin::Instance.new
|
plugin = Plugin::Instance.new
|
||||||
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
|
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
|
||||||
plugin.activate!
|
plugin.activate!
|
||||||
|
expect(DiscoursePluginRegistry.auth_providers.count).to eq(0)
|
||||||
expect(plugin.auth_providers.count).to eq(1)
|
plugin.notify_before_auth
|
||||||
auth_provider = plugin.auth_providers[0]
|
|
||||||
expect(auth_provider.authenticator.name).to eq('ubuntu')
|
|
||||||
expect(DiscoursePluginRegistry.auth_providers.count).to eq(1)
|
expect(DiscoursePluginRegistry.auth_providers.count).to eq(1)
|
||||||
|
auth_provider = DiscoursePluginRegistry.auth_providers.to_a[0]
|
||||||
|
expect(auth_provider.authenticator.name).to eq('ubuntu')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "finds all the custom assets" do
|
it "finds all the custom assets" do
|
||||||
|
|
Loading…
Reference in New Issue