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

Drop other sessions of user when creating your first device

Oliver Günther3a6f47b3a3f4
diff --git a/app/services/sessions/drop_all_sessions_service.rb b/app/services/sessions/drop_all_sessions_service.rb
new file mode 100644
index 00000000000..55babcedc80
--- /dev/null
+++ b/app/services/sessions/drop_all_sessions_service.rb
@@ -0,0 +1,46 @@
+#-- 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_relative './base_service'
+
+module Sessions
+ class DropAllSessionsService < BaseService
+ class << self
+ ##
+ # Drop all sessions for the given user
+ def call(user)
+ return false unless active_record_sessions?
+
+ ::Sessions::UserSession
+ .for_user(user)
+ .delete_all
+
+ true
+ end
+ end
+ end
+end
diff --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.rb
index 6185465ffac..3efe381397c 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
@@ -86,18 +86,28 @@ module ::TwoFactorAuthentication
service = token_service(@device)
result = service.verify(params[:otp])
+ has_default = ::TwoFactorAuthentication::Device.has_default?(target_user)
+ if confirm_and_save(result)
+ logout_other_sessions unless has_default
+ redirect_to registration_success_path
+ else
+ redirect_to action: :confirm, device_id: @device.id
+ end
+ end
+
+ def confirm_and_save(result)
if result.success? && @device.confirm_registration_and_save
flash[:notice] = t('two_factor_authentication.devices.registration_complete')
- return redirect_to registration_success_path
+ true
elsif !result.success?
flash[:notice] = nil
flash[:error] = t('two_factor_authentication.devices.registration_failed_token_invalid')
+ false
else
flash[:notice] = nil
flash[:error] = t('two_factor_authentication.devices.registration_failed_update')
+ false
end
-
- redirect_to action: :confirm, device_id: @device.id
end
def request_token_for_device(device, locals)
@@ -138,6 +148,14 @@ module ::TwoFactorAuthentication
)
end
+ def logout_other_sessions
+ if current_user == target_user
+ ::Sessions::DropOtherSessionsService.call(target_user, session)
+ else
+ ::Sessions::DropAllSessionsService.call(target_user)
+ end
+ end
+
def permitted_device_params
params.require(:device).permit(:phone_number, :channel, :otp_secret, :identifier)
end
diff --git a/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb b/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb
index f9cbef97c03..e3aac29957e 100644
--- a/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb
+++ b/modules/two_factor_authentication/app/controllers/two_factor_authentication/my/two_factor_devices_controller.rb
@@ -69,24 +69,6 @@ module ::TwoFactorAuthentication
)
end
- ##
- # Validate the token input
- def validate_device_token
- service = token_service(@device)
- result = service.verify(params[:otp])
-
- if result.success? && @device.confirm_registration_and_save
- flash[:notice] = t('two_factor_authentication.devices.registration_complete')
- return redirect_to action: :index
- elsif !result.success?
- flash[:error] = t('two_factor_authentication.devices.registration_failed_token_invalid')
- else
- flash[:error] = t('two_factor_authentication.devices.registration_failed_update')
- end
-
- redirect_to action: :confirm, device_id: @device.id
- end
-
def request_token_for_device(device, locals)
transmit = token_service(device).request
@@ -112,6 +94,10 @@ module ::TwoFactorAuthentication
false
end
+ def registration_success_path
+ url_for(action: :index)
+ end
+
def set_user_variables
@user = current_user
@default_device = @user.otp_devices.get_default
diff --git a/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb b/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb
index d8fc5879ab6..0f234c54c8f 100644
--- a/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb
+++ b/modules/two_factor_authentication/app/models/two_factor_authentication/device.rb
@@ -18,7 +18,7 @@ module TwoFactorAuthentication
end
def self.has_default?(user)
- Device.where(user_id: user.id, active: true, default: true).exists?
+ Device.exists?(user_id: user.id, active: true, default: true)
end
def has_default?
diff --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.rb
index 34a8aae4c59..a68c8ca73d9 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
@@ -185,7 +185,10 @@ describe TwoFactorAuthentication::ForcedRegistration::TwoFactorDevicesController
expect(device.default).to be false
end
- it 'activates the device when entered correctly' do
+ it 'activates the device when entered correctly and logs out the user' do
+ allow(::Sessions::DropAllSessionsService)
+ .to receive(:call)
+
allow_any_instance_of(TwoFactorAuthentication::TokenService)
.to receive(:verify)
.with('1234')
@@ -197,6 +200,9 @@ describe TwoFactorAuthentication::ForcedRegistration::TwoFactorDevicesController
device.reload
expect(device.active).to be true
expect(device.default).to be true
+
+ expect(::Sessions::DropAllSessionsService)
+ .to have_received(:call).with(user)
end
end
end
diff --git a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb
index 7f0f043db41..4c254b70b16 100644
--- a/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb
+++ b/modules/two_factor_authentication/spec/controllers/two_factor_authentication/my/two_factor_devices_controller_spec.rb
@@ -179,36 +179,42 @@ describe TwoFactorAuthentication::My::TwoFactorDevicesController do
expect(device.default).to be false
end
- it 'activates the device when entered correctly' do
- allow_any_instance_of(TwoFactorAuthentication::TokenService)
- .to receive(:verify)
- .with('1234')
- .and_return(ServiceResult.success)
+ context 'when user has another active session' do
+ let!(:plain_session) { create(:user_session, user:) }
+ let!(:user_session) { Sessions::UserSession.find_by(session_id: plain_session.session_id) }
- post :confirm, params: { device_id: device.id, otp: '1234' }
- expect(response).to redirect_to action: :index
- expect(flash[:notice]).to include I18n.t('two_factor_authentication.devices.registration_complete')
- device.reload
- expect(device.active).to be true
- expect(device.default).to be true
- end
-
- context 'with another default device present' do
- let!(:default_device) { create(:two_factor_authentication_device_totp, user:, default: true) }
-
- it 'activates the device when entered correctly' do
+ it 'activates the device when entered correctly and clears other sessions' do
allow_any_instance_of(TwoFactorAuthentication::TokenService)
- .to receive(:verify)
- .with('1234')
- .and_return(ServiceResult.success)
+ .to receive(:verify)
+ .with('1234')
+ .and_return(ServiceResult.success)
post :confirm, params: { device_id: device.id, otp: '1234' }
expect(response).to redirect_to action: :index
expect(flash[:notice]).to include I18n.t('two_factor_authentication.devices.registration_complete')
device.reload
expect(device.active).to be true
- # but does not set default
- expect(device.default).to be false
+ expect(device.default).to be true
+ expect { user_session.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ context 'with another default device present' do
+ let!(:default_device) { create(:two_factor_authentication_device_totp, user:, default: true) }
+
+ it 'activates the device when entered correctly' do
+ allow_any_instance_of(TwoFactorAuthentication::TokenService)
+ .to receive(:verify)
+ .with('1234')
+ .and_return(ServiceResult.success)
+
+ post :confirm, params: { device_id: device.id, otp: '1234' }
+ expect(response).to redirect_to action: :index
+ expect(flash[:notice]).to include I18n.t('two_factor_authentication.devices.registration_complete')
+ device.reload
+ expect(device.active).to be true
+ # but does not set default
+ expect(device.default).to be false
+ end
end
end
end