OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow to configure SSO with fallback to two-way OAuth 2.0
Jan Sandbrinkb58722c29478
diff --git a/modules/storages/app/common/storages/peripherals/nextcloud_storage_wizard.rb b/modules/storages/app/common/storages/peripherals/nextcloud_storage_wizard.rbindex 2b00127002f..65d2aa4f021 100644--- a/modules/storages/app/common/storages/peripherals/nextcloud_storage_wizard.rb+++ b/modules/storages/app/common/storages/peripherals/nextcloud_storage_wizard.rb@@ -44,13 +44,13 @@ module Storagesstep :oauth_application,section: :oauth_configuration,- if: ->(storage) { !storage.authenticate_via_idp? },+ if: ->(storage) { storage.authenticate_via_storage? },completed_if: ->(storage) { storage.oauth_application.present? },preparation: :prepare_oauth_applicationstep :oauth_client,section: :oauth_configuration,- if: ->(storage) { !storage.authenticate_via_idp? },+ if: ->(storage) { storage.authenticate_via_storage? },completed_if: ->(storage) { storage.oauth_client.present? },preparation: ->(storage) { storage.build_oauth_client }diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rbindex 4e7e3599db5..ee354bdd5ab 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb@@ -43,13 +43,13 @@ module Storagesdef call(user:, storage:)with_tagged_logger do- sso_preferred = storage.audience.present? && oidc_provider_for(user)+ sso_preferred = storage.authenticate_via_idp? && oidc_provider_for(user)if sso_preferred::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::SsoUserToken.strategy.with_user(user)- elsif storage.oauth_client.present?+ elsif storage.authenticate_via_storage?::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rbindex 67012bb3233..2df3d807c74 100644--- a/modules/storages/app/models/storages/nextcloud_storage.rb+++ b/modules/storages/app/models/storages/nextcloud_storage.rb@@ -35,7 +35,7 @@ module Storagesusername: "OpenProject"}.freeze- AUTHENTICATION_METHODS = %w[two_way_oauth2 oauth2_sso].freeze+ AUTHENTICATION_METHODS = %w[two_way_oauth2 oauth2_sso oauth2_sso_with_two_way_oauth2_fallback].freezestore_attribute :provider_fields, :automatically_managed, :booleanstore_attribute :provider_fields, :username, :string@@ -71,7 +71,11 @@ module Storagesenddef authenticate_via_idp?- authentication_method == "oauth2_sso"+ %w[oauth2_sso oauth2_sso_with_two_way_oauth2_fallback].include?(authentication_method)+ end++ def authenticate_via_storage?+ %w[two_way_oauth2 oauth2_sso_with_two_way_oauth2_fallback].include?(authentication_method)enddef configuration_checksdiff --git a/modules/storages/app/services/storages/storages/create_service.rb b/modules/storages/app/services/storages/storages/create_service.rbindex fcaef35b5d6..f26041f03fc 100644--- a/modules/storages/app/services/storages/storages/create_service.rb+++ b/modules/storages/app/services/storages/storages/create_service.rb@@ -48,8 +48,6 @@ module Storages::Storagesreturn service_call unless create_oauth_app?storage = service_call.result- # Automatically create an OAuthApplication object for the Nextcloud storage- # using values from storage (particularly :host) as defaultsif storage.provider_type_nextcloud? && !storage.authenticate_via_idp?persist_service_result = ::Storages::OAuthApplications::CreateService.new(storage:, user:).callstorage.oauth_application = persist_service_result.result if persist_service_result.success?diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.ymlindex c883aa19a46..a1da54a66cc 100644--- a/modules/storages/config/locales/en.yml+++ b/modules/storages/config/locales/en.yml@@ -7,6 +7,7 @@ en:storages/nextcloud_storage:authentication_methods:oauth2_sso: Single-Sign-On through OpenID Connect Identity Provider+ oauth2_sso_with_two_way_oauth2_fallback: Single-Sign-On with Two-Way OAuth 2.0 as fallbacktwo_way_oauth2: Two-way OAuth 2.0 authorization code flownextcloud_audience: Nextcloud Audiencestorages/project_storage:diff --git a/modules/storages/spec/common/storages/peripherals/nextcloud_storage_wizard_spec.rb b/modules/storages/spec/common/storages/peripherals/nextcloud_storage_wizard_spec.rbindex 6af89ee88fe..2232ca82d33 100644--- a/modules/storages/spec/common/storages/peripherals/nextcloud_storage_wizard_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/nextcloud_storage_wizard_spec.rb@@ -152,6 +152,7 @@ RSpec.describe Storages::Peripherals::NextcloudStorageWizard docontext "and the nextcloud audience was set" dobefore do+ wizard.prepare_next_stepmodel.nextcloud_audience = "nextcloud"end@@ -189,4 +190,107 @@ RSpec.describe Storages::Peripherals::NextcloudStorageWizard doendendend++ context "when name and host were set and authentication method is SSO with fallback to Two-Way OAuth 2.0" do+ before do+ model.name = "Karl"+ model.host = "https://nextcloud.local/"+ model.authentication_method = "oauth2_sso_with_two_way_oauth2_fallback"+ model.save!+ end++ it "has general_information step completed" do+ expect(wizard.completed_steps).to eq(%i[general_information])+ end++ it "has new steps pending in correct order" do+ expect(wizard.pending_steps).to eq(%i[nextcloud_audience+ oauth_application+ oauth_client+ automatically_managed_folders])+ end++ context "and the nextcloud audience was set" do+ before do+ wizard.prepare_next_step+ model.nextcloud_audience = "nextcloud"+ end++ it "has nextcloud_audience step completed" do+ expect(wizard.completed_steps).to eq(%i[general_information nextcloud_audience])+ end++ it "has no oauth_application yet" do+ expect(model.oauth_application).to be_nil+ end++ context "and the next step was prepared" do+ before do+ wizard.prepare_next_step+ end++ it "automatically finished the oauth_application step" do+ expect(wizard.completed_steps).to eq(%i[general_information+ nextcloud_audience+ oauth_application])+ end++ it "now has an oauth_application" do+ expect(model.oauth_application).to be_present+ end++ it "has no oauth_client yet" do+ expect(model.oauth_client).to be_nil+ end++ context "and the next step was prepared" do+ before do+ wizard.prepare_next_step+ end++ it "finishes the oauth_client step" do+ expect(wizard.completed_steps).to eq(%i[general_information+ nextcloud_audience+ oauth_application+ oauth_client])+ end++ it "now has an unsaved oauth_client" do+ expect(model.oauth_client).to be_present+ expect(model.oauth_client).not_to be_persisted+ end++ it "still didn't specify how to manage folders" do+ expect(model).to be_automatic_management_unspecified+ end++ context "and after preparing the next step" do+ before do+ wizard.prepare_next_step+ end++ it "enabled automatic storage management, but didn't persist it" do+ expect(model).to be_automatic_management_enabled++ before, after = model.changes["provider_fields"]+ expect(before.keys).not_to include("automatically_managed")+ expect(after.keys).to include("automatically_managed")+ end++ it "has no pending steps" do+ expect(wizard.pending_steps).to be_empty+ end++ it "has all steps completed" do+ expect(wizard.completed_steps).to eq(%i[general_information+ nextcloud_audience+ oauth_application+ oauth_client+ automatically_managed_folders])+ end+ end+ end+ end+ end+ endenddiff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies/user_bound_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies/user_bound_spec.rbindex 9b58d0f9a8b..831bb63a303 100644--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies/user_bound_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies/user_bound_spec.rb@@ -46,7 +46,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategiendcontext "if file storage is configured for sso and oauth" do- let(:storage) { create(:nextcloud_storage_configured, :oidc_sso_enabled) }+ let(:storage) { create(:nextcloud_storage_configured, :oidc_sso_with_fallback) }it "must use an SsoUserToken strategy" dostrategy = described_class.call(user:, storage:)@@ -77,7 +77,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategiendcontext "if file storage is configured for sso and oauth" do- let(:storage) { create(:nextcloud_storage_configured, :oidc_sso_enabled) }+ let(:storage) { create(:nextcloud_storage_configured, :oidc_sso_with_fallback) }it "must use an OAuthUserToken strategy" dostrategy = described_class.call(user:, storage:)@@ -93,15 +93,14 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategiexpect(strategy.key).to eq(:oauth_user_token)endend- end- context "if file storage is not fully configured" do- let(:user) { create(:user) }- let(:storage) { create(:nextcloud_storage) }+ context "if file storage is configured for oauth only, but client and app not fully configured" do+ let(:storage) { create(:nextcloud_storage) }- it "must return the failure strategy" do- strategy = described_class.call(user:, storage:)- expect(strategy.key).to eq(:failure)+ it "returns the configured strategy" do+ strategy = described_class.call(user:, storage:)+ expect(strategy.key).to eq(:oauth_user_token)+ endendendenddiff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rbindex 5ee3cd603a6..7ee7564ce49 100644--- a/modules/storages/spec/factories/storage_factory.rb+++ b/modules/storages/spec/factories/storage_factory.rb@@ -99,6 +99,11 @@ FactoryBot.define doauthentication_method { "oauth2_sso" }end+ trait :oidc_sso_with_fallback do+ nextcloud_audience { "nextcloud" }+ authentication_method { "oauth2_sso_with_two_way_oauth2_fallback" }+ end+trait :as_automatically_managed doautomatic_management_enabled { true }username { "OpenProject" }