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
Refactor `OAuthClientController#redirect_user_or_admin` (#19887)
Yauheni Suhakoua1742b38a2df
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rbindex fd56e57e468..30590a48ff9 100644--- a/app/controllers/oauth_clients_controller.rb+++ b/app/controllers/oauth_clients_controller.rb@@ -219,9 +219,13 @@ class OAuthClientsController < ApplicationController# rubocop:enable Metrics/AbcSize+ def oauth_integration+ @oauth_client&.integration+ end+def redirect_user_or_admin(redirect_uri = nil)# This needs to be modified as soon as we support more integration types.- if User.current.admin && redirect_uri && (nextcloud? || one_drive? || share_point?)+ if User.current.admin && redirect_uri && oauth_integration.try(:supports_oauth_redirect?)yieldelsif redirect_uriflash[:error] = [t(:"oauth_client.errors.oauth_issue_contact_admin")]@@ -231,18 +235,6 @@ class OAuthClientsController < ApplicationControllerendend- def nextcloud?- @oauth_client&.integration&.provider_type == ::Storages::NextcloudStorage.name- end-- def one_drive?- @oauth_client&.integration&.provider_type == ::Storages::OneDriveStorage.name- end-- def share_point?- @oauth_client&.integration&.provider_type == ::Storages::SharePointStorage.name- end-def get_redirect_uri::OAuthClients::RedirectUriFromStateService.new(state: @oauth_state, cookies:)diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rbindex 2d07c894523..aa34bdbedc7 100644--- a/modules/storages/app/models/storages/storage.rb+++ b/modules/storages/app/models/storages/storage.rb@@ -115,6 +115,12 @@ module StoragesOAuthClientToken.exists?(user:, oauth_client:)end+ # For the time being, all Storages support OAuth redirect.+ # If a storage does not support OAuth redirect, it should override this method.+ def supports_oauth_redirect?+ true+ end+def health_notifications_should_be_sent?# it is a fallback for already created storages without health_notifications_enabled configured.(health_notifications_enabled.nil? && automatic_management_enabled?) || health_notifications_enabled?diff --git a/modules/storages/spec/models/storages/storage_spec.rb b/modules/storages/spec/models/storages/storage_spec.rbindex f746fb18fe2..f8f5e690cfb 100644--- a/modules/storages/spec/models/storages/storage_spec.rb+++ b/modules/storages/spec/models/storages/storage_spec.rb@@ -70,6 +70,14 @@ RSpec.describe Storages::Storage doendend+ describe "#supports_oauth_redirect?" do+ let(:storage) { described_class.new }++ it "returns true by default (to be overridden by subclasses)" do+ expect(storage).to be_supports_oauth_redirect+ end+ end+describe "#health_notifications_should_be_sent?" dolet(:storage) { build(:storage, provider_fields: {}) }diff --git a/spec/requests/oauth_clients/callback_flow_spec.rb b/spec/requests/oauth_clients/callback_flow_spec.rbindex 8a22f004bd7..8968d4b18a2 100644--- a/spec/requests/oauth_clients/callback_flow_spec.rb+++ b/spec/requests/oauth_clients/callback_flow_spec.rb@@ -49,9 +49,10 @@ RSpec.describe "OAuthClient callback endpoint" dolet(:oauth_client) docreate(:oauth_client,client_id: "kETWr2XsjPxhVbN7Q5jmPq83xribuUTRzgfXthpYT0vSqyJWm4dOnivKzHiZasf0",- client_secret: "J1sg4L5PYbM2RZL3pUyxTnamvfpcP5eUcCPmeCQHJO60Gy6CJIdDaF4yXOeC8BPS")+ client_secret: "J1sg4L5PYbM2RZL3pUyxTnamvfpcP5eUcCPmeCQHJO60Gy6CJIdDaF4yXOeC8BPS",+ integration: integration)end- let(:storage) { oauth_client.integration }+ let(:integration) { create(:nextcloud_storage) }let(:rack_oauth2_client) { instance_double(Rack::OAuth2::Client) }let(:connection_manager) { instance_double(OAuthClients::ConnectionManager) }let(:uri) { URI(File.join("oauth_clients", oauth_client.client_id, "callback")) }@@ -106,7 +107,7 @@ RSpec.describe "OAuthClient callback endpoint" docontext "without errors" doit "redirects to the URL that was referenced by the state param and held by a cookie", :storage_server_helpers,:webmock do- stub_nextcloud_user_query(storage.host)+ stub_nextcloud_user_query(integration.host)uri.query = URI.encode_www_form([["code", code], ["state", state]])get uri.to_s@@ -156,6 +157,23 @@ RSpec.describe "OAuthClient callback endpoint" doit_behaves_like "with errors and state param with cookie, not being admin"endend++ context "with integration which does not support OAuth redirect" do+ let(:current_user) { create(:admin) }+ let(:integration) { create(:scim_client) }++ before do+ uri.query = URI.encode_www_form([["state", state]])+ get uri.to_s++ subject+ end++ it "redirects to the redirect uri" do+ expect(response).to have_http_status :found+ expect(response.location).to eq redirect_uri+ end+ endendcontext "without code param, but with state param," do