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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Changing mail address should invalidate tokens on top of session
Oliver Günther99e915b2c99b
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rbindex 52937b948e4..7fcd54b5a31 100644--- a/app/controllers/my_controller.rb+++ b/app/controllers/my_controller.rb@@ -50,12 +50,6 @@ class MyController < ApplicationControllerdef update_accountwrite_settings-- # If mail changed, expire all other sessions- if @user.previous_changes['mail'] && ::Sessions::DropOtherSessionsService.call(@user, session)- flash[:info] = "#{flash[:notice]} #{t(:notice_account_other_session_expired)}"- flash.delete :notice- endenddef settings; end@@ -203,6 +197,7 @@ class MyController < ApplicationControllerif result&.successflash[:notice] = notice_account_updated+ handle_email_changeselseflash[:error] = error_account_update_failed(result)end@@ -212,6 +207,17 @@ class MyController < ApplicationControllerhelper_method :has_tokens?+ def handle_email_changes+ # If mail changed, expire all other sessions+ if @user.previous_changes['mail']+ Users::DropTokensService.new(current_user: @user).call!+ Sessions::DropOtherSessionsService.call!(@user, session)++ flash[:info] = "#{flash[:notice]} #{t(:notice_account_other_session_expired)}"+ flash.delete :notice+ end+ end+def has_tokens?Setting.feeds_enabled? || Setting.rest_api_enabled? || current_user.ical_tokens.any?enddiff --git a/app/services/sessions/drop_all_sessions_service.rb b/app/services/sessions/drop_all_sessions_service.rbindex 5577bb5d90d..f2de58b3fed 100644--- a/app/services/sessions/drop_all_sessions_service.rb+++ b/app/services/sessions/drop_all_sessions_service.rb@@ -30,12 +30,10 @@ module Sessionsclass << self### Drop all sessions for the given user- def call(user)+ def call!(user)::Sessions::UserSession.for_user(user).delete_all-- trueendendenddiff --git a/app/services/sessions/drop_other_sessions_service.rb b/app/services/sessions/drop_other_sessions_service.rbindex 040ad943ba1..a09cdc86d0f 100644--- a/app/services/sessions/drop_other_sessions_service.rb+++ b/app/services/sessions/drop_other_sessions_service.rb@@ -31,13 +31,11 @@ module Sessions### Drop all other sessions for the current user.# This can only be done when active record sessions are used.- def call(user, session)+ def call!(user, session)::Sessions::UserSession.for_user(user).where.not(session_id: session.id.private_id).delete_all-- trueendendenddiff --git a/app/services/users/change_password_service.rb b/app/services/users/change_password_service.rbindex 624bb7654dd..b6ccf84b0a2 100644--- a/app/services/users/change_password_service.rb+++ b/app/services/users/change_password_service.rb@@ -45,13 +45,12 @@ module Userscurrent_user.activate if current_user.invited?if current_user.save- invalidate_recovery_tokens- invalidate_invitation_tokens-+ invalidate_tokens+ invalidate_other_sessionslog_success::ServiceResult.new success: true,result: current_user,- **invalidate_session_result+ **update_messageelselog_failure::ServiceResult.new success: false,@@ -64,23 +63,20 @@ module Usersprivate- def invalidate_recovery_tokens- Token::Recovery.where(user: current_user).delete_all+ def invalidate_tokens+ ::Users::DropTokensService+ .new(current_user: current_user)+ .call!end- def invalidate_invitation_tokens- Token::Invitation.where(user: current_user).delete_all+ def invalidate_other_sessions+ ::Sessions::DropOtherSessionsService.call!(current_user, session)end- def invalidate_session_result+ def update_messageupdate_message = I18n.t(:notice_account_password_updated)-- if ::Sessions::DropOtherSessionsService.call(current_user, session)- expiry_message = I18n.t(:notice_account_other_session_expired)- { message_type: :info, message: "#{update_message} #{expiry_message}" }- else- { message: update_message }- end+ expiry_message = I18n.t(:notice_account_other_session_expired)+ { message_type: :info, message: "#{update_message} #{expiry_message}" }enddef log_successdiff --git a/app/services/users/drop_tokens_service.rb b/app/services/users/drop_tokens_service.rbnew file mode 100644index 00000000000..e70b48f1faa--- /dev/null+++ b/app/services/users/drop_tokens_service.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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.+#++++module Users+ ##+ # Remove all recovery and invitation tokens for the user+ class DropTokensService+ attr_accessor :user++ def initialize(current_user:)+ @user = current_user+ end++ def call!+ invalidate_recovery_tokens+ invalidate_invitation_tokens+ end++ private++ def invalidate_recovery_tokens+ Token::Recovery.where(user: user).delete_all+ end++ def invalidate_invitation_tokens+ Token::Invitation.where(user: user).delete_all+ end+ end+enddiff --git a/app/services/users/login_service.rb b/app/services/users/login_service.rbindex 243677d9bf6..817b94920db 100644--- a/app/services/users/login_service.rb+++ b/app/services/users/login_service.rb@@ -76,12 +76,16 @@ module Usersdef successful_loginuser.log_successful_login+ Users::DropTokensService+ .new(current_user: user)+ .call!+context = { user:, request:, session: }OpenProject::Hook.call_hook(:user_logged_in, context)enddef reset_session!- ::Sessions::DropAllSessionsService.call(user) if drop_old_sessions?+ ::Sessions::DropAllSessionsService.call!(user) if drop_old_sessions?controller.reset_sessionenddiff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rbindex bf5ca78a4af..d4fe3153116 100644--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/base_controller.rb@@ -200,10 +200,10 @@ module ::TwoFactorAuthenticationdef logout_other_sessionsif current_user == target_userRails.logger.info { "First 2FA device registered for #{target_user}, terminating other logged in sessions." }- ::Sessions::DropOtherSessionsService.call(target_user, session)+ ::Sessions::DropOtherSessionsService.call!(target_user, session)elseRails.logger.info { "First 2FA device registered for #{target_user}, terminating logged in sessions." }- ::Sessions::DropAllSessionsService.call(target_user)+ ::Sessions::DropAllSessionsService.call!(target_user)endenddiff --git a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rb b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rbindex 914fefebb05..2f07ea2454c 100644--- a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rb+++ b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/forced_registration/two_factor_devices_controller_spec.rb@@ -187,7 +187,7 @@ RSpec.describe TwoFactorAuthentication::ForcedRegistration::TwoFactorDevicesContit "activates the device when entered correctly and logs out the user" doallow(Sessions::DropAllSessionsService)- .to receive(:call)+ .to receive(:call!)# rubocop:disable RSpec/AnyInstanceallow_any_instance_of(TwoFactorAuthentication::TokenService)@@ -204,7 +204,7 @@ RSpec.describe TwoFactorAuthentication::ForcedRegistration::TwoFactorDevicesContexpect(device.default).to be trueexpect(Sessions::DropAllSessionsService)- .to have_received(:call).with(user)+ .to have_received(:call!).with(user)endendenddiff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rbindex c5cfc3781c4..627a500e55a 100644--- a/spec/controllers/my_controller_spec.rb+++ b/spec/controllers/my_controller_spec.rb@@ -157,13 +157,19 @@ RSpec.describe MyController dodescribe "settings" dodescribe "PATCH" dolet(:language) { "en" }+ let(:params) do+ {+ user: { language: },+ pref: { auto_hide_popups: 0 }+ }+ endbefore doas_logged_in_user user douser.pref.comments_sorting = "desc"user.pref.auto_hide_popups = true- patch :update_settings, params: { user: { language: }, pref: { auto_hide_popups: 0 } }+ patch :update_settings, params:endend@@ -200,6 +206,28 @@ RSpec.describe MyController doendend+ describe "changing changing mail" do+ let!(:recovery_token) { create(:recovery_token, user:) }+ let!(:plain_session) { create(:user_session, user:, session_id: "internal_foobar") }+ let!(:user_session) { Sessions::UserSession.find_by(session_id: "internal_foobar") }++ let(:params) do+ { user: { mail: "[email protected]"} }+ end++ it "clears other sessions and removes tokens" do+ as_logged_in_user user do+ patch :update_settings, params:+ end++ expect(flash[:info]).to include(I18n.t(:notice_account_updated))+ expect(flash[:info]).to include(I18n.t(:notice_account_other_session_expired))++ expect(Token::Recovery.where(user:)).to be_empty+ expect { user_session.reload }.to raise_error(ActiveRecord::RecordNotFound)+ end+ end+describe "settings:auto_hide_popups" docontext "with render_views" dobefore dodiff --git a/spec/services/users/drop_tokens_service_spec.rb b/spec/services/users/drop_tokens_service_spec.rbnew file mode 100644index 00000000000..5a4c93c030c--- /dev/null+++ b/spec/services/users/drop_tokens_service_spec.rb@@ -0,0 +1,61 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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"++RSpec.describe Users::DropTokensService, type: :model do+ shared_let(:input_user) { create(:user) }+ shared_let(:other_user) { create(:user) }++ let(:instance) { described_class.new(current_user: input_user) }+ subject { instance.call! }++ describe "Invitation token" do+ let!(:invitation_token) { create(:invitation_token, user: input_user) }+ let!(:other_invitation_token) { create(:invitation_token, user: other_user) }++ it "removes only the tokens from that user" do+ subject++ expect(Token::Invitation.exists?(invitation_token.id)).to be false+ expect(Token::Invitation.exists?(other_invitation_token.id)).to be true+ end+ end++ describe "Password reset token" do+ let!(:reset_token) { create(:recovery_token, user: input_user) }+ let!(:other_reset_token) { create(:recovery_token, user: other_user) }++ it "removes only the tokens from that user" do+ subject++ expect(Token::Recovery.exists?(reset_token.id)).to be false+ expect(Token::Recovery.exists?(other_reset_token.id)).to be true+ end+ end+enddiff --git a/spec/services/users/login_service_spec.rb b/spec/services/users/login_service_spec.rbindex b5b0be2ce55..13803f5bbdd 100644--- a/spec/services/users/login_service_spec.rb+++ b/spec/services/users/login_service_spec.rb@@ -47,6 +47,10 @@ RSpec.describe Users::LoginService, type: :model dosubject { instance.call! }before do+ allow(Sessions::DropOtherSessionsService)+ .to receive(:call)+ .with(input_user, session)+allow(controller).to(receive(:reset_session)) dosession.clear@@ -194,4 +198,16 @@ RSpec.describe Users::LoginService, type: :model doendendend++ describe "removal of tokens" do+ let!(:invitation_token) { create(:invitation_token, user: input_user) }+ let!(:recovery_token) { create(:recovery_token, user: input_user) }++ it "removes those token on successful login" do+ subject++ expect(Token::Invitation.exists?(invitation_token.id)).to be false+ expect(Token::Recovery.exists?(recovery_token.id)).to be false+ end+ endend