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

[#50907] Implement /oauth_clients/:oauth_client_id/ensure_access

Pavel Balashoudcded13d7809
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rb
index f678ed817c8..cf0a9b3a98b 100644
--- a/app/controllers/oauth_clients_controller.rb
+++ b/app/controllers/oauth_clients_controller.rb
@@ -31,13 +31,14 @@
# This controller handles OAuth2 Authorization Code Grant redirects from a Authorization Server to
# "callback" endpoint.
class OAuthClientsController < ApplicationController
- before_action :set_oauth_state
- before_action :find_oauth_client
- before_action :set_redirect_uri
- before_action :set_code
- before_action :set_connection_manager
+ before_action :require_login
+ before_action :set_oauth_state, only: [:callback]
+ before_action :find_oauth_client, only: [:callback]
+ before_action :set_redirect_uri, only: [:callback]
+ before_action :set_code, only: [:callback]
+ before_action :set_connection_manager, only: [:callback]
- after_action :clear_oauth_state_cookie
+ after_action :clear_oauth_state_cookie, only: [:callback]
# Provide the OAuth2 "callback" endpoint.
# The Authorization Server redirects
@@ -68,6 +69,41 @@ class OAuthClientsController < ApplicationController
end
end
+ def ensure_access
+ client_id = params.fetch(:oauth_client_id)
+ storage_id = params.fetch(:storage_id)
+ oauth_client = OAuthClient.find_by(client_id:, integration_id: storage_id)
+ if oauth_client.nil?
+ flash[:error] = [I18n.t('oauth_client.errors.oauth_client_not_found'),
+ I18n.t('oauth_client.errors.oauth_client_not_found_explanation')]
+
+ if User.current.admin?
+ redirect_to admin_settings_storages_path
+ else
+ redirect_to root_url
+ end
+ else
+ connection_manager = OAuthClients::ConnectionManager.new(
+ user: User.current,
+ configuration: oauth_client.integration.oauth_configuration
+ )
+
+ # check if the origin is the same
+ destination_url = if params[:destination_url].present? && params[:destination_url].start_with?(root_url)
+ params[:destination_url]
+ else
+ root_url
+ end
+ if connection_manager.authorization_state == :connected
+ redirect_to(destination_url)
+ else
+ nonce = SecureRandom.uuid
+ cookies["oauth_state_#{nonce}"] = { value: { href: destination_url, storageId: storage_id }.to_json, expires: 1.hour }
+ redirect_to(connection_manager.get_authorization_uri(state: nonce))
+ end
+ end
+ end
+
private
def set_oauth_state
@@ -171,7 +207,7 @@ class OAuthClientsController < ApplicationController
flash[:error] = [t(:'oauth_client.errors.oauth_issue_contact_admin')]
redirect_to redirect_uri
else
- redirect_to ::API::V3::Utilities::PathHelper::ApiV3Path::root_url
+ redirect_to root_url
end
end
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index 45c05a1dd1f..be8f33bb1ad 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -197,7 +197,6 @@ module OAuthClients
OAuthClientToken.find_by(user_id: @user, oauth_client_id: @oauth_client.id)
end
- # rubocop:disable Metrics/AbcSize
def request_new_token(options = {})
rack_access_token = rack_oauth_client(options).access_token!(:body)
@@ -218,8 +217,6 @@ module OAuthClients
)
end
- # rubocop:enable Metrics/AbcSize
-
def i18n_rack_oauth2_error_message(rack_oauth2_client_exception)
i18n_key = "oauth_client.errors.rack_oauth2.#{rack_oauth2_client_exception.message}"
if I18n.exists? i18n_key
diff --git a/config/routes.rb b/config/routes.rb
index fb2aba23b3a..4629452dc87 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -83,7 +83,7 @@ OpenProject::Application.routes.draw do
match '/account/register', action: 'register', via: %i[get post patch]
get '/account/activate', action: 'activate'
- match '/login', action: 'login', as: 'signin', via: %i[get post]
+ match '/login', action: 'login', as: 'signin', via: %i[get post]
get '/login/internal', action: 'internal_login', as: 'internal_signin'
get '/logout', action: 'logout', as: 'signout'
@@ -617,6 +617,7 @@ OpenProject::Application.routes.draw do
# OAuthClient needs a "callback" URL that Nextcloud calls with a "code" (see OAuth2 RFC)
scope 'oauth_clients/:oauth_client_id' do
get 'callback', controller: 'oauth_clients', action: :callback
+ get 'ensure_access', controller: 'oauth_clients', action: :ensure_access, as: 'oauth_clients_ensure_access'
end
if OpenProject::Configuration.lookbook_enabled?
diff --git a/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.ts b/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.ts
index 3eea6cf7b41..fd116f10f9a 100644
--- a/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.ts
+++ b/frontend/src/app/shared/components/storages/storage-login-button/storage-login-button.component.ts
@@ -76,7 +76,10 @@ export class StorageLoginButtonComponent implements OnInit {
this.cookieService.set(
`oauth_state_${nonce}`,
JSON.stringify({ href: window.location.href, storageId: this.input.storageId }),
- { path: '/' },
+ {
+ path: '/',
+ expires: 1/24, // roughly 1 hour
+ },
);
}
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
index df301b57206..979005c1b0d 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
@@ -41,7 +41,7 @@ module Storages
end
def authorization_state_check(token)
- util = StorageInteraction::Nextcloud::Util
+ util = ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
authorization_check_wrapper do
Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
diff --git a/modules/storages/app/components/storages/project_storages/members/row_component.rb b/modules/storages/app/components/storages/project_storages/members/row_component.rb
index 465a6ad9791..d69ab910c74 100644
--- a/modules/storages/app/components/storages/project_storages/members/row_component.rb
+++ b/modules/storages/app/components/storages/project_storages/members/row_component.rb
@@ -53,13 +53,17 @@ module Storages::ProjectStorages::Members
connection_result = storage_connection_status
if connection_result == :not_connected
+ ensure_access_url = oauth_clients_ensure_access_url(
+ oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id
+ )
helpers.op_icon('icon-warning -warning') +
- content_tag(:span,
- I18n.t("storages.member_connection_status.not_connected",
- files_label: content_tag(:span,
- I18n.t('storages.label_files'),
- class: 'text-bold')).html_safe,
- class: 'pl-2')
+ content_tag(
+ :span,
+ I18n.t("storages.member_connection_status.not_connected",
+ link: link_to(I18n.t("storages.member_connection_status.link"), ensure_access_url),
+ class: 'pl-2').html_safe
+ )
else
I18n.t("storages.member_connection_status.#{connection_result}")
end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 6f335165cbf..561aa982fb0 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -213,7 +213,8 @@ en:
member_connection_status:
connected: "Connected"
connected_no_permissions: "User role has no storages permissions"
- not_connected: "Not connected. The user should login to the storage via the %{files_label} tab of a work package to connect."
+ not_connected: "Not connected. The user should login to the storage via the following %{link}."
+ link: "link"
no_results: "No storages set up yet."
members_no_results: "No members to display."
provider_types:
diff --git a/modules/storages/spec/controller/storages_controller_spec.rb b/modules/storages/spec/controllers/storages/admin/storages_controller_spec.rb
similarity index 96%
rename from modules/storages/spec/controller/storages_controller_spec.rb
rename to modules/storages/spec/controllers/storages/admin/storages_controller_spec.rb
index a94e300a51e..50d13fc1efe 100644
--- a/modules/storages/spec/controller/storages_controller_spec.rb
+++ b/modules/storages/spec/controllers/storages/admin/storages_controller_spec.rb
@@ -33,7 +33,7 @@ require_module_spec_helper
# These specs mainly check that error messages from a sub-service
# (about unsafe hosts with HTTP protocol) are passed to the main form.
-RSpec.describe Storages::Admin::StoragesController, type: :controller, webmock: true do
+RSpec.describe Storages::Admin::StoragesController, :webmock do
render_views # rendering views is stubbed by default in controller specs
include StorageServerHelpers
diff --git a/modules/storages/spec/features/view_project_storage_members_spec.rb b/modules/storages/spec/features/view_project_storage_members_spec.rb
index 8c63d60df1c..2414d862761 100644
--- a/modules/storages/spec/features/view_project_storage_members_spec.rb
+++ b/modules/storages/spec/features/view_project_storage_members_spec.rb
@@ -72,11 +72,11 @@ RSpec.describe 'Project storage members connection status view' do
aggregate_failures 'Verifying Connection Statuses' do
[
- [user, 'Not connected. The user should login to the storage via the Files tab of a work package to connect.'],
+ [user, 'Not connected. The user should login to the storage via the following link.'],
[admin_user, 'Connected'],
[connected_user, 'Connected'],
[connected_no_permissions_user, 'User role has no storages permissions'],
- [disconnected_user, 'Not connected. The user should login to the storage via the Files tab of a work package to connect.']
+ [disconnected_user, 'Not connected. The user should login to the storage via the following link.']
].each do |(principal, status)|
expect(page).to have_selector("#member-#{principal.id} .name", text: principal.name)
expect(page).to have_selector("#member-#{principal.id} .status", text: status)
diff --git a/spec/requests/oauth_clients/callback_flow_spec.rb b/spec/requests/oauth_clients/callback_flow_spec.rb
index 223266b2fdb..ac320447bc1 100644
--- a/spec/requests/oauth_clients/callback_flow_spec.rb
+++ b/spec/requests/oauth_clients/callback_flow_spec.rb
@@ -57,125 +57,134 @@ RSpec.describe 'OAuthClient callback endpoint' do
subject(:response) { last_response }
- before do
- login_as current_user
-
- allow(Rack::OAuth2::Client).to receive(:new).and_return(rack_oauth2_client)
- allow(rack_oauth2_client)
- .to receive(:access_token!)
- .with(:body)
- .and_return(Rack::OAuth2::AccessToken::Bearer.new(access_token: 'xyzaccesstoken',
- refresh_token: 'xyzrefreshtoken'))
- allow(rack_oauth2_client).to receive(:authorization_code=)
- state_cookie = CGI.escape({ href: redirect_uri, storageId: oauth_client.integration_id }.to_json)
- set_cookie "oauth_state_asdf1234=#{state_cookie}"
- end
-
- # rubocop:disable RSpec/Rails/HaveHttpStatus
- shared_examples 'with errors and state param with cookie, not being admin' do
- it 'redirects to URI referenced in the state param and held in a cookie' do
- expect(response.status).to eq(302)
- expect(response.location).to eq redirect_uri
+ context 'when user is not logged in' do
+ it 'requires login' do
+ get uri.to_s
+ expect(last_response.status).to eq(401)
end
end
- shared_examples 'with errors, being an admin' do
- it 'redirects to admin settings for the storage' do
- expect(response.status).to eq(302)
- expect(URI(response.location).path).to eq edit_admin_settings_storage_path(oauth_client.integration)
+ context 'when user is logged in' do
+ before do
+ login_as current_user
+
+ allow(Rack::OAuth2::Client).to receive(:new).and_return(rack_oauth2_client)
+ allow(rack_oauth2_client)
+ .to receive(:access_token!)
+ .with(:body)
+ .and_return(Rack::OAuth2::AccessToken::Bearer.new(access_token: 'xyzaccesstoken',
+ refresh_token: 'xyzrefreshtoken'))
+ allow(rack_oauth2_client).to receive(:authorization_code=)
+ state_cookie = CGI.escape({ href: redirect_uri, storageId: oauth_client.integration_id }.to_json)
+ set_cookie "oauth_state_asdf1234=#{state_cookie}"
end
- end
- shared_examples 'fallback redirect' do
- it 'redirects to home' do
- expect(response.status).to eq(302)
- expect(URI(response.location).path).to eq API::V3::Utilities::PathHelper::ApiV3Path::root_path
+ # rubocop:disable RSpec/Rails/HaveHttpStatus
+ shared_examples 'with errors and state param with cookie, not being admin' do
+ it 'redirects to URI referenced in the state param and held in a cookie' do
+ expect(response.status).to eq(302)
+ expect(response.location).to eq redirect_uri
+ end
end
- end
- context 'with valid params' do
- context 'without errors' do
- before do
- uri.query = URI.encode_www_form([['code', code], ['state', state]])
- get uri.to_s
+ shared_examples 'with errors, being an admin' do
+ it 'redirects to admin settings for the storage' do
+ expect(response.status).to eq(302)
+ expect(URI(response.location).path).to eq edit_admin_settings_storage_path(oauth_client.integration)
end
+ end
- it 'redirects to the URL that was referenced by the state param and held by a cookie' do
- expect(rack_oauth2_client).to have_received(:authorization_code=).with(code)
- expect(response.status).to eq 302
- expect(response.location).to eq redirect_uri
- expect(OAuthClientToken.count).to eq 1
- expect(OAuthClientToken.last.access_token).to eq 'xyzaccesstoken'
- expect(OAuthClientToken.last.refresh_token).to eq 'xyzrefreshtoken'
+ shared_examples 'fallback redirect' do
+ it 'redirects to home' do
+ expect(response.status).to eq(302)
+ expect(URI(response.location).path).to eq API::V3::Utilities::PathHelper::ApiV3Path::root_path
end
end
- context 'with a OAuth state cookie containing a URL pointing to a different host' do
- let(:redirect_uri) { "https://some-other-domain.com/foo/bar" }
+ context 'with valid params' do
+ context 'without errors' do
+ before do
+ uri.query = URI.encode_www_form([['code', code], ['state', state]])
+ get uri.to_s
+ end
+
+ it 'redirects to the URL that was referenced by the state param and held by a cookie' do
+ expect(rack_oauth2_client).to have_received(:authorization_code=).with(code)
+ expect(response.status).to eq 302
+ expect(response.location).to eq redirect_uri
+ expect(OAuthClientToken.count).to eq 1
+ expect(OAuthClientToken.last.access_token).to eq 'xyzaccesstoken'
+ expect(OAuthClientToken.last.refresh_token).to eq 'xyzrefreshtoken'
+ end
+ end
- before do
- uri.query = URI.encode_www_form([['code', code], ['state', state]])
- get uri.to_s
+ context 'with a OAuth state cookie containing a URL pointing to a different host' do
+ let(:redirect_uri) { "https://some-other-domain.com/foo/bar" }
- subject
+ before do
+ uri.query = URI.encode_www_form([['code', code], ['state', state]])
+ get uri.to_s
+
+ subject
+ end
+
+ it_behaves_like 'fallback redirect'
end
- it_behaves_like 'fallback redirect'
- end
+ context 'with some other error, having a state param' do
+ before do
+ allow(OAuthClients::ConnectionManager)
+ .to receive(:new).and_return(connection_manager)
+ allow(connection_manager)
+ .to receive(:code_to_token).with(code).and_return(ServiceResult.failure)
- context 'with some other error, having a state param' do
- before do
- allow(OAuthClients::ConnectionManager)
- .to receive(:new).and_return(connection_manager)
- allow(connection_manager)
- .to receive(:code_to_token).with(code).and_return(ServiceResult.failure)
+ uri.query = URI.encode_www_form([['code', code], ['state', state]])
+ get uri.to_s
- uri.query = URI.encode_www_form([['code', code], ['state', state]])
- get uri.to_s
+ subject
+ end
- subject
+ context 'with current_user being an admin' do
+ let(:current_user) { create(:admin) }
+
+ it_behaves_like 'with errors, being an admin'
+ end
+
+ context 'with current_user not being an admin' do
+ it_behaves_like 'with errors and state param with cookie, not being admin'
+ end
end
+ end
- context 'with current_user being an admin' do
- let(:current_user) { create(:admin) }
+ context 'without code param, but with state param,' do
+ before do
+ uri.query = URI.encode_www_form([['state', state]])
+ get uri.to_s
- it_behaves_like 'with errors, being an admin'
+ subject
end
context 'with current_user not being an admin' do
it_behaves_like 'with errors and state param with cookie, not being admin'
end
- end
- end
- context 'without code param, but with state param,' do
- before do
- uri.query = URI.encode_www_form([['state', state]])
- get uri.to_s
-
- subject
- end
+ context 'with current_user being an admin' do
+ let(:current_user) { create(:admin) }
- context 'with current_user not being an admin' do
- it_behaves_like 'with errors and state param with cookie, not being admin'
+ it_behaves_like 'with errors, being an admin'
+ end
end
- context 'with current_user being an admin' do
- let(:current_user) { create(:admin) }
-
- it_behaves_like 'with errors, being an admin'
- end
- end
+ context 'without state param' do
+ before do
+ uri.query = URI.encode_www_form([['code', code]])
+ get uri.to_s
- context 'without state param' do
- before do
- uri.query = URI.encode_www_form([['code', code]])
- get uri.to_s
+ subject
+ end
- subject
+ it_behaves_like 'fallback redirect'
end
-
- it_behaves_like 'fallback redirect'
+ # rubocop:enable RSpec/Rails/HaveHttpStatus
end
- # rubocop:enable RSpec/Rails/HaveHttpStatus
end
diff --git a/spec/requests/oauth_clients/ensure_access_flow_spec.rb b/spec/requests/oauth_clients/ensure_access_flow_spec.rb
new file mode 100644
index 00000000000..b434c59874f
--- /dev/null
+++ b/spec/requests/oauth_clients/ensure_access_flow_spec.rb
@@ -0,0 +1,183 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+require_module_spec_helper
+
+RSpec.describe "/oauth_clients/:oauth_client_id/ensure_access endpoint", :webmock do
+ shared_let(:user) { create(:user) }
+ shared_let(:storage) { create(:nextcloud_storage, :with_oauth_client) }
+ shared_let(:oauth_client) { storage.oauth_client }
+
+ describe '#ensure_access' do
+ context 'when user is not logged in' do
+ it 'requires login' do
+ get oauth_clients_ensure_access_url(oauth_client_id: oauth_client.client_id)
+ expect(last_response.status).to eq(401)
+ end
+ end
+
+ context 'when user is logged in' do
+ before { login_as(user) }
+
+ it 'responds with 400 when storage_id parameter is absent' do
+ get oauth_clients_ensure_access_url(oauth_client_id: oauth_client.client_id)
+ expect(last_response.status).to eq(400)
+ expect(last_response.body).to eq("Required parameter missing: storage_id")
+ end
+
+ context 'when storage_id parameter is present' do
+ context 'when user is not "connected"' do
+ let(:nonce) { '57a17c3f-b2ed-446e-9dd8-651ba3aec37d' }
+
+ before do
+ allow(SecureRandom).to receive(:uuid).and_call_original.ordered
+ allow(SecureRandom).to receive(:uuid).and_return(nonce).ordered
+ end
+
+ context 'when destination_url parameter is absent' do
+ it 'redirects to storage authorization_uri with oauth_state_* cookie set' do
+ get oauth_clients_ensure_access_url(oauth_client_id: oauth_client.client_id, storage_id: storage.id)
+
+ oauth_client = storage.oauth_client
+ expect(last_response.status).to eq(302)
+ expect(last_response.body).to eq(
+ "<html><body>You are being <a href=\"#{storage.host}/index.php/apps/oauth2/authorize?client_id=" \
+ "#{oauth_client.client_id}&amp;redirect_uri=#{CGI.escape(OpenProject::Application.root_url)}%2Foauth_clients%2F#{oauth_client.client_id}%2F" \
+ "callback&amp;response_type=code&amp;state=#{nonce}\">redirected</a>." \
+ "</body></html>"
+ )
+ expect(last_response.cookies["oauth_state_#{nonce}"]).to eq(["%7B%22href%22%3A%22http%3A%2F%2Fwww.example.com%2F%22%2C%22storageId%22%3A%22#{storage.id}%22%7D"])
+ end
+ end
+
+ context 'when destination_url parameter is present' do
+ context 'when destination_url is the same origin as OP' do
+ it 'redirects to storage authorization_uri with oauth_state_* cookie set' do
+ get oauth_clients_ensure_access_url(oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id,
+ destination_url: "#{root_url}123")
+
+ oauth_client = storage.oauth_client
+ expect(last_response.status).to eq(302)
+ expect(last_response.body).to eq(
+ "<html><body>You are being <a href=\"#{storage.host}/index.php/apps/oauth2/authorize?client_id=" \
+ "#{oauth_client.client_id}&amp;redirect_uri=#{CGI.escape(OpenProject::Application.root_url)}%2Foauth_clients%2F#{oauth_client.client_id}%2F" \
+ "callback&amp;response_type=code&amp;state=#{nonce}\">redirected</a>." \
+ "</body></html>"
+ )
+ expect(last_response.cookies["oauth_state_#{nonce}"]).to eq(["%7B%22href%22%3A%22http%3A%2F%2Fwww.example.com%2F123%22%2C%22storageId%22%3A%22#{storage.id}%22%7D"])
+ end
+ end
+
+ context 'when destination_url is not the same origin as OP' do
+ it 'redirects to storage authorization_uri with oauth_state_* cookie set' do
+ get oauth_clients_ensure_access_url(oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id,
+ destination_url: "#{storage.host}/index.php")
+
+ oauth_client = storage.oauth_client
+ expect(last_response.status).to eq(302)
+ expect(last_response.body).to eq(
+ "<html><body>You are being <a href=\"#{storage.host}/index.php/apps/oauth2/authorize?client_id=" \
+ "#{oauth_client.client_id}&amp;redirect_uri=#{CGI.escape(OpenProject::Application.root_url)}%2Foauth_clients%2F#{oauth_client.client_id}%2F" \
+ "callback&amp;response_type=code&amp;state=#{nonce}\">redirected</a>." \
+ "</body></html>"
+ )
+ expect(last_response.cookies["oauth_state_#{nonce}"]).to eq(["%7B%22href%22%3A%22http%3A%2F%2Fwww.example.com%2F%22%2C%22storageId%22%3A%22#{storage.id}%22%7D"])
+ end
+ end
+ end
+ end
+
+ context 'when user is "connected"' do
+ let!(:oauth_client_token) do
+ create(:oauth_client_token, oauth_client:, user:)
+ end
+
+ before do
+ stub_request(:get, "#{storage.host}/ocs/v1.php/cloud/user")
+ .with(
+ headers: {
+ 'Accept' => 'application/json',
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
+ 'Authorization' => "Bearer #{oauth_client_token.access_token}",
+ 'Ocs-Apirequest' => 'true',
+ 'User-Agent' => 'Ruby'
+ }
+ ).to_return(status: 200, body: "", headers: {})
+ end
+
+ context 'when destination_url parameter is absent' do
+ it 'redirects to root_url' do
+ get oauth_clients_ensure_access_url(oauth_client_id: oauth_client.client_id, storage_id: storage.id)
+
+ expect(last_response.status).to eq(302)
+ expect(last_response.body).to eq(
+ "<html><body>You are being <a href=\"http://www.example.com/\">redirected</a>.</body></html>"
+ )
+ expect(last_response.cookies.keys).to eq(["_open_project_session"])
+ end
+ end
+
+ context 'when destination_url parameter is present' do
+ context 'when destination_url is the same origin as OP' do
+ it 'redirects to destination_url' do
+ get oauth_clients_ensure_access_url(oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id,
+ destination_url: "#{root_url}123")
+
+ storage.oauth_client
+ expect(last_response.status).to eq(302)
+ expect(last_response.body).to eq(
+ "<html><body>You are being <a href=\"http://www.example.com/123\">redirected</a>.</body></html>"
+ )
+ expect(last_response.cookies.keys).to eq(["_open_project_session"])
+ end
+ end
+
+ context 'when destination_url is not the same origin as OP' do
+ it 'redirects to root_url' do
+ get oauth_clients_ensure_access_url(oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id,
+ destination_url: "#{storage.host}/index.php")
+
+ storage.oauth_client
+ expect(last_response.status).to eq(302)
+ expect(last_response.body).to eq(
+ "<html><body>You are being <a href=\"http://www.example.com/\">redirected</a>.</body></html>"
+ )
+ expect(last_response.cookies.keys).to eq(["_open_project_session"])
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end