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

invalidate recovery tokens after password change

Markus Kahld6e63adbe24f
diff --git a/app/services/users/change_password_service.rb b/app/services/users/change_password_service.rb
index c5fcba510d5..889a118fd48 100644
--- a/app/services/users/change_password_service.rb
+++ b/app/services/users/change_password_service.rb
@@ -42,6 +42,8 @@ module Users
current_user.force_password_change = false
if current_user.save
+ invalidate_recovery_tokens
+
log_success
::ServiceResult.new success: true,
result: current_user,
@@ -58,6 +60,10 @@ module Users
private
+ def invalidate_recovery_tokens
+ Token::Recovery.where(user: current_user).delete_all
+ end
+
def invalidate_session_result
update_message = I18n.t(:notice_account_password_updated)
diff --git a/spec/services/users/change_password_service_spec.rb b/spec/services/users/change_password_service_spec.rb
new file mode 100644
index 00000000000..edf2c2f7921
--- /dev/null
+++ b/spec/services/users/change_password_service_spec.rb
@@ -0,0 +1,55 @@
+#-- 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'
+
+describe Users::ChangePasswordService do
+ let(:user) { create(:user, password: old_password, password_confirmation: old_password) }
+ let(:old_password) { 'AdminAdmin42' }
+ let(:new_password) { 'SoreThroat33' }
+ let(:session) { ActionController::TestSession.new }
+ let(:instance) { described_class.new current_user: user, session: }
+
+ let(:result) do
+ instance.call new_password:, new_password_confirmation: new_password
+ end
+
+ it "changes the user's password" do
+ expect(result).to be_success
+ expect(user.check_password?(old_password)).to be false
+ expect(user.check_password?(new_password)).to be true
+ end
+
+ context "with existing password recovery tokens" do
+ let!(:recovery_token) { create(:recovery_token, user:) }
+
+ it 'invalidates the existing tokens' do
+ expect(result).to be_success
+ expect(Token::Recovery.where(user:).count).to eq 0
+ end
+ end
+end