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

feat[Op#49396]: Flash (optional) success modal on oauth callback

Kabiru Mwenja604bdb4e35f7
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rb
index 90804313f79..abecfac759b 100644
--- a/app/controllers/oauth_clients_controller.rb
+++ b/app/controllers/oauth_clients_controller.rb
@@ -56,7 +56,7 @@ class OAuthClientsController < ApplicationController
if service_result.success?
# Redirect the user to the page that initially wanted to access the OAuth2 resource.
# "state" is a nonce that identifies a cookie which holds that page's URL.
- redirect_to @redirect_uri
+ redirect_to @redirect_uri, @redirect_uri_params
else
# We got a list of errors from ::OAuthClients::ConnectionManager
set_oauth_errors(service_result)
@@ -154,7 +154,8 @@ class OAuthClientsController < ApplicationController
.call
if service_result.success?
- @redirect_uri = service_result.result
+ @redirect_uri = service_result.result[:redirect_uri]
+ @redirect_uri_params = service_result.result[:redirect_uri_params]
else
# To protect against CSRF we cancel this request. There was either no
# state parameter given, or there was no corresponding cookie present.
diff --git a/app/services/oauth_clients/redirect_uri_from_state_service.rb b/app/services/oauth_clients/redirect_uri_from_state_service.rb
index 29803390d4f..f0d31ef1ca0 100644
--- a/app/services/oauth_clients/redirect_uri_from_state_service.rb
+++ b/app/services/oauth_clients/redirect_uri_from_state_service.rb
@@ -26,9 +26,6 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require "rack/oauth2"
-require "uri/http"
-
module OAuthClients
class RedirectUriFromStateService
def initialize(state:, cookies:)
@@ -37,10 +34,13 @@ module OAuthClients
end
def call
- redirect_uri = oauth_state_cookie
+ return ServiceResult.failure if @state.blank?
+
+ redirect_uri = oauth_state_cookie[:href]
+ redirect_uri_params = oauth_state_cookie.fetch(:href_params, {})
if redirect_uri.present? && ::API::V3::Utilities::PathHelper::ApiV3Path::same_origin?(redirect_uri)
- ServiceResult.success(result: redirect_uri)
+ ServiceResult.success(result: { redirect_uri:, redirect_uri_params: })
else
ServiceResult.failure
end
@@ -49,13 +49,13 @@ module OAuthClients
private
def oauth_state_cookie
- return nil if @state.blank?
+ @oauth_state_cookie ||= load_oauth_state_cookie
+ end
- state_cookie = @cookies["oauth_state_#{@state}"]
- return nil if state_cookie.blank?
+ def load_oauth_state_cookie
+ return if (cookie = @cookies["oauth_state_#{@state}"]).blank?
- state_value = MultiJson.load(@cookies["oauth_state_#{@state}"], symbolize_keys: true)
- state_value[:href]
+ MultiJson.load(cookie, symbolize_keys: true)
end
end
end
diff --git a/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.html.erb b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.html.erb
index fed79dfc586..e59265a76e6 100644
--- a/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.html.erb
+++ b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.html.erb
@@ -23,16 +23,18 @@
) { cancel_button_text }
)
- concat(
- render(
- Primer::Beta::Button.new(
- scheme: :primary,
- tag: :a,
- href: confirm_button_url,
- aria: { label: confirm_button_text }
- )
- ) { confirm_button_text }
- )
+ if waiting?
+ concat(
+ render(
+ Primer::Beta::Button.new(
+ scheme: :primary,
+ tag: :a,
+ href: confirm_button_url,
+ aria: { label: confirm_button_text }
+ )
+ ) { confirm_button_text }
+ )
+ end
end
end
%>
diff --git a/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb
index 451bc8e1aa6..12614630991 100644
--- a/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb
+++ b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb
@@ -30,12 +30,10 @@
#
module Storages::Admin
class OAuthAccessGrantNudgeModalComponent < ApplicationComponent # rubocop:disable OpenProject/AddPreviewForViewComponent
- options title: I18n.t('storages.oauth_grant_nudge_modal.title'),
- body_text: I18n.t('storages.oauth_grant_nudge_modal.body'),
- dialog_id: 'storages--oauth-grant-nudge-modal-component',
+ options dialog_id: 'storages--oauth-grant-nudge-modal-component',
dialog_body_id: 'storages--oauth-grant-nudge-modal-body-component',
- cancel_button_text: I18n.t('storages.oauth_grant_nudge_modal.cancel_button_label'),
- confirm_button_text: I18n.t('storages.oauth_grant_nudge_modal.confirm_button_label')
+ confirm_button_text: I18n.t('storages.oauth_grant_nudge_modal.confirm_button_label'),
+ state: :waiting
attr_reader :project_storage
@@ -46,6 +44,33 @@ module Storages::Admin
private
+ def title
+ return unless waiting?
+
+ I18n.t('storages.oauth_grant_nudge_modal.title')
+ end
+
+ def cancel_button_text
+ if waiting?
+ I18n.t('storages.oauth_grant_nudge_modal.cancel_button_label')
+ else
+ I18n.t('button_close')
+ end
+ end
+
+ def body_text
+ case options[:state].to_sym
+ when :waiting
+ I18n.t('storages.oauth_grant_nudge_modal.body')
+ when :success
+ concat(render(Storages::OpenProjectStorageModalComponent::Body.new(:success, success_subtitle_notice: :ready)))
+ end
+ end
+
+ def waiting?
+ options[:state] == :waiting
+ end
+
def confirm_button_url
url_helpers.oauth_access_grant_project_settings_project_storage_path(
project_id: project_storage.project.id,
diff --git a/modules/storages/app/controllers/storages/admin/project_storages_controller.rb b/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
index 359b66244db..9a5d19aeb77 100644
--- a/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
@@ -106,7 +106,8 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
nonce = SecureRandom.uuid
cookies["oauth_state_#{nonce}"] = {
value: { href: project_settings_project_storages_url(project_id: @project_storage.project_id),
- projectStorageId: @project_storage.id }.to_json,
+ storageId: @project_storage.storage_id,
+ href_params: oauth_access_grant_nudge_modal(:success) }.to_json,
expires: 1.hour
}
redirect_to(connection_manager.get_authorization_uri(state: nonce))
@@ -206,14 +207,21 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
def redirect_to_project_storages_path_with_nudge_modal
redirect_to(
project_settings_project_storages_path,
+ oauth_access_grant_nudge_modal(:waiting)
+ )
+ end
+
+ def oauth_access_grant_nudge_modal(state)
+ {
flash: {
modal: {
type: 'Storages::Admin::OAuthAccessGrantNudgeModalComponent',
parameters: {
- project_storage_id: Storages::ProjectStorage.last.id
+ project_storage_id: @project_storage.id,
+ state:
}
}
}
- )
+ }
end
end