Skip to content

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

Refactor `OAuthClientController#redirect_user_or_admin` (#19887)

Yauheni Suhakoua1742b38a2df
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rb
index 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?)
yield
elsif redirect_uri
flash[:error] = [t(:"oauth_client.errors.oauth_issue_contact_admin")]
@@ -231,18 +235,6 @@ class OAuthClientsController < ApplicationController
end
end
- 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.rb
index 2d07c894523..aa34bdbedc7 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -115,6 +115,12 @@ module Storages
OAuthClientToken.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.rb
index 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 do
end
end
+ 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?" do
let(:storage) { build(:storage, provider_fields: {}) }
diff --git a/spec/requests/oauth_clients/callback_flow_spec.rb b/spec/requests/oauth_clients/callback_flow_spec.rb
index 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" do
let(:oauth_client) do
create(: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" do
context "without errors" do
it "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" do
it_behaves_like "with errors and state param with cookie, not being admin"
end
end
+
+ 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
+ end
end
context "without code param, but with state param," do