From 6f5135a6b2a5433cb6df442fc368743bc1df19e2 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 16 Sep 2024 12:10:55 +0200 Subject: [PATCH] refactor: extract agent_connect logout_url to a agent_connect_service --- app/controllers/users/sessions_controller.rb | 10 ++-------- app/services/agent_connect_service.rb | 6 ++++++ spec/services/agent_connect_service_spec.rb | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 spec/services/agent_connect_service_spec.rb diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 1d5e49100..601f533dc 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -59,7 +59,8 @@ class Users::SessionsController < Devise::SessionsController end if agent_connect_id_token.present? - return redirect_to build_agent_connect_logout_url(agent_connect_id_token), allow_other_host: true + return redirect_to AgentConnectService.logout_url(agent_connect_id_token, host_with_port: request.host_with_port), + allow_other_host: true end end @@ -110,11 +111,4 @@ class Users::SessionsController < Devise::SessionsController def logout redirect_to root_path, notice: I18n.t('devise.sessions.signed_out') end - - private - - def build_agent_connect_logout_url(id_token) - h = { id_token_hint: id_token, post_logout_redirect_uri: logout_url } - "#{AGENT_CONNECT[:end_session_endpoint]}?#{h.to_query}" - end end diff --git a/app/services/agent_connect_service.rb b/app/services/agent_connect_service.rb index 69581d9b2..6b2dc6ba1 100644 --- a/app/services/agent_connect_service.rb +++ b/app/services/agent_connect_service.rb @@ -39,6 +39,12 @@ class AgentConnectService [access_token.userinfo!.raw_attributes, access_token.id_token, amr] end + def self.logout_url(id_token, host_with_port:) + app_logout = Rails.application.routes.url_helpers.logout_url(host: host_with_port) + h = { id_token_hint: id_token, post_logout_redirect_uri: app_logout } + "#{AGENT_CONNECT[:end_session_endpoint]}?#{h.to_query}" + end + private # TODO: remove this block when migration to new domain is done diff --git a/spec/services/agent_connect_service_spec.rb b/spec/services/agent_connect_service_spec.rb new file mode 100644 index 000000000..f0aa30db7 --- /dev/null +++ b/spec/services/agent_connect_service_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +describe AgentConnectService do + describe '.logout_url' do + let(:id_token) { 'id_token' } + + before do + ::AGENT_CONNECT ||= {} + allow(AGENT_CONNECT).to receive(:[]) + .with(:end_session_endpoint).and_return("https://agent-connect.fr/logout") + end + + subject { described_class.logout_url(id_token, host_with_port: 'test.host') } + + it 'returns the correct url' do + expect(subject).to eq("https://agent-connect.fr/logout?id_token_hint=id_token&post_logout_redirect_uri=http%3A%2F%2Ftest.host%2Flogout") + end + end +end