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

Fix auth source login when user changes

Oliver Güntherf7c2644f533b
diff --git a/app/controllers/concerns/auth_source_sso.rb b/app/controllers/concerns/auth_source_sso.rb
index f5b434fc3b5..d43efcb4690 100644
--- a/app/controllers/concerns/auth_source_sso.rb
+++ b/app/controllers/concerns/auth_source_sso.rb
@@ -6,15 +6,36 @@ module AuthSourceSSO
def find_current_user
user = super
- return user if user || sso_in_progress!
- return nil unless header_name
+ # Do nothing if sso disabled
+ return user unless auth_source_sso_enabled?
+ # Return super auth if SSO already in progress
+ return user if sso_in_progress!
+
+ # Get the header-provided login value
+ login = read_sso_login
+
+ # Log out the current user if the login does not match
+ logged_user = match_sso_with_logged_user(login, user)
+
+ # Return the logged in user if matches
+ return logged_user if logged_user.present?
Rails.logger.debug { "Starting header-based auth source SSO for #{header_name}='#{op_auth_header_value}'" }
- perform_header_sso
+ perform_header_sso login
end
- def perform_header_sso
- if login = read_sso_login
+ def match_sso_with_logged_user(login, user)
+ return if user.nil?
+ return user if user.login == login
+
+ Rails.logger.warn { "Header-based auth source SSO user changed from #{user.login} to #{login}. Re-authenticating" }
+ ::Users::LogoutService.new(controller: self).call(user)
+
+ nil
+ end
+
+ def perform_header_sso(login)
+ if login
user = find_user_from_auth_source(login) || create_user_from_auth_source(login)
handle_sso_for! user, login
@@ -31,6 +52,10 @@ module AuthSourceSSO
@sso_config ||= OpenProject::Configuration.auth_source_sso.try(:with_indifferent_access)
end
+ def auth_source_sso_enabled?
+ header_name.present?
+ end
+
def op_auth_header_value
String(request.headers[header_name])
end
diff --git a/spec/controllers/concerns/auth_source_sso_spec.rb b/spec/controllers/concerns/auth_source_sso_spec.rb
index 74d9ca3856c..1a0d3153108 100644
--- a/spec/controllers/concerns/auth_source_sso_spec.rb
+++ b/spec/controllers/concerns/auth_source_sso_spec.rb
@@ -148,4 +148,28 @@ describe MyController, type: :controller do
it_should_behave_like "auth source sso failure"
end
end
+
+ context 'when the logged-in user differs from the header' do
+ let(:other_user) { FactoryBot.create :user, login: 'other_user' }
+ let(:session_update_time) { 1.minute.ago }
+ let(:service) { Users::LogoutService.new(controller: controller) }
+
+ before do
+ session[:user_id] = other_user.id
+ session[:updated_at] = session_update_time
+ end
+
+ it 'logs out the user and logs it in again' do
+ allow(::Users::LogoutService).to receive(:new).and_return(service)
+ allow(service).to receive(:call).with(other_user).and_call_original
+
+ get :account
+
+ expect(service).to have_received(:call).with(other_user)
+ expect(response).to redirect_to my_page_path
+ expect(user.reload.last_login_on).to be_within(10.seconds).of(Time.now.utc)
+ expect(session[:user_id]).to eq user.id
+ expect(session[:updated_at]).to be > session_update_time
+ end
+ end
end