diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 128fa7c71..4f0c6c4b0 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -7,7 +7,7 @@ class SessionsController < Devise::SessionsController if active_provider.providable_type == 'DatabaseProvider' super else - redirect_post "/users/auth/#{active_provider.strategy_name}", params: { authenticity_token: form_authenticity_token } + redirect_post "/users/auth/#{active_provider.strategy_name}" end end end diff --git a/app/models/auth_provider.rb b/app/models/auth_provider.rb index 40e55de23..2bcab4c13 100644 --- a/app/models/auth_provider.rb +++ b/app/models/auth_provider.rb @@ -30,6 +30,7 @@ class AuthProvider < ApplicationRecord validates_with UserUidMappedValidator, if: -> { %w[OAuth2Provider OpenIdConnectProvider].include?(providable_type) } before_create :set_initial_state + after_update :write_config def build_providable(params) raise "Unknown providable_type: #{providable_type}" unless PROVIDABLE_TYPES.include?(providable_type) @@ -114,4 +115,10 @@ class AuthProvider < ApplicationRecord # no providers in the database, he we will be 'active' (see seeds.rb) self.status = 'pending' unless AuthProvider.count.zero? end + + def write_config + return unless status == 'active' + + ProviderConfig.write_active_provider + end end diff --git a/test/helpers/auth_provider_helper.rb b/test/helpers/auth_provider_helper.rb index 7ba055a8d..07d6fb6ab 100644 --- a/test/helpers/auth_provider_helper.rb +++ b/test/helpers/auth_provider_helper.rb @@ -41,7 +41,7 @@ module AuthProviderHelper issuer: 'https://sso.sleede.dev/auth/realms/master', discovery: true, client_auth_method: 'basic', - scope: %w[openid profile email toto], + scope: %w[openid profile email], prompt: 'consent', send_scope_to_token_endpoint: true, profile_url: 'https://sso.sleede.dev/auth/realms/master/account/', diff --git a/test/integration/auth_providers_test.rb b/test/integration/auth_providers_test.rb index 9cb3e878f..f7474ab7f 100644 --- a/test/integration/auth_providers_test.rb +++ b/test/integration/auth_providers_test.rb @@ -13,6 +13,9 @@ class AuthProvidersTest < ActionDispatch::IntegrationTest end test 'create an auth external provider and activate it' do + # clean any existing auth provider config + FileUtils.rm('config/auth_provider.yml', force: true) + name = 'GitHub' post '/api/auth_providers', params: { @@ -43,6 +46,15 @@ class AuthProvidersTest < ActionDispatch::IntegrationTest User.find_each do |u| assert_not_nil u.auth_token end + + # Check the configuration file + assert File.exist?('config/auth_provider.yml') + config = ProviderConfig.new + assert_equal 'OAuth2Provider', config.providable_type + assert_equal name, config.name + + # clean test provider config + FileUtils.rm('config/auth_provider.yml', force: true) end test 'update an authentication provider' do diff --git a/test/integration/open_id_connect_test.rb b/test/integration/open_id_connect_test.rb index 1dc8ff638..5cc17d415 100644 --- a/test/integration/open_id_connect_test.rb +++ b/test/integration/open_id_connect_test.rb @@ -13,6 +13,9 @@ class OpenIdConnectTest < ActionDispatch::IntegrationTest end test 'create and activate an OIDC provider' do + # clean any existing auth provider config + FileUtils.rm('config/auth_provider.yml', force: true) + name = 'Sleede' post '/api/auth_providers', params: { @@ -42,21 +45,13 @@ class OpenIdConnectTest < ActionDispatch::IntegrationTest assert_equal 'active', db_provider&.status assert_equal AuthProvider.active.id, db_provider&.id - # TODO, login with the SSO (need debugging) - ## The following doesn't work but I can't find out why... Maybe configuring Devise like this is not the right way, - ## but when testing the process with Capybara, I always fall with the message "Not found. Authentication passthru." + # Check the configuration file + assert File.exist?('config/auth_provider.yml') + config = ProviderConfig.new + assert_equal 'OpenIdConnectProvider', config.providable_type + assert_equal name, config.name - # Simulate an application restart (reload routes and change devise setup) - # logout - # Devise.setup do |config| - # require_relative '../../lib/omni_auth/openid_connect' - # config.omniauth OmniAuth::Strategies::SsoOpenidConnectProvider.name&.to_sym, - # db_provider&.providable&.config - # end - # User.devise :omniauthable, omniauth_providers: [db_provider&.strategy_name&.to_sym] - # Rails.application.reload_routes! - # - # === OR === (need to try) - # Rails.application.reloader.reload! + # clean test provider config + FileUtils.rm('config/auth_provider.yml', force: true) end end